Path: blob/master/test/jdk/tools/launcher/modules/addreads/AddReadsTest.java
41155 views
/*1* Copyright (c) 2015, 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 jdk.compiler27* @build AddReadsTest28* jdk.test.lib.compiler.CompilerUtils29* jdk.test.lib.util.JarUtils30* @run testng AddReadsTest31* @summary Basic tests for java --add-reads32*/3334import java.nio.file.Path;35import java.nio.file.Paths;3637import jdk.test.lib.compiler.CompilerUtils;38import jdk.test.lib.process.OutputAnalyzer;39import jdk.test.lib.util.JarUtils;40import static jdk.test.lib.process.ProcessTools.*;4142import org.testng.annotations.BeforeTest;43import org.testng.annotations.DataProvider;44import org.testng.annotations.Test;45import static org.testng.Assert.*;4647/**48* The tests consists of two modules: m1 and junit.49* Code in module m1 calls into code in module junit but the module-info.java50* does not have a 'requires'. Instead a read edge is added via the command51* line option --add-reads.52*/5354@Test55public class AddReadsTest {5657private static final String TEST_SRC = System.getProperty("test.src");5859private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");60private static final Path CLASSES_DIR = Paths.get("classes");61private static final Path MODS_DIR = Paths.get("mods");6263private static final String MAIN = "m1/p.Main";646566@BeforeTest67public void setup() throws Exception {6869// javac -d classes src/junit/**70assertTrue(CompilerUtils71.compile(SRC_DIR.resolve("junit"), CLASSES_DIR));7273// javac -d mods/m1 -cp classes/ src/m1/**74assertTrue(CompilerUtils75.compile(SRC_DIR.resolve("m1"),76MODS_DIR.resolve("m1"),77"-cp", CLASSES_DIR.toString(),78"--add-reads", "m1=ALL-UNNAMED"));7980// jar cf mods/junit.jar -C classes .81JarUtils.createJarFile(MODS_DIR.resolve("junit.jar"), CLASSES_DIR);8283}8485private OutputAnalyzer run(String... options) throws Exception {86return executeTestJava(options)87.outputTo(System.out)88.errorTo(System.out);89}909192/**93* Run with junit as a module on the module path.94*/95public void testJUnitOnModulePath() throws Exception {9697// java --module-path mods --add-modules junit --add-reads m1=junit -m ..98int exitValue99= run("--module-path", MODS_DIR.toString(),100"--add-modules", "junit",101"--add-reads", "m1=junit",102"-m", MAIN)103.getExitValue();104105assertTrue(exitValue == 0);106}107108109/**110* Exercise --add-reads m1=ALL-UNNAMED by running with junit on the111* class path.112*/113public void testJUnitOnClassPath() throws Exception {114115// java --module-path mods -cp mods/junit.jar --add-reads m1=ALL-UNNAMED -m ..116String cp = MODS_DIR.resolve("junit.jar").toString();117int exitValue118= run("--module-path", MODS_DIR.toString(),119"-cp", cp,120"--add-reads", "m1=ALL-UNNAMED",121"-m", MAIN)122.getExitValue();123124assertTrue(exitValue == 0);125}126127128/**129* Run with junit as a module on the module path but without --add-reads.130*/131public void testJUnitOnModulePathMissingAddReads() throws Exception {132// java --module-path mods --add-modules junit --module ..133int exitValue134= run("--module-path", MODS_DIR.toString(),135"--add-modules", "junit",136"--module", MAIN)137.shouldContain("IllegalAccessError")138.getExitValue();139140assertTrue(exitValue != 0);141}142143144/**145* Run with junit on the class path but without --add-reads.146*/147public void testJUnitOnClassPathMissingAddReads() throws Exception {148// java --module-path mods -cp mods/junit.jar -m ..149String cp = MODS_DIR.resolve("junit.jar").toString();150int exitValue151= run("--module-path", MODS_DIR.toString(),152"-cp", cp,153"-m", MAIN)154.shouldContain("IllegalAccessError")155.getExitValue();156157assertTrue(exitValue != 0);158}159160161/**162* Exercise --add-reads with a more than one source module.163*/164public void testJUnitWithMultiValueOption() throws Exception {165166int exitValue167= run("--module-path", MODS_DIR.toString(),168"--add-modules", "java.xml,junit",169"--add-reads", "m1=java.xml,junit",170"--module", MAIN)171.getExitValue();172173assertTrue(exitValue == 0);174}175176177/**178* Exercise --add-reads where the target module is specified more than once179*/180public void testWithTargetSpecifiedManyTimes() throws Exception {181182int exitValue183= run("--module-path", MODS_DIR.toString(),184"--add-modules", "java.xml,junit",185"--add-reads", "m1=java.xml",186"--add-reads", "m1=junit",187"-m", MAIN)188.getExitValue();189190assertTrue(exitValue == 0);191}192193194/**195* Exercise --add-reads with missing source196*/197public void testWithMissingSource() throws Exception {198199// --add-exports $VALUE -version200assertTrue(run("--add-reads", "java.base", "-version").getExitValue() != 0);201}202203204/**205* Exercise --add-reads with unknown source/target module.206* Warning is emitted.207*/208@Test(dataProvider = "badvalues")209public void testWithBadValue(String value, String ignore) throws Exception {210211// --add-exports $VALUE -version212int exitValue = run("--add-reads", value, "-version")213.stderrShouldMatch("WARNING: Unknown module: .*.monkey")214.outputTo(System.out)215.errorTo(System.out)216.getExitValue();217218assertTrue(exitValue == 0);219}220221@DataProvider(name = "badvalues")222public Object[][] badValues() {223return new Object[][]{224225{ "java.monkey=java.base", null }, // unknown module226{ "java.base=sun.monkey", null }, // unknown source227228};229}230}231232233