1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.woopi.ant.taskdefs.junit;
19 import java.io.IOException;
20 import java.io.OutputStream;
21 import java.text.NumberFormat;
22 import junit.framework.AssertionFailedError;
23 import junit.framework.Test;
24 import org.apache.tools.ant.BuildException;
25
26 /***
27 * Prints short summary output of the test to Ant's logging system.
28 *
29 */
30
31 public class SummaryJUnitResultFormatter implements JUnitResultFormatter {
32
33 /***
34 * Formatter for timings.
35 */
36 private NumberFormat nf = NumberFormat.getInstance();
37 /***
38 * OutputStream to write to.
39 */
40 private OutputStream out;
41
42 private boolean withOutAndErr = false;
43 private String systemOutput = null;
44 private String systemError = null;
45
46 /***
47 * Empty
48 */
49 public SummaryJUnitResultFormatter() {
50 }
51 /***
52 * Empty
53 */
54 public void startTestSuite(JUnitTest suite) {
55 }
56 /***
57 * Empty
58 */
59 public void startTest(Test t) {
60 }
61 /***
62 * Empty
63 */
64 public void endTest(Test test) {
65 }
66 /***
67 * Empty
68 */
69 public void addFailure(Test test, Throwable t) {
70 }
71 /***
72 * Interface TestListener for JUnit > 3.4.
73 *
74 * <p>A Test failed.
75 */
76 public void addFailure(Test test, AssertionFailedError t) {
77 addFailure(test, (Throwable) t);
78 }
79 /***
80 * Empty
81 */
82 public void addError(Test test, Throwable t) {
83 }
84
85 public void setOutput(OutputStream out) {
86 this.out = out;
87 }
88
89 public void setSystemOutput(String out) {
90 systemOutput = out;
91 }
92
93 public void setSystemError(String err) {
94 systemError = err;
95 }
96
97 /***
98 * Should the output to System.out and System.err be written to
99 * the summary.
100 */
101 public void setWithOutAndErr(boolean value) {
102 withOutAndErr = value;
103 }
104
105 /***
106 * The whole testsuite ended.
107 */
108 public void endTestSuite(JUnitTest suite) throws BuildException {
109 String newLine = System.getProperty("line.separator");
110 StringBuffer sb = new StringBuffer("Tests run: ");
111 sb.append(suite.runCount());
112 sb.append(", Failures: ");
113 sb.append(suite.failureCount());
114 sb.append(", Errors: ");
115 sb.append(suite.errorCount());
116 sb.append(", Time elapsed: ");
117 sb.append(nf.format(suite.getRunTime() / 1000.0));
118 sb.append(" sec");
119 sb.append(newLine);
120
121 if (withOutAndErr) {
122 if (systemOutput != null && systemOutput.length() > 0) {
123 sb.append("Output:").append(newLine).append(systemOutput)
124 .append(newLine);
125 }
126
127 if (systemError != null && systemError.length() > 0) {
128 sb.append("Error: ").append(newLine).append(systemError)
129 .append(newLine);
130 }
131 }
132
133 try {
134 out.write(sb.toString().getBytes());
135 out.flush();
136 } catch (IOException ioex) {
137 throw new BuildException("Unable to write summary output", ioex);
138 } finally {
139 if (out != System.out && out != System.err) {
140 try {
141 out.close();
142 } catch (IOException e) {
143
144 }
145 }
146 }
147 }
148 }