/* * Copyright (c) 2019, 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 Java processes that are started with -classpath, -cp, or --class-path options // and with different combinations of VM and program args. privatevoid testClassPath() throws Exception { for (String cp : CP_OPTIONS) { for (String[] vma : VM_ARGS) { for (String[] arg : ARGS) { for (String[] modularOptions : EXTRA_MODULAR_OPTIONS) {
List<String> cmd = new LinkedList<>();
cmd.add(JAVA_PATH);
cmd.add(cp);
cmd.add(TEST_CLASSES.toAbsolutePath().toString()); for (String v : vma) {
cmd.add(v);
} if (modularOptions != null) {
cmd.add(modularOptions[0]);
cmd.add(modularOptions[1]);
}
cmd.add(TEST_PROCESS_MAIN_CLASS); for (String a : arg) {
cmd.add(a);
}
testProcessHelper(cmd, TEST_PROCESS_MAIN_CLASS);
}
}
}
}
}
// Test Java processes that are started with -jar option // and with different combinations of VM and program args. privatevoid testJar() throws Exception {
File jarFile = prepareJar(); for (String[] vma : VM_ARGS) { for (String[] arg : ARGS) {
List<String> cmd = new LinkedList<>();
cmd.add(JAVA_PATH); for (String v : vma) {
cmd.add(v);
}
cmd.add(JAR_OPTION);
cmd.add(jarFile.getAbsolutePath()); for (String a : arg) {
cmd.add(a);
}
testProcessHelper(cmd, jarFile.getAbsolutePath());
}
}
}
// Test Java processes that are started with -m or --module options // and with different combination of VM and program args. privatevoid testModule() throws Exception {
prepareModule(); for (String mp : MP_OPTIONS) { for (String m : MODULE_OPTIONS) { for (String[] vma : VM_ARGS) { for (String[] arg : ARGS) { for(String patchModuleOption : PATCH_MODULE_OPTIONS) {
List<String> cmd = new LinkedList<>();
cmd.add(JAVA_PATH);
cmd.add(mp);
cmd.add(TEST_MODULES.toAbsolutePath().toString()); if (patchModuleOption != null) {
cmd.add(patchModuleOption);
cmd.add(MODULE_NAME + "=" + TEST_MODULES.toAbsolutePath().toString());
} for (String v : vma) {
cmd.add(v);
} if (m.endsWith("=")) {
cmd.add(m + MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
} else {
cmd.add(m);
cmd.add(MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
} for (String a : arg) {
cmd.add(a);
}
testProcessHelper(cmd, MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
}
}
}
}
}
}
privatevoid checkMainClass(Process p, String expectedMainClass) {
String mainClass = callGetMainClass(p); // getMainClass() may return null, e.g. due to timing issues. // Attempt some limited retries. if (mainClass == null) {
System.err.println("Main class returned by ProcessHelper was null."); // sleep time doubles each round, altogether, wait no longer than 1 sec finalint MAX_RETRIES = 10; int retrycount = 0; long sleepms = 1; while (retrycount < MAX_RETRIES && mainClass == null) {
System.err.println("Retry " + retrycount + ", sleeping for " + sleepms + "ms."); try { Thread.sleep(sleepms);
} catch (InterruptedException e) { // ignore
}
mainClass = callGetMainClass(p);
retrycount++;
sleepms *= 2;
}
}
p.destroyForcibly(); if (!expectedMainClass.equals(mainClass)) { thrownew RuntimeException("Main class is wrong: " + mainClass);
}
}
privatevoid testProcessHelper(List<String> args, String expectedValue) throws Exception {
ProcessBuilder pb = new ProcessBuilder(args);
String cmd = pb.command().stream().collect(Collectors.joining(" "));
System.out.println("Starting the process:" + cmd);
Process p = ProcessTools.startProcess("test", pb); if (!p.isAlive()) { thrownew RuntimeException("Cannot start the process: " + cmd);
}
checkMainClass(p, expectedValue);
}
try (OutputStream out = Files.newOutputStream(jarfile);
JarOutputStream jos = new JarOutputStream(out)) { if (md != null) {
JarEntry je = new JarEntry("module-info.class");
jos.putNextEntry(je);
ModuleInfoWriter.write(md, jos);
jos.closeEntry();
}
for (Path entry : entries) {
String name = toJarEntryName(entry);
jos.putNextEntry(new JarEntry(name));
Files.copy(dir.resolve(entry), jos);
jos.closeEntry();
}
}
}
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.