Path: blob/master/test/jdk/tools/launcher/modules/addexports/AddExportsTest.java
41153 views
/*1* Copyright (c) 2014, 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* @library /test/lib26* @modules java.compiler27* jdk.compiler28* @build AddExportsTest jdk.test.lib.compiler.CompilerUtils29* @run testng AddExportsTest30* @summary Basic tests for java --add-exports31*/3233import java.nio.file.Path;34import java.nio.file.Paths;35import java.util.stream.Stream;3637import jdk.test.lib.compiler.CompilerUtils;38import jdk.test.lib.process.OutputAnalyzer;39import static jdk.test.lib.process.ProcessTools.*;4041import org.testng.annotations.BeforeTest;42import org.testng.annotations.DataProvider;43import org.testng.annotations.Test;44import static org.testng.Assert.*;454647@Test48public class AddExportsTest {4950private static final String TEST_SRC = System.getProperty("test.src");5152private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");53private static final Path MODS_DIR = Paths.get("mods");54private static final Path UPGRADE_MODS_DIRS = Paths.get("upgrademods");5556// test module m1 that uses Unsafe57private static final String TEST1_MODULE = "m1";58private static final String TEST1_MAIN_CLASS = "jdk.test1.Main";5960// test module m2 uses java.compiler internals61private static final String TEST2_MODULE = "m2";62private static final String TEST2_MAIN_CLASS = "jdk.test2.Main";6364// test module m3 uses m4 internals65private static final String TEST3_MODULE = "m3";66private static final String TEST3_MAIN_CLASS = "jdk.test3.Main";67private static final String TEST4_MODULE = "m4";686970@BeforeTest71public void compileTestModules() throws Exception {7273// javac -d mods/m1 src/m1/**74boolean compiled = CompilerUtils.compile(75SRC_DIR.resolve(TEST1_MODULE),76MODS_DIR.resolve(TEST1_MODULE),77"--add-exports", "java.base/jdk.internal.misc=m1");78assertTrue(compiled, "module " + TEST1_MODULE + " did not compile");7980// javac -d upgrademods/java.compiler src/java.compiler/**81compiled = CompilerUtils.compile(82SRC_DIR.resolve("java.compiler"),83UPGRADE_MODS_DIRS.resolve("java.compiler"));84assertTrue(compiled, "module java.compiler did not compile");8586// javac --upgrade-module-path upgrademods -d mods/m2 src/m2/**87compiled = CompilerUtils.compile(88SRC_DIR.resolve(TEST2_MODULE),89MODS_DIR.resolve(TEST2_MODULE),90"--upgrade-module-path", UPGRADE_MODS_DIRS.toString(),91"--add-exports", "java.compiler/javax.tools.internal=m2");92assertTrue(compiled, "module " + TEST2_MODULE + " did not compile");9394// javac -d mods/m3 src/m3/**95compiled = CompilerUtils.compile(96SRC_DIR.resolve(TEST3_MODULE),97MODS_DIR.resolve(TEST3_MODULE));98assertTrue(compiled, "module " + TEST3_MODULE + " did not compile");99100// javac -d mods/m4 src/m4/**101compiled = CompilerUtils.compile(102SRC_DIR.resolve(TEST4_MODULE),103MODS_DIR.resolve(TEST4_MODULE));104assertTrue(compiled, "module " + TEST4_MODULE + " did not compile");105}106107/**108* Sanity check with -version109*/110public void testSanity() throws Exception {111112int exitValue113= executeTestJava("--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED",114"-version")115.outputTo(System.out)116.errorTo(System.out)117.getExitValue();118119assertTrue(exitValue == 0);120}121122123/**124* Run class path application that uses jdk.internal.misc.Unsafe125*/126public void testUnnamedModule() throws Exception {127128// java --add-exports java.base/jdk.internal.misc=ALL-UNNAMED \129// -cp mods/$TESTMODULE jdk.test.UsesUnsafe130131String classpath = MODS_DIR.resolve(TEST1_MODULE).toString();132int exitValue133= executeTestJava("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED",134"-cp", classpath,135TEST1_MAIN_CLASS)136.outputTo(System.out)137.errorTo(System.out)138.getExitValue();139140assertTrue(exitValue == 0);141}142143144/**145* Run named module that uses jdk.internal.misc.Unsafe146*/147public void testNamedModule() throws Exception {148149// java --add-exports java.base/jdk.internal.misc=test \150// --module-path mods -m $TESTMODULE/$MAIN_CLASS151152String mid = TEST1_MODULE + "/" + TEST1_MAIN_CLASS;153int exitValue =154executeTestJava("--add-exports", "java.base/jdk.internal.misc=" + TEST1_MODULE,155"--module-path", MODS_DIR.toString(),156"-m", mid)157.outputTo(System.out)158.errorTo(System.out)159.getExitValue();160161assertTrue(exitValue == 0);162}163164/**165* Test --add-exports with upgraded module166*/167public void testWithUpgradedModule() throws Exception {168169// java --add-exports java.compiler/javax.tools.internal=m2170// --upgrade-module-path upgrademods --module-path mods -m ...171String mid = TEST2_MODULE + "/" + TEST2_MAIN_CLASS;172int exitValue = executeTestJava(173"--add-exports", "java.compiler/javax.tools.internal=m2",174"--upgrade-module-path", UPGRADE_MODS_DIRS.toString(),175"--module-path", MODS_DIR.toString(),176"-m", mid)177.outputTo(System.out)178.errorTo(System.out)179.getExitValue();180181assertTrue(exitValue == 0);182}183184/**185* Test --add-exports with module that is added to the set of root modules186* with --add-modules.187*/188public void testWithAddMods() throws Exception {189190// java --add-exports m4/jdk.test4=m3 --module-path mods -m ...191String mid = TEST3_MODULE + "/" + TEST3_MAIN_CLASS;192int exitValue = executeTestJava(193"--add-exports", "m4/jdk.test4=m3",194"--module-path", MODS_DIR.toString(),195"--add-modules", TEST4_MODULE,196"-m", mid)197.outputTo(System.out)198.errorTo(System.out)199.getExitValue();200201assertTrue(exitValue == 0);202}203204205/**206* --add-exports and --add-opens allows duplicates207*/208public void testWithDuplicateOption() throws Exception {209210int exitValue211= executeTestJava("--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED",212"--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED",213"--add-opens", "java.base/java.util=ALL-UNNAMED",214"--add-opens", "java.base/java.util=ALL-UNNAMED",215"-version")216.outputTo(System.out)217.errorTo(System.out)218.getExitValue();219220assertTrue(exitValue == 0);221}222223224private OutputAnalyzer execJava(String... options) {225try {226return executeTestJava(options);227} catch (Exception e) {228throw new Error(e);229}230}231232/**233* Exercise --add-exports and --add-opens with unknown values.234* Warning is emitted.235*/236@Test(dataProvider = "unknownvalues")237public void testWithUnknownValue(String value, String ignore) {238Stream.of("--add-exports", "--add-opens")239.forEach(option -> {240// --add-exports $VALUE -version241int exitValue = execJava(option, value, "-version")242.stderrShouldMatch("WARNING: .*.monkey.*")243.outputTo(System.out)244.errorTo(System.out)245.getExitValue();246247assertTrue(exitValue == 0);248});249}250251252@DataProvider(name = "unknownvalues")253public Object[][] unknownValues() {254return new Object[][]{255256{ "java.base/jdk.internal.misc=sun.monkey", null }, // unknown target257{ "java.monkey/sun.monkey=ALL-UNNAMED", null }, // unknown module258{ "java.base/sun.monkey=ALL-UNNAMED", null }, // unknown package259{ "java.monkey/sun.monkey=ALL-UNNAMED", null }, // unknown module/package260261};262}263264265/**266* Exercise --add-exports and --add-opens with bad values267*/268@Test(dataProvider = "badvalues")269public void testWithBadValue(String value, String ignore) {270Stream.of("--add-exports", "--add-opens")271.forEach(option -> {272// --add-exports $VALUE -version273int exitValue = execJava(option, value, "-version")274.outputTo(System.out)275.errorTo(System.out)276.getExitValue();277278assertTrue(exitValue != 0);279});280}281282@DataProvider(name = "badvalues")283public Object[][] badValues() {284return new Object[][]{285286{ "java.base/jdk.internal.misc", null }, // missing target287{ "java.base=ALL-UNNAMED", null }, // missing package288{ "java.base/=ALL-UNNAMED", null } // missing package289290};291}292}293294295