Path: blob/master/test/jdk/tools/launcher/modules/basic/BasicTest.java
41153 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @library /test/lib26* @modules jdk.compiler27* jdk.jartool28* jdk.jlink29* @build BasicTest jdk.test.lib.compiler.CompilerUtils30* @run testng BasicTest31* @bug 823407632* @summary Basic test of starting an application as a module33*/3435import java.io.File;36import java.nio.file.Files;37import java.nio.file.Path;38import java.nio.file.Paths;39import java.util.spi.ToolProvider;4041import jdk.test.lib.compiler.CompilerUtils;42import jdk.test.lib.process.ProcessTools;43import jdk.test.lib.process.OutputAnalyzer;44import jdk.test.lib.Utils;4546import org.testng.annotations.BeforeTest;47import org.testng.annotations.Test;48import static org.testng.Assert.*;495051@Test52public class BasicTest {53private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")54.orElseThrow(() ->55new RuntimeException("jar tool not found")56);57private static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")58.orElseThrow(() ->59new RuntimeException("jmod tool not found")60);6162private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));6364private static final String TEST_SRC = System.getProperty("test.src");6566private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");67private static final Path MODS_DIR = Paths.get("mods");6869// the module name of the test module70private static final String TEST_MODULE = "test";7172// the module main class73private static final String MAIN_CLASS = "jdk.test.Main";7475// for Windows specific launcher tests76static final boolean IS_WINDOWS = System.getProperty("os.name", "unknown").startsWith("Windows");7778@BeforeTest79public void compileTestModule() throws Exception {8081// javac -d mods/$TESTMODULE src/$TESTMODULE/**82boolean compiled83= CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),84MODS_DIR.resolve(TEST_MODULE));8586assertTrue(compiled, "test module did not compile");87}8889/**90* Execute "java" with the given arguments, returning the exit code.91*/92private int exec(String... args) throws Exception {93return ProcessTools.executeTestJava(args)94.outputTo(System.out)95.errorTo(System.out)96.getExitValue();97}9899100/**101* The initial module is loaded from an exploded module102*/103public void testRunWithExplodedModule() throws Exception {104String dir = MODS_DIR.toString();105String subdir = MODS_DIR.resolve(TEST_MODULE).toString();106String mid = TEST_MODULE + "/" + MAIN_CLASS;107108// java --module-path mods -module $TESTMODULE/$MAINCLASS109int exitValue = exec("--module-path", dir, "--module", mid);110assertTrue(exitValue == 0);111112// java --module-path mods/$TESTMODULE --module $TESTMODULE/$MAINCLASS113exitValue = exec("--module-path", subdir, "--module", mid);114assertTrue(exitValue == 0);115116// java --module-path=mods --module=$TESTMODULE/$MAINCLASS117exitValue = exec("--module-path=" + dir, "--module=" + mid);118assertTrue(exitValue == 0);119120// java --module-path=mods/$TESTMODULE --module=$TESTMODULE/$MAINCLASS121exitValue = exec("--module-path=" + subdir, "--module=" + mid);122assertTrue(exitValue == 0);123124// java -p mods -m $TESTMODULE/$MAINCLASS125exitValue = exec("-p", dir, "-m", mid);126assertTrue(exitValue == 0);127128// java -p mods/$TESTMODULE -m $TESTMODULE/$MAINCLASS129exitValue = exec("-p", subdir, "-m", mid);130assertTrue(exitValue == 0);131}132133134/**135* The initial module is loaded from a modular JAR file136*/137public void testRunWithModularJar() throws Exception {138Path dir = Files.createTempDirectory(USER_DIR, "mlib");139Path jar = dir.resolve("m.jar");140141// jar --create ...142String classes = MODS_DIR.resolve(TEST_MODULE).toString();143String[] args = {144"--create",145"--file=" + jar,146"--main-class=" + MAIN_CLASS,147"-C", classes, "."148};149int rc = JAR_TOOL.run(System.out, System.out, args);150assertTrue(rc == 0);151152// java --module-path mlib -module $TESTMODULE153int exitValue = exec("--module-path", dir.toString(),154"--module", TEST_MODULE);155assertTrue(exitValue == 0);156157// java --module-path mlib/m.jar -module $TESTMODULE158exitValue = exec("--module-path", jar.toString(),159"--module", TEST_MODULE);160assertTrue(exitValue == 0);161}162163164/**165* Attempt to run with the initial module packaged as a JMOD file.166*/167public void testTryRunWithJMod() throws Exception {168Path dir = Files.createTempDirectory(USER_DIR, "mlib");169170// jmod create ...171String cp = MODS_DIR.resolve(TEST_MODULE).toString();172String jmod = dir.resolve("m.jmod").toString();173String[] args = {174"create",175"--class-path", cp,176"--main-class", MAIN_CLASS,177jmod178};179180assertEquals(JMOD_TOOL.run(System.out, System.out, args), 0);181182// java --module-path mods --module $TESTMODULE183int exitValue = exec("--module-path", dir.toString(),184"--module", TEST_MODULE);185assertTrue(exitValue != 0);186}187188189/**190* Run the test with a non-existent file on the application module path.191* It should be silently ignored.192*/193public void testRunWithNonExistentEntry() throws Exception {194String mp = "DoesNotExist" + File.pathSeparator + MODS_DIR.toString();195String mid = TEST_MODULE + "/" + MAIN_CLASS;196197// java --module-path mods --module $TESTMODULE/$MAINCLASS198int exitValue = exec("--module-path", mp, "--module", mid);199assertTrue(exitValue == 0);200}201202203/**204* Attempt to run an unknown initial module205*/206public void testTryRunWithBadModule() throws Exception {207String modulepath = MODS_DIR.toString();208209// java --module-path mods -m $TESTMODULE210int exitValue = exec("--module-path", modulepath, "-m", "rhubarb");211assertTrue(exitValue != 0);212}213214215/**216* Attempt to run with -m specifying a main class that does not217* exist.218*/219public void testTryRunWithBadMainClass() throws Exception {220String modulepath = MODS_DIR.toString();221String mid = TEST_MODULE + "/p.rhubarb";222223// java --module-path mods -m $TESTMODULE/$MAINCLASS224int exitValue = exec("--module-path", modulepath, "-m", mid);225assertTrue(exitValue != 0);226}227228229/**230* Attempt to run with -m specifying a modular JAR that does not have231* a MainClass attribute232*/233public void testTryRunWithMissingMainClass() throws Exception {234Path dir = Files.createTempDirectory(USER_DIR, "mlib");235236// jar --create ...237String classes = MODS_DIR.resolve(TEST_MODULE).toString();238String jar = dir.resolve("m.jar").toString();239String[] args = {240"--create",241"--file=" + jar,242"-C", classes, "."243};244int rc = JAR_TOOL.run(System.out, System.out, args);245assertTrue(rc == 0);246247// java --module-path mods -m $TESTMODULE248int exitValue = exec("--module-path", dir.toString(), "-m", TEST_MODULE);249assertTrue(exitValue != 0);250}251252253/**254* Attempt to run with -m specifying a main class that is a different255* module to that specified to -m256*/257public void testTryRunWithMainClassInWrongModule() throws Exception {258String modulepath = MODS_DIR.toString();259String mid = "java.base/" + MAIN_CLASS;260261// java --module-path mods --module $TESTMODULE/$MAINCLASS262int exitValue = exec("--module-path", modulepath, "--module", mid);263assertTrue(exitValue != 0);264}265266267/**268* Helper method that creates a ProcessBuilder with command line arguments269* while setting the _JAVA_LAUNCHER_DEBUG environment variable.270*/271private ProcessBuilder createProcessWithLauncherDebugging(String... cmds) {272ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(Utils.addTestJavaOpts(cmds));273pb.environment().put("_JAVA_LAUNCHER_DEBUG", "true");274275return pb;276}277278/**279* Test the ability for the Windows launcher to do proper application argument280* detection and expansion, when using the long form module option and all passed in281* command line arguments are prefixed with a dash.282*283* These tests are not expected to work on *nixes, and are ignored.284*/285public void testWindowsWithLongFormModuleOption() throws Exception {286if (!IS_WINDOWS) {287return;288}289290String dir = MODS_DIR.toString();291String mid = TEST_MODULE + "/" + MAIN_CLASS;292293// java --module-path=mods --module=$TESTMODULE/$MAINCLASS --help294// We should be able to find the argument --help as an application argument295ProcessTools.executeProcess(296createProcessWithLauncherDebugging(297"--module-path=" + dir,298"--module=" + mid,299"--help"))300.outputTo(System.out)301.errorTo(System.out)302.shouldContain("F--help");303304// java --module-path=mods --module=$TESTMODULE/$MAINCLASS <...src/test>/*.java --help305// We should be able to see argument expansion happen306ProcessTools.executeProcess(307createProcessWithLauncherDebugging(308"--module-path=" + dir,309"--module=" + mid,310SRC_DIR.resolve(TEST_MODULE).toString() + "\\*.java",311"--help"))312.outputTo(System.out)313.errorTo(System.out)314.shouldContain("F--help")315.shouldContain("module-info.java");316}317318319/**320* Test that --module= is terminating for VM argument processing just like --module321*/322public void testLongFormModuleOptionTermination() throws Exception {323String dir = MODS_DIR.toString();324String mid = TEST_MODULE + "/" + MAIN_CLASS;325326// java --module-path=mods --module=$TESTMODULE/$MAINCLASS --module-path=mods --module=$TESTMODULE/$MAINCLASS327// The first --module= will terminate the VM arguments processing. The second pair of module-path and module will be328// deemed as application arguments329OutputAnalyzer output = ProcessTools.executeProcess(330createProcessWithLauncherDebugging(331"--module-path=" + dir,332"--module=" + mid,333"--module-path=" + dir,334"--module=" + mid))335.outputTo(System.out)336.errorTo(System.out)337.shouldContain("argv[ 0] = '--module-path=" + dir)338.shouldContain("argv[ 1] = '--module=" + mid);339340if (IS_WINDOWS) {341output.shouldContain("F--module-path=" + dir).shouldContain("F--module=" + mid);342}343344// java --module=$TESTMODULE/$MAINCLASS --module-path=mods345// This command line will not work as --module= is terminating and the module will be not found346int exitValue = exec("--module=" + mid, "--module-path" + dir);347assertTrue(exitValue != 0);348}349}350351352