1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package javax.jdo.util;
19
20 import java.io.PrintStream;
21
22 import junit.framework.AssertionFailedError;
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestResult;
26 import junit.textui.ResultPrinter;
27
28 /**
29 * Default result printer implementation for running tests in batch mode.
30 *
31 * @author Michael Bouschen
32 */
33 public class BatchResultPrinter
34 extends ResultPrinter
35 {
36 /** */
37 public BatchResultPrinter(PrintStream writer) {
38 super(writer);
39 }
40
41 /** Called in case of a test error. */
42 public void addError(Test test, Throwable t) {
43 getWriter().print(" ERROR");
44 }
45
46 /** Called in case of a test failure. */
47 public void addFailure(Test test, AssertionFailedError t) {
48 getWriter().print(" FAILURE");
49 }
50
51 /** Called when a test case is finished. */
52 public void endTest(Test test) {
53 getWriter().println();
54 }
55
56 /** Called when a test case is started. */
57 public void startTest(Test test) {
58 String testName;
59 if (test instanceof TestCase) {
60 testName = getClassBaseName(test) + "." + ((TestCase)test).getName();
61 }
62 else {
63 testName = test.toString();
64 }
65 getWriter().print("RUN " + testName);
66 }
67
68 /** */
69 protected void printHeader(long runTime) {
70 getWriter().println("Time: "+elapsedTimeAsString(runTime));
71 }
72
73 /** */
74 protected void printFooter(TestResult result) {
75 if (result.wasSuccessful()) {
76 getWriter().print("OK");
77 getWriter().println (" (" + result.runCount() + " test" + (result.runCount() == 1 ? "": "s") + ")");
78
79 } else {
80 getWriter().println("FAILURES!!!");
81 getWriter().println("Tests run: "+result.runCount()+
82 ", Failures: "+result.failureCount()+
83 ", Errors: "+result.errorCount());
84 }
85 }
86
87
88
89 /**
90 * @return Name of the class of the given object without package prefix
91 */
92 private String getClassBaseName(Object obj) {
93 if (obj == null) return null;
94 String className = obj.getClass().getName();
95 int index = className.lastIndexOf('.');
96 if (index != -1) {
97 className = className.substring(index + 1);
98 }
99 return className;
100 }
101
102 }
103
104