/* * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * Test ModuleFinder.of with no entries
*/ publicvoid testOfNoEntries() {
ModuleFinder finder = ModuleFinder.of();
assertTrue(finder.findAll().isEmpty());
assertFalse(finder.find("java.rhubarb").isPresent());
}
/** * Test ModuleFinder.of with one directory of modules
*/ publicvoid testOfOneDirectory() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
createExplodedModule(dir.resolve("m1"), "m1");
createModularJar(dir.resolve("m2.jar"), "m2");
// check that m1@1.0 (and not m1@2.0) is found
ModuleDescriptor m1 = finder.find("m1").get().descriptor();
assertEquals(m1.version().get().toString(), "1.0");
// check that m2@1.0 (and not m2@2.0) is found
ModuleDescriptor m2 = finder.find("m2").get().descriptor();
assertEquals(m2.version().get().toString(), "1.0");
}
/** * Test ModuleFinder.of with one JAR file
*/ publicvoid testOfOneJarFile() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
Path jar1 = createModularJar(dir.resolve("m1.jar"), "m1");
ModuleFinder finder = ModuleFinder.of(jar1);
assertTrue(finder.findAll().size() == 1);
assertTrue(finder.find("m1").isPresent());
assertFalse(finder.find("java.rhubarb").isPresent());
}
/** * Test ModuleFinder.of with two JAR files
*/ publicvoid testOfTwoJarFiles() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
/** * Test ModuleFinder.of with many JAR files
*/ publicvoid testOfManyJarFiles() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
// check that m1@1.0 (and not m1@2.0) is found
ModuleDescriptor m1 = finder.find("m1").get().descriptor();
assertEquals(m1.version().get().toString(), "1.0");
}
/** * Test ModuleFinder.of with one exploded module.
*/ publicvoid testOfOneExplodedModule() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
Path m1_dir = createExplodedModule(dir.resolve("m1"), "m1");
ModuleFinder finder = ModuleFinder.of(m1_dir);
assertTrue(finder.findAll().size() == 1);
assertTrue(finder.find("m1").isPresent());
assertFalse(finder.find("java.rhubarb").isPresent());
}
/** * Test ModuleFinder.of with two exploded modules.
*/ publicvoid testOfTwoExplodedModules() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
Path m1_dir = createExplodedModule(dir.resolve("m1"), "m1");
Path m2_dir = createExplodedModule(dir.resolve("m2"), "m2");
// m1 and m2 should be located in dir1
ModuleDescriptor m1 = finder.find("m1").get().descriptor();
assertEquals(m1.version().get().toString(), "1.0");
ModuleDescriptor m2 = finder.find("m2").get().descriptor();
assertEquals(m2.version().get().toString(), "1.0");
// m3 and m4 should be located in JAR files
ModuleDescriptor m3 = finder.find("m3").get().descriptor();
assertEquals(m3.version().get().toString(), "2.0");
ModuleDescriptor m4 = finder.find("m4").get().descriptor();
assertEquals(m4.version().get().toString(), "2.0");
// m5 and m6 should be located in JAR files
ModuleDescriptor m5 = finder.find("m5").get().descriptor();
assertEquals(m5.version().get().toString(), "4.0");
ModuleDescriptor m6 = finder.find("m6").get().descriptor();
assertEquals(m6.version().get().toString(), "4.0");
}
/** * Test ModuleFinder.of with a mix of module directories and exploded * modules.
*/ publicvoid testOfMixDirectoriesAndExplodedModules() throws Exception { // directory with m1@1.0 and m2@1.0
Path dir1 = Files.createTempDirectory(USER_DIR, "mods1");
createExplodedModule(dir1.resolve("m1"), "m1@1.0");
createModularJar(dir1.resolve("m2.jar"), "m2@1.0");
// m1 and m2 should be located in dir1
ModuleDescriptor m1 = finder.find("m1").get().descriptor();
assertEquals(m1.version().get().toString(), "1.0");
ModuleDescriptor m2 = finder.find("m2").get().descriptor();
assertEquals(m2.version().get().toString(), "1.0");
// m3 and m4 should be located in dir2
ModuleDescriptor m3 = finder.find("m3").get().descriptor();
assertEquals(m3.version().get().toString(), "2.0");
ModuleDescriptor m4 = finder.find("m4").get().descriptor();
assertEquals(m4.version().get().toString(), "2.0");
}
/** * Test ModuleFinder with a JAR file containing a mix of class and * non-class resources.
*/ publicvoid testOfOneJarFileWithResources() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
Path jar = createModularJar(dir.resolve("m.jar"), "m", "LICENSE", "README", "WEB-INF/tags", "p/Type.class", "p/resources/m.properties", "q-/Type.class", // not a legal package name "q-/resources/m/properties");
ModuleFinder finder = ModuleFinder.of(jar);
Optional<ModuleReference> mref = finder.find("m");
assertTrue(mref.isPresent(), "m1 not found");
/** * Test ModuleFinder with an exploded module containing a mix of class * and non-class resources
*/ publicvoid testOfOneExplodedModuleWithResources() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
Path m_dir = createExplodedModule(dir.resolve("m"), "m", "LICENSE", "README", "WEB-INF/tags", "p/Type.class", "p/resources/m.properties", "q-/Type.class", // not a legal package name "q-/resources/m/properties");
ModuleFinder finder = ModuleFinder.of(m_dir);
Optional<ModuleReference> mref = finder.find("m");
assertTrue(mref.isPresent(), "m not found");
/** * Test ModuleFinder with a JAR file containing a .class file in the top * level directory.
*/ publicvoid testOfOneJarFileWithTopLevelClass() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
Path jar = createModularJar(dir.resolve("m.jar"), "m", "Mojo.class");
/** * Test ModuleFinder with a JAR file containing a .class file in the top * level directory.
*/ publicvoid testOfOneExplodedModuleWithTopLevelClass() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
Path m_dir = createExplodedModule(dir.resolve("m"), "m", "Mojo.class");
/** * Test ModuleFinder.of with a path to a file that does not exist.
*/ publicvoid testOfWithDoesNotExistEntry() throws Exception {
Path dir1 = Files.createTempDirectory(USER_DIR, "mods1");
/** * Test ModuleFinder.of with a file path to an unrecognized file type.
*/ publicvoid testOfWithUnrecognizedEntry() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
Path mod = Files.createTempFile(dir, "m", ".junk");
/** * Test ModuleFinder.of with a file path to a directory containing a file * that will not be recognized as a module.
*/ publicvoid testOfWithUnrecognizedEntryInDirectory1() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
Files.createTempFile(dir, "m", ".junk");
ModuleFinder finder = ModuleFinder.of(dir);
assertFalse(finder.find("java.rhubarb").isPresent());
finder = ModuleFinder.of(dir);
assertTrue(finder.findAll().isEmpty());
}
/** * Test ModuleFinder.of with a file path to a directory containing a file * that will not be recognized as a module.
*/ publicvoid testOfWithUnrecognizedEntryInDirectory2() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
createModularJar(dir.resolve("m1.jar"), "m1");
Files.createTempFile(dir, "m2", ".junk");
ModuleFinder finder = ModuleFinder.of(dir);
assertTrue(finder.find("m1").isPresent());
assertFalse(finder.find("m2").isPresent());
finder = ModuleFinder.of(dir);
assertTrue(finder.findAll().size() == 1);
}
/** * Test ModuleFinder.of with a directory that contains two * versions of the same module
*/ publicvoid testOfDuplicateModulesInDirectory() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
createModularJar(dir.resolve("m1@1.0.jar"), "m1");
createModularJar(dir.resolve("m1@2.0.jar"), "m1");
/** * Test ModuleFinder.of with a truncated module-info.class
*/ publicvoid testOfWithTruncatedModuleInfo() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
// create an empty <dir>/rhubarb/module-info.class
Path subdir = Files.createDirectory(dir.resolve("rhubarb"));
Files.createFile(subdir.resolve("module-info.class"));
/** * Test ModuleFinder.compose with no module finders
*/ publicvoid testComposeOfNone() throws Exception {
ModuleFinder finder = ModuleFinder.of();
assertTrue(finder.findAll().isEmpty());
assertFalse(finder.find("java.rhubarb").isPresent());
}
/** * Test ModuleFinder.compose with one module finder
*/ publicvoid testComposeOfOne() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mods");
createModularJar(dir.resolve("m1.jar"), "m1");
createModularJar(dir.resolve("m2.jar"), "m2");
/** * Parses a string of the form {@code name[@version]} and returns a * ModuleDescriptor with that name and version. The ModuleDescriptor * will have a requires on java.base.
*/ static ModuleDescriptor newModuleDescriptor(String mid) {
String mn;
String vs; int i = mid.indexOf("@"); if (i == -1) {
mn = mid;
vs = null;
} else {
mn = mid.substring(0, i);
vs = mid.substring(i+1);
}
ModuleDescriptor.Builder builder
= ModuleDescriptor.newModule(mn).requires("java.base"); if (vs != null)
builder.version(vs); return builder.build();
}
/** * Creates an exploded module in the given directory and containing a * module descriptor with the given module name/version.
*/ static Path createExplodedModule(Path dir, String mid, String... entries) throws Exception
{
ModuleDescriptor descriptor = newModuleDescriptor(mid);
Files.createDirectories(dir);
Path mi = dir.resolve("module-info.class"); try (OutputStream out = Files.newOutputStream(mi)) {
ModuleInfoWriter.write(descriptor, out);
}
/** * Creates a JAR file with the given file path and containing a module * descriptor with the given module name/version.
*/ static Path createModularJar(Path file, String mid, String ... entries) throws Exception
{
ModuleDescriptor descriptor = newModuleDescriptor(mid); try (OutputStream out = Files.newOutputStream(file)) { try (JarOutputStream jos = new JarOutputStream(out)) {
JarEntry je = new JarEntry("module-info.class");
jos.putNextEntry(je);
ModuleInfoWriter.write(descriptor, jos);
jos.closeEntry();
for (String entry : entries) {
je = new JarEntry(entry);
jos.putNextEntry(je);
jos.closeEntry();
}
}
} return file;
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.18 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.