blob: da7b084dfd7c48e02d9e635ed125e63a391dd6a3 [file] [log] [blame]
ksrini20a64b22008-09-24 15:07:41 -07001
2/*
3 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 */
24
25import javax.tools.ToolProvider;
26import java.io.BufferedReader;
27import java.io.File;
28import java.io.FileNotFoundException;
29import java.io.FileOutputStream;
30import java.io.InputStreamReader;
31import java.io.PrintStream;
32import java.util.ArrayList;
33import java.util.List;
34import java.util.Map;
35import javax.tools.JavaCompiler;
36
37/**
38 * This class provides some common utilites for the launcher tests.
39 */
40public enum TestHelper {
41 INSTANCE;
ksrini11e7f1b2009-11-20 11:01:32 -080042 static final String JAVAHOME = System.getProperty("java.home");
ksrini20a64b22008-09-24 15:07:41 -070043 static final boolean isSDK = JAVAHOME.endsWith("jre");
44 static final String javaCmd;
ksrini11e7f1b2009-11-20 11:01:32 -080045 static final String java64Cmd;
ksrini20a64b22008-09-24 15:07:41 -070046 static final String javacCmd;
47 static final JavaCompiler compiler;
48
ksrini11e7f1b2009-11-20 11:01:32 -080049 static final boolean debug = Boolean.getBoolean("TestHelper.Debug");
ksrini20a64b22008-09-24 15:07:41 -070050 static final boolean isWindows =
51 System.getProperty("os.name", "unknown").startsWith("Windows");
ksrini11e7f1b2009-11-20 11:01:32 -080052 static final boolean is64Bit =
53 System.getProperty("sun.arch.data.model").equals("64");
54 static final boolean is32Bit =
55 System.getProperty("sun.arch.data.model").equals("32");
56 static final boolean isSolaris =
57 System.getProperty("os.name", "unknown").startsWith("SunOS");
58 static final boolean isLinux =
59 System.getProperty("os.name", "unknown").startsWith("Linux");
60 static final boolean isDualMode = isSolaris;
61 static final boolean isSparc = System.getProperty("os.arch").startsWith("sparc");
62
ksrini20a64b22008-09-24 15:07:41 -070063 static int testExitValue = 0;
64
65 static {
ksrini11e7f1b2009-11-20 11:01:32 -080066 if (is64Bit && is32Bit) {
67 throw new RuntimeException("arch model cannot be both 32 and 64 bit");
68 }
69 if (!is64Bit && !is32Bit) {
70 throw new RuntimeException("arch model is not 32 or 64 bit ?");
71 }
ksrini20a64b22008-09-24 15:07:41 -070072 compiler = ToolProvider.getSystemJavaCompiler();
73 File binDir = (isSDK) ? new File((new File(JAVAHOME)).getParentFile(), "bin")
74 : new File(JAVAHOME, "bin");
75 File javaCmdFile = (isWindows)
76 ? new File(binDir, "java.exe")
77 : new File(binDir, "java");
78 javaCmd = javaCmdFile.getAbsolutePath();
79 if (!javaCmdFile.canExecute()) {
80 throw new RuntimeException("java <" + TestHelper.javaCmd + "> must exist");
81 }
82
83 File javacCmdFile = (isWindows)
84 ? new File(binDir, "javac.exe")
85 : new File(binDir, "javac");
86 javacCmd = javacCmdFile.getAbsolutePath();
87 if (!javacCmdFile.canExecute()) {
88 throw new RuntimeException("java <" + javacCmd + "> must exist");
89 }
ksrini11e7f1b2009-11-20 11:01:32 -080090 if (isSolaris) {
91 File sparc64BinDir = new File(binDir,isSparc ? "sparcv9" : "amd64");
92 File java64CmdFile= new File(sparc64BinDir, "java");
93 if (java64CmdFile.exists() && java64CmdFile.canExecute()) {
94 java64Cmd = java64CmdFile.getAbsolutePath();
95 } else {
96 java64Cmd = null;
97 }
98 } else {
99 java64Cmd = null;
100 }
101 }
102
103 /*
104 * usually the jre/lib/arch-name is the same as os.arch, except for x86.
105 */
106 static String getJreArch() {
107 String arch = System.getProperty("os.arch");
108 return arch.equals("x86") ? "i386" : arch;
109 }
110
111 /*
112 * A convenience method to create a jar with jar file name and defs
113 */
114 static void createJar(File jarName, String... mainDefs)
115 throws FileNotFoundException{
116 createJar(null, jarName, new File("Foo"), mainDefs);
ksrini20a64b22008-09-24 15:07:41 -0700117 }
118
119 /*
ksrinicafaf7c2008-10-01 09:04:42 -0700120 * A convenience method to create a java file, compile and jar it up, using
121 * the sole class file name in the jar, as the Main-Class attribute value.
ksrini20a64b22008-09-24 15:07:41 -0700122 */
123 static void createJar(File jarName, File mainClass, String... mainDefs)
124 throws FileNotFoundException {
125 createJar(null, jarName, mainClass, mainDefs);
126 }
127
128 /*
ksrinicafaf7c2008-10-01 09:04:42 -0700129 * A generic jar file creator to create a java file, compile it
130 * and jar it up, a specific Main-Class entry name in the
131 * manifest can be specified or a null to use the sole class file name
132 * as the Main-Class attribute value.
ksrini20a64b22008-09-24 15:07:41 -0700133 */
ksrinicafaf7c2008-10-01 09:04:42 -0700134 static void createJar(String mEntry, File jarName, File mainClass,
135 String... mainDefs) throws FileNotFoundException {
ksrini20a64b22008-09-24 15:07:41 -0700136 if (jarName.exists()) {
137 jarName.delete();
138 }
139 PrintStream ps = new PrintStream(new FileOutputStream(mainClass + ".java"));
140 ps.println("public class Foo {");
141 if (mainDefs != null) {
142 for (String x : mainDefs) {
143 ps.println(x);
144 }
145 }
146 ps.println("}");
147 ps.close();
148
149 String compileArgs[] = {
150 mainClass + ".java"
151 };
152 if (compiler.run(null, null, null, compileArgs) != 0) {
153 throw new RuntimeException("compilation failed " + mainClass + ".java");
154 }
ksrinicafaf7c2008-10-01 09:04:42 -0700155 if (mEntry == null) {
ksrini20a64b22008-09-24 15:07:41 -0700156 mEntry = mainClass.getName();
157 }
158 String jarArgs[] = {
159 (debug) ? "cvfe" : "cfe",
160 jarName.getAbsolutePath(),
161 mEntry,
162 mainClass.getName() + ".class"
163 };
164 sun.tools.jar.Main jarTool =
165 new sun.tools.jar.Main(System.out, System.err, "JarCreator");
166 if (!jarTool.run(jarArgs)) {
167 throw new RuntimeException("jar creation failed " + jarName);
168 }
169 }
170
ksrini11e7f1b2009-11-20 11:01:32 -0800171 static TestResult doExec(String...cmds) {
172 return doExec(null, cmds);
173 }
174
ksrini20a64b22008-09-24 15:07:41 -0700175 /*
ksrinicafaf7c2008-10-01 09:04:42 -0700176 * A method which executes a java cmd and returns the results in a container
ksrini20a64b22008-09-24 15:07:41 -0700177 */
ksrini11e7f1b2009-11-20 11:01:32 -0800178 static TestResult doExec(Map<String, String> envToSet, String...cmds) {
ksrini20a64b22008-09-24 15:07:41 -0700179 String cmdStr = "";
180 for (String x : cmds) {
181 cmdStr = cmdStr.concat(x + " ");
182 }
183 ProcessBuilder pb = new ProcessBuilder(cmds);
184 Map<String, String> env = pb.environment();
ksrini11e7f1b2009-11-20 11:01:32 -0800185 if (envToSet != null) {
186 env.putAll(envToSet);
187 }
ksrini20a64b22008-09-24 15:07:41 -0700188 BufferedReader rdr = null;
189 try {
190 List<String> outputList = new ArrayList<String>();
191 pb.redirectErrorStream(true);
192 Process p = pb.start();
193 rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
194 String in = rdr.readLine();
195 while (in != null) {
196 outputList.add(in);
197 in = rdr.readLine();
198 }
199 p.waitFor();
200 p.destroy();
201 return new TestHelper.TestResult(cmdStr, p.exitValue(), outputList);
202 } catch (Exception ex) {
203 ex.printStackTrace();
204 throw new RuntimeException(ex.getMessage());
205 }
206 }
207
208 /*
209 * A class to encapsulate the test results and stuff, with some ease
210 * of use methods to check the test results.
211 */
212 static class TestResult {
213 StringBuilder status;
214 int exitValue;
215 List<String> testOutput;
216
217 public TestResult(String str, int rv, List<String> oList) {
ksrini11e7f1b2009-11-20 11:01:32 -0800218 status = new StringBuilder("Executed command: " + str + "\n");
ksrini20a64b22008-09-24 15:07:41 -0700219 exitValue = rv;
220 testOutput = oList;
221 }
222
ksrini11e7f1b2009-11-20 11:01:32 -0800223 void appendStatus(String x) {
224 status = status.append(" " + x + "\n");
225 }
226
ksrini20a64b22008-09-24 15:07:41 -0700227 void checkNegative() {
228 if (exitValue == 0) {
ksrini11e7f1b2009-11-20 11:01:32 -0800229 appendStatus("Error: test must not return 0 exit value");
ksrini20a64b22008-09-24 15:07:41 -0700230 testExitValue++;
231 }
232 }
233
234 void checkPositive() {
235 if (exitValue != 0) {
ksrini11e7f1b2009-11-20 11:01:32 -0800236 appendStatus("Error: test did not return 0 exit value");
ksrini20a64b22008-09-24 15:07:41 -0700237 testExitValue++;
238 }
239 }
240
241 boolean isOK() {
242 return exitValue == 0;
243 }
244
245 boolean isZeroOutput() {
246 if (!testOutput.isEmpty()) {
ksrini11e7f1b2009-11-20 11:01:32 -0800247 appendStatus("Error: No message from cmd please");
ksrini20a64b22008-09-24 15:07:41 -0700248 testExitValue++;
249 return false;
250 }
251 return true;
252 }
253
254 boolean isNotZeroOutput() {
255 if (testOutput.isEmpty()) {
ksrini11e7f1b2009-11-20 11:01:32 -0800256 appendStatus("Error: Missing message");
ksrini20a64b22008-09-24 15:07:41 -0700257 testExitValue++;
258 return false;
259 }
260 return true;
261 }
262
ksrini11e7f1b2009-11-20 11:01:32 -0800263 @Override
ksrini20a64b22008-09-24 15:07:41 -0700264 public String toString() {
ksrini11e7f1b2009-11-20 11:01:32 -0800265 status = status.append("++++Test Output Begin++++\n");
266 for (String x : testOutput) {
267 appendStatus(x);
ksrini20a64b22008-09-24 15:07:41 -0700268 }
ksrini11e7f1b2009-11-20 11:01:32 -0800269 status = status.append("++++Test Output End++++\n");
ksrini20a64b22008-09-24 15:07:41 -0700270 return status.toString();
271 }
272
273 boolean contains(String str) {
274 for (String x : testOutput) {
275 if (x.contains(str)) {
276 return true;
277 }
278 }
ksrini11e7f1b2009-11-20 11:01:32 -0800279 appendStatus("Error: string <" + str + "> not found");
280 testExitValue++;
281 return false;
282 }
283
284 boolean matches(String stringToMatch) {
285 for (String x : testOutput) {
286 if (x.matches(stringToMatch)) {
287 return true;
288 }
289 }
290 appendStatus("Error: string <" + stringToMatch + "> not found");
ksrini20a64b22008-09-24 15:07:41 -0700291 testExitValue++;
292 return false;
293 }
294 }
295}