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 AddExportsTest CompilerUtils jdk.testlibrary.* 29 * @run testng AddExportsTest 30 * @summary Basic tests for java --add-exports 31 */ 32 33 import java.nio.file.Path; 34 import java.nio.file.Paths; 35 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 @Test 45 public class AddExportsTest { 46 47 private static final String TEST_SRC = System.getProperty("test.src"); 48 49 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 50 private static final Path MODS_DIR = Paths.get("mods"); 51 private static final Path UPGRADE_MODS_DIRS = Paths.get("upgrademods"); 52 53 // test module m1 that uses Unsafe 54 private static final String TEST1_MODULE = "m1"; 55 private static final String TEST1_MAIN_CLASS = "jdk.test1.Main"; 56 87 "--upgrade-module-path", UPGRADE_MODS_DIRS.toString(), 88 "--add-exports", "java.transaction/javax.transaction.internal=m2"); 89 assertTrue(compiled, "module " + TEST2_MODULE + " did not compile"); 90 91 // javac -d mods/m3 src/m3/** 92 compiled = CompilerUtils.compile( 93 SRC_DIR.resolve(TEST3_MODULE), 94 MODS_DIR.resolve(TEST3_MODULE)); 95 assertTrue(compiled, "module " + TEST3_MODULE + " did not compile"); 96 97 // javac -d mods/m4 src/m4/** 98 compiled = CompilerUtils.compile( 99 SRC_DIR.resolve(TEST4_MODULE), 100 MODS_DIR.resolve(TEST4_MODULE)); 101 assertTrue(compiled, "module " + TEST4_MODULE + " did not compile"); 102 } 103 104 /** 105 * Sanity check with -version 106 */ 107 public void testSanity() throws Exception { 108 109 int exitValue 110 = executeTestJava("--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED", 111 "-version") 112 .outputTo(System.out) 113 .errorTo(System.out) 114 .getExitValue(); 115 116 assertTrue(exitValue == 0); 117 } 118 119 120 /** 121 * Run class path application that uses jdk.internal.misc.Unsafe 122 */ 123 public void testUnnamedModule() throws Exception { 124 125 // java --add-exports java.base/jdk.internal.misc=ALL-UNNAMED \ 126 // -cp mods/$TESTMODULE jdk.test.UsesUnsafe 127 128 String classpath = MODS_DIR.resolve(TEST1_MODULE).toString(); 129 int exitValue 130 = executeTestJava("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", 131 "-cp", classpath, 132 TEST1_MAIN_CLASS) 133 .outputTo(System.out) 134 .errorTo(System.out) 135 .getExitValue(); 136 137 assertTrue(exitValue == 0); 138 } 139 140 141 /** 142 * Run named module that uses jdk.internal.misc.Unsafe 143 */ 144 public void testNamedModule() throws Exception { 145 146 // java --add-exports java.base/jdk.internal.misc=test \ 147 // --module-path mods -m $TESTMODULE/$MAIN_CLASS 148 149 String mid = TEST1_MODULE + "/" + TEST1_MAIN_CLASS; 150 int exitValue = 151 executeTestJava("--add-exports", "java.base/jdk.internal.misc=" + TEST1_MODULE, 152 "--module-path", MODS_DIR.toString(), 153 "-m", mid) 154 .outputTo(System.out) 155 .errorTo(System.out) 156 .getExitValue(); 157 158 assertTrue(exitValue == 0); 159 } 160 161 162 /** 163 * Test --add-exports with upgraded module 164 */ 165 public void testWithUpgradedModule() throws Exception { 166 167 // java --add-exports java.transaction/javax.transaction.internal=m2 168 // --upgrade-module-path upgrademods --module-path mods -m ... 169 String mid = TEST2_MODULE + "/" + TEST2_MAIN_CLASS; 170 int exitValue = executeTestJava( 171 "--add-exports", "java.transaction/javax.transaction.internal=m2", 172 "--upgrade-module-path", UPGRADE_MODS_DIRS.toString(), 173 "--module-path", MODS_DIR.toString(), 174 "-m", mid) 175 .outputTo(System.out) 176 .errorTo(System.out) 177 .getExitValue(); 178 179 assertTrue(exitValue == 0); 180 } 181 182 183 /** 184 * Test --add-exports with module that is added to the set of root modules 185 * with --add-modules. 186 */ 187 public void testWithAddMods() throws Exception { 188 189 // java --add-exports m4/jdk.test4=m3 --module-path mods -m ... 190 String mid = TEST3_MODULE + "/" + TEST3_MAIN_CLASS; 191 int exitValue = executeTestJava( 192 "--add-exports", "m4/jdk.test4=m3", 193 "--module-path", MODS_DIR.toString(), 194 "--add-modules", TEST4_MODULE, 195 "-m", mid) 196 .outputTo(System.out) 197 .errorTo(System.out) 198 .getExitValue(); 199 200 assertTrue(exitValue == 0); 201 } 202 203 204 /** 205 * --add-exports can only be specified once 206 */ 207 public void testWithDuplicateOption() throws Exception { 208 209 int exitValue 210 = executeTestJava("--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED", 211 "--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED", 212 "-version") 213 .outputTo(System.out) 214 .errorTo(System.out) 215 .shouldContain("specified more than once") 216 .getExitValue(); 217 218 assertTrue(exitValue != 0); 219 } 220 221 222 /** 223 * Exercise --add-exports with bad values 224 */ 225 @Test(dataProvider = "badvalues") 226 public void testWithBadValue(String value, String ignore) throws Exception { 227 228 // --add-exports $VALUE -version 229 int exitValue = 230 executeTestJava("--add-exports", value, 231 "-version") 232 .outputTo(System.out) 233 .errorTo(System.out) 234 .getExitValue(); 235 236 assertTrue(exitValue != 0); 237 } 238 239 @DataProvider(name = "badvalues") 240 public Object[][] badValues() { 241 return new Object[][]{ 242 243 { "java.base/jdk.internal.misc", null }, // missing target 244 { "java.base/jdk.internal.misc=sun.monkey", null }, // unknown target 245 { "java.monkey/sun.monkey=ALL-UNNAMED", null }, // unknown module 246 { "java.base/sun.monkey=ALL-UNNAMED", null }, // unknown package 247 { "java.monkey/sun.monkey=ALL-UNNAMED", null }, // unknown module/package 248 { "java.base=ALL-UNNAMED", null }, // missing package 249 { "java.base/=ALL-UNNAMED", null } // missing package 250 251 }; 252 } 253 } | 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 AddExportsTest CompilerUtils jdk.testlibrary.tasks.* 29 * @run testng AddExportsTest 30 * @summary Basic tests for java --add-exports 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 @Test 46 public class AddExportsTest { 47 48 private static final String TEST_SRC = System.getProperty("test.src"); 49 50 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 51 private static final Path MODS_DIR = Paths.get("mods"); 52 private static final Path UPGRADE_MODS_DIRS = Paths.get("upgrademods"); 53 54 // test module m1 that uses Unsafe 55 private static final String TEST1_MODULE = "m1"; 56 private static final String TEST1_MAIN_CLASS = "jdk.test1.Main"; 57 88 "--upgrade-module-path", UPGRADE_MODS_DIRS.toString(), 89 "--add-exports", "java.transaction/javax.transaction.internal=m2"); 90 assertTrue(compiled, "module " + TEST2_MODULE + " did not compile"); 91 92 // javac -d mods/m3 src/m3/** 93 compiled = CompilerUtils.compile( 94 SRC_DIR.resolve(TEST3_MODULE), 95 MODS_DIR.resolve(TEST3_MODULE)); 96 assertTrue(compiled, "module " + TEST3_MODULE + " did not compile"); 97 98 // javac -d mods/m4 src/m4/** 99 compiled = CompilerUtils.compile( 100 SRC_DIR.resolve(TEST4_MODULE), 101 MODS_DIR.resolve(TEST4_MODULE)); 102 assertTrue(compiled, "module " + TEST4_MODULE + " did not compile"); 103 } 104 105 /** 106 * Sanity check with -version 107 */ 108 public void testSanity() { 109 new JavaTask() 110 .addExports("java.base", "jdk.internal.reflect") 111 .vmOptions("-version") 112 .run(); 113 } 114 115 116 /** 117 * Run class path application that uses jdk.internal.misc.Unsafe 118 */ 119 public void testUnnamedModule() { 120 121 // java --add-exports java.base/jdk.internal.misc=ALL-UNNAMED \ 122 // -cp mods/$TESTMODULE jdk.test.UsesUnsafe 123 124 new JavaTask() 125 .addExports("java.base", "jdk.internal.misc") 126 .vmOptions("-version") 127 .classPath(MODS_DIR.resolve(TEST1_MODULE)) 128 .mainClass(TEST1_MAIN_CLASS) 129 .run(); 130 } 131 132 133 /** 134 * Run named module that uses jdk.internal.misc.Unsafe 135 */ 136 public void testNamedModule() { 137 138 // java --add-exports java.base/jdk.internal.misc=test \ 139 // --module-path mods -m $TESTMODULE/$MAIN_CLASS 140 141 new JavaTask() 142 .addExports("java.base", "jdk.internal.misc", TEST1_MODULE) 143 .modulePath(MODS_DIR) 144 .module(TEST1_MODULE, TEST1_MAIN_CLASS) 145 .run(); 146 } 147 148 149 /** 150 * Test --add-exports with upgraded module 151 */ 152 public void testWithUpgradedModule() { 153 154 // java --add-exports java.transaction/javax.transaction.internal=m2 155 // --upgrade-module-path upgrademods --module-path mods -m ... 156 new JavaTask().ignoreStandardModuleOptions() 157 .addExports("java.transaction", "javax.transaction.internal", TEST2_MODULE) 158 .upgradeModulePath(UPGRADE_MODS_DIRS) 159 .modulePath(MODS_DIR.toString()) 160 .module(TEST2_MODULE, TEST2_MAIN_CLASS) 161 .run(); 162 } 163 164 165 /** 166 * Test --add-exports with module that is added to the set of root modules 167 * with --add-modules. 168 */ 169 public void testWithAddMods() { 170 171 // java --add-exports m4/jdk.test4=m3 --module-path mods -m ... 172 new JavaTask().ignoreStandardModuleOptions() 173 .addExports(TEST4_MODULE, "jdk.test4", TEST3_MODULE) 174 .addModules(TEST4_MODULE) 175 .modulePath(MODS_DIR) 176 .module(TEST3_MODULE, TEST3_MAIN_CLASS) 177 .run(); 178 } 179 180 181 /** 182 * --add-exports can only be specified once 183 */ 184 public void testWithDuplicateOption() { 185 new JavaTask() 186 .addExports("java.base", "jdk.internal.reflect") 187 .addExports("java.base", "jdk.internal.reflect") 188 .vmOptions("-version") 189 .run(Task.Expect.FAIL); 190 } 191 192 193 /** 194 * Exercise --add-exports with bad values 195 */ 196 @Test(dataProvider = "badvalues") 197 public void testWithBadValue(String value, String ignore) { 198 // --add-exports $VALUE -version 199 new JavaTask() 200 .vmOptions(value, "-version") 201 .run(Task.Expect.FAIL); 202 } 203 204 @DataProvider(name = "badvalues") 205 public Object[][] badValues() { 206 return new Object[][]{ 207 208 { "java.base/jdk.internal.misc", null }, // missing target 209 { "java.base/jdk.internal.misc=sun.monkey", null }, // unknown target 210 { "java.monkey/sun.monkey=ALL-UNNAMED", null }, // unknown module 211 { "java.base/sun.monkey=ALL-UNNAMED", null }, // unknown package 212 { "java.monkey/sun.monkey=ALL-UNNAMED", null }, // unknown module/package 213 { "java.base=ALL-UNNAMED", null }, // missing package 214 { "java.base/=ALL-UNNAMED", null } // missing package 215 216 }; 217 } 218 } |