// the pattern we hope to see in the output staticfinal Pattern ArgPattern = Pattern.compile("\\s*argv\\[[0-9]*\\].*=.*");
void checkArgumentParsing(String inArgs, String... expArgs) throws IOException {
List<String> scratchpad = new ArrayList<>();
scratchpad.add("set " + JLDEBUG_KEY + "=true"); // GAK, -version needs to be added so that windows can flush its stderr // exiting the process prematurely can terminate the stderr.
scratchpad.add(javaCmd + " -version " + inArgs);
File batFile = new File("atest.bat");
createAFile(batFile, scratchpad);
TestResult tr = doExec(batFile.getName());
ArrayList<String> expList = new ArrayList<>();
expList.add(javaCmd);
expList.add("-version");
expList.addAll(Arrays.asList(expArgs));
List<String> gotList = new ArrayList<>(); for (String x : tr.testOutput) {
Matcher m = ArgPattern.matcher(x); if (m.matches()) {
String a[] = x.split("=");
gotList.add(a[a.length - 1].trim());
}
} if (!gotList.equals(expList)) {
System.out.println(tr);
System.out.println("Expected args:");
System.out.println(expList);
System.out.println("Obtained args:");
System.out.println(gotList); thrownew RuntimeException("Error: args do not match");
}
System.out.println("\'" + inArgs + "\'" + " - Test passed");
}
/* *ThistestsgeneralquotingandarespecifictoWindows,*nixes *neednotworryaboutthis,thesehavebeentestedwithWindows *implementationandthosethatareknowntoworkareusedagainst *thejavaimplementation.NotethattheProcessBuildergetsinthe *waywhentestingsomeofthesearguments,thereforeweneedto *createandexecutea.batfilecontainingthearguments.
*/
@Test void testArgumentParsing() throws IOException { if (!isWindows) return; // no quotes
checkArgumentParsing("a b c d", "a", "b", "c", "d");
// single quotes
checkArgumentParsing("\"a b c d\"", "a b c d");
//double quotes
checkArgumentParsing("\"\"a b c d\"\"", "a", "b", "c", "d");
// triple quotes
checkArgumentParsing("\"\"\"a b c d\"\"\"", "\"a b c d\"");
// a literal within single quotes
checkArgumentParsing("\"a\"b c d\"e\"", "ab", "c", "de");
// a literal within double quotes
checkArgumentParsing("\"\"a\"b c d\"e\"\"", "ab c de");
// a literal quote
checkArgumentParsing("a\\\"b", "a\"b");
// double back-slash
checkArgumentParsing("\"a b c d\\\\\"", "a b c d\\");
// literals within dangling quotes, etc.
checkArgumentParsing("\"a b c\" d e", "a b c", "d", "e");
checkArgumentParsing("\"ab\\\"c\" \"\\\\\" d", "ab\"c", "\\", "d");
checkArgumentParsing("a\\\\\\c d\"e f\"g h", "a\\\\\\c", "de fg", "h");
checkArgumentParsing("a\\\\\\\"b c d", "a\\\"b", "c", "d");
checkArgumentParsing("a\\\\\\\\\"g c\" d e", "a\\\\g c", "d", "e");
ArrayList<String> expList = new ArrayList<>();
expList.addAll(Arrays.asList(expArgs));
List<String> gotList = new ArrayList<>(); for (String x : tr.testOutput) {
gotList.add(x.trim());
} if (!gotList.equals(expList)) {
System.out.println(tr);
System.out.println("Expected args:");
System.out.println(expList);
System.out.println("Obtained args:");
System.out.println(gotList); thrownew RuntimeException("Error: args do not match");
}
System.out.print("\'"); for (String x : inArgs) {
System.out.print(x + " ");
}
System.out.println("\'" + " - Test passed");
}
/* *Thesetestsarenotexpectedtoworkon*nixes,andareignored.
*/
@Test void testWildCardArgumentProcessing() throws IOException { if (!isWindows) return;
File cwd = new File(".");
File libDir = new File(cwd, "lib");
initDirWithJavaFiles(libDir);
initEmptyDir(new File(cwd, "empty"));
// test if javac (the command) can compile *.java
TestResult tr = doExec(javacCmd, libDir.getName() + File.separator + "*.java"); if (!tr.isOK()) {
System.out.println(tr); thrownew RuntimeException("Error: compiling java wildcards");
}
// test if javac (the command) can compile *.java with a vmoption
tr = doExec(javacCmd, "-cp", ".", "-J-showversion", "-J-Dsomeproperty=foo",
libDir.getName() + File.separator + "*.java"); if (!tr.isOK()) {
System.out.println(tr); thrownew RuntimeException("Error: compiling java wildcards with vmoptions");
}
// use the jar cmd to create jars using the ? wildcard
File jarFoo = new File(libDir, "Foo.jar");
tr = doExec(jarCmd, "cvf", jarFoo.getAbsolutePath(), "lib" + File.separator + "F?o.class"); if (!tr.isOK()) {
System.out.println(tr); thrownew RuntimeException("Error: creating jar with wildcards");
}
// now the litmus test!, this should work
checkArgumentWildcard("a", "a");
// test for basic expansion
checkArgumentWildcard("lib\\F*java", "lib\\Fbo.java", "lib\\Foo.java");
// basic expansion in quotes
checkArgumentWildcard("\"lib\\F*java\"", "lib\\F*java");
// suffix expansion in quotes
checkArgumentWildcard("\"lib\\*.class\"", "lib\\*.class");
// check for ? expansion now
checkArgumentWildcard("lib\\F?o.java", "lib\\Fbo.java", "lib\\Foo.java");
// check ? in quotes
checkArgumentWildcard("\"lib\\F?o.java\"", "lib\\F?o.java");
// check ? as suffixes
checkArgumentWildcard("lib\\F?o.????", "lib\\Fbo.java", "lib\\Foo.java");
// check ? in a leading role
checkArgumentWildcard("lib\\???.java", "lib\\Fbo.java", "lib\\Foo.java");
checkArgumentWildcard("\"lib\\???.java\"", "lib\\???.java");
// check ? prefixed with -
checkArgumentWildcard("-?", "-?");
// check * prefixed with -
checkArgumentWildcard("-*", "-*");
tr = doExec(javaCmd, "-classpath");
tr.checkNegative();
tr.isNotZeroOutput(); if (!tr.testStatus)
System.out.println(tr);
tr = doExec(javaCmd, "-jar");
tr.checkNegative();
tr.isNotZeroOutput(); if (!tr.testStatus)
System.out.println(tr);
tr = doExec(javacCmd, "-cp");
tr.checkNegative();
tr.isNotZeroOutput(); if (!tr.testStatus)
System.out.println(tr);
// Test for 6356475 "REGRESSION:"java -X" from cmdline fails"
tr = doExec(javaCmd, "-X");
tr.checkPositive();
tr.isNotZeroOutput(); if (!tr.testStatus)
System.out.println(tr);
tr = doExec(javaCmd, "-help");
tr.checkPositive();
tr.isNotZeroOutput(); if (!tr.testStatus)
System.out.println(tr);
// 6753938, test for non-negative exit value for an incorrectly formed // command line, '% java'
tr = doExec(javaCmd);
tr.checkNegative();
tr.isNotZeroOutput(); if (!tr.testStatus)
System.out.println(tr);
// 6753938, test for non-negative exit value for an incorrectly formed // command line, '% java -Xcomp'
tr = doExec(javaCmd, "-Xcomp");
tr.checkNegative();
tr.isNotZeroOutput(); if (!tr.testStatus)
System.out.println(tr);
// 7151434, test for non-negative exit value for an incorrectly formed // command line, '% java -jar -W', note the bogus -W
tr = doExec(javaCmd, "-jar", "-W");
tr.checkNegative();
tr.contains("Unrecognized option: -W"); if (!tr.testStatus)
System.out.println(tr);
}
/* *Tests-jarcommandonajarfilewith"long"(>260chars)fullpathonWindows
*/
@Test void testLongPathJarFile() throws IOException { if (!isWindows) { return;
} // put the jar file to a location with long path
String longPathPart = "longpathtest_longpathtest/";
String longPathStr = longPathPart.repeat(15);
Path longPath = Paths.get(longPathStr);
Path jarPath = Files.createDirectories(longPath).resolve("elp.jar");
File elp = jarPath.toFile();
createJar(elp, new File("Foo"), "public static void main(String[] args){ System.out.println(\"Hello from ELP\"); }");
System.out.println("execute " + elp.getAbsolutePath());
TestResult tr = doExec(javaCmd, "-jar", elp.getAbsolutePath());
tr.checkPositive();
tr.contains("Hello from ELP");
}
// a missing class
createJar("MIA", new File("some.jar"), new File("Foo"),
(String[])null);
tr = doExec(javaCmd, "-jar", "some.jar");
tr.contains("Error: Could not find or load main class MIA"); if (!tr.testStatus)
System.out.println(tr); // use classpath to check
tr = doExec(javaCmd, "-cp", "some.jar", "MIA");
tr.contains("Error: Could not find or load main class MIA"); if (!tr.testStatus)
System.out.println(tr);
// incorrect method access
createJar(new File("some.jar"), new File("Foo"), "private static void main(String[] args){}");
tr = doExec(javaCmd, "-jar", "some.jar");
tr.contains("Error: Main method not found in class Foo"); if (!tr.testStatus)
System.out.println(tr); // use classpath to check
tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
tr.contains("Error: Main method not found in class Foo"); if (!tr.testStatus)
System.out.println(tr);
// incorrect return type
createJar(new File("some.jar"), new File("Foo"), "public static int main(String[] args){return 1;}");
tr = doExec(javaCmd, "-jar", "some.jar");
tr.contains("Error: Main method must return a value of type void in class Foo"); if (!tr.testStatus)
System.out.println(tr); // use classpath to check
tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
tr.contains("Error: Main method must return a value of type void in class Foo"); if (!tr.testStatus)
System.out.println(tr);
// incorrect parameter type
createJar(new File("some.jar"), new File("Foo"), "public static void main(Object[] args){}");
tr = doExec(javaCmd, "-jar", "some.jar");
tr.contains("Error: Main method not found in class Foo"); if (!tr.testStatus)
System.out.println(tr); // use classpath to check
tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
tr.contains("Error: Main method not found in class Foo"); if (!tr.testStatus)
System.out.println(tr);
// incorrect method type - non-static
createJar(new File("some.jar"), new File("Foo"), "public void main(String[] args){}");
tr = doExec(javaCmd, "-jar", "some.jar");
tr.contains("Error: Main method is not static in class Foo"); if (!tr.testStatus)
System.out.println(tr); // use classpath to check
tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
tr.contains("Error: Main method is not static in class Foo"); if (!tr.testStatus)
System.out.println(tr);
// amongst a potpourri of kindred main methods, is the right one chosen ?
createJar(new File("some.jar"), new File("Foo"), "void main(Object[] args){}", "int main(Float[] args){return 1;}", "private void main() {}", "private static void main(int x) {}", "public int main(int argc, String[] argv) {return 1;}", "public static void main(String[] args) {System.out.println(\"THE_CHOSEN_ONE\");}");
tr = doExec(javaCmd, "-jar", "some.jar");
tr.contains("THE_CHOSEN_ONE"); if (!tr.testStatus)
System.out.println(tr); // use classpath to check
tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
tr.contains("THE_CHOSEN_ONE"); if (!tr.testStatus)
System.out.println(tr);
// test for extraneous whitespace in the Main-Class attribute
createJar(" Foo ", new File("some.jar"), new File("Foo"), "public static void main(String... args){}");
tr = doExec(javaCmd, "-jar", "some.jar");
tr.checkPositive(); if (!tr.testStatus)
System.out.println(tr);
} /* *tests6968053,ie.weturnonthe-Xdiag(fornow)flagandcheckif *thesuppressedstacktracesareexposed,ignorethesetestsforlocalized *locales,limitingtoEnglishonly.
*/
@Test void testDiagOptions() throws FileNotFoundException { if (!isEnglishLocale()) { // only english version return;
}
TestResult tr; // a missing class
createJar("MIA", new File("some.jar"), new File("Foo"),
(String[])null);
tr = doExec(javaCmd, "-Xdiag", "-jar", "some.jar");
tr.contains("Error: Could not find or load main class MIA");
tr.contains("java.lang.ClassNotFoundException: MIA"); if (!tr.testStatus)
System.out.println(tr);
// use classpath to check
tr = doExec(javaCmd, "-Xdiag", "-cp", "some.jar", "MIA");
tr.contains("Error: Could not find or load main class MIA");
tr.contains("java.lang.ClassNotFoundException: MIA"); if (!tr.testStatus)
System.out.println(tr);
// a missing class on the classpath
tr = doExec(javaCmd, "-Xdiag", "NonExistentClass");
tr.contains("Error: Could not find or load main class NonExistentClass");
tr.contains("java.lang.ClassNotFoundException: NonExistentClass"); if (!tr.testStatus)
System.out.println(tr);
}
/** *@paramargsthecommandlinearguments *@throwsjava.io.FileNotFoundException
*/ publicstaticvoid main(String[] args) throws Exception { if (debug) {
System.out.println("Starting Arrrghs tests");
}
Arrrghs a = new Arrrghs();
a.run(args); if (testExitValue > 0) {
System.out.println("Total of " + testExitValue + " failed");
System.exit(1);
} else {
System.out.println("All tests pass");
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet am 2026-06-10)
¤
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.