Path: blob/master/test/jdk/java/lang/module/customfs/ModulesInCustomFileSystem.java
41153 views
/*1* Copyright (c) 2017, 2019, 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 jdk.zipfs26* @library /test/lib27* @build ModulesInCustomFileSystem m1/* m2/*28* jdk.test.lib.util.JarUtils29* @run testng/othervm ModulesInCustomFileSystem30* @summary Test ModuleFinder to find modules in a custom file system31*/3233import java.io.File;34import java.lang.module.Configuration;35import java.lang.module.ModuleFinder;36import java.lang.module.ModuleReader;37import java.lang.module.ModuleReference;38import java.lang.reflect.Method;39import java.nio.file.FileSystem;40import java.nio.file.FileSystems;41import java.nio.file.Files;42import java.nio.file.Path;43import java.nio.file.Paths;44import java.util.Set;4546import jdk.test.lib.util.JarUtils;4748import org.testng.annotations.Test;49import static org.testng.Assert.*;5051@Test52public class ModulesInCustomFileSystem {53private static final Path HERE = Paths.get("");5455/**56* Test exploded modules in a Zip file system.57*/58public void testExplodedModulesInZipFileSystem() throws Exception {59Path m1 = findModuleDirectory("m1");60Path m2 = findModuleDirectory("m2");61Path mlib = m1.getParent();62assertEquals(mlib, m2.getParent());6364// create JAR file containing m1/** and m2/**65Path jar = Files.createTempDirectory(HERE, "mlib").resolve("modules.jar");66JarUtils.createJarFile(jar, mlib);67testZipFileSystem(jar);68}6970/**71* Test modular JARs in a Zip file system.72*/73public void testModularJARsInZipFileSystem() throws Exception {74Path m1 = findModuleDirectory("m1");75Path m2 = findModuleDirectory("m2");76Path contents = Files.createTempDirectory(HERE, "contents");77JarUtils.createJarFile(contents.resolve("m1.jar"), m1);78JarUtils.createJarFile(contents.resolve("m2.jar"), m2);7980// create JAR file containing m1.jar and m2.jar81Path jar = Files.createTempDirectory(HERE, "mlib").resolve("modules.jar");82JarUtils.createJarFile(jar, contents);83testZipFileSystem(jar);84}8586/**87* Opens a JAR file as a file system88*/89private void testZipFileSystem(Path zip) throws Exception {90try (FileSystem fs = FileSystems.newFileSystem(zip)) {91// ModuleFinder to find modules in top-level directory92Path top = fs.getPath("/");93ModuleFinder finder = ModuleFinder.of(top);9495// list the modules96listAllModules(finder);9798// load modules into child layer, invoking m1/p.Main99loadAndRunModule(finder);100}101}102103/**104* List all modules that the finder finds and the resources in the module.105*/106private void listAllModules(ModuleFinder finder) throws Exception {107for (ModuleReference mref : finder.findAll()) {108System.out.println(mref.descriptor());109try (ModuleReader reader = mref.open()) {110reader.list().forEach(name -> System.out.format(" %s%n", name));111}112}113}114115/**116* Creates a child layer with m1 and m2, invokes m1/p.Main to ensure that117* classes can be loaded.118*/119private void loadAndRunModule(ModuleFinder finder) throws Exception {120ModuleLayer bootLayer = ModuleLayer.boot();121Configuration cf = bootLayer.configuration()122.resolve(finder, ModuleFinder.of(), Set.of("m1"));123ClassLoader scl = ClassLoader.getSystemClassLoader();124ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);125Class<?> c = layer.findLoader("m1").loadClass("p.Main");126Method m = c.getMethod("main", String[].class);127m.invoke(null, (Object)new String[0]);128}129130/**131* Find the directory for a module on the module path132*/133private Path findModuleDirectory(String name) {134String mp = System.getProperty("jdk.module.path");135for (String element : mp.split(File.pathSeparator)) {136Path dir = Paths.get(element).resolve(name);137if (Files.exists(dir)) {138return dir;139}140}141assertFalse(true);142return null;143}144}145146147