source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/junit/BriefJUnitResultFormatter.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

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