1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
* Copyright (c) 2012, 2013, 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.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/*
* support infrastructure to invoke a java class from the command line
*/
class LUtils {
static final com.sun.tools.javac.Main javac =
new com.sun.tools.javac.Main();
static final File cwd = new File(".").getAbsoluteFile();
static final String JAVAHOME = System.getProperty("java.home");
static final boolean isWindows =
System.getProperty("os.name", "unknown").startsWith("Windows");
static final File JAVA_BIN_FILE = new File(JAVAHOME, "bin");
static final File JAVA_CMD = new File(JAVA_BIN_FILE,
isWindows ? "java.exe" : "java");
static final File JAR_BIN_FILE = new File(JAVAHOME, "bin");
static final File JAR_CMD = new File(JAR_BIN_FILE,
isWindows ? "jar.exe" : "jar");
protected LUtils() {
}
public static void compile(String... args) {
if (javac.compile(args) != 0) {
throw new RuntimeException("compilation fails");
}
}
static void createFile(File outFile, List<String> content) {
try {
Files.write(outFile.getAbsoluteFile().toPath(), content,
Charset.defaultCharset());
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
static File getClassFile(File javaFile) {
return javaFile.getName().endsWith(".java")
? new File(javaFile.getName().replace(".java", ".class"))
: null;
}
static String getSimpleName(File inFile) {
String fname = inFile.getName();
return fname.substring(0, fname.indexOf("."));
}
static TestResult doExec(String... cmds) {
return doExec(null, null, cmds);
}
/*
* A method which executes a java cmd and returns the results in a container
*/
static TestResult doExec(Map<String, String> envToSet,
java.util.Set<String> envToRemove, String... cmds) {
String cmdStr = "";
for (String x : cmds) {
cmdStr = cmdStr.concat(x + " ");
}
ProcessBuilder pb = new ProcessBuilder(cmds);
Map<String, String> env = pb.environment();
if (envToRemove != null) {
for (String key : envToRemove) {
env.remove(key);
}
}
if (envToSet != null) {
env.putAll(envToSet);
}
BufferedReader rdr = null;
try {
List<String> outputList = new ArrayList<>();
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();
return new TestResult(cmdStr, p.exitValue(), outputList,
env, new Throwable("current stack of the test"));
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
static class TestResult {
String cmd;
int exitValue;
List<String> testOutput;
Map<String, String> env;
Throwable t;
public TestResult(String str, int rv, List<String> oList,
Map<String, String> env, Throwable t) {
cmd = str;
exitValue = rv;
testOutput = oList;
this.env = env;
this.t = t;
}
void assertZero(String message) {
if (exitValue != 0) {
System.err.println(this);
throw new RuntimeException(message);
}
}
@Override
public String toString() {
StringWriter sw = new StringWriter();
PrintWriter status = new PrintWriter(sw);
status.println("Cmd: " + cmd);
status.println("Return code: " + exitValue);
status.println("Environment variable:");
for (String x : env.keySet()) {
status.println("\t" + x + "=" + env.get(x));
}
status.println("Output:");
for (String x : testOutput) {
status.println("\t" + x);
}
status.println("Exception:");
status.println(t.getMessage());
t.printStackTrace(status);
return sw.getBuffer().toString();
}
}
}
|