1 /* 2 * Copyright (c) 2014, 2017, 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 /test/lib 27 * @modules jdk.jlink/jdk.tools.jmod 28 * jdk.compiler 29 * @build AddModsTest jdk.test.lib.compiler.CompilerUtils jdk.testlibrary.* 30 * @run testng AddModsTest 31 * @summary Basic test for java --add-modules 32 */ 33 34 import java.io.File; 35 import java.nio.file.Path; 36 import java.nio.file.Paths; 37 38 import jdk.test.lib.compiler.CompilerUtils; 39 import static jdk.testlibrary.ProcessTools.*; 40 41 import org.testng.annotations.BeforeTest; 42 import org.testng.annotations.Test; 43 import static org.testng.Assert.*; 44 45 46 @Test 47 public class AddModsTest { 48 49 private static final String TEST_SRC = System.getProperty("test.src"); 50 51 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 52 private static final Path MODS1_DIR = Paths.get("mods1"); 53 private static final Path MODS2_DIR = Paths.get("mods2"); 54 55 // test module / main class 56 private static final String TEST_MODULE = "test"; 57 private static final String TEST_MAIN_CLASS = "test.Main"; 58 private static final String TEST_MID = TEST_MODULE + "/" + TEST_MAIN_CLASS; 59 60 // logger module 61 private static final String LOGGER_MODULE = "logger"; 62 63 64 @BeforeTest 65 public void compile() throws Exception { 66 // javac -d mods1/test src/test/** 67 boolean compiled = CompilerUtils.compile( 68 SRC_DIR.resolve(TEST_MODULE), 69 MODS1_DIR.resolve(TEST_MODULE) 70 ); 71 assertTrue(compiled, "test did not compile"); 72 73 // javac -d mods1/logger src/logger/** 74 compiled= CompilerUtils.compile( 75 SRC_DIR.resolve(LOGGER_MODULE), 76 MODS2_DIR.resolve(LOGGER_MODULE) 77 ); 78 assertTrue(compiled, "test did not compile"); 79 } 80 81 82 /** 83 * Basic test of --add-modules ALL-DEFAULT. Module java.sql should be 84 * resolved and the types in that module should be visible. 85 */ 86 public void testAddDefaultModules1() throws Exception { 87 88 // java --add-modules ALL-DEFAULT --module-path mods1 -m test ... 89 int exitValue 90 = executeTestJava("--module-path", MODS1_DIR.toString(), 91 "--add-modules", "ALL-DEFAULT", 92 "-m", TEST_MID, 93 "java.sql.Connection") 94 .outputTo(System.out) 95 .errorTo(System.out) 96 .getExitValue(); 97 98 assertTrue(exitValue == 0); 99 } 100 101 /** 102 * Basic test of --add-modules ALL-DEFAULT. Module java.xml.ws.annotation 103 * should not resolved and so the types in that module should not be 104 * visible. 105 */ 106 public void testAddDefaultModules2() throws Exception { 107 108 // java --add-modules ALL-DEFAULT --module-path mods1 -m test ... 109 int exitValue 110 = executeTestJava("--module-path", MODS1_DIR.toString(), 111 "--add-modules", "ALL-DEFAULT", 112 "-m", TEST_MID, 113 "javax.annotation.Generated") 114 .outputTo(System.out) 115 .errorTo(System.out) 116 .shouldContain("ClassNotFoundException") 117 .getExitValue(); 118 119 assertTrue(exitValue != 0); 120 } 121 122 /** 123 * Basic test of --add-modules ALL-SYSTEM. All system modules should be resolved 124 * and thus all types in those modules should be visible. 125 */ 126 public void testAddSystemModules() throws Exception { 127 128 // java --add-modules ALL-SYSTEM --module-path mods1 -m test ... 129 int exitValue 130 = executeTestJava("--module-path", MODS1_DIR.toString(), 131 "--add-modules", "ALL-SYSTEM", 132 "-m", TEST_MID, 133 "java.sql.Connection", 134 "javax.annotation.Generated") 135 .outputTo(System.out) 136 .errorTo(System.out) 137 .getExitValue(); 138 139 assertTrue(exitValue == 0); 140 } 141 142 143 /** 144 * Run test on class path to load a type in a module on the application 145 * module path, uses {@code --add-modules logger}. 146 */ 147 public void testRunWithAddMods() throws Exception { 148 149 // java --module-path mods --add-modules logger -cp classes test.Main 150 String classpath = MODS1_DIR.resolve(TEST_MODULE).toString(); 151 String modulepath = MODS2_DIR.toString(); 152 int exitValue 153 = executeTestJava("--module-path", modulepath, 154 "--add-modules", LOGGER_MODULE, 155 "-cp", classpath, 156 TEST_MAIN_CLASS, 157 "logger.Logger") 158 .outputTo(System.out) 159 .errorTo(System.out) 160 .getExitValue(); 161 162 assertTrue(exitValue == 0); 163 } 164 165 /** 166 * Run application on class path that makes use of module on the 167 * application module path. Does not use --add-modules and so should 168 * fail at run-time. 169 */ 170 public void testRunMissingAddMods() throws Exception { 171 172 // java --module-path mods -cp classes test.Main 173 String classpath = MODS1_DIR.resolve(TEST_MODULE).toString(); 174 String modulepath = MODS1_DIR.toString(); 175 int exitValue 176 = executeTestJava("--module-path", modulepath, 177 "-cp", classpath, 178 TEST_MAIN_CLASS, 179 "logger.Logger") 180 .outputTo(System.out) 181 .errorTo(System.out) 182 .shouldContain("ClassNotFoundException") 183 .getExitValue(); 184 185 assertTrue(exitValue != 0); 186 } 187 188 189 /** 190 * Run test on class path to load a type in a module on the application 191 * module path, uses {@code --add-modules ALL-MODULE-PATH}. 192 */ 193 public void testAddAllModulePath() throws Exception { 194 195 // java --module-path mods --add-modules ALL-MODULE-PATH -cp classes test.Main 196 String classpath = MODS1_DIR.resolve(TEST_MODULE).toString(); 197 String modulepath = MODS1_DIR.toString(); 198 int exitValue 199 = executeTestJava("--module-path", modulepath, 200 "--add-modules", "ALL-MODULE-PATH", 201 "-cp", classpath, 202 TEST_MAIN_CLASS) 203 .outputTo(System.out) 204 .errorTo(System.out) 205 .getExitValue(); 206 207 assertTrue(exitValue == 0); 208 } 209 210 211 /** 212 * Test {@code --add-modules ALL-MODULE-PATH} without {@code --module-path}. 213 */ 214 public void testAddAllModulePathWithNoModulePath() throws Exception { 215 216 // java --add-modules ALL-MODULE-PATH -version 217 int exitValue 218 = executeTestJava("--add-modules", "ALL-MODULE-PATH", 219 "-version") 220 .outputTo(System.out) 221 .errorTo(System.out) 222 .getExitValue(); 223 224 assertTrue(exitValue == 0); 225 } 226 227 228 /** 229 * Tests {@code --add-modules} be specified more than once. 230 */ 231 public void testWithMultipleAddModules() throws Exception { 232 233 String modulepath = MODS1_DIR.toString() + File.pathSeparator + 234 MODS2_DIR.toString(); 235 int exitValue 236 = executeTestJava("--module-path", modulepath, 237 "--add-modules", LOGGER_MODULE, 238 "--add-modules", TEST_MODULE, 239 "-m", TEST_MID, 240 "logger.Logger") 241 .outputTo(System.out) 242 .errorTo(System.out) 243 .getExitValue(); 244 245 assertTrue(exitValue == 0); 246 } 247 248 249 /** 250 * Attempt to run with a bad module name specified to --add-modules 251 */ 252 public void testRunWithBadAddMods() throws Exception { 253 254 // java --module-path mods --add-modules DoesNotExist -m test ... 255 int exitValue 256 = executeTestJava("--module-path", MODS1_DIR.toString(), 257 "--add-modules", "DoesNotExist", 258 "-m", TEST_MID) 259 .outputTo(System.out) 260 .errorTo(System.out) 261 .shouldContain("DoesNotExist") 262 .getExitValue(); 263 264 assertTrue(exitValue != 0); 265 } 266 267 }