/* * Copyright (c) 2008, 2020, 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.
*/
/** * This class provides some common utilities for the launcher tests.
*/ publicclass TestHelper { // commonly used jtreg constants staticfinal File TEST_CLASSES_DIR; staticfinal File TEST_SOURCES_DIR;
static {
String tmp = System.getProperty("test.classes", null); if (tmp == null) { thrownew Error("property test.classes not defined ??");
}
TEST_CLASSES_DIR = new File(tmp).getAbsoluteFile();
tmp = System.getProperty("test.src", null); if (tmp == null) { thrownew Error("property test.src not defined ??");
}
TEST_SOURCES_DIR = new File(tmp).getAbsoluteFile();
if (is64Bit && is32Bit) { thrownew RuntimeException("arch model cannot be both 32 and 64 bit");
} if (!is64Bit && !is32Bit) { thrownew RuntimeException("arch model is not 32 or 64 bit ?");
}
File binDir = new File(JAVAHOME, "bin");
JAVA_BIN = binDir.getAbsolutePath();
File libDir = new File(JAVAHOME, "lib");
JAVA_LIB = libDir.getAbsolutePath();
File javaCmdFile = (isWindows)
? new File(binDir, "java.exe")
: new File(binDir, "java");
javaCmd = javaCmdFile.getAbsolutePath(); if (!javaCmdFile.canExecute()) { thrownew RuntimeException("java <" + TestHelper.javaCmd + "> must exist and should be executable");
}
File javacCmdFile = (isWindows)
? new File(binDir, "javac.exe")
: new File(binDir, "javac");
javacCmd = javacCmdFile.getAbsolutePath();
File jarCmdFile = (isWindows)
? new File(binDir, "jar.exe")
: new File(binDir, "jar");
jarCmd = jarCmdFile.getAbsolutePath(); if (!jarCmdFile.canExecute()) { thrownew RuntimeException("java <" + TestHelper.jarCmd + "> must exist and should be executable");
}
if (isWindows) {
File javawCmdFile = new File(binDir, "javaw.exe");
javawCmd = javawCmdFile.getAbsolutePath(); if (!javawCmdFile.canExecute()) { thrownew RuntimeException("java <" + javawCmd + "> must exist and should be executable");
}
} else {
javawCmd = null;
}
if (!javacCmdFile.canExecute()) { thrownew RuntimeException("java <" + javacCmd + "> must exist and should be executable");
}
/* * A convenience method to create a jar with jar file name and defs
*/ staticvoid createJar(File jarName, String... mainDefs) throws FileNotFoundException{
createJar(null, jarName, new File("Foo"), mainDefs);
}
/* * A convenience method to create a java file, compile and jar it up, using * the sole class file name in the jar, as the Main-Class attribute value.
*/ staticvoid createJar(File jarName, File mainClass, String... mainDefs) throws FileNotFoundException {
createJar(null, jarName, mainClass, mainDefs);
}
/* * A convenience method to compile java files.
*/ staticvoid compile(String... compilerArgs) { if (compiler.run(System.out, System.err, compilerArgs) != 0) {
String sarg = ""; for (String x : compilerArgs) {
sarg.concat(x + " ");
} thrownew Error("compilation failed: " + sarg);
}
}
/* * A generic jar file creator to create a java file, compile it * and jar it up, a specific Main-Class entry name in the * manifest can be specified or a null to use the sole class file name * as the Main-Class attribute value.
*/ staticvoid createJar(String mEntry, File jarName, File mainClass,
String... mainDefs) throws FileNotFoundException { if (jarName.exists()) {
jarName.delete();
} try (PrintStream ps = new PrintStream(new FileOutputStream(mainClass + ".java"))) {
ps.println("public class Foo {"); if (mainDefs != null) { for (String x : mainDefs) {
ps.println(x);
}
}
ps.println("}");
}
/** * Attempt to create a file at the given location. If an IOException * occurs then back off for a moment and try again. When a number of * attempts fail, give up and throw an exception.
*/ void createAFile(File aFile, List<String> lines) throws IOException {
createAFile(aFile, lines, true);
}
void createAFile(File aFile, List<String> lines, boolean endWithNewline) throws IOException {
IOException cause = null; for (int attempts = 0; attempts < 10; attempts++) { try { if (endWithNewline) {
Files.write(aFile.getAbsoluteFile().toPath(),
lines, Charset.defaultCharset(),
CREATE, TRUNCATE_EXISTING, WRITE);
} else {
Files.write(aFile.getAbsoluteFile().toPath(),
String.join(System.lineSeparator(), lines).getBytes(Charset.defaultCharset()),
CREATE, TRUNCATE_EXISTING, WRITE);
} if (cause != null) { /* * report attempts and errors that were encountered * for diagnostic purposes
*/
System.err.println("Created batch file " +
aFile + " in " + (attempts + 1) + " attempts");
System.err.println("Errors encountered: " + cause);
cause.printStackTrace();
} return;
} catch (IOException ioe) { if (cause != null) { // chain the exceptions so they all get reported for diagnostics
cause.addSuppressed(ioe);
} else {
cause = ioe;
}
}
try { Thread.sleep(500);
} catch (InterruptedException ie) { if (cause != null) { // cause should alway be non-null here
ie.addSuppressed(cause);
} thrownew RuntimeException("Interrupted while creating batch file", ie);
}
} thrownew RuntimeException("Unable to create batch file", cause);
}
/** * Helper method to initialize a simple module that just prints the passed in arguments
*/ staticvoid createEchoArgumentsModule(File modulesDir) throws IOException { if (modulesDir.exists()) {
recursiveDelete(modulesDir);
}
modulesDir.mkdirs();
File srcDir = new File(modulesDir, "src");
File modsDir = new File(modulesDir, "mods");
File testDir = new File(srcDir, "test");
File launcherTestDir = new File(testDir, "launcher");
srcDir.mkdir();
modsDir.mkdir();
testDir.mkdir();
launcherTestDir.mkdir();
staticclass ToolFilter implements FileFilter { final List<String> exclude = new ArrayList<>(); protected ToolFilter(String... exclude) { for (String x : exclude) {
String str = x + ((isWindows) ? EXE_FILE_EXT : ""); this.exclude.add(str.toLowerCase());
}
}
@Override publicboolean accept(File pathname) { if (!pathname.isFile() || !pathname.canExecute()) { returnfalse;
}
String name = pathname.getName().toLowerCase(); if (isWindows) { if (!name.endsWith(EXE_FILE_EXT)) { returnfalse;
}
} elseif (isMacOSX) { if (name.endsWith(MAC_DSYM_EXT)) { returnfalse;
}
} else { if (name.endsWith(NIX_DBGINFO_EXT)) { returnfalse;
}
} for (String x : exclude) { if (name.endsWith(x)) { returnfalse;
}
} returntrue;
}
}
/* * A class to encapsulate the test results and stuff, with some ease * of use methods to check the test results.
*/ staticclass TestResult {
PrintWriter status;
StringWriter sw; int exitValue;
List<String> testOutput;
Map<String, String> env;
Throwable t; boolean testStatus;
public TestResult(String str, int rv, List<String> oList,
Map<String, String> env, Throwable t) {
sw = new StringWriter();
status = new PrintWriter(sw);
status.println("Executed command: " + str + "\n");
exitValue = rv;
testOutput = oList; this.env = env; this.t = t;
testStatus = true;
}
@Override public String toString() {
status.println("++++Begin Test Info++++");
status.println("Test Status: " + (testStatus ? "PASS" : "FAIL"));
status.println("++++Test Environment++++"); for (String x : env.keySet()) {
indentStatus(x + "=" + env.get(x));
}
status.println("++++Test Output++++"); for (String x : testOutput) {
indentStatus(x);
}
status.println("++++Test Stack Trace++++");
status.println(t.toString()); for (StackTraceElement e : t.getStackTrace()) {
indentStatus(e.toString());
}
status.println("++++End of Test Info++++");
status.flush();
String out = sw.toString();
status.close(); return out;
}
boolean contains(String str) { for (String x : testOutput) { if (x.contains(str)) { returntrue;
}
}
appendError("string <" + str + "> not found"); returnfalse;
}
boolean notContains(String str) { for (String x : testOutput) { if (x.contains(str)) {
appendError("string <" + str + "> found"); returnfalse;
}
} returntrue;
}
boolean matches(String regexToMatch) { for (String x : testOutput) { if (x.matches(regexToMatch)) { returntrue;
}
}
appendError("regex <" + regexToMatch + "> not matched"); returnfalse;
}
boolean notMatches(String regexToMatch) { for (String x : testOutput) { if (!x.matches(regexToMatch)) { returntrue;
}
}
appendError("regex <" + regexToMatch + "> matched"); returnfalse;
}
} /** * Indicates that the annotated method is a test method.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) public @interface Test {}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.4 Sekunden
(vorverarbeitet)
¤
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.