1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/*
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @library /lib/testlibrary /test/lib
* @modules jdk.compiler
* jdk.jartool
* jdk.jlink
* @build BasicTest jdk.test.lib.compiler.CompilerUtils jdk.testlibrary.*
* @run testng BasicTest
* @summary Basic test of starting an application as a module
*/
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.spi.ToolProvider;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.testlibrary.ProcessTools;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
@Test
public class BasicTest {
private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
.orElseThrow(() ->
new RuntimeException("jar tool not found")
);
private static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
.orElseThrow(() ->
new RuntimeException("jmod tool not found")
);
private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
private static final String TEST_SRC = System.getProperty("test.src");
private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
private static final Path MODS_DIR = Paths.get("mods");
// the module name of the test module
private static final String TEST_MODULE = "test";
// the module main class
private static final String MAIN_CLASS = "jdk.test.Main";
@BeforeTest
public void compileTestModule() throws Exception {
// javac -d mods/$TESTMODULE src/$TESTMODULE/**
boolean compiled
= CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
MODS_DIR.resolve(TEST_MODULE));
assertTrue(compiled, "test module did not compile");
}
/**
* Execute "java" with the given arguments, returning the exit code.
*/
private int exec(String... args) throws Exception {
return ProcessTools.executeTestJava(args)
.outputTo(System.out)
.errorTo(System.out)
.getExitValue();
}
/**
* The initial module is loaded from an exploded module
*/
public void testRunWithExplodedModule() throws Exception {
String dir = MODS_DIR.toString();
String subdir = MODS_DIR.resolve(TEST_MODULE).toString();
String mid = TEST_MODULE + "/" + MAIN_CLASS;
// java --module-path mods -module $TESTMODULE/$MAINCLASS
int exitValue = exec("--module-path", dir, "--module", mid);
assertTrue(exitValue == 0);
// java --module-path mods/$TESTMODULE --module $TESTMODULE/$MAINCLASS
exitValue = exec("--module-path", subdir, "--module", mid);
assertTrue(exitValue == 0);
// java --module-path=mods --module=$TESTMODULE/$MAINCLASS
exitValue = exec("--module-path=" + dir, "--module=" + mid);
assertTrue(exitValue == 0);
// java --module-path=mods/$TESTMODULE --module=$TESTMODULE/$MAINCLASS
exitValue = exec("--module-path=" + subdir, "--module=" + mid);
assertTrue(exitValue == 0);
// java -p mods -m $TESTMODULE/$MAINCLASS
exitValue = exec("-p", dir, "-m", mid);
assertTrue(exitValue == 0);
// java -p mods/$TESTMODULE -m $TESTMODULE/$MAINCLASS
exitValue = exec("-p", subdir, "-m", mid);
assertTrue(exitValue == 0);
}
/**
* The initial module is loaded from a modular JAR file
*/
public void testRunWithModularJar() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mlib");
Path jar = dir.resolve("m.jar");
// jar --create ...
String classes = MODS_DIR.resolve(TEST_MODULE).toString();
String[] args = {
"--create",
"--file=" + jar,
"--main-class=" + MAIN_CLASS,
"-C", classes, "."
};
int rc = JAR_TOOL.run(System.out, System.out, args);
assertTrue(rc == 0);
// java --module-path mlib -module $TESTMODULE
int exitValue = exec("--module-path", dir.toString(),
"--module", TEST_MODULE);
assertTrue(exitValue == 0);
// java --module-path mlib/m.jar -module $TESTMODULE
exitValue = exec("--module-path", jar.toString(),
"--module", TEST_MODULE);
assertTrue(exitValue == 0);
}
/**
* Attempt to run with the initial module packaged as a JMOD file.
*/
public void testTryRunWithJMod() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mlib");
// jmod create ...
String cp = MODS_DIR.resolve(TEST_MODULE).toString();
String jmod = dir.resolve("m.jmod").toString();
String[] args = {
"create",
"--class-path", cp,
"--main-class", MAIN_CLASS,
jmod
};
assertEquals(JMOD_TOOL.run(System.out, System.out, args), 0);
// java --module-path mods --module $TESTMODULE
int exitValue = exec("--module-path", dir.toString(),
"--module", TEST_MODULE);
assertTrue(exitValue != 0);
}
/**
* Run the test with a non-existent file on the application module path.
* It should be silently ignored.
*/
public void testRunWithNonExistentEntry() throws Exception {
String mp = "DoesNotExist" + File.pathSeparator + MODS_DIR.toString();
String mid = TEST_MODULE + "/" + MAIN_CLASS;
// java --module-path mods --module $TESTMODULE/$MAINCLASS
int exitValue = exec("--module-path", mp, "--module", mid);
assertTrue(exitValue == 0);
}
/**
* Attempt to run an unknown initial module
*/
public void testTryRunWithBadModule() throws Exception {
String modulepath = MODS_DIR.toString();
// java --module-path mods -m $TESTMODULE
int exitValue = exec("--module-path", modulepath, "-m", "rhubarb");
assertTrue(exitValue != 0);
}
/**
* Attempt to run with -m specifying a main class that does not
* exist.
*/
public void testTryRunWithBadMainClass() throws Exception {
String modulepath = MODS_DIR.toString();
String mid = TEST_MODULE + "/p.rhubarb";
// java --module-path mods -m $TESTMODULE/$MAINCLASS
int exitValue = exec("--module-path", modulepath, "-m", mid);
assertTrue(exitValue != 0);
}
/**
* Attempt to run with -m specifying a modular JAR that does not have
* a MainClass attribute
*/
public void testTryRunWithMissingMainClass() throws Exception {
Path dir = Files.createTempDirectory(USER_DIR, "mlib");
// jar --create ...
String classes = MODS_DIR.resolve(TEST_MODULE).toString();
String jar = dir.resolve("m.jar").toString();
String[] args = {
"--create",
"--file=" + jar,
"-C", classes, "."
};
int rc = JAR_TOOL.run(System.out, System.out, args);
assertTrue(rc == 0);
// java --module-path mods -m $TESTMODULE
int exitValue = exec("--module-path", dir.toString(), "-m", TEST_MODULE);
assertTrue(exitValue != 0);
}
/**
* Attempt to run with -m specifying a main class that is a different
* module to that specified to -m
*/
public void testTryRunWithMainClassInWrongModule() throws Exception {
String modulepath = MODS_DIR.toString();
String mid = "java.base/" + MAIN_CLASS;
// java --module-path mods --module $TESTMODULE/$MAINCLASS
int exitValue = exec("--module-path", modulepath, "--module", mid);
assertTrue(exitValue != 0);
}
}
|