KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > junit > BriefJUnitResultFormatter


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  */

18
19 package org.apache.tools.ant.taskdefs.optional.junit;
20
21 import java.io.OutputStream JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.text.NumberFormat JavaDoc;
25
26 import junit.framework.AssertionFailedError;
27 import junit.framework.Test;
28
29 import org.apache.tools.ant.util.FileUtils;
30 import org.apache.tools.ant.util.StringUtils;
31
32 /**
33  * Prints plain text output of the test to a specified Writer.
34  * Inspired by the PlainJUnitResultFormatter.
35  *
36  * @see FormatterElement
37  * @see PlainJUnitResultFormatter
38  */

39 public class BriefJUnitResultFormatter implements JUnitResultFormatter {
40
41     /**
42      * Where to write the log to.
43      */

44     private OutputStream JavaDoc out;
45
46     /**
47      * Used for writing the results.
48      */

49     private PrintWriter JavaDoc output;
50
51     /**
52      * Used as part of formatting the results.
53      */

54     private StringWriter JavaDoc results;
55
56     /**
57      * Used for writing formatted results to.
58      */

59     private PrintWriter JavaDoc resultWriter;
60
61     /**
62      * Formatter for timings.
63      */

64     private NumberFormat JavaDoc numberFormat = NumberFormat.getInstance();
65
66     /**
67      * Output suite has written to System.out
68      */

69     private String JavaDoc systemOutput = null;
70
71     /**
72      * Output suite has written to System.err
73      */

74     private String JavaDoc systemError = null;
75
76     /**
77      * Constructor for BriefJUnitResultFormatter.
78      */

79     public BriefJUnitResultFormatter() {
80         results = new StringWriter JavaDoc();
81         resultWriter = new PrintWriter JavaDoc(results);
82     }
83
84     /**
85      * Sets the stream the formatter is supposed to write its results to.
86      * @param out the output stream to write to
87      */

88     public void setOutput(OutputStream JavaDoc out) {
89         this.out = out;
90         output = new PrintWriter JavaDoc(out);
91     }
92
93     /**
94      * @see JUnitResultFormatter#setSystemOutput(String)
95      */

96     /** {@inheritDoc}. */
97     public void setSystemOutput(String JavaDoc out) {
98         systemOutput = out;
99     }
100
101     /**
102      * @see JUnitResultFormatter#setSystemError(String)
103      */

104     /** {@inheritDoc}. */
105     public void setSystemError(String JavaDoc err) {
106         systemError = err;
107     }
108
109
110     /**
111      * The whole testsuite started.
112      * @param suite the test suite
113      */

114     public void startTestSuite(JUnitTest suite) {
115         if (output == null) {
116             return; // Quick return - no output do nothing.
117
}
118         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Testsuite: ");
119         sb.append(suite.getName());
120         sb.append(StringUtils.LINE_SEP);
121         output.write(sb.toString());
122         output.flush();
123     }
124
125     /**
126      * The whole testsuite ended.
127      * @param suite the test suite
128      */

129     public void endTestSuite(JUnitTest suite) {
130         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Tests run: ");
131         sb.append(suite.runCount());
132         sb.append(", Failures: ");
133         sb.append(suite.failureCount());
134         sb.append(", Errors: ");
135         sb.append(suite.errorCount());
136         sb.append(", Time elapsed: ");
137         sb.append(numberFormat.format(suite.getRunTime() / 1000.0));
138         sb.append(" sec");
139         sb.append(StringUtils.LINE_SEP);
140         sb.append(StringUtils.LINE_SEP);
141
142         // append the err and output streams to the log
143
if (systemOutput != null && systemOutput.length() > 0) {
144             sb.append("------------- Standard Output ---------------")
145                     .append(StringUtils.LINE_SEP)
146                     .append(systemOutput)
147                     .append("------------- ---------------- ---------------")
148                     .append(StringUtils.LINE_SEP);
149         }
150
151         if (systemError != null && systemError.length() > 0) {
152             sb.append("------------- Standard Error -----------------")
153                     .append(StringUtils.LINE_SEP)
154                     .append(systemError)
155                     .append("------------- ---------------- ---------------")
156                     .append(StringUtils.LINE_SEP);
157         }
158
159         if (output != null) {
160             try {
161                 output.write(sb.toString());
162                 resultWriter.close();
163                 output.write(results.toString());
164                 output.flush();
165             } finally {
166                 if (out != System.out && out != System.err) {
167                     FileUtils.close(out);
168                 }
169             }
170         }
171     }
172
173     /**
174      * A test started.
175      * @param test a test
176      */

177     public void startTest(Test test) {
178     }
179
180     /**
181      * A test ended.
182      * @param test a test
183      */

184     public void endTest(Test test) {
185     }
186
187     /**
188      * Interface TestListener for JUnit <= 3.4.
189      *
190      * <p>A Test failed.
191      * @param test a test
192      * @param t the exception thrown by the test
193      */

194     public void addFailure(Test test, Throwable JavaDoc t) {
195         formatError("\tFAILED", test, t);
196     }
197
198     /**
199      * Interface TestListener for JUnit &gt; 3.4.
200      *
201      * <p>A Test failed.
202      * @param test a test
203      * @param t the assertion failed by the test
204      */

205     public void addFailure(Test test, AssertionFailedError t) {
206         addFailure(test, (Throwable JavaDoc) t);
207     }
208
209     /**
210      * A test caused an error.
211      * @param test a test
212      * @param error the error thrown by the test
213      */

214     public void addError(Test test, Throwable JavaDoc error) {
215         formatError("\tCaused an ERROR", test, error);
216     }
217
218     /**
219      * Format the test for printing..
220      * @param test a test
221      * @return the formatted testname
222      */

223     protected String JavaDoc formatTest(Test test) {
224         if (test == null) {
225             return "Null Test: ";
226         } else {
227             return "Testcase: " + test.toString() + ":";
228         }
229     }
230
231     /**
232      * Format an error and print it.
233      * @param type the type of error
234      * @param test the test that failed
235      * @param error the exception that the test threw
236      */

237     protected synchronized void formatError(String JavaDoc type, Test test,
238                                             Throwable JavaDoc error) {
239         if (test != null) {
240             endTest(test);
241         }
242
243         resultWriter.println(formatTest(test) + type);
244         resultWriter.println(error.getMessage());
245         String JavaDoc strace = JUnitTestRunner.getFilteredTrace(error);
246         resultWriter.println(strace);
247         resultWriter.println();
248     }
249 }
250
Popular Tags