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.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 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 } 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 | 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") 51 .orElseThrow(() -> 52 new RuntimeException("jar tool not found") 53 ); 54 private static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod") 55 .orElseThrow(() -> 56 new RuntimeException("jmod tool not found") 57 ); 58 59 private static final Path USER_DIR = Paths.get(System.getProperty("user.dir")); 60 61 private static final String TEST_SRC = System.getProperty("test.src"); 62 63 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 64 private static final Path MODS_DIR = Paths.get("mods"); 65 66 // the module name of the test module 67 private static final String TEST_MODULE = "test"; 68 69 // the module main class 70 private static final String MAIN_CLASS = "jdk.test.Main"; 71 72 73 @BeforeTest 74 public void compileTestModule() throws Exception { 75 76 // javac -d mods/$TESTMODULE src/$TESTMODULE/** 77 boolean compiled 124 exitValue = exec("-p", subdir, "-m", mid); 125 assertTrue(exitValue == 0); 126 } 127 128 129 /** 130 * The initial module is loaded from a modular JAR file 131 */ 132 public void testRunWithModularJar() throws Exception { 133 Path dir = Files.createTempDirectory(USER_DIR, "mlib"); 134 Path jar = dir.resolve("m.jar"); 135 136 // jar --create ... 137 String classes = MODS_DIR.resolve(TEST_MODULE).toString(); 138 String[] args = { 139 "--create", 140 "--file=" + jar, 141 "--main-class=" + MAIN_CLASS, 142 "-C", classes, "." 143 }; 144 int rc = JAR_TOOL.run(System.out, System.out, args); 145 assertTrue(rc == 0); 146 147 // java --module-path mlib -module $TESTMODULE 148 int exitValue = exec("--module-path", dir.toString(), 149 "--module", TEST_MODULE); 150 assertTrue(exitValue == 0); 151 152 // java --module-path mlib/m.jar -module $TESTMODULE 153 exitValue = exec("--module-path", jar.toString(), 154 "--module", TEST_MODULE); 155 assertTrue(exitValue == 0); 156 } 157 158 159 /** 160 * Attempt to run with the initial module packaged as a JMOD file. 161 */ 162 public void testTryRunWithJMod() throws Exception { 163 Path dir = Files.createTempDirectory(USER_DIR, "mlib"); 164 165 // jmod create ... 166 String cp = MODS_DIR.resolve(TEST_MODULE).toString(); 167 String jmod = dir.resolve("m.jmod").toString(); 168 String[] args = { 169 "create", 170 "--class-path", cp, 171 "--main-class", MAIN_CLASS, 172 jmod 173 }; 174 175 assertEquals(JMOD_TOOL.run(System.out, System.out, args), 0); 176 177 // java --module-path mods --module $TESTMODULE 178 int exitValue = exec("--module-path", dir.toString(), 179 "--module", TEST_MODULE); 180 assertTrue(exitValue != 0); 181 } 182 183 184 /** 185 * Run the test with a non-existent file on the application module path. 186 * It should be silently ignored. 187 */ 188 public void testRunWithNonExistentEntry() throws Exception { 189 String mp = "DoesNotExist" + File.pathSeparator + MODS_DIR.toString(); 190 String mid = TEST_MODULE + "/" + MAIN_CLASS; 191 192 // java --module-path mods --module $TESTMODULE/$MAINCLASS 193 int exitValue = exec("--module-path", mp, "--module", mid); 194 assertTrue(exitValue == 0); 195 } 219 int exitValue = exec("--module-path", modulepath, "-m", mid); 220 assertTrue(exitValue != 0); 221 } 222 223 224 /** 225 * Attempt to run with -m specifying a modular JAR that does not have 226 * a MainClass attribute 227 */ 228 public void testTryRunWithMissingMainClass() throws Exception { 229 Path dir = Files.createTempDirectory(USER_DIR, "mlib"); 230 231 // jar --create ... 232 String classes = MODS_DIR.resolve(TEST_MODULE).toString(); 233 String jar = dir.resolve("m.jar").toString(); 234 String[] args = { 235 "--create", 236 "--file=" + jar, 237 "-C", classes, "." 238 }; 239 int rc = JAR_TOOL.run(System.out, System.out, args); 240 assertTrue(rc == 0); 241 242 // java --module-path mods -m $TESTMODULE 243 int exitValue = exec("--module-path", dir.toString(), "-m", TEST_MODULE); 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 --module-path mods --module $TESTMODULE/$MAINCLASS 257 int exitValue = exec("--module-path", modulepath, "--module", mid); 258 assertTrue(exitValue != 0); 259 } 260 |