1 /* 2 * Copyright (c) 2015, 2016, 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/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 }