19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 * or visit www.oracle.com if you need additional information or have any |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
|
24 import java.lang.annotation.ElementType; |
|
25 import java.lang.annotation.Retention; |
|
26 import java.lang.annotation.RetentionPolicy; |
|
27 import java.lang.annotation.Target; |
|
28 import java.lang.reflect.Method; |
|
29 import java.util.regex.Pattern; |
24 import java.io.StringWriter; |
30 import java.io.StringWriter; |
25 import java.io.PrintWriter; |
31 import java.io.PrintWriter; |
26 import java.util.Set; |
32 import java.util.Set; |
27 import java.io.BufferedReader; |
33 import java.io.BufferedReader; |
28 import java.io.File; |
34 import java.io.File; |
61 static final boolean isSDK = JAVAHOME.endsWith("jre"); |
67 static final boolean isSDK = JAVAHOME.endsWith("jre"); |
62 static final String javaCmd; |
68 static final String javaCmd; |
63 static final String javawCmd; |
69 static final String javawCmd; |
64 static final String java64Cmd; |
70 static final String java64Cmd; |
65 static final String javacCmd; |
71 static final String javacCmd; |
|
72 static final String jarCmd; |
|
73 |
66 static final JavaCompiler compiler; |
74 static final JavaCompiler compiler; |
67 |
75 |
68 static final boolean debug = Boolean.getBoolean("TestHelper.Debug"); |
76 static final boolean debug = Boolean.getBoolean("TestHelper.Debug"); |
69 static final boolean isWindows = |
77 static final boolean isWindows = |
70 System.getProperty("os.name", "unknown").startsWith("Windows"); |
78 System.getProperty("os.name", "unknown").startsWith("Windows"); |
129 File javacCmdFile = (isWindows) |
137 File javacCmdFile = (isWindows) |
130 ? new File(binDir, "javac.exe") |
138 ? new File(binDir, "javac.exe") |
131 : new File(binDir, "javac"); |
139 : new File(binDir, "javac"); |
132 javacCmd = javacCmdFile.getAbsolutePath(); |
140 javacCmd = javacCmdFile.getAbsolutePath(); |
133 |
141 |
|
142 File jarCmdFile = (isWindows) |
|
143 ? new File(binDir, "jar.exe") |
|
144 : new File(binDir, "jar"); |
|
145 jarCmd = jarCmdFile.getAbsolutePath(); |
|
146 if (!jarCmdFile.canExecute()) { |
|
147 throw new RuntimeException("java <" + TestHelper.jarCmd + |
|
148 "> must exist and should be executable"); |
|
149 } |
|
150 |
134 if (isWindows) { |
151 if (isWindows) { |
135 File javawCmdFile = new File(binDir, "javaw.exe"); |
152 File javawCmdFile = new File(binDir, "javaw.exe"); |
136 javawCmd = javawCmdFile.getAbsolutePath(); |
153 javawCmd = javawCmdFile.getAbsolutePath(); |
137 if (!javawCmdFile.canExecute()) { |
154 if (!javawCmdFile.canExecute()) { |
138 throw new RuntimeException("java <" + javawCmd + |
155 throw new RuntimeException("java <" + javawCmd + |
154 } else { |
171 } else { |
155 java64Cmd = null; |
172 java64Cmd = null; |
156 } |
173 } |
157 } else { |
174 } else { |
158 java64Cmd = null; |
175 java64Cmd = null; |
|
176 } |
|
177 } |
|
178 void run(String[] args) throws Exception { |
|
179 int passed = 0, failed = 0; |
|
180 final Pattern p = (args != null && args.length > 0) |
|
181 ? Pattern.compile(args[0]) |
|
182 : null; |
|
183 for (Method m : this.getClass().getDeclaredMethods()) { |
|
184 boolean selected = (p == null) |
|
185 ? m.isAnnotationPresent(Test.class) |
|
186 : p.matcher(m.getName()).matches(); |
|
187 if (selected) { |
|
188 try { |
|
189 m.invoke(this, (Object[]) null); |
|
190 System.out.println(m.getName() + ": OK"); |
|
191 passed++; |
|
192 } catch (Throwable ex) { |
|
193 System.out.printf("Test %s failed: %s %n", m, ex.getCause()); |
|
194 failed++; |
|
195 } |
|
196 } |
|
197 } |
|
198 System.out.printf("Passed: %d, Failed %d%n", passed, failed); |
|
199 if (failed > 0) { |
|
200 throw new RuntimeException("Tests failed: " + failed); |
|
201 } |
|
202 if (passed == 0 && failed == 0) { |
|
203 throw new AssertionError("No test(s) selected: passed = " + |
|
204 passed + ", failed = " + failed + " ??????????"); |
159 } |
205 } |
160 } |
206 } |
161 |
207 |
162 /* |
208 /* |
163 * is a dual mode available in the test jdk |
209 * is a dual mode available in the test jdk |
393 StringWriter sw; |
439 StringWriter sw; |
394 int exitValue; |
440 int exitValue; |
395 List<String> testOutput; |
441 List<String> testOutput; |
396 Map<String, String> env; |
442 Map<String, String> env; |
397 Throwable t; |
443 Throwable t; |
|
444 boolean testStatus; |
398 |
445 |
399 public TestResult(String str, int rv, List<String> oList, |
446 public TestResult(String str, int rv, List<String> oList, |
400 Map<String, String> env, Throwable t) { |
447 Map<String, String> env, Throwable t) { |
401 sw = new StringWriter(); |
448 sw = new StringWriter(); |
402 status = new PrintWriter(sw); |
449 status = new PrintWriter(sw); |
403 status.println("Executed command: " + str + "\n"); |
450 status.println("Executed command: " + str + "\n"); |
404 exitValue = rv; |
451 exitValue = rv; |
405 testOutput = oList; |
452 testOutput = oList; |
406 this.env = env; |
453 this.env = env; |
407 this.t = t; |
454 this.t = t; |
|
455 testStatus = true; |
408 } |
456 } |
409 |
457 |
410 void appendError(String x) { |
458 void appendError(String x) { |
411 status.println(TEST_PREFIX + x); |
459 status.println(TEST_PREFIX + x); |
412 } |
460 } |
416 } |
464 } |
417 |
465 |
418 void checkNegative() { |
466 void checkNegative() { |
419 if (exitValue == 0) { |
467 if (exitValue == 0) { |
420 appendError("test must not return 0 exit value"); |
468 appendError("test must not return 0 exit value"); |
|
469 testStatus = false; |
421 testExitValue++; |
470 testExitValue++; |
422 } |
471 } |
423 } |
472 } |
424 |
473 |
425 void checkPositive() { |
474 void checkPositive() { |
426 if (exitValue != 0) { |
475 if (exitValue != 0) { |
|
476 testStatus = false; |
427 appendError("test did not return 0 exit value"); |
477 appendError("test did not return 0 exit value"); |
428 testExitValue++; |
478 testExitValue++; |
429 } |
479 } |
430 } |
480 } |
431 |
481 |
433 return exitValue == 0; |
483 return exitValue == 0; |
434 } |
484 } |
435 |
485 |
436 boolean isZeroOutput() { |
486 boolean isZeroOutput() { |
437 if (!testOutput.isEmpty()) { |
487 if (!testOutput.isEmpty()) { |
|
488 testStatus = false; |
438 appendError("No message from cmd please"); |
489 appendError("No message from cmd please"); |
439 testExitValue++; |
490 testExitValue++; |
440 return false; |
491 return false; |
441 } |
492 } |
442 return true; |
493 return true; |
443 } |
494 } |
444 |
495 |
445 boolean isNotZeroOutput() { |
496 boolean isNotZeroOutput() { |
446 if (testOutput.isEmpty()) { |
497 if (testOutput.isEmpty()) { |
|
498 testStatus = false; |
447 appendError("Missing message"); |
499 appendError("Missing message"); |
448 testExitValue++; |
500 testExitValue++; |
449 return false; |
501 return false; |
450 } |
502 } |
451 return true; |
503 return true; |
452 } |
504 } |
453 |
505 |
454 @Override |
506 @Override |
455 public String toString() { |
507 public String toString() { |
456 status.println("++++Begin Test Info++++"); |
508 status.println("++++Begin Test Info++++"); |
|
509 status.println("Test Status: " + (testStatus ? "PASS" : "FAIL")); |
457 status.println("++++Test Environment++++"); |
510 status.println("++++Test Environment++++"); |
458 for (String x : env.keySet()) { |
511 for (String x : env.keySet()) { |
459 indentStatus(x + "=" + env.get(x)); |
512 indentStatus(x + "=" + env.get(x)); |
460 } |
513 } |
461 status.println("++++Test Output++++"); |
514 status.println("++++Test Output++++"); |