/* * Copyright (c) 2017, 2021, 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.
*/
publicstaticvoid main(String... args) throws Exception { new SourceMode().run(args);
}
// To reduce the chance of creating shebang lines that are too long, // use a shorter path for a java command if the standard path is too long. privatefinal Path shebangJavaCmd;
// Whether or not to automatically skip the shebang tests privatefinalboolean skipShebangTest;
if (isWindows) { // Skip shebang tests on Windows, because that requires Cygwin.
skipShebangTest = true;
shebangJavaCmd = null;
} else { // Try to ensure the path to the Java launcher is reasonably short, // to work around the mostly undocumented limit of 120 characters // for a shebang line. // The value of 120 is the typical kernel compile-time buffer limit. // The following limit of 80 allows room for arguments to be placed // after the path to the launcher on the shebang line.
Path cmd = Paths.get(javaCmd); if (cmd.toString().length() < 80) {
shebangJavaCmd = cmd;
} else { // Create a small image in the current directory, such that // the path for the launcher is just "tmpJDK/bin/java".
Path tmpJDK = Paths.get("tmpJDK");
ToolProvider jlink = ToolProvider.findFirst("jlink")
.orElseThrow(() -> new Exception("cannot find jlink"));
jlink.run(System.out, System.err, "--add-modules", "jdk.compiler,jdk.zipfs", "--output", tmpJDK.toString());
shebangJavaCmd = tmpJDK.resolve("bin").resolve("java");
}
log.println("Using java command: " + shebangJavaCmd);
skipShebangTest = false;
}
}
// java --source N -jar simple.jar
@Test void testSourceJarConflict() throws IOException {
starting("testSourceJarConflict");
Path base = Files.createDirectories(Paths.get("testSourceJarConflict"));
Path file = getSimpleFile("Simple.java", false);
Path classes = Files.createDirectories(base.resolve("classes"));
compile("-d", classes.toString(), file.toString());
Path simpleJar = base.resolve("simple.jar");
createJar("cf", simpleJar.toString(), "-C", classes.toString(), ".");
TestResult tr =
doExec(javaCmd, "--source", thisVersion, "-jar", simpleJar.toString()); if (tr.isOK())
error(tr, "Command succeeded unexpectedly"); if (!tr.contains("Option -jar is not allowed with --source"))
error(tr, "Expected output not found");
show(tr);
}
// java --source N -m jdk.compiler
@Test void testSourceModuleConflict() throws IOException {
starting("testSourceModuleConflict");
TestResult tr = doExec(javaCmd, "--source", thisVersion, "-m", "jdk.compiler"); if (tr.isOK())
error(tr, "Command succeeded unexpectedly"); if (!tr.contains("Option -m is not allowed with --source"))
error(tr, "Expected output not found");
show(tr);
}
// #!.../java --source N -version
@Test void testTerminalOptionInShebang() throws IOException {
starting("testTerminalOptionInShebang"); if (skipShebangTest || isAIX || isMacOSX) { // On MacOSX, we cannot distinguish between terminal options on the // shebang line and those on the command line. // On Solaris, all options after the first on the shebang line are // ignored. Similar on AIX.
log.println("SKIPPED"); return;
}
Path base = Files.createDirectories(
Paths.get("testTerminalOptionInShebang"));
Path bad = base.resolve("bad");
createFile(bad, List.of( "#!" + shebangJavaCmd + " --source " + thisVersion + " -version"));
setExecutable(bad);
TestResult tr = doExec(bad.toString()); if (!tr.contains("Option -version is not allowed in this context"))
error(tr, "Expected output not found");
show(tr);
}
// #!.../java --source N @bad.at (contains -version)
@Test void testTerminalOptionInShebangAtFile() throws IOException {
starting("testTerminalOptionInShebangAtFile"); if (skipShebangTest || isAIX || isMacOSX) { // On MacOSX, we cannot distinguish between terminal options in a // shebang @-file and those on the command line. // On Solaris, all options after the first on the shebang line are // ignored. Similar on AIX.
log.println("SKIPPED"); return;
} // Use a short directory name, to avoid line length limitations
Path base = Files.createDirectories(Paths.get("testBadAtFile"));
Path bad_at = base.resolve("bad.at");
createFile(bad_at, List.of("-version"));
Path bad = base.resolve("bad");
createFile(bad, List.of( "#!" + shebangJavaCmd + " --source " + thisVersion + " @" + bad_at));
setExecutable(bad);
TestResult tr = doExec(bad.toString()); if (!tr.contains("Option -version in @testBadAtFile/bad.at is "
+ "not allowed in this context"))
error(tr, "Expected output not found");
show(tr);
}
// #!.../java --source N HelloWorld
@Test void testMainClassInShebang() throws IOException {
starting("testMainClassInShebang"); if (skipShebangTest || isAIX || isMacOSX) { // On MacOSX, we cannot distinguish between a main class on the // shebang line and one on the command line. // On Solaris, all options after the first on the shebang line are // ignored. Similar on AIX.
log.println("SKIPPED"); return;
}
Path base = Files.createDirectories(Paths.get("testMainClassInShebang"));
Path bad = base.resolve("bad");
createFile(bad, List.of( "#!" + shebangJavaCmd + " --source " + thisVersion + " HelloWorld"));
setExecutable(bad);
TestResult tr = doExec(bad.toString()); if (!tr.contains("Cannot specify main class in this context"))
error(tr, "Expected output not found");
show(tr);
}
privatevoid show(TestResult tr) {
log.println("*** Test Output:"); for (String line: tr.testOutput) {
log.println(line);
}
log.println("*** End Of Test Output:");
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.