1 /* 2 * Copyright (c) 2014, 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 * jdk.jartool 29 * jdk.jlink 30 * @build BasicTest CompilerUtils jdk.testlibrary.* 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.testlibrary.ProcessTools; 42 43 import org.testng.annotations.BeforeTest; 44 import org.testng.annotations.Test; 45 import static org.testng.Assert.*; 46 47 48 @Test 49 public class BasicTest { 50 private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar").get(); 51 private static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod").get(); 52 53 private static final Path USER_DIR = Paths.get(System.getProperty("user.dir")); 54 55 private static final String TEST_SRC = System.getProperty("test.src"); 56 57 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 58 private static final Path MODS_DIR = Paths.get("mods"); 59 60 // the module name of the test module 61 private static final String TEST_MODULE = "test"; 62 63 // the module main class 64 private static final String MAIN_CLASS = "jdk.test.Main"; 65 66 67 @BeforeTest 68 public void compileTestModule() throws Exception { 69 70 // javac -d mods/$TESTMODULE src/$TESTMODULE/** 71 boolean compiled 72 = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE), 73 MODS_DIR.resolve(TEST_MODULE)); 74 75 assertTrue(compiled, "test module did not compile"); 76 } 77 78 /** 79 * Execute "java" with the given arguments, returning the exit code. 80 */ 81 private int exec(String... args) throws Exception { 82 return ProcessTools.executeTestJava(args) 83 .outputTo(System.out) 84 .errorTo(System.out) 85 .getExitValue(); 86 } 87 88 89 /** 90 * The initial module is loaded from an exploded module 91 */ 92 public void testRunWithExplodedModule() throws Exception { 93 String dir = MODS_DIR.toString(); 94 String subdir = MODS_DIR.resolve(TEST_MODULE).toString(); 95 String mid = TEST_MODULE + "/" + MAIN_CLASS; 96 97 // java --module-path mods -module $TESTMODULE/$MAINCLASS 98 int exitValue = exec("--module-path", dir, "--module", mid); 99 assertTrue(exitValue == 0); 100 101 // java --module-path mods/$TESTMODULE --module $TESTMODULE/$MAINCLASS 102 exitValue = exec("--module-path", subdir, "--module", mid); 103 assertTrue(exitValue == 0); 104 105 // java --module-path=mods --module=$TESTMODULE/$MAINCLASS 106 exitValue = exec("--module-path=" + dir, "--module=" + mid); 107 assertTrue(exitValue == 0); 108 109 // java --module-path=mods/$TESTMODULE --module=$TESTMODULE/$MAINCLASS 110 exitValue = exec("--module-path=" + subdir, "--module=" + mid); 111 assertTrue(exitValue == 0); 112 113 // java -p mods -m $TESTMODULE/$MAINCLASS 114 exitValue = exec("-p", dir, "-m", mid); 115 assertTrue(exitValue == 0); 116 117 // java -p mods/$TESTMODULE -m $TESTMODULE/$MAINCLASS 118 exitValue = exec("-p", subdir, "-m", mid); 119 assertTrue(exitValue == 0); 120 } 121 122 123 /** 124 * The initial module is loaded from a modular JAR file 125 */ 126 public void testRunWithModularJar() throws Exception { 127 Path dir = Files.createTempDirectory(USER_DIR, "mlib"); 128 Path jar = dir.resolve("m.jar"); 129 130 // jar --create ... 131 String classes = MODS_DIR.resolve(TEST_MODULE).toString(); 132 String[] args = { 133 "--create", 134 "--file=" + jar, 135 "--main-class=" + MAIN_CLASS, 136 "-C", classes, "." 137 }; 138 int rc = JAR_TOOL.run(System.out, System.out, args); 139 assertTrue(rc == 0); 140 141 // java --module-path mlib -module $TESTMODULE 142 int exitValue = exec("--module-path", dir.toString(), 143 "--module", TEST_MODULE); 144 assertTrue(exitValue == 0); 145 146 // java --module-path mlib/m.jar -module $TESTMODULE 147 exitValue = exec("--module-path", jar.toString(), 148 "--module", TEST_MODULE); 149 assertTrue(exitValue == 0); 150 } 151 152 153 /** 154 * Attempt to run with the initial module packaged as a JMOD file. 155 */ 156 public void testTryRunWithJMod() throws Exception { 157 Path dir = Files.createTempDirectory(USER_DIR, "mlib"); 158 159 // jmod create ... 160 String cp = MODS_DIR.resolve(TEST_MODULE).toString(); 161 String jmod = dir.resolve("m.jmod").toString(); 162 String[] args = { 163 "create", 164 "--class-path", cp, 165 "--main-class", MAIN_CLASS, 166 jmod 167 }; 168 169 assertEquals(JMOD_TOOL.run(System.out, System.out, args), 0); 170 171 // java --module-path mods --module $TESTMODULE 172 int exitValue = exec("--module-path", dir.toString(), 173 "--module", TEST_MODULE); 174 assertTrue(exitValue != 0); 175 } 176 177 178 /** 179 * Run the test with a non-existent file on the application module path. 180 * It should be silently ignored. 181 */ 182 public void testRunWithNonExistentEntry() throws Exception { 183 String mp = "DoesNotExist" + File.pathSeparator + MODS_DIR.toString(); 184 String mid = TEST_MODULE + "/" + MAIN_CLASS; 185 186 // java --module-path mods --module $TESTMODULE/$MAINCLASS 187 int exitValue = exec("--module-path", mp, "--module", mid); 188 assertTrue(exitValue == 0); 189 } 190 191 192 /** 193 * Attempt to run an unknown initial module 194 */ 195 public void testTryRunWithBadModule() throws Exception { 196 String modulepath = MODS_DIR.toString(); 197 198 // java --module-path mods -m $TESTMODULE 199 int exitValue = exec("--module-path", modulepath, "-m", "rhubarb"); 200 assertTrue(exitValue != 0); 201 } 202 203 204 /** 205 * Attempt to run with -m specifying a main class that does not 206 * exist. 207 */ 208 public void testTryRunWithBadMainClass() throws Exception { 209 String modulepath = MODS_DIR.toString(); 210 String mid = TEST_MODULE + "/p.rhubarb"; 211 212 // java --module-path mods -m $TESTMODULE/$MAINCLASS 213 int exitValue = exec("--module-path", modulepath, "-m", mid); 214 assertTrue(exitValue != 0); 215 } 216 217 218 /** 219 * Attempt to run with -m specifying a modular JAR that does not have 220 * a MainClass attribute 221 */ 222 public void testTryRunWithMissingMainClass() throws Exception { 223 Path dir = Files.createTempDirectory(USER_DIR, "mlib"); 224 225 // jar --create ... 226 String classes = MODS_DIR.resolve(TEST_MODULE).toString(); 227 String jar = dir.resolve("m.jar").toString(); 228 String[] args = { 229 "--create", 230 "--file=" + jar, 231 "-C", classes, "." 232 }; 233 int rc = JAR_TOOL.run(System.out, System.out, args); 234 assertTrue(rc == 0); 235 236 // java --module-path mods -m $TESTMODULE 237 int exitValue = exec("--module-path", dir.toString(), "-m", TEST_MODULE); 238 assertTrue(exitValue != 0); 239 } 240 241 242 /** 243 * Attempt to run with -m specifying a main class that is a different 244 * module to that specified to -m 245 */ 246 public void testTryRunWithMainClassInWrongModule() throws Exception { 247 String modulepath = MODS_DIR.toString(); 248 String mid = "java.base/" + MAIN_CLASS; 249 250 // java --module-path mods --module $TESTMODULE/$MAINCLASS 251 int exitValue = exec("--module-path", modulepath, "--module", mid); 252 assertTrue(exitValue != 0); 253 } 254 255 }