Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
* contributor license agreements. See the NOTICE file distributed with |
4 |
* this work for additional information regarding copyright ownership. |
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
* (the "License"); you may not use this file except in compliance with |
7 |
* the License. You may obtain a copy of the License at |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, software |
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
* See the License for the specific language governing permissions and |
15 |
* limitations under the License. |
16 |
* |
17 |
* NOTE: |
18 |
* This file was added by Mathias Ricken to include support for a |
19 |
* "quiet" JUnit formatter. |
20 |
* For more information, please go to |
21 |
* |
22 |
* http://www.superscalar.org/files/apache-ant-1.7.0-quiet/ |
23 |
* |
24 |
* Thank you. |
25 |
*/ |
26 |
|
27 |
package org.apache.tools.ant.taskdefs.optional.junit; |
28 |
|
29 |
import java.io.OutputStream; |
30 |
import java.io.PrintWriter; |
31 |
import java.io.StringWriter; |
32 |
import java.text.NumberFormat; |
33 |
|
34 |
import junit.framework.AssertionFailedError; |
35 |
import junit.framework.Test; |
36 |
|
37 |
import org.apache.tools.ant.util.FileUtils; |
38 |
import org.apache.tools.ant.util.StringUtils; |
39 |
|
40 |
/** |
41 |
* Only prints out errors, no summaries. |
42 |
* |
43 |
* @see FormatterElement |
44 |
* @see PlainJUnitResultFormatter |
45 |
*/ |
46 |
public class QuietJUnitResultFormatter implements JUnitResultFormatter { |
47 |
|
48 |
/** |
49 |
* Where to write the log to. |
50 |
*/ |
51 |
private OutputStream out; |
52 |
|
53 |
/** |
54 |
* Used for writing the results. |
55 |
*/ |
56 |
private PrintWriter output; |
57 |
|
58 |
/** |
59 |
* Used as part of formatting the results. |
60 |
*/ |
61 |
private StringWriter results; |
62 |
|
63 |
/** |
64 |
* Used for writing formatted results to. |
65 |
*/ |
66 |
private PrintWriter resultWriter; |
67 |
|
68 |
/** |
69 |
* Formatter for timings. |
70 |
*/ |
71 |
private NumberFormat numberFormat = NumberFormat.getInstance(); |
72 |
|
73 |
/** |
74 |
* Output suite has written to System.out |
75 |
*/ |
76 |
private String systemOutput = null; |
77 |
|
78 |
/** |
79 |
* Output suite has written to System.err |
80 |
*/ |
81 |
private String systemError = null; |
82 |
|
83 |
/** |
84 |
* Constructor for QuietJUnitResultFormatter. |
85 |
*/ |
86 |
public QuietJUnitResultFormatter() { |
87 |
results = new StringWriter(); |
88 |
resultWriter = new PrintWriter(results); |
89 |
} |
90 |
|
91 |
/** |
92 |
* Sets the stream the formatter is supposed to write its results to. |
93 |
* @param out the output stream to write to |
94 |
*/ |
95 |
public void setOutput(OutputStream out) { |
96 |
this.out = out; |
97 |
output = new PrintWriter(out); |
98 |
} |
99 |
|
100 |
/** |
101 |
* @see JUnitResultFormatter#setSystemOutput(String) |
102 |
*/ |
103 |
/** {@inheritDoc}. */ |
104 |
public void setSystemOutput(String out) { |
105 |
systemOutput = out; |
106 |
} |
107 |
|
108 |
/** |
109 |
* @see JUnitResultFormatter#setSystemError(String) |
110 |
*/ |
111 |
/** {@inheritDoc}. */ |
112 |
public void setSystemError(String err) { |
113 |
systemError = err; |
114 |
} |
115 |
|
116 |
/** |
117 |
* String generated when the testsuite was started. |
118 |
*/ |
119 |
private String _startTestSuiteOutput = ""; |
120 |
|
121 |
/** |
122 |
* The whole testsuite started. |
123 |
* @param suite the test suite |
124 |
*/ |
125 |
public void startTestSuite(JUnitTest suite) { |
126 |
if (output == null) { |
127 |
return; // Quick return - no output do nothing. |
128 |
} |
129 |
StringBuffer sb = new StringBuffer("Testsuite: "); |
130 |
sb.append(suite.getName()); |
131 |
sb.append(StringUtils.LINE_SEP); |
132 |
_startTestSuiteOutput = sb.toString(); |
133 |
} |
134 |
|
135 |
/** |
136 |
* The whole testsuite ended. |
137 |
* @param suite the test suite |
138 |
*/ |
139 |
public void endTestSuite(JUnitTest suite) { |
140 |
StringBuffer sb = new StringBuffer("Tests run: "); |
141 |
sb.append(suite.runCount()); |
142 |
sb.append(", Failures: "); |
143 |
sb.append(suite.failureCount()); |
144 |
sb.append(", Errors: "); |
145 |
sb.append(suite.errorCount()); |
146 |
sb.append(", Time elapsed: "); |
147 |
sb.append(numberFormat.format(suite.getRunTime() / 1000.0)); |
148 |
sb.append(" sec"); |
149 |
sb.append(StringUtils.LINE_SEP); |
150 |
sb.append(StringUtils.LINE_SEP); |
151 |
|
152 |
// append the err and output streams to the log |
153 |
if (systemOutput != null && systemOutput.length() > 0) { |
154 |
sb.append("------------- Standard Output ---------------") |
155 |
.append(StringUtils.LINE_SEP) |
156 |
.append(systemOutput) |
157 |
.append("------------- ---------------- ---------------") |
158 |
.append(StringUtils.LINE_SEP); |
159 |
} |
160 |
|
161 |
if (systemError != null && systemError.length() > 0) { |
162 |
sb.append("------------- Standard Error -----------------") |
163 |
.append(StringUtils.LINE_SEP) |
164 |
.append(systemError) |
165 |
.append("------------- ---------------- ---------------") |
166 |
.append(StringUtils.LINE_SEP); |
167 |
} |
168 |
|
169 |
if (output != null) { |
170 |
if ((suite.failureCount()!=0) || (suite.errorCount()!=0)) { |
171 |
try { |
172 |
output.write(_startTestSuiteOutput); |
173 |
output.write(sb.toString()); |
174 |
resultWriter.close(); |
175 |
output.write(results.toString()); |
176 |
output.flush(); |
177 |
} finally { |
178 |
if (out != System.out && out != System.err) { |
179 |
FileUtils.close(out); |
180 |
} |
181 |
} |
182 |
} |
183 |
} |
184 |
} |
185 |
|
186 |
/** |
187 |
* A test started. |
188 |
* @param test a test |
189 |
*/ |
190 |
public void startTest(Test test) { |
191 |
} |
192 |
|
193 |
/** |
194 |
* A test ended. |
195 |
* @param test a test |
196 |
*/ |
197 |
public void endTest(Test test) { |
198 |
} |
199 |
|
200 |
/** |
201 |
* Interface TestListener for JUnit <= 3.4. |
202 |
* |
203 |
* <p>A Test failed. |
204 |
* @param test a test |
205 |
* @param t the exception thrown by the test |
206 |
*/ |
207 |
public void addFailure(Test test, Throwable t) { |
208 |
formatError("\tFAILED", test, t); |
209 |
} |
210 |
|
211 |
/** |
212 |
* Interface TestListener for JUnit > 3.4. |
213 |
* |
214 |
* <p>A Test failed. |
215 |
* @param test a test |
216 |
* @param t the assertion failed by the test |
217 |
*/ |
218 |
public void addFailure(Test test, AssertionFailedError t) { |
219 |
addFailure(test, (Throwable) t); |
220 |
} |
221 |
|
222 |
/** |
223 |
* A test caused an error. |
224 |
* @param test a test |
225 |
* @param error the error thrown by the test |
226 |
*/ |
227 |
public void addError(Test test, Throwable error) { |
228 |
formatError("\tCaused an ERROR", test, error); |
229 |
} |
230 |
|
231 |
/** |
232 |
* Format the test for printing.. |
233 |
* @param test a test |
234 |
* @return the formatted testname |
235 |
*/ |
236 |
protected String formatTest(Test test) { |
237 |
if (test == null) { |
238 |
return "Null Test: "; |
239 |
} else { |
240 |
return "Testcase: " + test.toString() + ":"; |
241 |
} |
242 |
} |
243 |
|
244 |
/** |
245 |
* Format an error and print it. |
246 |
* @param type the type of error |
247 |
* @param test the test that failed |
248 |
* @param error the exception that the test threw |
249 |
*/ |
250 |
protected synchronized void formatError(String type, Test test, |
251 |
Throwable error) { |
252 |
if (test != null) { |
253 |
endTest(test); |
254 |
} |
255 |
|
256 |
resultWriter.println(formatTest(test) + type); |
257 |
resultWriter.println(error.getMessage()); |
258 |
String strace = JUnitTestRunner.getFilteredTrace(error); |
259 |
resultWriter.println(strace); |
260 |
resultWriter.println(); |
261 |
} |
262 |
} |