Path: blob/master/test/jdk/java/lang/module/ModuleFinderTest.java
41149 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @modules java.base/jdk.internal.module26* @build ModuleFinderTest27* @run testng ModuleFinderTest28* @summary Basic tests for java.lang.module.ModuleFinder29*/3031import java.io.File;32import java.io.OutputStream;33import java.lang.module.FindException;34import java.lang.module.InvalidModuleDescriptorException;35import java.lang.module.ModuleDescriptor;36import java.lang.module.ModuleFinder;37import java.lang.module.ModuleReference;38import java.nio.file.Files;39import java.nio.file.Path;40import java.nio.file.Paths;41import java.util.Optional;42import java.util.Set;43import java.util.jar.JarEntry;44import java.util.jar.JarOutputStream;45import java.util.stream.Collectors;4647import jdk.internal.module.ModuleInfoWriter;4849import org.testng.annotations.Test;50import static org.testng.Assert.*;5152@Test53public class ModuleFinderTest {5455private static final Path USER_DIR56= Paths.get(System.getProperty("user.dir"));575859/**60* Test ModuleFinder.ofSystem61*/62public void testOfSystem() {63ModuleFinder finder = ModuleFinder.ofSystem();6465assertTrue(finder.find("java.se").isPresent());66assertTrue(finder.find("java.base").isPresent());67assertFalse(finder.find("java.rhubarb").isPresent());6869Set<String> names = finder.findAll().stream()70.map(ModuleReference::descriptor)71.map(ModuleDescriptor::name)72.collect(Collectors.toSet());73assertTrue(names.contains("java.se"));74assertTrue(names.contains("java.base"));75assertFalse(names.contains("java.rhubarb"));76}777879/**80* Test ModuleFinder.of with no entries81*/82public void testOfNoEntries() {83ModuleFinder finder = ModuleFinder.of();84assertTrue(finder.findAll().isEmpty());85assertFalse(finder.find("java.rhubarb").isPresent());86}878889/**90* Test ModuleFinder.of with one directory of modules91*/92public void testOfOneDirectory() throws Exception {93Path dir = Files.createTempDirectory(USER_DIR, "mods");94createExplodedModule(dir.resolve("m1"), "m1");95createModularJar(dir.resolve("m2.jar"), "m2");9697ModuleFinder finder = ModuleFinder.of(dir);98assertTrue(finder.findAll().size() == 2);99assertTrue(finder.find("m1").isPresent());100assertTrue(finder.find("m2").isPresent());101assertFalse(finder.find("java.rhubarb").isPresent());102}103104105/**106* Test ModuleFinder.of with two directories107*/108public void testOfTwoDirectories() throws Exception {109Path dir1 = Files.createTempDirectory(USER_DIR, "mods1");110createExplodedModule(dir1.resolve("m1"), "[email protected]");111createModularJar(dir1.resolve("m2.jar"), "[email protected]");112113Path dir2 = Files.createTempDirectory(USER_DIR, "mods2");114createExplodedModule(dir2.resolve("m1"), "[email protected]");115createModularJar(dir2.resolve("m2.jar"), "[email protected]");116createExplodedModule(dir2.resolve("m3"), "m3");117createModularJar(dir2.resolve("m4.jar"), "m4");118119ModuleFinder finder = ModuleFinder.of(dir1, dir2);120assertTrue(finder.findAll().size() == 4);121assertTrue(finder.find("m1").isPresent());122assertTrue(finder.find("m2").isPresent());123assertTrue(finder.find("m3").isPresent());124assertTrue(finder.find("m4").isPresent());125assertFalse(finder.find("java.rhubarb").isPresent());126127// check that [email protected] (and not [email protected]) is found128ModuleDescriptor m1 = finder.find("m1").get().descriptor();129assertEquals(m1.version().get().toString(), "1.0");130131// check that [email protected] (and not [email protected]) is found132ModuleDescriptor m2 = finder.find("m2").get().descriptor();133assertEquals(m2.version().get().toString(), "1.0");134}135136137/**138* Test ModuleFinder.of with one JAR file139*/140public void testOfOneJarFile() throws Exception {141Path dir = Files.createTempDirectory(USER_DIR, "mods");142Path jar1 = createModularJar(dir.resolve("m1.jar"), "m1");143144ModuleFinder finder = ModuleFinder.of(jar1);145assertTrue(finder.findAll().size() == 1);146assertTrue(finder.find("m1").isPresent());147assertFalse(finder.find("java.rhubarb").isPresent());148}149150151/**152* Test ModuleFinder.of with two JAR files153*/154public void testOfTwoJarFiles() throws Exception {155Path dir = Files.createTempDirectory(USER_DIR, "mods");156157Path jar1 = createModularJar(dir.resolve("m1.jar"), "m1");158Path jar2 = createModularJar(dir.resolve("m2.jar"), "m2");159160ModuleFinder finder = ModuleFinder.of(jar1, jar2);161assertTrue(finder.findAll().size() == 2);162assertTrue(finder.find("m1").isPresent());163assertTrue(finder.find("m2").isPresent());164assertFalse(finder.find("java.rhubarb").isPresent());165}166167168/**169* Test ModuleFinder.of with many JAR files170*/171public void testOfManyJarFiles() throws Exception {172Path dir = Files.createTempDirectory(USER_DIR, "mods");173174Path jar1 = createModularJar(dir.resolve("[email protected]"), "[email protected]");175Path jar2 = createModularJar(dir.resolve("[email protected]"), "m2");176Path jar3 = createModularJar(dir.resolve("[email protected]"), "[email protected]"); // shadowed177Path jar4 = createModularJar(dir.resolve("[email protected]"), "m3");178179ModuleFinder finder = ModuleFinder.of(jar1, jar2, jar3, jar4);180assertTrue(finder.findAll().size() == 3);181assertTrue(finder.find("m1").isPresent());182assertTrue(finder.find("m2").isPresent());183assertTrue(finder.find("m3").isPresent());184assertFalse(finder.find("java.rhubarb").isPresent());185186// check that [email protected] (and not [email protected]) is found187ModuleDescriptor m1 = finder.find("m1").get().descriptor();188assertEquals(m1.version().get().toString(), "1.0");189}190191192/**193* Test ModuleFinder.of with one exploded module.194*/195public void testOfOneExplodedModule() throws Exception {196Path dir = Files.createTempDirectory(USER_DIR, "mods");197Path m1_dir = createExplodedModule(dir.resolve("m1"), "m1");198199ModuleFinder finder = ModuleFinder.of(m1_dir);200assertTrue(finder.findAll().size() == 1);201assertTrue(finder.find("m1").isPresent());202assertFalse(finder.find("java.rhubarb").isPresent());203}204205206/**207* Test ModuleFinder.of with two exploded modules.208*/209public void testOfTwoExplodedModules() throws Exception {210Path dir = Files.createTempDirectory(USER_DIR, "mods");211Path m1_dir = createExplodedModule(dir.resolve("m1"), "m1");212Path m2_dir = createExplodedModule(dir.resolve("m2"), "m2");213214ModuleFinder finder = ModuleFinder.of(m1_dir, m2_dir);215assertTrue(finder.findAll().size() == 2);216assertTrue(finder.find("m1").isPresent());217assertTrue(finder.find("m2").isPresent());218assertFalse(finder.find("java.rhubarb").isPresent());219}220221222/**223* Test ModuleFinder.of with a mix of module directories and JAR files.224*/225public void testOfMixDirectoriesAndJars() throws Exception {226227// directory with [email protected] and [email protected]228Path dir1 = Files.createTempDirectory(USER_DIR, "mods1");229createExplodedModule(dir1.resolve("m1"), "[email protected]");230createModularJar(dir1.resolve("m2.jar"), "[email protected]");231232// JAR files: [email protected], [email protected], [email protected], [email protected]233Path dir2 = Files.createTempDirectory(USER_DIR, "mods2");234Path jar1 = createModularJar(dir2.resolve("m1.jar"), "[email protected]");235Path jar2 = createModularJar(dir2.resolve("m2.jar"), "[email protected]");236Path jar3 = createModularJar(dir2.resolve("m3.jar"), "[email protected]");237Path jar4 = createModularJar(dir2.resolve("m4.jar"), "[email protected]");238239// directory with [email protected] and [email protected]240Path dir3 = Files.createTempDirectory(USER_DIR, "mods3");241createExplodedModule(dir3.resolve("m3"), "[email protected]");242createModularJar(dir3.resolve("m4.jar"), "[email protected]");243244// JAR files: m5 and m6245Path dir4 = Files.createTempDirectory(USER_DIR, "mods4");246Path jar5 = createModularJar(dir4.resolve("m5.jar"), "[email protected]");247Path jar6 = createModularJar(dir4.resolve("m6.jar"), "[email protected]");248249250ModuleFinder finder251= ModuleFinder.of(dir1, jar1, jar2, jar3, jar4, dir3, jar5, jar6);252assertTrue(finder.findAll().size() == 6);253assertTrue(finder.find("m1").isPresent());254assertTrue(finder.find("m2").isPresent());255assertTrue(finder.find("m3").isPresent());256assertTrue(finder.find("m4").isPresent());257assertTrue(finder.find("m5").isPresent());258assertTrue(finder.find("m6").isPresent());259assertFalse(finder.find("java.rhubarb").isPresent());260261// m1 and m2 should be located in dir1262ModuleDescriptor m1 = finder.find("m1").get().descriptor();263assertEquals(m1.version().get().toString(), "1.0");264ModuleDescriptor m2 = finder.find("m2").get().descriptor();265assertEquals(m2.version().get().toString(), "1.0");266267// m3 and m4 should be located in JAR files268ModuleDescriptor m3 = finder.find("m3").get().descriptor();269assertEquals(m3.version().get().toString(), "2.0");270ModuleDescriptor m4 = finder.find("m4").get().descriptor();271assertEquals(m4.version().get().toString(), "2.0");272273// m5 and m6 should be located in JAR files274ModuleDescriptor m5 = finder.find("m5").get().descriptor();275assertEquals(m5.version().get().toString(), "4.0");276ModuleDescriptor m6 = finder.find("m6").get().descriptor();277assertEquals(m6.version().get().toString(), "4.0");278}279280281/**282* Test ModuleFinder.of with a mix of module directories and exploded283* modules.284*/285public void testOfMixDirectoriesAndExplodedModules() throws Exception {286// directory with [email protected] and [email protected]287Path dir1 = Files.createTempDirectory(USER_DIR, "mods1");288createExplodedModule(dir1.resolve("m1"), "[email protected]");289createModularJar(dir1.resolve("m2.jar"), "[email protected]");290291// exploded modules: [email protected], [email protected], [email protected], [email protected]292Path dir2 = Files.createTempDirectory(USER_DIR, "mods2");293Path m1_dir = createExplodedModule(dir2.resolve("m1"), "[email protected]");294Path m2_dir = createExplodedModule(dir2.resolve("m2"), "[email protected]");295Path m3_dir = createExplodedModule(dir2.resolve("m3"), "[email protected]");296Path m4_dir = createExplodedModule(dir2.resolve("m4"), "[email protected]");297298ModuleFinder finder = ModuleFinder.of(dir1, m1_dir, m2_dir, m3_dir, m4_dir);299assertTrue(finder.findAll().size() == 4);300assertTrue(finder.find("m1").isPresent());301assertTrue(finder.find("m2").isPresent());302assertTrue(finder.find("m3").isPresent());303assertTrue(finder.find("m4").isPresent());304assertFalse(finder.find("java.rhubarb").isPresent());305306// m1 and m2 should be located in dir1307ModuleDescriptor m1 = finder.find("m1").get().descriptor();308assertEquals(m1.version().get().toString(), "1.0");309ModuleDescriptor m2 = finder.find("m2").get().descriptor();310assertEquals(m2.version().get().toString(), "1.0");311312// m3 and m4 should be located in dir2313ModuleDescriptor m3 = finder.find("m3").get().descriptor();314assertEquals(m3.version().get().toString(), "2.0");315ModuleDescriptor m4 = finder.find("m4").get().descriptor();316assertEquals(m4.version().get().toString(), "2.0");317}318319320/**321* Test ModuleFinder with a JAR file containing a mix of class and322* non-class resources.323*/324public void testOfOneJarFileWithResources() throws Exception {325Path dir = Files.createTempDirectory(USER_DIR, "mods");326Path jar = createModularJar(dir.resolve("m.jar"), "m",327"LICENSE",328"README",329"WEB-INF/tags",330"p/Type.class",331"p/resources/m.properties",332"q-/Type.class", // not a legal package name333"q-/resources/m/properties");334335ModuleFinder finder = ModuleFinder.of(jar);336Optional<ModuleReference> mref = finder.find("m");337assertTrue(mref.isPresent(), "m1 not found");338339ModuleDescriptor descriptor = mref.get().descriptor();340341assertTrue(descriptor.packages().size() == 2);342assertTrue(descriptor.packages().contains("p"));343assertTrue(descriptor.packages().contains("p.resources"));344}345346347/**348* Test ModuleFinder with an exploded module containing a mix of class349* and non-class resources350*/351public void testOfOneExplodedModuleWithResources() throws Exception {352Path dir = Files.createTempDirectory(USER_DIR, "mods");353Path m_dir = createExplodedModule(dir.resolve("m"), "m",354"LICENSE",355"README",356"WEB-INF/tags",357"p/Type.class",358"p/resources/m.properties",359"q-/Type.class", // not a legal package name360"q-/resources/m/properties");361362ModuleFinder finder = ModuleFinder.of(m_dir);363Optional<ModuleReference> mref = finder.find("m");364assertTrue(mref.isPresent(), "m not found");365366ModuleDescriptor descriptor = mref.get().descriptor();367368assertTrue(descriptor.packages().size() == 2);369assertTrue(descriptor.packages().contains("p"));370assertTrue(descriptor.packages().contains("p.resources"));371}372373374/**375* Test ModuleFinder with a JAR file containing a .class file in the top376* level directory.377*/378public void testOfOneJarFileWithTopLevelClass() throws Exception {379Path dir = Files.createTempDirectory(USER_DIR, "mods");380Path jar = createModularJar(dir.resolve("m.jar"), "m", "Mojo.class");381382ModuleFinder finder = ModuleFinder.of(jar);383try {384finder.find("m");385assertTrue(false);386} catch (FindException e) {387assertTrue(e.getCause() instanceof InvalidModuleDescriptorException);388assertTrue(e.getCause().getMessage().contains("Mojo.class"));389}390391finder = ModuleFinder.of(jar);392try {393finder.findAll();394assertTrue(false);395} catch (FindException e) {396assertTrue(e.getCause() instanceof InvalidModuleDescriptorException);397assertTrue(e.getCause().getMessage().contains("Mojo.class"));398}399}400401/**402* Test ModuleFinder with a JAR file containing a .class file in the top403* level directory.404*/405public void testOfOneExplodedModuleWithTopLevelClass() throws Exception {406Path dir = Files.createTempDirectory(USER_DIR, "mods");407Path m_dir = createExplodedModule(dir.resolve("m"), "m", "Mojo.class");408409ModuleFinder finder = ModuleFinder.of(m_dir);410try {411finder.find("m");412assertTrue(false);413} catch (FindException e) {414assertTrue(e.getCause() instanceof InvalidModuleDescriptorException);415assertTrue(e.getCause().getMessage().contains("Mojo.class"));416}417418finder = ModuleFinder.of(m_dir);419try {420finder.findAll();421assertTrue(false);422} catch (FindException e) {423assertTrue(e.getCause() instanceof InvalidModuleDescriptorException);424assertTrue(e.getCause().getMessage().contains("Mojo.class"));425}426}427428429/**430* Test ModuleFinder.of with a path to a file that does not exist.431*/432public void testOfWithDoesNotExistEntry() throws Exception {433Path dir1 = Files.createTempDirectory(USER_DIR, "mods1");434435Path dir2 = Files.createTempDirectory(USER_DIR, "mods2");436createModularJar(dir2.resolve("m2.jar"), "[email protected]");437438Files.delete(dir1);439440ModuleFinder finder = ModuleFinder.of(dir1, dir2);441442assertTrue(finder.find("m2").isPresent());443assertTrue(finder.findAll().size() == 1);444assertFalse(finder.find("java.rhubarb").isPresent());445}446447448/**449* Test ModuleFinder.of with a file path to an unrecognized file type.450*/451public void testOfWithUnrecognizedEntry() throws Exception {452Path dir = Files.createTempDirectory(USER_DIR, "mods");453Path mod = Files.createTempFile(dir, "m", ".junk");454455ModuleFinder finder = ModuleFinder.of(mod);456try {457finder.find("java.rhubarb");458assertTrue(false);459} catch (FindException e) {460// expected461}462463finder = ModuleFinder.of(mod);464try {465finder.findAll();466assertTrue(false);467} catch (FindException e) {468// expected469}470}471472473/**474* Test ModuleFinder.of with a file path to a directory containing a file475* that will not be recognized as a module.476*/477public void testOfWithUnrecognizedEntryInDirectory1() throws Exception {478Path dir = Files.createTempDirectory(USER_DIR, "mods");479Files.createTempFile(dir, "m", ".junk");480481ModuleFinder finder = ModuleFinder.of(dir);482assertFalse(finder.find("java.rhubarb").isPresent());483484finder = ModuleFinder.of(dir);485assertTrue(finder.findAll().isEmpty());486}487488489/**490* Test ModuleFinder.of with a file path to a directory containing a file491* that will not be recognized as a module.492*/493public void testOfWithUnrecognizedEntryInDirectory2() throws Exception {494Path dir = Files.createTempDirectory(USER_DIR, "mods");495createModularJar(dir.resolve("m1.jar"), "m1");496Files.createTempFile(dir, "m2", ".junk");497498ModuleFinder finder = ModuleFinder.of(dir);499assertTrue(finder.find("m1").isPresent());500assertFalse(finder.find("m2").isPresent());501502finder = ModuleFinder.of(dir);503assertTrue(finder.findAll().size() == 1);504}505506507/**508* Test ModuleFinder.of with a directory that contains two509* versions of the same module510*/511public void testOfDuplicateModulesInDirectory() throws Exception {512Path dir = Files.createTempDirectory(USER_DIR, "mods");513createModularJar(dir.resolve("[email protected]"), "m1");514createModularJar(dir.resolve("[email protected]"), "m1");515516ModuleFinder finder = ModuleFinder.of(dir);517try {518finder.find("m1");519assertTrue(false);520} catch (FindException expected) { }521522finder = ModuleFinder.of(dir);523try {524finder.findAll();525assertTrue(false);526} catch (FindException expected) { }527}528529530/**531* Test ModuleFinder.of with a directory containing hidden files532*/533public void testOfWithHiddenFiles() throws Exception {534Path dir = Files.createTempDirectory(USER_DIR, "mods");535createExplodedModule(dir.resolve("m"), "m",536"com/.ignore",537"com/foo/.ignore",538"com/foo/foo.properties");539540ModuleFinder finder = ModuleFinder.of(dir);541ModuleReference mref = finder.find("m").orElse(null);542assertNotNull(mref);543544Set<String> expectedPackages;545if (System.getProperty("os.name").startsWith("Windows")) {546expectedPackages = Set.of("com", "com.foo");547} else {548expectedPackages = Set.of("com.foo");549}550assertEquals(mref.descriptor().packages(), expectedPackages);551}552553554/**555* Test ModuleFinder.of with a truncated module-info.class556*/557public void testOfWithTruncatedModuleInfo() throws Exception {558Path dir = Files.createTempDirectory(USER_DIR, "mods");559560// create an empty <dir>/rhubarb/module-info.class561Path subdir = Files.createDirectory(dir.resolve("rhubarb"));562Files.createFile(subdir.resolve("module-info.class"));563564ModuleFinder finder = ModuleFinder.of(dir);565try {566finder.find("rhubarb");567assertTrue(false);568} catch (FindException e) {569assertTrue(e.getCause() instanceof InvalidModuleDescriptorException);570}571572finder = ModuleFinder.of(dir);573try {574finder.findAll();575assertTrue(false);576} catch (FindException e) {577assertTrue(e.getCause() instanceof InvalidModuleDescriptorException);578}579}580581582/**583* Test ModuleFinder.compose with no module finders584*/585public void testComposeOfNone() throws Exception {586ModuleFinder finder = ModuleFinder.of();587assertTrue(finder.findAll().isEmpty());588assertFalse(finder.find("java.rhubarb").isPresent());589}590591592/**593* Test ModuleFinder.compose with one module finder594*/595public void testComposeOfOne() throws Exception {596Path dir = Files.createTempDirectory(USER_DIR, "mods");597createModularJar(dir.resolve("m1.jar"), "m1");598createModularJar(dir.resolve("m2.jar"), "m2");599600ModuleFinder finder1 = ModuleFinder.of(dir);601602ModuleFinder finder = ModuleFinder.compose(finder1);603assertTrue(finder.findAll().size() == 2);604assertTrue(finder.find("m1").isPresent());605assertTrue(finder.find("m2").isPresent());606assertFalse(finder.find("java.rhubarb").isPresent());607}608609610/**611* Test ModuleFinder.compose with two module finders612*/613public void testComposeOfTwo() throws Exception {614Path dir1 = Files.createTempDirectory(USER_DIR, "mods1");615createModularJar(dir1.resolve("m1.jar"), "[email protected]");616createModularJar(dir1.resolve("m2.jar"), "[email protected]");617618Path dir2 = Files.createTempDirectory(USER_DIR, "mods2");619createModularJar(dir2.resolve("m1.jar"), "[email protected]");620createModularJar(dir2.resolve("m2.jar"), "[email protected]");621createModularJar(dir2.resolve("m3.jar"), "m3");622createModularJar(dir2.resolve("m4.jar"), "m4");623624ModuleFinder finder1 = ModuleFinder.of(dir1);625ModuleFinder finder2 = ModuleFinder.of(dir2);626627ModuleFinder finder = ModuleFinder.compose(finder1, finder2);628assertTrue(finder.findAll().size() == 4);629assertTrue(finder.find("m1").isPresent());630assertTrue(finder.find("m2").isPresent());631assertTrue(finder.find("m3").isPresent());632assertTrue(finder.find("m4").isPresent());633assertFalse(finder.find("java.rhubarb").isPresent());634635// check that [email protected] is found636ModuleDescriptor m1 = finder.find("m1").get().descriptor();637assertEquals(m1.version().get().toString(), "1.0");638639// check that [email protected] is found640ModuleDescriptor m2 = finder.find("m2").get().descriptor();641assertEquals(m2.version().get().toString(), "1.0");642}643644645/**646* Test ModuleFinder.compose with three module finders647*/648public void testComposeOfThree() throws Exception {649Path dir1 = Files.createTempDirectory(USER_DIR, "mods1");650createModularJar(dir1.resolve("m1.jar"), "[email protected]");651createModularJar(dir1.resolve("m2.jar"), "[email protected]");652653Path dir2 = Files.createTempDirectory(USER_DIR, "mods2");654createModularJar(dir2.resolve("m1.jar"), "[email protected]");655createModularJar(dir2.resolve("m2.jar"), "[email protected]");656createModularJar(dir2.resolve("m3.jar"), "[email protected]");657createModularJar(dir2.resolve("m4.jar"), "[email protected]");658659Path dir3 = Files.createTempDirectory(USER_DIR, "mods3");660createModularJar(dir3.resolve("m3.jar"), "[email protected]");661createModularJar(dir3.resolve("m4.jar"), "[email protected]");662createModularJar(dir3.resolve("m5.jar"), "m5");663createModularJar(dir3.resolve("m6.jar"), "m6");664665ModuleFinder finder1 = ModuleFinder.of(dir1);666ModuleFinder finder2 = ModuleFinder.of(dir2);667ModuleFinder finder3 = ModuleFinder.of(dir3);668669ModuleFinder finder = ModuleFinder.compose(finder1, finder2, finder3);670assertTrue(finder.findAll().size() == 6);671assertTrue(finder.find("m1").isPresent());672assertTrue(finder.find("m2").isPresent());673assertTrue(finder.find("m3").isPresent());674assertTrue(finder.find("m4").isPresent());675assertTrue(finder.find("m5").isPresent());676assertTrue(finder.find("m6").isPresent());677assertFalse(finder.find("java.rhubarb").isPresent());678679// check that [email protected] is found680ModuleDescriptor m1 = finder.find("m1").get().descriptor();681assertEquals(m1.version().get().toString(), "1.0");682683// check that [email protected] is found684ModuleDescriptor m2 = finder.find("m2").get().descriptor();685assertEquals(m2.version().get().toString(), "1.0");686687// check that [email protected] is found688ModuleDescriptor m3 = finder.find("m3").get().descriptor();689assertEquals(m3.version().get().toString(), "2.0");690691// check that [email protected] is found692ModuleDescriptor m4 = finder.find("m4").get().descriptor();693assertEquals(m4.version().get().toString(), "2.0");694}695696697/**698* Test null handling699*/700public void testNulls() {701702// ofSystem703try {704ModuleFinder.ofSystem().find(null);705assertTrue(false);706} catch (NullPointerException expected) { }707708// of709Path dir = Paths.get("d");710try {711ModuleFinder.of().find(null);712assertTrue(false);713} catch (NullPointerException expected) { }714try {715ModuleFinder.of((Path)null);716assertTrue(false);717} catch (NullPointerException expected) { }718try {719ModuleFinder.of((Path[])null);720assertTrue(false);721} catch (NullPointerException expected) { }722try {723ModuleFinder.of(dir, null);724assertTrue(false);725} catch (NullPointerException expected) { }726try {727ModuleFinder.of(null, dir);728assertTrue(false);729} catch (NullPointerException expected) { }730731// compose732ModuleFinder finder = ModuleFinder.of();733try {734ModuleFinder.compose((ModuleFinder)null);735assertTrue(false);736} catch (NullPointerException expected) { }737try {738ModuleFinder.compose((ModuleFinder[])null);739assertTrue(false);740} catch (NullPointerException expected) { }741try {742ModuleFinder.compose(finder, null);743assertTrue(false);744} catch (NullPointerException expected) { }745try {746ModuleFinder.compose(null, finder);747assertTrue(false);748} catch (NullPointerException expected) { }749750}751752753/**754* Parses a string of the form {@code name[@version]} and returns a755* ModuleDescriptor with that name and version. The ModuleDescriptor756* will have a requires on java.base.757*/758static ModuleDescriptor newModuleDescriptor(String mid) {759String mn;760String vs;761int i = mid.indexOf("@");762if (i == -1) {763mn = mid;764vs = null;765} else {766mn = mid.substring(0, i);767vs = mid.substring(i+1);768}769ModuleDescriptor.Builder builder770= ModuleDescriptor.newModule(mn).requires("java.base");771if (vs != null)772builder.version(vs);773return builder.build();774}775776/**777* Creates an exploded module in the given directory and containing a778* module descriptor with the given module name/version.779*/780static Path createExplodedModule(Path dir, String mid, String... entries)781throws Exception782{783ModuleDescriptor descriptor = newModuleDescriptor(mid);784Files.createDirectories(dir);785Path mi = dir.resolve("module-info.class");786try (OutputStream out = Files.newOutputStream(mi)) {787ModuleInfoWriter.write(descriptor, out);788}789790for (String entry : entries) {791Path file = dir.resolve(entry.replace('/', File.separatorChar));792Files.createDirectories(file.getParent());793Files.createFile(file);794}795796return dir;797}798799/**800* Creates a JAR file with the given file path and containing a module801* descriptor with the given module name/version.802*/803static Path createModularJar(Path file, String mid, String ... entries)804throws Exception805{806ModuleDescriptor descriptor = newModuleDescriptor(mid);807try (OutputStream out = Files.newOutputStream(file)) {808try (JarOutputStream jos = new JarOutputStream(out)) {809810JarEntry je = new JarEntry("module-info.class");811jos.putNextEntry(je);812ModuleInfoWriter.write(descriptor, jos);813jos.closeEntry();814815for (String entry : entries) {816je = new JarEntry(entry);817jos.putNextEntry(je);818jos.closeEntry();819}820}821822}823return file;824}825826}827828829830