|
1 |
|
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 |
|
25 import javax.tools.ToolProvider; |
|
26 import java.io.BufferedReader; |
|
27 import java.io.File; |
|
28 import java.io.FileNotFoundException; |
|
29 import java.io.FileOutputStream; |
|
30 import java.io.InputStreamReader; |
|
31 import java.io.PrintStream; |
|
32 import java.util.ArrayList; |
|
33 import java.util.List; |
|
34 import java.util.Map; |
|
35 import javax.tools.JavaCompiler; |
|
36 |
|
37 /** |
|
38 * This class provides some common utilites for the launcher tests. |
|
39 */ |
|
40 public 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 /* |
|
75 * A generic jar file creator which creates the java file, compiles it |
|
76 * and jar's it up for use. |
|
77 */ |
|
78 static void createJar(File jarName, File mainClass, String... mainDefs) |
|
79 throws FileNotFoundException { |
|
80 createJar(null, jarName, mainClass, mainDefs); |
|
81 } |
|
82 |
|
83 /* |
|
84 * A method which takes manifest entry to specify a specific manifest |
|
85 * Main-Class name. |
|
86 */ |
|
87 static void createJar(String mEntry, File jarName, File mainClass, String... mainDefs) |
|
88 throws FileNotFoundException { |
|
89 if (jarName.exists()) { |
|
90 jarName.delete(); |
|
91 } |
|
92 PrintStream ps = new PrintStream(new FileOutputStream(mainClass + ".java")); |
|
93 ps.println("public class Foo {"); |
|
94 if (mainDefs != null) { |
|
95 for (String x : mainDefs) { |
|
96 ps.println(x); |
|
97 } |
|
98 } |
|
99 ps.println("}"); |
|
100 ps.close(); |
|
101 |
|
102 String compileArgs[] = { |
|
103 mainClass + ".java" |
|
104 }; |
|
105 if (compiler.run(null, null, null, compileArgs) != 0) { |
|
106 throw new RuntimeException("compilation failed " + mainClass + ".java"); |
|
107 } |
|
108 |
|
109 if (mEntry == null && mainDefs == null) { |
|
110 mEntry = "MIA"; |
|
111 } else { |
|
112 mEntry = mainClass.getName(); |
|
113 } |
|
114 String jarArgs[] = { |
|
115 (debug) ? "cvfe" : "cfe", |
|
116 jarName.getAbsolutePath(), |
|
117 mEntry, |
|
118 mainClass.getName() + ".class" |
|
119 }; |
|
120 sun.tools.jar.Main jarTool = |
|
121 new sun.tools.jar.Main(System.out, System.err, "JarCreator"); |
|
122 if (!jarTool.run(jarArgs)) { |
|
123 throw new RuntimeException("jar creation failed " + jarName); |
|
124 } |
|
125 } |
|
126 |
|
127 /* |
|
128 * A method which executes a java cmd and returs the results in a container |
|
129 */ |
|
130 static TestResult doExec(String...cmds) { |
|
131 String cmdStr = ""; |
|
132 for (String x : cmds) { |
|
133 cmdStr = cmdStr.concat(x + " "); |
|
134 } |
|
135 ProcessBuilder pb = new ProcessBuilder(cmds); |
|
136 Map<String, String> env = pb.environment(); |
|
137 BufferedReader rdr = null; |
|
138 try { |
|
139 List<String> outputList = new ArrayList<String>(); |
|
140 pb.redirectErrorStream(true); |
|
141 Process p = pb.start(); |
|
142 rdr = new BufferedReader(new InputStreamReader(p.getInputStream())); |
|
143 String in = rdr.readLine(); |
|
144 while (in != null) { |
|
145 outputList.add(in); |
|
146 in = rdr.readLine(); |
|
147 } |
|
148 p.waitFor(); |
|
149 p.destroy(); |
|
150 return new TestHelper.TestResult(cmdStr, p.exitValue(), outputList); |
|
151 } catch (Exception ex) { |
|
152 ex.printStackTrace(); |
|
153 throw new RuntimeException(ex.getMessage()); |
|
154 } |
|
155 } |
|
156 |
|
157 /* |
|
158 * A class to encapsulate the test results and stuff, with some ease |
|
159 * of use methods to check the test results. |
|
160 */ |
|
161 static class TestResult { |
|
162 StringBuilder status; |
|
163 int exitValue; |
|
164 List<String> testOutput; |
|
165 |
|
166 public TestResult(String str, int rv, List<String> oList) { |
|
167 status = new StringBuilder(str); |
|
168 exitValue = rv; |
|
169 testOutput = oList; |
|
170 } |
|
171 |
|
172 void checkNegative() { |
|
173 if (exitValue == 0) { |
|
174 status = status.append(" Error: test must not return 0 exit value"); |
|
175 testExitValue++; |
|
176 } |
|
177 } |
|
178 |
|
179 void checkPositive() { |
|
180 if (exitValue != 0) { |
|
181 status = status.append(" Error: test did not return 0 exit value"); |
|
182 testExitValue++; |
|
183 } |
|
184 } |
|
185 |
|
186 boolean isOK() { |
|
187 return exitValue == 0; |
|
188 } |
|
189 |
|
190 boolean isZeroOutput() { |
|
191 if (!testOutput.isEmpty()) { |
|
192 status = status.append(" Error: No message from cmd please"); |
|
193 testExitValue++; |
|
194 return false; |
|
195 } |
|
196 return true; |
|
197 } |
|
198 |
|
199 boolean isNotZeroOutput() { |
|
200 if (testOutput.isEmpty()) { |
|
201 status = status.append(" Error: Missing message"); |
|
202 testExitValue++; |
|
203 return false; |
|
204 } |
|
205 return true; |
|
206 } |
|
207 |
|
208 public String toString() { |
|
209 if (debug) { |
|
210 for (String x : testOutput) { |
|
211 status = status.append(x + "\n"); |
|
212 } |
|
213 } |
|
214 return status.toString(); |
|
215 } |
|
216 |
|
217 boolean contains(String str) { |
|
218 for (String x : testOutput) { |
|
219 if (x.contains(str)) { |
|
220 return true; |
|
221 } |
|
222 } |
|
223 status = status.append(" Error: string <" + str + "> not found "); |
|
224 testExitValue++; |
|
225 return false; |
|
226 } |
|
227 } |
|
228 } |