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 /test/lib 27 * @modules jdk.compiler 28 * jdk.jartool 29 * jdk.jlink 30 * @build BasicTest jdk.test.lib.compiler.CompilerUtils 31 * @run testng BasicTest 32 * @summary Basic test of starting an application as a module 33 */ 34 35 import java.io.File; 36 import java.nio.file.Files; 37 import java.nio.file.Path; 38 import java.nio.file.Paths; 39 import java.util.spi.ToolProvider; 40 41 import jdk.test.lib.compiler.CompilerUtils; 42 import jdk.test.lib.process.ProcessTools; 43 44 import org.testng.annotations.BeforeTest; 45 import org.testng.annotations.Test; 46 import static org.testng.Assert.*; 47 48 49 @Test 50 public class BasicTest { 51 private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar") 52 .orElseThrow(() -> 53 new RuntimeException("jar tool not found") 54 ); 55 private static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod") 56 .orElseThrow(() -> 57 new RuntimeException("jmod tool not found") 58 ); 59 60 private static final Path USER_DIR = Paths.get(System.getProperty("user.dir")); 61 62 private static final String TEST_SRC = System.getProperty("test.src"); 63 64 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 65 private static final Path MODS_DIR = Paths.get("mods"); 66 67 // the module name of the test module 68 private static final String TEST_MODULE = "test"; 69 70 // the module main class 71 private static final String MAIN_CLASS = "jdk.test.Main"; 72 73 74 @BeforeTest 75 public void compileTestModule() throws Exception { 76 77 // javac -d mods/$TESTMODULE src/$TESTMODULE/** 78 boolean compiled 79 = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE), 80 MODS_DIR.resolve(TEST_MODULE)); 81 82 assertTrue(compiled, "test module did not compile"); 83 } 84 85 /** 86 * Execute "java" with the given arguments, returning the exit code. 87 */ 88 private int exec(String... args) throws Exception { 89 return ProcessTools.executeTestJava(args) 90 .outputTo(System.out) 91 .errorTo(System.out) 92 .getExitValue(); 242 243 // java --module-path mods -m $TESTMODULE 244 int exitValue = exec("--module-path", dir.toString(), "-m", TEST_MODULE); 245 assertTrue(exitValue != 0); 246 } 247 248 249 /** 250 * Attempt to run with -m specifying a main class that is a different 251 * module to that specified to -m 252 */ 253 public void testTryRunWithMainClassInWrongModule() throws Exception { 254 String modulepath = MODS_DIR.toString(); 255 String mid = "java.base/" + MAIN_CLASS; 256 257 // java --module-path mods --module $TESTMODULE/$MAINCLASS 258 int exitValue = exec("--module-path", modulepath, "--module", mid); 259 assertTrue(exitValue != 0); 260 } 261 262 } | 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 /test/lib 27 * @modules jdk.compiler 28 * jdk.jartool 29 * jdk.jlink 30 * @build BasicTest jdk.test.lib.compiler.CompilerUtils 31 * @run testng BasicTest 32 * @bug 8234076 33 * @summary Basic test of starting an application as a module 34 */ 35 36 import java.io.File; 37 import java.nio.file.Files; 38 import java.nio.file.Path; 39 import java.nio.file.Paths; 40 import java.util.spi.ToolProvider; 41 42 import jdk.test.lib.compiler.CompilerUtils; 43 import jdk.test.lib.process.ProcessTools; 44 import jdk.test.lib.process.OutputAnalyzer; 45 import jdk.test.lib.Utils; 46 47 import org.testng.annotations.BeforeTest; 48 import org.testng.annotations.Test; 49 import static org.testng.Assert.*; 50 51 52 @Test 53 public class BasicTest { 54 private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar") 55 .orElseThrow(() -> 56 new RuntimeException("jar tool not found") 57 ); 58 private static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod") 59 .orElseThrow(() -> 60 new RuntimeException("jmod tool not found") 61 ); 62 63 private static final Path USER_DIR = Paths.get(System.getProperty("user.dir")); 64 65 private static final String TEST_SRC = System.getProperty("test.src"); 66 67 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 68 private static final Path MODS_DIR = Paths.get("mods"); 69 70 // the module name of the test module 71 private static final String TEST_MODULE = "test"; 72 73 // the module main class 74 private static final String MAIN_CLASS = "jdk.test.Main"; 75 76 // for Windows specific launcher tests 77 static final boolean IS_WINDOWS = System.getProperty("os.name", "unknown").startsWith("Windows"); 78 79 @BeforeTest 80 public void compileTestModule() throws Exception { 81 82 // javac -d mods/$TESTMODULE src/$TESTMODULE/** 83 boolean compiled 84 = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE), 85 MODS_DIR.resolve(TEST_MODULE)); 86 87 assertTrue(compiled, "test module did not compile"); 88 } 89 90 /** 91 * Execute "java" with the given arguments, returning the exit code. 92 */ 93 private int exec(String... args) throws Exception { 94 return ProcessTools.executeTestJava(args) 95 .outputTo(System.out) 96 .errorTo(System.out) 97 .getExitValue(); 247 248 // java --module-path mods -m $TESTMODULE 249 int exitValue = exec("--module-path", dir.toString(), "-m", TEST_MODULE); 250 assertTrue(exitValue != 0); 251 } 252 253 254 /** 255 * Attempt to run with -m specifying a main class that is a different 256 * module to that specified to -m 257 */ 258 public void testTryRunWithMainClassInWrongModule() throws Exception { 259 String modulepath = MODS_DIR.toString(); 260 String mid = "java.base/" + MAIN_CLASS; 261 262 // java --module-path mods --module $TESTMODULE/$MAINCLASS 263 int exitValue = exec("--module-path", modulepath, "--module", mid); 264 assertTrue(exitValue != 0); 265 } 266 267 268 /** 269 * Helper method that creates a ProcessBuilder with command line arguments 270 * while setting the _JAVA_LAUNCHER_DEBUG environment variable. 271 */ 272 private ProcessBuilder createProcessWithLauncherDebugging(String... cmds) { 273 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(Utils.addTestJavaOpts(cmds)); 274 pb.environment().put("_JAVA_LAUNCHER_DEBUG", "true"); 275 276 return pb; 277 } 278 279 /** 280 * Test the ability for the Windows launcher to do proper application argument 281 * detection and expansion, when using the long form module option and all passed in 282 * command line arguments are prefixed with a dash. 283 * 284 * These tests are not expected to work on *nixes, and are ignored. 285 */ 286 public void testWindowsWithLongFormModuleOption() throws Exception { 287 if (!IS_WINDOWS) { 288 return; 289 } 290 291 String dir = MODS_DIR.toString(); 292 String mid = TEST_MODULE + "/" + MAIN_CLASS; 293 294 // java --module-path=mods --module=$TESTMODULE/$MAINCLASS --help 295 // We should be able to find the argument --help as an application argument 296 ProcessTools.executeProcess( 297 createProcessWithLauncherDebugging( 298 "--module-path=" + dir, 299 "--module=" + mid, 300 "--help")) 301 .outputTo(System.out) 302 .errorTo(System.out) 303 .shouldContain("F--help"); 304 305 // java --module-path=mods --module=$TESTMODULE/$MAINCLASS <...src/test>/*.java --help 306 // We should be able to see argument expansion happen 307 ProcessTools.executeProcess( 308 createProcessWithLauncherDebugging( 309 "--module-path=" + dir, 310 "--module=" + mid, 311 SRC_DIR.resolve(TEST_MODULE).toString() + "\\*.java", 312 "--help")) 313 .outputTo(System.out) 314 .errorTo(System.out) 315 .shouldContain("F--help") 316 .shouldContain("module-info.java"); 317 } 318 319 320 /** 321 * Test that --module= is terminating for VM argument processing just like --module 322 */ 323 public void testLongFormModuleOptionTermination() throws Exception { 324 String dir = MODS_DIR.toString(); 325 String mid = TEST_MODULE + "/" + MAIN_CLASS; 326 327 // java --module-path=mods --module=$TESTMODULE/$MAINCLASS --module-path=mods --module=$TESTMODULE/$MAINCLASS 328 // The first --module= will terminate the VM arguments processing. The second pair of module-path and module will be 329 // deemed as application arguments 330 OutputAnalyzer output = ProcessTools.executeProcess( 331 createProcessWithLauncherDebugging( 332 "--module-path=" + dir, 333 "--module=" + mid, 334 "--module-path=" + dir, 335 "--module=" + mid)) 336 .outputTo(System.out) 337 .errorTo(System.out) 338 .shouldContain("argv[ 0] = '--module-path=" + dir) 339 .shouldContain("argv[ 1] = '--module=" + mid); 340 341 if (IS_WINDOWS) { 342 output.shouldContain("F--module-path=" + dir).shouldContain("F--module=" + mid); 343 } 344 345 // java --module=$TESTMODULE/$MAINCLASS --module-path=mods 346 // This command line will not work as --module= is terminating and the module will be not found 347 int exitValue = exec("--module=" + mid, "--module-path" + dir); 348 assertTrue(exitValue != 0); 349 } 350 } |