31 */ 32 33 import java.io.File; 34 import java.lang.module.Configuration; 35 import java.lang.module.ModuleFinder; 36 import java.lang.module.ModuleReader; 37 import java.lang.module.ModuleReference; 38 import java.lang.reflect.Method; 39 import java.nio.file.FileSystem; 40 import java.nio.file.FileSystems; 41 import java.nio.file.Files; 42 import java.nio.file.Path; 43 import java.nio.file.Paths; 44 import java.util.Set; 45 46 import org.testng.annotations.Test; 47 import static org.testng.Assert.*; 48 49 @Test 50 public class ModulesInCustomFileSystem { 51 52 /** 53 * Test exploded modules in a JAR file system. 54 */ 55 public void testExplodedModulesInJarFileSystem() throws Exception { 56 Path m1 = findModuleDirectory("m1"); 57 Path m2 = findModuleDirectory("m2"); 58 Path mlib = m1.getParent(); 59 assertEquals(mlib, m2.getParent()); 60 61 // create JAR file containing m1/** and m2/** 62 Path jar = Files.createTempDirectory("mlib").resolve("modules.jar"); 63 JarUtils.createJarFile(jar, mlib); 64 testJarFileSystem(jar); 65 } 66 67 /** 68 * Test modular JARs in a JAR file system 69 */ 70 public void testModularJARsInJarFileSystem() throws Exception { 71 Path m1 = findModuleDirectory("m1"); 72 Path m2 = findModuleDirectory("m2"); 73 Path contents = Files.createTempDirectory("contents"); 74 JarUtils.createJarFile(contents.resolve("m1.jar"), m1); 75 JarUtils.createJarFile(contents.resolve("m2.jar"), m2); 76 77 // create JAR file containing m1.jar and m2.jar 78 Path jar = Files.createTempDirectory("mlib").resolve("modules.jar"); 79 JarUtils.createJarFile(jar, contents); 80 testJarFileSystem(jar); 81 } 82 83 /** 84 * Opens a JAR file as a file system 85 */ 86 private void testJarFileSystem(Path jar) throws Exception { 87 ClassLoader scl = ClassLoader.getSystemClassLoader(); 88 try (FileSystem fs = FileSystems.newFileSystem(jar, scl)) { 89 // ModuleFinder to find modules in top-level directory 90 Path top = fs.getPath("/"); 91 ModuleFinder finder = ModuleFinder.of(top); 92 93 // list the modules 94 listAllModules(finder); 95 96 // load modules into child layer, invoking m1/p.Main 97 loadAndRunModule(finder); 98 } | 31 */ 32 33 import java.io.File; 34 import java.lang.module.Configuration; 35 import java.lang.module.ModuleFinder; 36 import java.lang.module.ModuleReader; 37 import java.lang.module.ModuleReference; 38 import java.lang.reflect.Method; 39 import java.nio.file.FileSystem; 40 import java.nio.file.FileSystems; 41 import java.nio.file.Files; 42 import java.nio.file.Path; 43 import java.nio.file.Paths; 44 import java.util.Set; 45 46 import org.testng.annotations.Test; 47 import static org.testng.Assert.*; 48 49 @Test 50 public class ModulesInCustomFileSystem { 51 private static final Path HERE = Paths.get(""); 52 53 /** 54 * Test exploded modules in a JAR file system. 55 */ 56 public void testExplodedModulesInJarFileSystem() throws Exception { 57 Path m1 = findModuleDirectory("m1"); 58 Path m2 = findModuleDirectory("m2"); 59 Path mlib = m1.getParent(); 60 assertEquals(mlib, m2.getParent()); 61 62 // create JAR file containing m1/** and m2/** 63 Path jar = Files.createTempDirectory(HERE, "mlib").resolve("modules.jar"); 64 JarUtils.createJarFile(jar, mlib); 65 testJarFileSystem(jar); 66 } 67 68 /** 69 * Test modular JARs in a JAR file system 70 */ 71 public void testModularJARsInJarFileSystem() throws Exception { 72 Path m1 = findModuleDirectory("m1"); 73 Path m2 = findModuleDirectory("m2"); 74 Path contents = Files.createTempDirectory(HERE, "contents"); 75 JarUtils.createJarFile(contents.resolve("m1.jar"), m1); 76 JarUtils.createJarFile(contents.resolve("m2.jar"), m2); 77 78 // create JAR file containing m1.jar and m2.jar 79 Path jar = Files.createTempDirectory(HERE, "mlib").resolve("modules.jar"); 80 JarUtils.createJarFile(jar, contents); 81 testJarFileSystem(jar); 82 } 83 84 /** 85 * Opens a JAR file as a file system 86 */ 87 private void testJarFileSystem(Path jar) throws Exception { 88 ClassLoader scl = ClassLoader.getSystemClassLoader(); 89 try (FileSystem fs = FileSystems.newFileSystem(jar, scl)) { 90 // ModuleFinder to find modules in top-level directory 91 Path top = fs.getPath("/"); 92 ModuleFinder finder = ModuleFinder.of(top); 93 94 // list the modules 95 listAllModules(finder); 96 97 // load modules into child layer, invoking m1/p.Main 98 loadAndRunModule(finder); 99 } |