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
|
|
|
*/
|
|
|
|
|
|
|
|
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-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;
|
|
|
|
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.*;
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
public enum TestHelper {
|
|
|
|
INSTANCE;
|
2009-11-20 19:01:32 +00:00
|
|
|
static final String JAVAHOME = System.getProperty("java.home");
|
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;
|
|
|
|
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");
|
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-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";
|
|
|
|
|
2008-09-24 22:07:41 +00:00
|
|
|
static int testExitValue = 0;
|
|
|
|
|
|
|
|
static {
|
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");
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
return doExec(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
|
|
|
*/
|
2009-11-20 19:01:32 +00:00
|
|
|
static TestResult doExec(Map<String, String> envToSet, 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();
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
StringBuilder status;
|
|
|
|
int exitValue;
|
|
|
|
List<String> testOutput;
|
2011-04-07 19:06:32 +00:00
|
|
|
Map<String, String> env;
|
|
|
|
Throwable t;
|
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) {
|
2009-11-20 19:01:32 +00:00
|
|
|
status = new StringBuilder("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;
|
2008-09-24 22:07:41 +00:00
|
|
|
}
|
|
|
|
|
2009-11-20 19:01:32 +00:00
|
|
|
void appendStatus(String x) {
|
|
|
|
status = status.append(" " + x + "\n");
|
|
|
|
}
|
|
|
|
|
2008-09-24 22:07:41 +00:00
|
|
|
void checkNegative() {
|
|
|
|
if (exitValue == 0) {
|
2009-11-20 19:01:32 +00:00
|
|
|
appendStatus("Error: test must not return 0 exit value");
|
2008-09-24 22:07:41 +00:00
|
|
|
testExitValue++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkPositive() {
|
|
|
|
if (exitValue != 0) {
|
2009-11-20 19:01:32 +00:00
|
|
|
appendStatus("Error: 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()) {
|
2009-11-20 19:01:32 +00:00
|
|
|
appendStatus("Error: No message from cmd please");
|
2008-09-24 22:07:41 +00:00
|
|
|
testExitValue++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isNotZeroOutput() {
|
|
|
|
if (testOutput.isEmpty()) {
|
2009-11-20 19:01:32 +00:00
|
|
|
appendStatus("Error: 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() {
|
2011-04-07 19:06:32 +00:00
|
|
|
status.append("++++Begin Test Info++++\n");
|
|
|
|
status.append("++++Test Environment++++\n");
|
|
|
|
for (String x : env.keySet()) {
|
|
|
|
status.append(x).append("=").append(env.get(x)).append("\n");
|
|
|
|
}
|
|
|
|
status.append("++++Test Output++++\n");
|
2009-11-20 19:01:32 +00:00
|
|
|
for (String x : testOutput) {
|
|
|
|
appendStatus(x);
|
2008-09-24 22:07:41 +00:00
|
|
|
}
|
2011-04-07 19:06:32 +00:00
|
|
|
status.append("++++Test Stack Trace++++\n");
|
|
|
|
status.append(t.toString());
|
|
|
|
for (StackTraceElement e : t.getStackTrace()) {
|
|
|
|
status.append(e.toString());
|
|
|
|
}
|
|
|
|
status.append("++++End of Test Info++++\n");
|
2008-09-24 22:07:41 +00:00
|
|
|
return status.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean contains(String str) {
|
|
|
|
for (String x : testOutput) {
|
|
|
|
if (x.contains(str)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-11-20 19:01:32 +00:00
|
|
|
appendStatus("Error: string <" + str + "> not found");
|
|
|
|
testExitValue++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean matches(String stringToMatch) {
|
|
|
|
for (String x : testOutput) {
|
|
|
|
if (x.matches(stringToMatch)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appendStatus("Error: string <" + stringToMatch + "> not found");
|
2008-09-24 22:07:41 +00:00
|
|
|
testExitValue++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|