1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @library ../../lib /lib/testlibrary 27 * @build AddReadsTest CompilerUtils jdk.testlibrary.* 28 * @run testng AddReadsTest 29 * @summary Basic tests for java -XaddReads 30 */ 31 32 import java.nio.file.Path; 33 import java.nio.file.Paths; 34 35 import jdk.testlibrary.OutputAnalyzer; 36 import static jdk.testlibrary.ProcessTools.*; 37 38 import org.testng.annotations.BeforeTest; 39 import org.testng.annotations.DataProvider; 40 import org.testng.annotations.Test; 41 import static org.testng.Assert.*; 42 43 /** 44 * The tests consists of two modules: m1 and junit. 45 * Code in module m1 calls into code in module junit but the module-info.java 46 * does not have a 'requires'. Instead a read edge is added via the command 47 * line option -XaddReads. 48 */ 49 50 @Test 51 public class AddReadsTest { 52 53 private static final String TEST_SRC = System.getProperty("test.src"); 54 55 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 56 private static final Path CLASSES_DIR = Paths.get("classes"); 57 private static final Path MODS_DIR = Paths.get("mods"); 58 59 private static final String MAIN = "m1/p.Main"; 60 61 62 @BeforeTest 63 public void setup() throws Exception { 64 65 // javac -d classes src/junit/** 66 assertTrue(CompilerUtils 67 .compile(SRC_DIR.resolve("junit"), CLASSES_DIR)); 68 69 // javac -d mods/m1 -cp classes/ src/m1/** 70 assertTrue(CompilerUtils 71 .compile(SRC_DIR.resolve("m1"), 72 MODS_DIR.resolve("m1"), 73 "-cp", CLASSES_DIR.toString())); 74 75 // jar cf mods/junit.jar -C classes . 76 JarUtils.createJarFile(MODS_DIR.resolve("junit.jar"), CLASSES_DIR); 77 78 } 79 80 private OutputAnalyzer run(String... options) throws Exception { 81 return executeTestJava(options) 82 .outputTo(System.out) 83 .errorTo(System.out); 84 } 85 86 87 /** 88 * Run with junit as a module on the module path. 89 */ 90 public void testJUnitOnModulePath() throws Exception { 91 92 // java -mp mods -addmods junit -XaddReads:m1=junit -m .. 93 int exitValue 94 = run("-mp", MODS_DIR.toString(), 95 "-addmods", "junit", 96 "-XaddReads:m1=junit", 97 "-m", MAIN) 98 .getExitValue(); 99 100 assertTrue(exitValue == 0); 101 } 102 103 104 /** 105 * Exercise -XaddReads:m1=ALL-UNNAMED by running with junit on the 106 * class path. 107 */ 108 public void testJUnitOnClassPath() throws Exception { 109 110 // java -mp mods -cp mods/junit.jar -XaddReads:m1=ALL-UNNAMED -m .. 111 String cp = MODS_DIR.resolve("junit.jar").toString(); 112 int exitValue 113 = run("-mp", MODS_DIR.toString(), 114 "-cp", cp, 115 "-XaddReads:m1=ALL-UNNAMED", 116 "-m", MAIN) 117 .getExitValue(); 118 119 assertTrue(exitValue == 0); 120 } 121 122 123 /** 124 * Run with junit as a module on the module path but without -XaddReads. 125 */ 126 public void testJUnitOnModulePathMissingAddReads() throws Exception { 127 // java -mp mods -addmods junit -m .. 128 int exitValue 129 = run("-mp", MODS_DIR.toString(), 130 "-addmods", "junit", 131 "-m", MAIN) 132 .shouldContain("IllegalAccessError") 133 .getExitValue(); 134 135 assertTrue(exitValue != 0); 136 } 137 138 139 /** 140 * Run with junit on the class path but without -XaddReads. 141 */ 142 public void testJUnitOnClassPathMissingAddReads() throws Exception { 143 // java -mp mods -cp mods/junit.jar -m .. 144 String cp = MODS_DIR.resolve("junit.jar").toString(); 145 int exitValue 146 = run("-mp", MODS_DIR.toString(), 147 "-cp", cp, 148 "-m", MAIN) 149 .shouldContain("IllegalAccessError") 150 .getExitValue(); 151 152 assertTrue(exitValue != 0); 153 } 154 155 /** 156 * Exercise -XaddReads with a more than one module in the option value 157 */ 158 public void testJUnitWithMultiValueOption() throws Exception { 159 160 int exitValue 161 = run("-mp", MODS_DIR.toString(), 162 "-addmods", "java.xml,junit", 163 "-XaddReads:m1=java.xml,m1=junit", 164 "-m", MAIN) 165 .getExitValue(); 166 167 assertTrue(exitValue == 0); 168 } 169 170 171 /** 172 * Exercise -XaddReads with bad values 173 */ 174 @Test(dataProvider = "badvalues") 175 public void testWithBadValue(String value, String ignore) throws Exception { 176 177 // -XaddExports:$VALUE -version 178 assertTrue(run("-XaddReads:" + value, "-version").getExitValue() != 0); 179 } 180 181 @DataProvider(name = "badvalues") 182 public Object[][] badValues() { 183 return new Object[][]{ 184 185 { "java.base", null }, // missing source 186 { "java.monkey=java.base", null }, // unknown module 187 { "java.base=sun.monkey", null }, // unknown source 188 189 }; 190 } 191 }