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.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.text.NumberFormat;
24 import junit.framework.AssertionFailedError;
25 import junit.framework.Test;
26 import org.apache.tools.ant.BuildException;
27
28 /***
29 * Prints plain text output of the test to a specified Writer.
30 * Inspired by the PlainJUnitResultFormatter.
31 *
32 *
33 * @see FormatterElement
34 * @see PlainJUnitResultFormatter
35 */
36 public class BriefJUnitResultFormatter implements JUnitResultFormatter {
37
38 /***
39 * Where to write the log to.
40 */
41 private OutputStream out;
42
43 /***
44 * Used for writing the results.
45 */
46 private PrintWriter output;
47
48 /***
49 * Used as part of formatting the results.
50 */
51 private StringWriter results;
52
53 /***
54 * Used for writing formatted results to.
55 */
56 private PrintWriter resultWriter;
57
58 /***
59 * Formatter for timings.
60 */
61 private NumberFormat numberFormat = NumberFormat.getInstance();
62
63 /***
64 * Output suite has written to System.out
65 */
66 private String systemOutput = null;
67
68 /***
69 * Output suite has written to System.err
70 */
71 private String systemError = null;
72
73 public BriefJUnitResultFormatter() {
74 results = new StringWriter();
75 resultWriter = new PrintWriter(results);
76 }
77
78 /***
79 * Sets the stream the formatter is supposed to write its results to.
80 */
81 public void setOutput(OutputStream out) {
82 this.out = out;
83 output = new PrintWriter(out);
84 }
85
86 public void setSystemOutput(String out) {
87 systemOutput = out;
88 }
89
90 public void setSystemError(String err) {
91 systemError = err;
92 }
93
94
95 /***
96 * The whole testsuite started.
97 */
98 public void startTestSuite(JUnitTest suite) throws BuildException {
99 }
100
101 /***
102 * The whole testsuite ended.
103 */
104 public void endTestSuite(JUnitTest suite) throws BuildException {
105 String newLine = System.getProperty("line.separator");
106 StringBuffer sb = new StringBuffer("Testsuite: ");
107 sb.append(suite.getName());
108 sb.append(newLine);
109 sb.append("Tests run: ");
110 sb.append(suite.runCount());
111 sb.append(", Failures: ");
112 sb.append(suite.failureCount());
113 sb.append(", Errors: ");
114 sb.append(suite.errorCount());
115 sb.append(", Time elapsed: ");
116 sb.append(numberFormat.format(suite.getRunTime() / 1000.0));
117 sb.append(" sec");
118 sb.append(newLine);
119 sb.append(newLine);
120
121
122 if (systemOutput != null && systemOutput.length() > 0) {
123 sb.append("------------- Standard Output ---------------")
124 .append(newLine)
125 .append(systemOutput)
126 .append("------------- ---------------- ---------------")
127 .append(newLine);
128 }
129
130 if (systemError != null && systemError.length() > 0) {
131 sb.append("------------- Standard Error -----------------")
132 .append(newLine)
133 .append(systemError)
134 .append("------------- ---------------- ---------------")
135 .append(newLine);
136 }
137
138 if (output != null) {
139 try {
140 output.write(sb.toString());
141 resultWriter.close();
142 output.write(results.toString());
143 output.flush();
144 } finally {
145 if (out != System.out && out != System.err) {
146 try {
147 out.close();
148 } catch (IOException e) {
149
150 }
151 }
152 }
153 }
154 }
155
156 /***
157 * A test started.
158 */
159 public void startTest(Test test) {
160 }
161
162 /***
163 * A test ended.
164 */
165 public void endTest(Test test) {
166 }
167
168 /***
169 * Interface TestListener for JUnit <= 3.4.
170 *
171 * <p>A Test failed.
172 */
173 public void addFailure(Test test, Throwable t) {
174 formatError("\tFAILED", test, t);
175 }
176
177 /***
178 * Interface TestListener for JUnit > 3.4.
179 *
180 * <p>A Test failed.
181 */
182 public void addFailure(Test test, AssertionFailedError t) {
183 addFailure(test, (Throwable) t);
184 }
185
186 /***
187 * A test caused an error.
188 */
189 public void addError(Test test, Throwable error) {
190 formatError("\tCaused an ERROR", test, error);
191 }
192
193 /***
194 * Format the test for printing..
195 */
196 protected String formatTest(Test test) {
197 if (test == null) {
198 return "Null Test: ";
199 } else {
200 return "Testcase: " + test.toString() + ":";
201 }
202 }
203
204 /***
205 * Format an error and print it.
206 */
207 protected synchronized void formatError(String type, Test test,
208 Throwable error) {
209 if (test != null) {
210 endTest(test);
211 }
212
213 resultWriter.println(formatTest(test) + type);
214 resultWriter.println(error.getMessage());
215 String strace = JUnitTestRunner.getFilteredTrace(error);
216 resultWriter.println(strace);
217 resultWriter.println();
218 }
219 }