Path: blob/master/test/jdk/tools/jlink/plugins/SystemModuleDescriptors/UserModuleTest.java
41153 views
/*1* Copyright (c) 2016, 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*/2223import java.io.File;24import java.io.IOException;25import java.nio.file.Files;26import java.nio.file.Path;27import java.nio.file.Paths;28import java.util.Arrays;29import java.util.Set;30import java.util.spi.ToolProvider;31import java.util.stream.Collectors;32import java.util.stream.Stream;3334import jdk.test.lib.compiler.CompilerUtils;35import jdk.test.lib.util.FileUtils;3637import static jdk.test.lib.process.ProcessTools.*;3839import org.testng.annotations.BeforeTest;40import org.testng.annotations.Test;41import static org.testng.Assert.*;4243/**44* @test45* @bug 8142968 8173381 817474046* @library /test/lib47* @modules jdk.compiler jdk.jlink48* @modules java.base/jdk.internal.module49* @modules java.base/jdk.internal.org.objectweb.asm50* @build jdk.test.lib.compiler.CompilerUtils51* jdk.test.lib.util.FileUtils52* jdk.test.lib.Platform53* ModuleTargetHelper UserModuleTest jdk.test.lib.process.ProcessTools54* @run testng UserModuleTest55*/5657public class UserModuleTest {58private static final String JAVA_HOME = System.getProperty("java.home");59private static final String TEST_SRC = System.getProperty("test.src");6061private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");62private static final Path MODS_DIR = Paths.get("mods");63private static final Path JMODS_DIR = Paths.get("jmods");6465private static final Path IMAGE = Paths.get("image");66private static final String MAIN_MID = "m1/p1.Main";6768// the names of the modules in this test69private static String[] modules = new String[] {"m1", "m2", "m3", "m4", "m5"};707172private static boolean hasJmods() {73if (!Files.exists(Paths.get(JAVA_HOME, "jmods"))) {74System.err.println("Test skipped. NO jmods directory");75return false;76}77return true;78}7980/*81* Compiles all modules used by the test82*/83@BeforeTest84public void compileAll() throws Throwable {85if (!hasJmods()) return;8687for (String mn : modules) {88Path msrc = SRC_DIR.resolve(mn);89assertTrue(CompilerUtils.compile(msrc, MODS_DIR,90"--module-source-path", SRC_DIR.toString(),91"--add-exports", "java.base/jdk.internal.module=" + mn,92"--add-exports", "java.base/jdk.internal.org.objectweb.asm=" + mn));93}9495if (Files.exists(IMAGE)) {96FileUtils.deleteFileTreeUnchecked(IMAGE);97}9899createImage(IMAGE, "m1", "m3");100101createJmods("m1", "m4");102}103104/*105* Test the image created when linking with a module with106* no Packages attribute107*/108@Test109public void testPackagesAttribute() throws Throwable {110if (!hasJmods()) return;111112Path java = IMAGE.resolve("bin").resolve("java");113assertTrue(executeProcess(java.toString(),114"--add-exports", "java.base/jdk.internal.module=m1,m4",115"--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1,m4",116"-m", MAIN_MID)117.outputTo(System.out)118.errorTo(System.out)119.getExitValue() == 0);120}121122/*123* Test the image created when linking with an open module124*/125@Test126public void testOpenModule() throws Throwable {127if (!hasJmods()) return;128129Path java = IMAGE.resolve("bin").resolve("java");130assertTrue(executeProcess(java.toString(), "-m", "m3/p3.Main")131.outputTo(System.out)132.errorTo(System.out)133.getExitValue() == 0);134}135136/*137* Disable the fast loading of system modules.138* Parsing module-info.class139*/140@Test141public void disableSystemModules() throws Throwable {142if (!hasJmods()) return;143144Path java = IMAGE.resolve("bin").resolve("java");145assertTrue(executeProcess(java.toString(),146"--add-exports", "java.base/jdk.internal.module=m1,m4",147"--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1,m4",148"-Djdk.system.module.finder.disabledFastPath",149"-m", MAIN_MID)150.outputTo(System.out)151.errorTo(System.out)152.getExitValue() == 0);153}154155/*156* Test the optimization that deduplicates Set<String> on targets of exports,157* uses, provides.158*/159@Test160public void testDedupSet() throws Throwable {161if (!hasJmods()) return;162163Path dir = Paths.get("dedupSetTest");164createImage(dir, "m1", "m2", "m3", "m4");165Path java = dir.resolve("bin").resolve("java");166assertTrue(executeProcess(java.toString(),167"--add-exports", "java.base/jdk.internal.module=m1,m4",168"--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1,m4",169"-m", MAIN_MID)170.outputTo(System.out)171.errorTo(System.out)172.getExitValue() == 0);173}174175@Test176public void testRequiresStatic() throws Throwable {177if (!hasJmods()) return;178179Path dir = Paths.get("requiresStatic");180createImage(dir, "m5");181Path java = dir.resolve("bin").resolve("java");182assertTrue(executeProcess(java.toString(), "-m", "m5/p5.Main")183.outputTo(System.out)184.errorTo(System.out)185.getExitValue() == 0);186187// run with m3 present188assertTrue(executeProcess(java.toString(),189"--module-path", MODS_DIR.toString(),190"--add-modules", "m3",191"-m", "m5/p5.Main")192.outputTo(System.out)193.errorTo(System.out)194.getExitValue() == 0);195}196197@Test198public void testRequiresStatic2() throws Throwable {199if (!hasJmods()) return;200201Path dir = Paths.get("requiresStatic2");202createImage(dir, "m3", "m5");203204Path java = dir.resolve("bin").resolve("java");205assertTrue(executeProcess(java.toString(), "-m", "m5/p5.Main")206.outputTo(System.out)207.errorTo(System.out)208.getExitValue() == 0);209210// boot layer with m3 and m5211assertTrue(executeProcess(java.toString(),212"--add-modules", "m3",213"-m", "m5/p5.Main")214.outputTo(System.out)215.errorTo(System.out)216.getExitValue() == 0);217}218219private void createJmods(String... modules) throws IOException {220ModuleTargetHelper.ModuleTarget mt = ModuleTargetHelper.getJavaBaseTarget();221if (mt == null) {222throw new RuntimeException("ModuleTarget is missing for java.base");223}224225String[] values = mt.targetPlatform().split("-");226String osName = values[0];227String osArch = values[1];228229// create JMOD files230Files.createDirectories(JMODS_DIR);231Stream.of(modules).forEach(mn ->232assertTrue(jmod("create",233"--class-path", MODS_DIR.resolve(mn).toString(),234"--target-platform", mt.targetPlatform(),235"--main-class", mn.replace('m', 'p') + ".Main",236JMODS_DIR.resolve(mn + ".jmod").toString()) == 0)237);238}239240241/**242* Verify the module descriptor if package p4.dummy is excluded at link time.243*/244@Test245public void testModulePackagesAttribute() throws Throwable {246if (!hasJmods()) return;247248// create an image using JMOD files249Path dir = Paths.get("packagesTest");250String mp = Paths.get(JAVA_HOME, "jmods").toString() +251File.pathSeparator + JMODS_DIR.toString();252253Set<String> modules = Set.of("m1", "m4");254assertTrue(JLINK_TOOL.run(System.out, System.out,255"--output", dir.toString(),256"--exclude-resources", "m4/p4/dummy/*",257"--add-modules", modules.stream().collect(Collectors.joining(",")),258"--module-path", mp) == 0);259260// verify ModuleDescriptor261Path java = dir.resolve("bin").resolve("java");262assertTrue(executeProcess(java.toString(),263"--add-exports", "java.base/jdk.internal.module=m1,m4",264"--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1,m4",265"--add-modules=m1", "-m", "m4")266.outputTo(System.out)267.errorTo(System.out)268.getExitValue() == 0);269}270271/**272* Verify the plugin to retain ModuleTarget attribute273*/274@Test275public void testRetainModuleTarget() throws Throwable {276if (!hasJmods()) return;277278// create an image using JMOD files279Path dir = Paths.get("retainModuleTargetTest");280String mp = Paths.get(JAVA_HOME, "jmods").toString() +281File.pathSeparator + JMODS_DIR.toString();282283Set<String> modules = Set.of("m1", "m4");284assertTrue(JLINK_TOOL.run(System.out, System.out,285"--output", dir.toString(),286"--exclude-resources", "m4/p4/dummy/*",287"--add-modules", modules.stream().collect(Collectors.joining(",")),288"--module-path", mp) == 0);289290// verify ModuleDescriptor291Path java = dir.resolve("bin").resolve("java");292assertTrue(executeProcess(java.toString(),293"--add-exports", "java.base/jdk.internal.module=m1,m4",294"--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1,m4",295"--add-modules=m1", "-m", "m4", "retainModuleTarget")296.outputTo(System.out)297.errorTo(System.out)298.getExitValue() == 0);299}300301static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")302.orElseThrow(() ->303new RuntimeException("jlink tool not found")304);305306static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")307.orElseThrow(() ->308new RuntimeException("jmod tool not found")309);310311static final String MODULE_PATH = Paths.get(JAVA_HOME, "jmods").toString()312+ File.pathSeparator + MODS_DIR.toString();313314private void createImage(Path outputDir, String... modules) throws Throwable {315assertTrue(JLINK_TOOL.run(System.out, System.out,316"--output", outputDir.toString(),317"--add-modules", Arrays.stream(modules).collect(Collectors.joining(",")),318"--module-path", MODULE_PATH) == 0);319}320321private static int jmod(String... options) {322System.out.println("jmod " + Arrays.asList(options));323return JMOD_TOOL.run(System.out, System.out, options);324}325}326327328