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