Path: blob/master/test/jdk/java/lang/ModuleLayer/automatic/AutomaticModulesTest.java
41153 views
/*1* Copyright (c) 2018, 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 821182526* @modules jdk.compiler27* @library /test/lib28* @build jdk.test.lib.compiler.CompilerUtils jdk.test.lib.util.JarUtils29* @run testng/othervm AutomaticModulesTest30* @summary Tests automatic modules in module layers31*/3233import java.nio.file.Files;34import java.nio.file.Path;35import java.lang.ModuleLayer.Controller;36import java.lang.module.*;37import java.lang.reflect.Method;38import java.util.List;39import java.util.Set;4041import jdk.test.lib.compiler.CompilerUtils;42import jdk.test.lib.util.JarUtils;4344import org.testng.annotations.BeforeTest;45import org.testng.annotations.Test;46import static org.testng.Assert.*;4748/**49* This test uses two modules:50* m requires alib and has an entry point p.Main51* alib is an automatic module52*/5354@Test55public class AutomaticModulesTest {5657private static final String TEST_SRC = System.getProperty("test.src");5859private static final Path CLASSES = Path.of("classes");60private static final Path LIB = Path.of("lib");61private static final Path MODS = Path.of("mods");6263@BeforeTest64public void setup() throws Exception {65// javac -d classes src/alib/**66// jar cf lib/alib.jar -C classes .67Files.createDirectory(CLASSES);68assertTrue(CompilerUtils.compile(Path.of(TEST_SRC, "src", "alib"), CLASSES));69JarUtils.createJarFile(LIB.resolve("alib.jar"), CLASSES);7071// javac -p lib -d mods/m - src/m/**72Path src = Path.of(TEST_SRC, "src", "m");73Path output = Files.createDirectories(MODS.resolve("m"));74assertTrue(CompilerUtils.compile(src, output, "-p", LIB.toString()));75}7677/**78* Create a module layer with modules m and alib mapped to the same class79* loader.80*/81public void testOneLoader() throws Exception {82Configuration cf = ModuleLayer.boot()83.configuration()84.resolve(ModuleFinder.of(), ModuleFinder.of(MODS, LIB), Set.of("m"));85ResolvedModule m = cf.findModule("m").orElseThrow();86ResolvedModule alib = cf.findModule("alib").orElseThrow();87assertTrue(m.reads().contains(alib));88assertTrue(alib.reference().descriptor().isAutomatic());89ModuleLayer bootLayer = ModuleLayer.boot();90ClassLoader scl = ClassLoader.getSystemClassLoader();91Controller controller = ModuleLayer.defineModulesWithOneLoader(cf, List.of(bootLayer), scl);92invokeMain(controller, "m/p.Main");93}9495/**96* Create a module layer with modules m and alib mapped to different class97* loaders. This will test that L(m) delegates to L(alib) in the same layer.98*/99public void testManyLoaders() throws Exception {100Configuration cf = ModuleLayer.boot()101.configuration()102.resolve(ModuleFinder.of(), ModuleFinder.of(MODS, LIB), Set.of("m"));103ResolvedModule m = cf.findModule("m").orElseThrow();104ResolvedModule alib = cf.findModule("alib").orElseThrow();105assertTrue(m.reads().contains(alib));106assertTrue(alib.reference().descriptor().isAutomatic());107ModuleLayer bootLayer = ModuleLayer.boot();108ClassLoader scl = ClassLoader.getSystemClassLoader();109Controller controller = ModuleLayer.defineModulesWithManyLoaders(cf, List.of(bootLayer), scl);110invokeMain(controller, "m/p.Main");111}112113/**114* Create a module layer with alib and another module layer with m.115* This will test that L(m) delegates to L(alib) in a parent layer.116*/117public void testAutomaticModuleInParent() throws Exception {118ModuleLayer bootLayer = ModuleLayer.boot();119ClassLoader scl = ClassLoader.getSystemClassLoader();120121// configuration/layer containing alib122Configuration cf1 = bootLayer123.configuration()124.resolve(ModuleFinder.of(), ModuleFinder.of(LIB), Set.of("alib"));125ModuleLayer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl);126127// configuration/layer containing m128Configuration cf2 = cf1.resolve(ModuleFinder.of(), ModuleFinder.of(MODS), Set.of("m"));129Controller controller = ModuleLayer.defineModulesWithOneLoader(cf2, List.of(layer1), scl);130131invokeMain(controller, "m/p.Main");132}133134/**135* Invokes the main method of the given entry point (module-name/class-name)136*/137private void invokeMain(Controller controller, String entry) throws Exception {138String[] s = entry.split("/");139String moduleName = s[0];140String className = s[1];141int pos = className.lastIndexOf('.');142String packageName = className.substring(0, pos);143ModuleLayer layer = controller.layer();144Module module = layer.findModule(moduleName).orElseThrow();145controller.addExports(module, packageName, this.getClass().getModule());146ClassLoader loader = layer.findLoader(moduleName);147Class<?> c = loader.loadClass(className);148Method m = c.getMethod("main", String[].class);149m.invoke(null, (Object)new String[0]);150}151}152153154