blob: fb63114b6d582864263766e257506e0b139fcdf3 [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;
42 static final String JAVAHOME = System.getProperty("java.home", ".");
43 static final boolean isSDK = JAVAHOME.endsWith("jre");
44 static final String javaCmd;
45 static final String javacCmd;
46 static final JavaCompiler compiler;
47
48 static final boolean debug = Boolean.getBoolean("Arrrghs.Debug");
49 static final boolean isWindows =
50 System.getProperty("os.name", "unknown").startsWith("Windows");
51 static int testExitValue = 0;
52
53 static {
54 compiler = ToolProvider.getSystemJavaCompiler();
55 File binDir = (isSDK) ? new File((new File(JAVAHOME)).getParentFile(), "bin")
56 : new File(JAVAHOME, "bin");
57 File javaCmdFile = (isWindows)
58 ? new File(binDir, "java.exe")
59 : new File(binDir, "java");
60 javaCmd = javaCmdFile.getAbsolutePath();
61 if (!javaCmdFile.canExecute()) {
62 throw new RuntimeException("java <" + TestHelper.javaCmd + "> must exist");
63 }
64
65 File javacCmdFile = (isWindows)
66 ? new File(binDir, "javac.exe")
67 : new File(binDir, "javac");
68 javacCmd = javacCmdFile.getAbsolutePath();
69 if (!javacCmdFile.canExecute()) {
70 throw new RuntimeException("java <" + javacCmd + "> must exist");
71 }
72 }
73
74 /*
ksrinicafaf7c2008-10-01 09:04:42 -070075 * A convenience method to create a java file, compile and jar it up, using
76 * the sole class file name in the jar, as the Main-Class attribute value.
ksrini20a64b22008-09-24 15:07:41 -070077 */
78 static void createJar(File jarName, File mainClass, String... mainDefs)
79 throws FileNotFoundException {
80 createJar(null, jarName, mainClass, mainDefs);
81 }
82
83 /*
ksrinicafaf7c2008-10-01 09:04:42 -070084 * A generic jar file creator to create a java file, compile it
85 * and jar it up, a specific Main-Class entry name in the
86 * manifest can be specified or a null to use the sole class file name
87 * as the Main-Class attribute value.
ksrini20a64b22008-09-24 15:07:41 -070088 */
ksrinicafaf7c2008-10-01 09:04:42 -070089 static void createJar(String mEntry, File jarName, File mainClass,
90 String... mainDefs) throws FileNotFoundException {
ksrini20a64b22008-09-24 15:07:41 -070091 if (jarName.exists()) {
92 jarName.delete();
93 }
94 PrintStream ps = new PrintStream(new FileOutputStream(mainClass + ".java"));
95 ps.println("public class Foo {");
96 if (mainDefs != null) {
97 for (String x : mainDefs) {
98 ps.println(x);
99 }
100 }
101 ps.println("}");
102 ps.close();
103
104 String compileArgs[] = {
105 mainClass + ".java"
106 };
107 if (compiler.run(null, null, null, compileArgs) != 0) {
108 throw new RuntimeException("compilation failed " + mainClass + ".java");
109 }
ksrinicafaf7c2008-10-01 09:04:42 -0700110 if (mEntry == null) {
ksrini20a64b22008-09-24 15:07:41 -0700111 mEntry = mainClass.getName();
112 }
113 String jarArgs[] = {
114 (debug) ? "cvfe" : "cfe",
115 jarName.getAbsolutePath(),
116 mEntry,
117 mainClass.getName() + ".class"
118 };
119 sun.tools.jar.Main jarTool =
120 new sun.tools.jar.Main(System.out, System.err, "JarCreator");
121 if (!jarTool.run(jarArgs)) {
122 throw new RuntimeException("jar creation failed " + jarName);
123 }
124 }
125
126 /*
ksrinicafaf7c2008-10-01 09:04:42 -0700127 * A method which executes a java cmd and returns the results in a container
ksrini20a64b22008-09-24 15:07:41 -0700128 */
129 static TestResult doExec(String...cmds) {
130 String cmdStr = "";
131 for (String x : cmds) {
132 cmdStr = cmdStr.concat(x + " ");
133 }
134 ProcessBuilder pb = new ProcessBuilder(cmds);
135 Map<String, String> env = pb.environment();
136 BufferedReader rdr = null;
137 try {
138 List<String> outputList = new ArrayList<String>();
139 pb.redirectErrorStream(true);
140 Process p = pb.start();
141 rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
142 String in = rdr.readLine();
143 while (in != null) {
144 outputList.add(in);
145 in = rdr.readLine();
146 }
147 p.waitFor();
148 p.destroy();
149 return new TestHelper.TestResult(cmdStr, p.exitValue(), outputList);
150 } catch (Exception ex) {
151 ex.printStackTrace();
152 throw new RuntimeException(ex.getMessage());
153 }
154 }
155
156 /*
157 * A class to encapsulate the test results and stuff, with some ease
158 * of use methods to check the test results.
159 */
160 static class TestResult {
161 StringBuilder status;
162 int exitValue;
163 List<String> testOutput;
164
165 public TestResult(String str, int rv, List<String> oList) {
166 status = new StringBuilder(str);
167 exitValue = rv;
168 testOutput = oList;
169 }
170
171 void checkNegative() {
172 if (exitValue == 0) {
173 status = status.append(" Error: test must not return 0 exit value");
174 testExitValue++;
175 }
176 }
177
178 void checkPositive() {
179 if (exitValue != 0) {
180 status = status.append(" Error: test did not return 0 exit value");
181 testExitValue++;
182 }
183 }
184
185 boolean isOK() {
186 return exitValue == 0;
187 }
188
189 boolean isZeroOutput() {
190 if (!testOutput.isEmpty()) {
191 status = status.append(" Error: No message from cmd please");
192 testExitValue++;
193 return false;
194 }
195 return true;
196 }
197
198 boolean isNotZeroOutput() {
199 if (testOutput.isEmpty()) {
200 status = status.append(" Error: Missing message");
201 testExitValue++;
202 return false;
203 }
204 return true;
205 }
206
207 public String toString() {
208 if (debug) {
209 for (String x : testOutput) {
210 status = status.append(x + "\n");
211 }
212 }
213 return status.toString();
214 }
215
216 boolean contains(String str) {
217 for (String x : testOutput) {
218 if (x.contains(str)) {
219 return true;
220 }
221 }
222 status = status.append(" Error: string <" + str + "> not found ");
223 testExitValue++;
224 return false;
225 }
226 }
227}