Path: blob/master/test/jdk/java/lang/module/customfs/ModulesInCustomFileSystem.java
66645 views
/*1* Copyright (c) 2017, 2022, 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* @bug 8178380 828244426* @modules jdk.zipfs27* @library /test/lib28* @build ModulesInCustomFileSystem m1/* m2/*29* jdk.test.lib.util.JarUtils30* @run testng/othervm ModulesInCustomFileSystem31* @summary Test ModuleFinder to find modules in a custom file system32*/3334import java.io.File;35import java.lang.module.Configuration;36import java.lang.module.ModuleFinder;37import java.lang.module.ModuleReader;38import java.lang.module.ModuleReference;39import java.lang.reflect.Method;40import java.nio.file.FileSystem;41import java.nio.file.FileSystems;42import java.nio.file.Files;43import java.nio.file.Path;44import java.nio.file.Paths;45import java.util.Set;4647import jdk.test.lib.util.JarUtils;4849import org.testng.annotations.Test;50import static org.testng.Assert.*;5152@Test53public class ModulesInCustomFileSystem {54private static final Path HERE = Paths.get("");5556/**57* Test exploded modules in a Zip file system.58*/59public void testExplodedModulesInZipFileSystem() throws Exception {60Path m1 = findModuleDirectory("m1");61Path m2 = findModuleDirectory("m2");62Path mlib = m1.getParent();63assertEquals(mlib, m2.getParent());6465// create JAR file containing m1/** and m2/**66Path jar = Files.createTempDirectory(HERE, "mlib").resolve("modules.jar");67JarUtils.createJarFile(jar, mlib);68testZipFileSystem(jar);69}7071/**72* Test modular JARs in a Zip file system.73*/74public void testModularJARsInZipFileSystem() throws Exception {75Path m1 = findModuleDirectory("m1");76Path m2 = findModuleDirectory("m2");77Path contents = Files.createTempDirectory(HERE, "contents");78JarUtils.createJarFile(contents.resolve("m1.jar"), m1);79JarUtils.createJarFile(contents.resolve("m2.jar"), m2);8081// create JAR file containing m1.jar and m2.jar82Path jar = Files.createTempDirectory(HERE, "mlib").resolve("modules.jar");83JarUtils.createJarFile(jar, contents);84testZipFileSystem(jar);85}8687/**88* Opens a JAR file as a file system89*/90private void testZipFileSystem(Path zip) throws Exception {91try (FileSystem fs = FileSystems.newFileSystem(zip)) {92// ModuleFinder to find modules in top-level directory93Path top = fs.getPath("/");94ModuleFinder finder = ModuleFinder.of(top);9596// list the modules97listAllModules(finder);9899// load modules into child layer, invoking m1/p.Main100loadAndRunModule(finder);101}102}103104/**105* List all modules that the finder finds and the resources in the module.106*/107private void listAllModules(ModuleFinder finder) throws Exception {108for (ModuleReference mref : finder.findAll()) {109System.out.println(mref.descriptor());110try (ModuleReader reader = mref.open()) {111reader.list().forEach(name -> System.out.format(" %s%n", name));112}113}114}115116/**117* Creates a child layer with m1 and m2, invokes m1/p.Main to ensure that118* classes can be loaded.119*/120private void loadAndRunModule(ModuleFinder finder) throws Exception {121ModuleLayer bootLayer = ModuleLayer.boot();122Configuration cf = bootLayer.configuration()123.resolve(finder, ModuleFinder.of(), Set.of("m1"));124ClassLoader scl = ClassLoader.getSystemClassLoader();125ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);126Class<?> c = layer.findLoader("m1").loadClass("p.Main");127Method m = c.getMethod("main", String[].class);128m.invoke(null, (Object)new String[0]);129}130131/**132* Find the directory for a module on the module path133*/134private Path findModuleDirectory(String name) {135String mp = System.getProperty("jdk.module.path");136for (String element : mp.split(File.pathSeparator)) {137Path dir = Paths.get(element).resolve(name);138if (Files.exists(dir)) {139return dir;140}141}142assertFalse(true);143return null;144}145}146147148