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