1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8211825
* @modules jdk.compiler
* @library /test/lib
* @build jdk.test.lib.compiler.CompilerUtils jdk.test.lib.util.JarUtils
* @run testng/othervm AutomaticModulesTest
* @summary Tests automatic modules in module layers
*/
import java.nio.file.Files;
import java.nio.file.Path;
import java.lang.ModuleLayer.Controller;
import java.lang.module.*;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.util.JarUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/**
* This test uses two modules:
* m requires alib and has an entry point p.Main
* alib is an automatic module
*/
@Test
public class AutomaticModulesTest {
private static final String TEST_SRC = System.getProperty("test.src");
private static final Path CLASSES = Path.of("classes");
private static final Path LIB = Path.of("lib");
private static final Path MODS = Path.of("mods");
@BeforeTest
public void setup() throws Exception {
// javac -d classes src/alib/**
// jar cf lib/alib.jar -C classes .
Files.createDirectory(CLASSES);
assertTrue(CompilerUtils.compile(Path.of(TEST_SRC, "src", "alib"), CLASSES));
JarUtils.createJarFile(LIB.resolve("alib.jar"), CLASSES);
// javac -p lib -d mods/m - src/m/**
Path src = Path.of(TEST_SRC, "src", "m");
Path output = Files.createDirectories(MODS.resolve("m"));
assertTrue(CompilerUtils.compile(src, output, "-p", LIB.toString()));
}
/**
* Create a module layer with modules m and alib mapped to the same class
* loader.
*/
public void testOneLoader() throws Exception {
Configuration cf = ModuleLayer.boot()
.configuration()
.resolve(ModuleFinder.of(), ModuleFinder.of(MODS, LIB), Set.of("m"));
ResolvedModule m = cf.findModule("m").orElseThrow();
ResolvedModule alib = cf.findModule("alib").orElseThrow();
assertTrue(m.reads().contains(alib));
assertTrue(alib.reference().descriptor().isAutomatic());
ModuleLayer bootLayer = ModuleLayer.boot();
ClassLoader scl = ClassLoader.getSystemClassLoader();
Controller controller = ModuleLayer.defineModulesWithOneLoader(cf, List.of(bootLayer), scl);
invokeMain(controller, "m/p.Main");
}
/**
* Create a module layer with modules m and alib mapped to different class
* loaders. This will test that L(m) delegates to L(alib) in the same layer.
*/
public void testManyLoaders() throws Exception {
Configuration cf = ModuleLayer.boot()
.configuration()
.resolve(ModuleFinder.of(), ModuleFinder.of(MODS, LIB), Set.of("m"));
ResolvedModule m = cf.findModule("m").orElseThrow();
ResolvedModule alib = cf.findModule("alib").orElseThrow();
assertTrue(m.reads().contains(alib));
assertTrue(alib.reference().descriptor().isAutomatic());
ModuleLayer bootLayer = ModuleLayer.boot();
ClassLoader scl = ClassLoader.getSystemClassLoader();
Controller controller = ModuleLayer.defineModulesWithManyLoaders(cf, List.of(bootLayer), scl);
invokeMain(controller, "m/p.Main");
}
/**
* Create a module layer with alib and another module layer with m.
* This will test that L(m) delegates to L(alib) in a parent layer.
*/
public void testAutomaticModuleInParent() throws Exception {
ModuleLayer bootLayer = ModuleLayer.boot();
ClassLoader scl = ClassLoader.getSystemClassLoader();
// configuration/layer containing alib
Configuration cf1 = bootLayer
.configuration()
.resolve(ModuleFinder.of(), ModuleFinder.of(LIB), Set.of("alib"));
ModuleLayer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl);
// configuration/layer containing m
Configuration cf2 = cf1.resolve(ModuleFinder.of(), ModuleFinder.of(MODS), Set.of("m"));
Controller controller = ModuleLayer.defineModulesWithOneLoader(cf2, List.of(layer1), scl);
invokeMain(controller, "m/p.Main");
}
/**
* Invokes the main method of the given entry point (module-name/class-name)
*/
private void invokeMain(Controller controller, String entry) throws Exception {
String[] s = entry.split("/");
String moduleName = s[0];
String className = s[1];
int pos = className.lastIndexOf('.');
String packageName = className.substring(0, pos);
ModuleLayer layer = controller.layer();
Module module = layer.findModule(moduleName).orElseThrow();
controller.addExports(module, packageName, this.getClass().getModule());
ClassLoader loader = layer.findLoader(moduleName);
Class<?> c = loader.loadClass(className);
Method m = c.getMethod("main", String[].class);
m.invoke(null, (Object)new String[0]);
}
}
|