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/testlibrary 27 * @modules jdk.compiler 28 * @build AddReadsTest CompilerUtils JarUtils jdk.testlibrary.* 29 * @run testng AddReadsTest 30 * @summary Basic tests for java --add-reads 31 */ 32 33 import java.nio.file.Path; 34 import java.nio.file.Paths; 35 36 import jdk.testlibrary.OutputAnalyzer; 37 import static jdk.testlibrary.ProcessTools.*; 38 39 import org.testng.annotations.BeforeTest; 40 import org.testng.annotations.DataProvider; 41 import org.testng.annotations.Test; 42 import static org.testng.Assert.*; 43 44 /** 45 * The tests consists of two modules: m1 and junit. 46 * Code in module m1 calls into code in module junit but the module-info.java 47 * does not have a 'requires'. Instead a read edge is added via the command 48 * line option --add-reads. 49 */ 50 51 @Test 52 public class AddReadsTest { 53 54 private static final String TEST_SRC = System.getProperty("test.src"); 55 56 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 57 private static final Path CLASSES_DIR = Paths.get("classes"); 58 private static final Path MODS_DIR = Paths.get("mods"); 59 60 private static final String MAIN = "m1/p.Main"; 61 62 63 @BeforeTest 64 public void setup() throws Exception { 65 66 // javac -d classes src/junit/** 67 assertTrue(CompilerUtils 68 .compile(SRC_DIR.resolve("junit"), CLASSES_DIR)); 69 70 // javac -d mods/m1 -cp classes/ src/m1/** 71 assertTrue(CompilerUtils 72 .compile(SRC_DIR.resolve("m1"), 73 MODS_DIR.resolve("m1"), 74 "-cp", CLASSES_DIR.toString(), 75 "--add-reads", "m1=ALL-UNNAMED")); 76 77 // jar cf mods/junit.jar -C classes . 78 JarUtils.createJarFile(MODS_DIR.resolve("junit.jar"), CLASSES_DIR); 79 80 } 81 82 private OutputAnalyzer run(String... options) throws Exception { 83 return executeTestJava(options) 84 .outputTo(System.out) 85 .errorTo(System.out); 86 } 87 88 89 /** 90 * Run with junit as a module on the module path. 91 */ 92 public void testJUnitOnModulePath() throws Exception { 93 94 // java --module-path mods --add-modules junit --add-reads m1=junit -m .. 95 int exitValue 96 = run("--module-path", MODS_DIR.toString(), 97 "--add-modules", "junit", 98 "--add-reads", "m1=junit", 99 "-m", MAIN) 100 .getExitValue(); 101 102 assertTrue(exitValue == 0); 103 } 104 105 106 /** 107 * Exercise --add-reads m1=ALL-UNNAMED by running with junit on the 108 * class path. 109 */ 110 public void testJUnitOnClassPath() throws Exception { 111 112 // java --module-path mods -cp mods/junit.jar --add-reads m1=ALL-UNNAMED -m .. 113 String cp = MODS_DIR.resolve("junit.jar").toString(); 114 int exitValue 115 = run("--module-path", MODS_DIR.toString(), 116 "-cp", cp, 117 "--add-reads", "m1=ALL-UNNAMED", 118 "-m", MAIN) 119 .getExitValue(); 120 121 assertTrue(exitValue == 0); 122 } 123 124 125 /** 126 * Run with junit as a module on the module path but without --add-reads. 127 */ 128 public void testJUnitOnModulePathMissingAddReads() throws Exception { 129 // java --module-path mods --add-modules junit --module .. 130 int exitValue 131 = run("--module-path", MODS_DIR.toString(), 132 "--add-modules", "junit", 133 "--module", MAIN) 134 .shouldContain("IllegalAccessError") 135 .getExitValue(); 136 137 assertTrue(exitValue != 0); 138 } 139 140 141 /** 142 * Run with junit on the class path but without --add-reads. 143 */ 144 public void testJUnitOnClassPathMissingAddReads() throws Exception { 145 // java --module-path mods -cp mods/junit.jar -m .. 146 String cp = MODS_DIR.resolve("junit.jar").toString(); 147 int exitValue 148 = run("--module-path", MODS_DIR.toString(), 149 "-cp", cp, 150 "-m", MAIN) 151 .shouldContain("IllegalAccessError") 152 .getExitValue(); 153 154 assertTrue(exitValue != 0); 155 } 156 157 158 /** 159 * Exercise --add-reads with a more than one source module. 160 */ 161 public void testJUnitWithMultiValueOption() throws Exception { 162 163 int exitValue 164 = run("--module-path", MODS_DIR.toString(), 165 "--add-modules", "java.xml,junit", 166 "--add-reads", "m1=java.xml,junit", 167 "--module", MAIN) 168 .getExitValue(); 169 170 assertTrue(exitValue == 0); 171 } 172 173 174 /** 175 * Exercise --add-reads where the target module is specified more than once 176 */ 177 public void testWithTargetSpecifiedManyTimes() throws Exception { 178 179 int exitValue 180 = run("--module-path", MODS_DIR.toString(), 181 "--add-modules", "java.xml,junit", 182 "--add-reads", "m1=java.xml", 183 "--add-reads", "m1=junit", 184 "-m", MAIN) 185 .shouldContain("specified more than once") 186 .getExitValue(); 187 188 assertTrue(exitValue != 0); 189 } 190 191 192 /** 193 * Exercise --add-reads with bad values 194 */ 195 @Test(dataProvider = "badvalues") 196 public void testWithBadValue(String value, String ignore) throws Exception { 197 198 // --add-exports $VALUE -version 199 assertTrue(run("--add-reads", value, "-version").getExitValue() != 0); 200 } 201 202 @DataProvider(name = "badvalues") 203 public Object[][] badValues() { 204 return new Object[][]{ 205 206 { "java.base", null }, // missing source 207 { "java.monkey=java.base", null }, // unknown module 208 { "java.base=sun.monkey", null }, // unknown source 209 210 }; 211 } 212 } | 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/testlibrary 27 * @modules jdk.compiler 28 * @build AddReadsTest CompilerUtils JarUtils jdk.testlibrary.tasks.* 29 * @run testng AddReadsTest 30 * @summary Basic tests for java --add-reads 31 */ 32 33 import java.nio.file.Path; 34 import java.nio.file.Paths; 35 36 import jdk.testlibrary.tasks.JavaTask; 37 import jdk.testlibrary.tasks.Task; 38 39 import org.testng.annotations.BeforeTest; 40 import org.testng.annotations.DataProvider; 41 import org.testng.annotations.Test; 42 import static org.testng.Assert.*; 43 44 /** 45 * The tests consists of two modules: m1 and junit. 46 * Code in module m1 calls into code in module junit but the module-info.java 47 * does not have a 'requires'. Instead a read edge is added via the command 48 * line option --add-reads. 49 */ 50 51 @Test 52 public class AddReadsTest { 53 54 private static final String TEST_SRC = System.getProperty("test.src"); 55 56 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 57 private static final Path CLASSES_DIR = Paths.get("classes"); 58 private static final Path MODS_DIR = Paths.get("mods"); 59 60 private static final String M1 = "m1"; 61 private static final String MAIN = "p.Main"; 62 63 64 @BeforeTest 65 public void setup() throws Exception { 66 67 // javac -d classes src/junit/** 68 assertTrue(CompilerUtils 69 .compile(SRC_DIR.resolve("junit"), CLASSES_DIR)); 70 71 // javac -d mods/m1 -cp classes/ src/m1/** 72 assertTrue(CompilerUtils 73 .compile(SRC_DIR.resolve("m1"), 74 MODS_DIR.resolve("m1"), 75 "-cp", CLASSES_DIR.toString(), 76 "--add-reads", "m1=ALL-UNNAMED")); 77 78 // jar cf mods/junit.jar -C classes . 79 JarUtils.createJarFile(MODS_DIR.resolve("junit.jar"), CLASSES_DIR); 80 81 } 82 83 /** 84 * Run with junit as a module on the module path. 85 */ 86 public void testJUnitOnModulePath() { 87 // java --module-path mods --add-modules junit --add-reads m1=junit -m .. 88 new JavaTask() 89 .modulePath(MODS_DIR) 90 .addModules("junit") 91 .addReads(M1, "junit") 92 .module(M1, MAIN) 93 .run(); 94 } 95 96 97 /** 98 * Exercise --add-reads m1=ALL-UNNAMED by running with junit on the 99 * class path. 100 */ 101 public void testJUnitOnClassPath() { 102 // java --module-path mods -cp mods/junit.jar --add-reads m1=ALL-UNNAMED -m .. 103 new JavaTask() 104 .modulePath(MODS_DIR) 105 .classPath(MODS_DIR.resolve("junit.jar")) 106 .addReads(M1) 107 .module(M1, MAIN) 108 .run(); 109 } 110 111 112 /** 113 * Run with junit as a module on the module path but without --add-reads. 114 */ 115 public void testJUnitOnModulePathMissingAddReads() { 116 // java --module-path mods --add-modules junit --module .. 117 new JavaTask() 118 .modulePath(MODS_DIR) 119 .addModules("junit") 120 .module(M1, MAIN) 121 .run(Task.Expect.FAIL) 122 .shouldContain(Task.OutputKind.STDERR, "IllegalAccessError"); 123 } 124 125 126 /** 127 * Run with junit on the class path but without --add-reads. 128 */ 129 public void testJUnitOnClassPathMissingAddReads() { 130 // java --module-path mods -cp mods/junit.jar -m .. 131 new JavaTask() 132 .modulePath(MODS_DIR) 133 .classPath(MODS_DIR.resolve("junit.jar")) 134 .module(M1, MAIN) 135 .run(Task.Expect.FAIL) 136 .shouldContain(Task.OutputKind.STDERR, "IllegalAccessError"); 137 } 138 139 140 /** 141 * Exercise --add-reads with a more than one source module. 142 */ 143 public void testJUnitWithMultiValueOption() { 144 new JavaTask() 145 .modulePath(MODS_DIR) 146 .addModules("java.xml", "junit") 147 .addReads(M1, "java.xml", "junit") 148 .module(M1, MAIN) 149 .run(); 150 } 151 152 153 /** 154 * Exercise --add-reads where the target module is specified more than once 155 */ 156 public void testWithTargetSpecifiedManyTimes() { 157 new JavaTask() 158 .modulePath(MODS_DIR) 159 .addModules("java.xml", "junit") 160 .addReads(M1, "java.xml") 161 .addReads(M1, "junit") 162 .module(M1, MAIN) 163 .run(Task.Expect.FAIL) 164 .shouldContain(Task.OutputKind.STDOUT, "specified more than once"); 165 } 166 167 168 /** 169 * Exercise --add-reads with bad values 170 */ 171 @Test(dataProvider = "badvalues") 172 public void testWithBadValue(String value, String ignore) { 173 // --add-exports $VALUE -version 174 new JavaTask() 175 .vmOptions(value, "-version") 176 .run(Task.Expect.FAIL); 177 } 178 179 @DataProvider(name = "badvalues") 180 public Object[][] badValues() { 181 return new Object[][]{ 182 183 { "java.base", null }, // missing source 184 { "java.monkey=java.base", null }, // unknown module 185 { "java.base=sun.monkey", null }, // unknown source 186 187 }; 188 } 189 } |