2008-09-24 22:07:41 +00:00
|
|
|
/*
|
2012-01-11 16:14:47 +00:00
|
|
|
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
|
2008-09-24 22:07:41 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2010-05-25 22:58:33 +00:00
|
|
|
* 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.
|
2008-09-24 22:07:41 +00:00
|
|
|
*/
|
|
|
|
|
2012-07-31 13:14:28 +00:00
|
|
|
import java.lang.annotation.ElementType;
|
|
|
|
import java.lang.annotation.Retention;
|
|
|
|
import java.lang.annotation.RetentionPolicy;
|
|
|
|
import java.lang.annotation.Target;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.regex.Pattern;
|
2012-04-24 17:37:01 +00:00
|
|
|
import java.io.StringWriter;
|
|
|
|
import java.io.PrintWriter;
|
2012-04-24 16:39:40 +00:00
|
|
|
import java.util.Set;
|
2008-09-24 22:07:41 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
2012-01-11 16:14:47 +00:00
|
|
|
import java.io.FileFilter;
|
2008-09-24 22:07:41 +00:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
2011-04-07 19:06:32 +00:00
|
|
|
import java.io.IOException;
|
2008-09-24 22:07:41 +00:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.PrintStream;
|
2012-01-28 18:46:46 +00:00
|
|
|
import java.nio.charset.Charset;
|
2012-01-11 16:14:47 +00:00
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
2011-04-07 19:06:32 +00:00
|
|
|
import java.nio.file.Files;
|
2012-01-11 16:14:47 +00:00
|
|
|
import java.nio.file.FileVisitResult;
|
|
|
|
import java.nio.file.SimpleFileVisitor;
|
2011-04-07 19:06:32 +00:00
|
|
|
import java.nio.file.Path;
|
2008-09-24 22:07:41 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2012-01-28 18:46:46 +00:00
|
|
|
import java.util.Locale;
|
2008-09-24 22:07:41 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import javax.tools.JavaCompiler;
|
2012-01-11 16:14:47 +00:00
|
|
|
import javax.tools.ToolProvider;
|
2008-09-24 22:07:41 +00:00
|
|
|
|
2011-04-07 19:06:32 +00:00
|
|
|
import static java.nio.file.StandardCopyOption.*;
|
2012-01-28 18:46:46 +00:00
|
|
|
import static java.nio.file.StandardOpenOption.*;
|
2011-04-07 19:06:32 +00:00
|
|
|
|
2008-09-24 22:07:41 +00:00
|
|
|
/**
|
2011-04-07 19:06:32 +00:00
|
|
|
* This class provides some common utilities for the launcher tests.
|
2008-09-24 22:07:41 +00:00
|
|
|
*/
|
2012-01-28 18:46:46 +00:00
|
|
|
public class TestHelper {
|
2012-01-24 17:58:44 +00:00
|
|
|
// commonly used jtreg constants
|
|
|
|
static final File TEST_CLASSES_DIR;
|
|
|
|
static final File TEST_SOURCES_DIR;
|
|
|
|
|
2009-11-20 19:01:32 +00:00
|
|
|
static final String JAVAHOME = System.getProperty("java.home");
|
2012-03-30 00:49:34 +00:00
|
|
|
static final String JAVA_BIN;
|
2008-09-24 22:07:41 +00:00
|
|
|
static final boolean isSDK = JAVAHOME.endsWith("jre");
|
|
|
|
static final String javaCmd;
|
2012-01-03 16:33:30 +00:00
|
|
|
static final String javawCmd;
|
2009-11-20 19:01:32 +00:00
|
|
|
static final String java64Cmd;
|
2008-09-24 22:07:41 +00:00
|
|
|
static final String javacCmd;
|
2012-07-31 13:14:28 +00:00
|
|
|
static final String jarCmd;
|
|
|
|
|
2008-09-24 22:07:41 +00:00
|
|
|
static final JavaCompiler compiler;
|
|
|
|
|
2009-11-20 19:01:32 +00:00
|
|
|
static final boolean debug = Boolean.getBoolean("TestHelper.Debug");
|
2008-09-24 22:07:41 +00:00
|
|
|
static final boolean isWindows =
|
|
|
|
System.getProperty("os.name", "unknown").startsWith("Windows");
|
2012-03-06 20:34:38 +00:00
|
|
|
static final boolean isMacOSX =
|
2012-04-16 22:09:53 +00:00
|
|
|
System.getProperty("os.name", "unknown").contains("OS X");
|
2009-11-20 19:01:32 +00:00
|
|
|
static final boolean is64Bit =
|
|
|
|
System.getProperty("sun.arch.data.model").equals("64");
|
|
|
|
static final boolean is32Bit =
|
|
|
|
System.getProperty("sun.arch.data.model").equals("32");
|
|
|
|
static final boolean isSolaris =
|
|
|
|
System.getProperty("os.name", "unknown").startsWith("SunOS");
|
|
|
|
static final boolean isLinux =
|
|
|
|
System.getProperty("os.name", "unknown").startsWith("Linux");
|
|
|
|
static final boolean isDualMode = isSolaris;
|
|
|
|
static final boolean isSparc = System.getProperty("os.arch").startsWith("sparc");
|
|
|
|
|
2012-01-28 18:46:46 +00:00
|
|
|
// make a note of the golden default locale
|
|
|
|
static final Locale DefaultLocale = Locale.getDefault();
|
|
|
|
|
2012-01-11 16:14:47 +00:00
|
|
|
static final String JAVA_FILE_EXT = ".java";
|
|
|
|
static final String CLASS_FILE_EXT = ".class";
|
|
|
|
static final String JAR_FILE_EXT = ".jar";
|
2012-03-30 00:49:34 +00:00
|
|
|
static final String EXE_FILE_EXT = ".exe";
|
2012-01-28 18:46:46 +00:00
|
|
|
static final String JLDEBUG_KEY = "_JAVA_LAUNCHER_DEBUG";
|
|
|
|
static final String EXPECTED_MARKER = "TRACER_MARKER:About to EXEC";
|
2012-04-24 17:37:01 +00:00
|
|
|
static final String TEST_PREFIX = "###TestError###: ";
|
2012-01-11 16:14:47 +00:00
|
|
|
|
2008-09-24 22:07:41 +00:00
|
|
|
static int testExitValue = 0;
|
|
|
|
|
|
|
|
static {
|
2012-01-24 17:58:44 +00:00
|
|
|
String tmp = System.getProperty("test.classes", null);
|
|
|
|
if (tmp == null) {
|
|
|
|
throw new Error("property test.classes not defined ??");
|
|
|
|
}
|
|
|
|
TEST_CLASSES_DIR = new File(tmp).getAbsoluteFile();
|
|
|
|
|
|
|
|
tmp = System.getProperty("test.src", null);
|
|
|
|
if (tmp == null) {
|
|
|
|
throw new Error("property test.src not defined ??");
|
|
|
|
}
|
|
|
|
TEST_SOURCES_DIR = new File(tmp).getAbsoluteFile();
|
|
|
|
|
2009-11-20 19:01:32 +00:00
|
|
|
if (is64Bit && is32Bit) {
|
|
|
|
throw new RuntimeException("arch model cannot be both 32 and 64 bit");
|
|
|
|
}
|
|
|
|
if (!is64Bit && !is32Bit) {
|
|
|
|
throw new RuntimeException("arch model is not 32 or 64 bit ?");
|
|
|
|
}
|
2008-09-24 22:07:41 +00:00
|
|
|
compiler = ToolProvider.getSystemJavaCompiler();
|
|
|
|
File binDir = (isSDK) ? new File((new File(JAVAHOME)).getParentFile(), "bin")
|
|
|
|
: new File(JAVAHOME, "bin");
|
2012-03-30 00:49:34 +00:00
|
|
|
JAVA_BIN = binDir.getAbsolutePath();
|
2008-09-24 22:07:41 +00:00
|
|
|
File javaCmdFile = (isWindows)
|
|
|
|
? new File(binDir, "java.exe")
|
|
|
|
: new File(binDir, "java");
|
|
|
|
javaCmd = javaCmdFile.getAbsolutePath();
|
|
|
|
if (!javaCmdFile.canExecute()) {
|
2012-01-03 16:33:30 +00:00
|
|
|
throw new RuntimeException("java <" + TestHelper.javaCmd +
|
|
|
|
"> must exist and should be executable");
|
2008-09-24 22:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
File javacCmdFile = (isWindows)
|
|
|
|
? new File(binDir, "javac.exe")
|
|
|
|
: new File(binDir, "javac");
|
|
|
|
javacCmd = javacCmdFile.getAbsolutePath();
|
2012-01-03 16:33:30 +00:00
|
|
|
|
2012-07-31 13:14:28 +00:00
|
|
|
File jarCmdFile = (isWindows)
|
|
|
|
? new File(binDir, "jar.exe")
|
|
|
|
: new File(binDir, "jar");
|
|
|
|
jarCmd = jarCmdFile.getAbsolutePath();
|
|
|
|
if (!jarCmdFile.canExecute()) {
|
|
|
|
throw new RuntimeException("java <" + TestHelper.jarCmd +
|
|
|
|
"> must exist and should be executable");
|
|
|
|
}
|
|
|
|
|
2012-01-03 16:33:30 +00:00
|
|
|
if (isWindows) {
|
|
|
|
File javawCmdFile = new File(binDir, "javaw.exe");
|
|
|
|
javawCmd = javawCmdFile.getAbsolutePath();
|
|
|
|
if (!javawCmdFile.canExecute()) {
|
|
|
|
throw new RuntimeException("java <" + javawCmd +
|
|
|
|
"> must exist and should be executable");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
javawCmd = null;
|
|
|
|
}
|
|
|
|
|
2008-09-24 22:07:41 +00:00
|
|
|
if (!javacCmdFile.canExecute()) {
|
2012-01-03 16:33:30 +00:00
|
|
|
throw new RuntimeException("java <" + javacCmd +
|
|
|
|
"> must exist and should be executable");
|
2008-09-24 22:07:41 +00:00
|
|
|
}
|
2009-11-20 19:01:32 +00:00
|
|
|
if (isSolaris) {
|
|
|
|
File sparc64BinDir = new File(binDir,isSparc ? "sparcv9" : "amd64");
|
|
|
|
File java64CmdFile= new File(sparc64BinDir, "java");
|
|
|
|
if (java64CmdFile.exists() && java64CmdFile.canExecute()) {
|
|
|
|
java64Cmd = java64CmdFile.getAbsolutePath();
|
|
|
|
} else {
|
|
|
|
java64Cmd = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
java64Cmd = null;
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 13:14:28 +00:00
|
|
|
void run(String[] args) throws Exception {
|
|
|
|
int passed = 0, failed = 0;
|
|
|
|
final Pattern p = (args != null && args.length > 0)
|
|
|
|
? Pattern.compile(args[0])
|
|
|
|
: null;
|
|
|
|
for (Method m : this.getClass().getDeclaredMethods()) {
|
|
|
|
boolean selected = (p == null)
|
|
|
|
? m.isAnnotationPresent(Test.class)
|
|
|
|
: p.matcher(m.getName()).matches();
|
|
|
|
if (selected) {
|
|
|
|
try {
|
|
|
|
m.invoke(this, (Object[]) null);
|
|
|
|
System.out.println(m.getName() + ": OK");
|
|
|
|
passed++;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
System.out.printf("Test %s failed: %s %n", m, ex.getCause());
|
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.printf("Passed: %d, Failed %d%n", passed, failed);
|
|
|
|
if (failed > 0) {
|
|
|
|
throw new RuntimeException("Tests failed: " + failed);
|
|
|
|
}
|
|
|
|
if (passed == 0 && failed == 0) {
|
|
|
|
throw new AssertionError("No test(s) selected: passed = " +
|
|
|
|
passed + ", failed = " + failed + " ??????????");
|
|
|
|
}
|
|
|
|
}
|
2009-11-20 19:01:32 +00:00
|
|
|
|
2011-04-07 19:06:32 +00:00
|
|
|
/*
|
|
|
|
* is a dual mode available in the test jdk
|
|
|
|
*/
|
|
|
|
static boolean dualModePresent() {
|
|
|
|
return isDualMode && java64Cmd != null;
|
|
|
|
}
|
|
|
|
|
2009-11-20 19:01:32 +00:00
|
|
|
/*
|
|
|
|
* usually the jre/lib/arch-name is the same as os.arch, except for x86.
|
|
|
|
*/
|
|
|
|
static String getJreArch() {
|
|
|
|
String arch = System.getProperty("os.arch");
|
|
|
|
return arch.equals("x86") ? "i386" : arch;
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:06:32 +00:00
|
|
|
/*
|
|
|
|
* get the complementary jre arch ie. if sparc then return sparcv9 and
|
|
|
|
* vice-versa.
|
|
|
|
*/
|
|
|
|
static String getComplementaryJreArch() {
|
|
|
|
String arch = System.getProperty("os.arch");
|
|
|
|
if (arch != null) {
|
|
|
|
switch (arch) {
|
|
|
|
case "sparc":
|
|
|
|
return "sparcv9";
|
|
|
|
case "sparcv9":
|
|
|
|
return "sparc";
|
|
|
|
case "x86":
|
|
|
|
return "amd64";
|
|
|
|
case "amd64":
|
|
|
|
return "i386";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2009-11-20 19:01:32 +00:00
|
|
|
/*
|
|
|
|
* A convenience method to create a jar with jar file name and defs
|
|
|
|
*/
|
|
|
|
static void createJar(File jarName, String... mainDefs)
|
|
|
|
throws FileNotFoundException{
|
|
|
|
createJar(null, jarName, new File("Foo"), mainDefs);
|
2008-09-24 22:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-10-01 16:04:42 +00:00
|
|
|
* 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.
|
2008-09-24 22:07:41 +00:00
|
|
|
*/
|
|
|
|
static void createJar(File jarName, File mainClass, String... mainDefs)
|
|
|
|
throws FileNotFoundException {
|
|
|
|
createJar(null, jarName, mainClass, mainDefs);
|
|
|
|
}
|
|
|
|
|
2012-01-28 18:46:46 +00:00
|
|
|
/*
|
|
|
|
* A convenience method to compile java files.
|
|
|
|
*/
|
|
|
|
static void compile(String... compilerArgs) {
|
|
|
|
if (compiler.run(null, null, null, compilerArgs) != 0) {
|
|
|
|
String sarg = "";
|
|
|
|
for (String x : compilerArgs) {
|
|
|
|
sarg.concat(x + " ");
|
|
|
|
}
|
|
|
|
throw new Error("compilation failed: " + sarg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-24 22:07:41 +00:00
|
|
|
/*
|
2008-10-01 16:04:42 +00:00
|
|
|
* 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.
|
2008-09-24 22:07:41 +00:00
|
|
|
*/
|
2008-10-01 16:04:42 +00:00
|
|
|
static void createJar(String mEntry, File jarName, File mainClass,
|
|
|
|
String... mainDefs) throws FileNotFoundException {
|
2008-09-24 22:07:41 +00:00
|
|
|
if (jarName.exists()) {
|
|
|
|
jarName.delete();
|
|
|
|
}
|
2011-07-19 17:58:50 +00:00
|
|
|
try (PrintStream ps = new PrintStream(new FileOutputStream(mainClass + ".java"))) {
|
|
|
|
ps.println("public class Foo {");
|
|
|
|
if (mainDefs != null) {
|
|
|
|
for (String x : mainDefs) {
|
|
|
|
ps.println(x);
|
|
|
|
}
|
2008-09-24 22:07:41 +00:00
|
|
|
}
|
2011-07-19 17:58:50 +00:00
|
|
|
ps.println("}");
|
2008-09-24 22:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String compileArgs[] = {
|
|
|
|
mainClass + ".java"
|
|
|
|
};
|
|
|
|
if (compiler.run(null, null, null, compileArgs) != 0) {
|
|
|
|
throw new RuntimeException("compilation failed " + mainClass + ".java");
|
|
|
|
}
|
2008-10-01 16:04:42 +00:00
|
|
|
if (mEntry == null) {
|
2008-09-24 22:07:41 +00:00
|
|
|
mEntry = mainClass.getName();
|
|
|
|
}
|
|
|
|
String jarArgs[] = {
|
|
|
|
(debug) ? "cvfe" : "cfe",
|
|
|
|
jarName.getAbsolutePath(),
|
|
|
|
mEntry,
|
|
|
|
mainClass.getName() + ".class"
|
|
|
|
};
|
2011-07-19 17:58:50 +00:00
|
|
|
createJar(jarArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void createJar(String... args) {
|
2008-09-24 22:07:41 +00:00
|
|
|
sun.tools.jar.Main jarTool =
|
|
|
|
new sun.tools.jar.Main(System.out, System.err, "JarCreator");
|
2011-07-19 17:58:50 +00:00
|
|
|
if (!jarTool.run(args)) {
|
|
|
|
String message = "jar creation failed with command:";
|
|
|
|
for (String x : args) {
|
|
|
|
message = message.concat(" " + x);
|
|
|
|
}
|
|
|
|
throw new RuntimeException(message);
|
2008-09-24 22:07:41 +00:00
|
|
|
}
|
2011-07-19 17:58:50 +00:00
|
|
|
}
|
2008-09-24 22:07:41 +00:00
|
|
|
|
2011-04-07 19:06:32 +00:00
|
|
|
static void copyFile(File src, File dst) throws IOException {
|
|
|
|
Path parent = dst.toPath().getParent();
|
|
|
|
if (parent != null) {
|
|
|
|
Files.createDirectories(parent);
|
|
|
|
}
|
|
|
|
Files.copy(src.toPath(), dst.toPath(), COPY_ATTRIBUTES, REPLACE_EXISTING);
|
|
|
|
}
|
|
|
|
|
2012-01-28 18:46:46 +00:00
|
|
|
static void createFile(File outFile, List<String> content) throws IOException {
|
|
|
|
Files.write(outFile.getAbsoluteFile().toPath(), content,
|
|
|
|
Charset.defaultCharset(), CREATE_NEW);
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:06:32 +00:00
|
|
|
static void recursiveDelete(File target) throws IOException {
|
|
|
|
if (!target.exists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Files.walkFileTree(target.toPath(), new SimpleFileVisitor<Path>() {
|
|
|
|
@Override
|
|
|
|
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
|
|
|
|
try {
|
|
|
|
Files.deleteIfExists(dir);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
System.out.println("Error: could not delete: " + dir.toString());
|
|
|
|
System.out.println(ex.getMessage());
|
|
|
|
return FileVisitResult.TERMINATE;
|
|
|
|
}
|
|
|
|
return FileVisitResult.CONTINUE;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
|
|
|
|
try {
|
|
|
|
Files.deleteIfExists(file);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
System.out.println("Error: could not delete: " + file.toString());
|
|
|
|
System.out.println(ex.getMessage());
|
|
|
|
return FileVisitResult.TERMINATE;
|
|
|
|
}
|
|
|
|
return FileVisitResult.CONTINUE;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-11-20 19:01:32 +00:00
|
|
|
static TestResult doExec(String...cmds) {
|
2012-04-24 16:39:40 +00:00
|
|
|
return doExec(null, null, cmds);
|
2009-11-20 19:01:32 +00:00
|
|
|
}
|
|
|
|
|
2012-04-24 16:39:40 +00:00
|
|
|
static TestResult doExec(Map<String, String> envToSet, String...cmds) {
|
|
|
|
return doExec(envToSet, null, cmds);
|
|
|
|
}
|
2008-09-24 22:07:41 +00:00
|
|
|
/*
|
2008-10-01 16:04:42 +00:00
|
|
|
* A method which executes a java cmd and returns the results in a container
|
2008-09-24 22:07:41 +00:00
|
|
|
*/
|
2012-04-24 16:39:40 +00:00
|
|
|
static TestResult doExec(Map<String, String> envToSet,
|
|
|
|
Set<String> envToRemove, String...cmds) {
|
2008-09-24 22:07:41 +00:00
|
|
|
String cmdStr = "";
|
|
|
|
for (String x : cmds) {
|
|
|
|
cmdStr = cmdStr.concat(x + " ");
|
|
|
|
}
|
|
|
|
ProcessBuilder pb = new ProcessBuilder(cmds);
|
|
|
|
Map<String, String> env = pb.environment();
|
2012-04-24 16:39:40 +00:00
|
|
|
if (envToRemove != null) {
|
|
|
|
for (String key : envToRemove) {
|
|
|
|
env.remove(key);
|
|
|
|
}
|
|
|
|
}
|
2009-11-20 19:01:32 +00:00
|
|
|
if (envToSet != null) {
|
|
|
|
env.putAll(envToSet);
|
|
|
|
}
|
2008-09-24 22:07:41 +00:00
|
|
|
BufferedReader rdr = null;
|
|
|
|
try {
|
2011-04-07 19:06:32 +00:00
|
|
|
List<String> outputList = new ArrayList<>();
|
2008-09-24 22:07:41 +00:00
|
|
|
pb.redirectErrorStream(true);
|
|
|
|
Process p = pb.start();
|
|
|
|
rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
|
|
|
String in = rdr.readLine();
|
|
|
|
while (in != null) {
|
|
|
|
outputList.add(in);
|
|
|
|
in = rdr.readLine();
|
|
|
|
}
|
|
|
|
p.waitFor();
|
|
|
|
p.destroy();
|
2011-04-07 19:06:32 +00:00
|
|
|
|
|
|
|
return new TestHelper.TestResult(cmdStr, p.exitValue(), outputList,
|
|
|
|
env, new Throwable("current stack of the test"));
|
2008-09-24 22:07:41 +00:00
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
throw new RuntimeException(ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-11 16:14:47 +00:00
|
|
|
static FileFilter createFilter(final String extension) {
|
|
|
|
return new FileFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(File pathname) {
|
|
|
|
String name = pathname.getName();
|
|
|
|
if (name.endsWith(extension)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-01-28 18:46:46 +00:00
|
|
|
static boolean isEnglishLocale() {
|
|
|
|
return Locale.getDefault().getLanguage().equals("en");
|
|
|
|
}
|
|
|
|
|
2008-09-24 22:07:41 +00:00
|
|
|
/*
|
|
|
|
* A class to encapsulate the test results and stuff, with some ease
|
|
|
|
* of use methods to check the test results.
|
|
|
|
*/
|
|
|
|
static class TestResult {
|
2012-04-24 17:37:01 +00:00
|
|
|
PrintWriter status;
|
|
|
|
StringWriter sw;
|
2008-09-24 22:07:41 +00:00
|
|
|
int exitValue;
|
|
|
|
List<String> testOutput;
|
2011-04-07 19:06:32 +00:00
|
|
|
Map<String, String> env;
|
|
|
|
Throwable t;
|
2012-07-31 13:14:28 +00:00
|
|
|
boolean testStatus;
|
2008-09-24 22:07:41 +00:00
|
|
|
|
2011-04-07 19:06:32 +00:00
|
|
|
public TestResult(String str, int rv, List<String> oList,
|
|
|
|
Map<String, String> env, Throwable t) {
|
2012-04-24 17:37:01 +00:00
|
|
|
sw = new StringWriter();
|
|
|
|
status = new PrintWriter(sw);
|
|
|
|
status.println("Executed command: " + str + "\n");
|
2008-09-24 22:07:41 +00:00
|
|
|
exitValue = rv;
|
|
|
|
testOutput = oList;
|
2011-04-07 19:06:32 +00:00
|
|
|
this.env = env;
|
|
|
|
this.t = t;
|
2012-07-31 13:14:28 +00:00
|
|
|
testStatus = true;
|
2008-09-24 22:07:41 +00:00
|
|
|
}
|
|
|
|
|
2012-04-24 17:37:01 +00:00
|
|
|
void appendError(String x) {
|
|
|
|
status.println(TEST_PREFIX + x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void indentStatus(String x) {
|
|
|
|
status.println(" " + x);
|
2009-11-20 19:01:32 +00:00
|
|
|
}
|
|
|
|
|
2008-09-24 22:07:41 +00:00
|
|
|
void checkNegative() {
|
|
|
|
if (exitValue == 0) {
|
2012-04-24 17:37:01 +00:00
|
|
|
appendError("test must not return 0 exit value");
|
2012-07-31 13:14:28 +00:00
|
|
|
testStatus = false;
|
2008-09-24 22:07:41 +00:00
|
|
|
testExitValue++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkPositive() {
|
|
|
|
if (exitValue != 0) {
|
2012-07-31 13:14:28 +00:00
|
|
|
testStatus = false;
|
2012-04-24 17:37:01 +00:00
|
|
|
appendError("test did not return 0 exit value");
|
2008-09-24 22:07:41 +00:00
|
|
|
testExitValue++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isOK() {
|
|
|
|
return exitValue == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isZeroOutput() {
|
|
|
|
if (!testOutput.isEmpty()) {
|
2012-07-31 13:14:28 +00:00
|
|
|
testStatus = false;
|
2012-04-24 17:37:01 +00:00
|
|
|
appendError("No message from cmd please");
|
2008-09-24 22:07:41 +00:00
|
|
|
testExitValue++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isNotZeroOutput() {
|
|
|
|
if (testOutput.isEmpty()) {
|
2012-07-31 13:14:28 +00:00
|
|
|
testStatus = false;
|
2012-04-24 17:37:01 +00:00
|
|
|
appendError("Missing message");
|
2008-09-24 22:07:41 +00:00
|
|
|
testExitValue++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-20 19:01:32 +00:00
|
|
|
@Override
|
2008-09-24 22:07:41 +00:00
|
|
|
public String toString() {
|
2012-04-24 17:37:01 +00:00
|
|
|
status.println("++++Begin Test Info++++");
|
2012-07-31 13:14:28 +00:00
|
|
|
status.println("Test Status: " + (testStatus ? "PASS" : "FAIL"));
|
2012-04-24 17:37:01 +00:00
|
|
|
status.println("++++Test Environment++++");
|
2011-04-07 19:06:32 +00:00
|
|
|
for (String x : env.keySet()) {
|
2012-04-24 17:37:01 +00:00
|
|
|
indentStatus(x + "=" + env.get(x));
|
2011-04-07 19:06:32 +00:00
|
|
|
}
|
2012-04-24 17:37:01 +00:00
|
|
|
status.println("++++Test Output++++");
|
2009-11-20 19:01:32 +00:00
|
|
|
for (String x : testOutput) {
|
2012-04-24 17:37:01 +00:00
|
|
|
indentStatus(x);
|
2008-09-24 22:07:41 +00:00
|
|
|
}
|
2012-04-24 17:37:01 +00:00
|
|
|
status.println("++++Test Stack Trace++++");
|
|
|
|
status.println(t.toString());
|
2011-04-07 19:06:32 +00:00
|
|
|
for (StackTraceElement e : t.getStackTrace()) {
|
2012-04-24 17:37:01 +00:00
|
|
|
indentStatus(e.toString());
|
2011-04-07 19:06:32 +00:00
|
|
|
}
|
2012-04-24 17:37:01 +00:00
|
|
|
status.println("++++End of Test Info++++");
|
|
|
|
status.flush();
|
|
|
|
String out = sw.toString();
|
|
|
|
status.close();
|
|
|
|
return out;
|
2008-09-24 22:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean contains(String str) {
|
|
|
|
for (String x : testOutput) {
|
|
|
|
if (x.contains(str)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-04-24 17:37:01 +00:00
|
|
|
appendError("string <" + str + "> not found");
|
2009-11-20 19:01:32 +00:00
|
|
|
testExitValue++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean matches(String stringToMatch) {
|
|
|
|
for (String x : testOutput) {
|
|
|
|
if (x.matches(stringToMatch)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-04-24 17:37:01 +00:00
|
|
|
appendError("string <" + stringToMatch + "> not found");
|
2008-09-24 22:07:41 +00:00
|
|
|
testExitValue++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 13:14:28 +00:00
|
|
|
/**
|
|
|
|
* Indicates that the annotated method is a test method.
|
|
|
|
*/
|
|
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
|
|
@Target(ElementType.METHOD)
|
|
|
|
public @interface Test {}
|
2008-09-24 22:07:41 +00:00
|
|
|
}
|