KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.text.NumberFormat JavaDoc;
26 import java.util.Hashtable JavaDoc;
27
28 import junit.framework.AssertionFailedError;
29 import junit.framework.Test;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.util.FileUtils;
33 import org.apache.tools.ant.util.StringUtils;
34
35
36 /**
37  * Prints plain text output of the test to a specified Writer.
38  *
39  */

40
41 public class PlainJUnitResultFormatter implements JUnitResultFormatter {
42
43     /**
44      * Formatter for timings.
45      */

46     private NumberFormat JavaDoc nf = NumberFormat.getInstance();
47     /**
48      * Timing helper.
49      */

50     private Hashtable JavaDoc testStarts = new Hashtable JavaDoc();
51     /**
52      * Where to write the log to.
53      */

54     private OutputStream JavaDoc out;
55     /**
56      * Helper to store intermediate output.
57      */

58     private StringWriter JavaDoc inner;
59     /**
60      * Convenience layer on top of {@link #inner inner}.
61      */

62     private PrintWriter JavaDoc wri;
63     /**
64      * Suppress endTest if testcase failed.
65      */

66     private Hashtable JavaDoc failed = new Hashtable JavaDoc();
67
68     private String JavaDoc systemOutput = null;
69     private String JavaDoc systemError = null;
70
71     /** No arg constructor */
72     public PlainJUnitResultFormatter() {
73         inner = new StringWriter JavaDoc();
74         wri = new PrintWriter JavaDoc(inner);
75     }
76
77     /** {@inheritDoc}. */
78     public void setOutput(OutputStream JavaDoc out) {
79         this.out = out;
80     }
81
82     /** {@inheritDoc}. */
83     public void setSystemOutput(String JavaDoc out) {
84         systemOutput = out;
85     }
86
87     /** {@inheritDoc}. */
88     public void setSystemError(String JavaDoc err) {
89         systemError = err;
90     }
91
92     /**
93      * The whole testsuite started.
94      * @param suite the test suite
95      * @throws BuildException if unable to write the output
96      */

97     public void startTestSuite(JUnitTest suite) throws BuildException {
98         if (out == null) {
99             return; // Quick return - no output do nothing.
100
}
101         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Testsuite: ");
102         sb.append(suite.getName());
103         sb.append(StringUtils.LINE_SEP);
104         try {
105             out.write(sb.toString().getBytes());
106             out.flush();
107         } catch (IOException JavaDoc ex) {
108             throw new BuildException("Unable to write output", ex);
109         }
110     }
111
112     /**
113      * The whole testsuite ended.
114      * @param suite the test suite
115      * @throws BuildException if unable to write the output
116      */

117     public void endTestSuite(JUnitTest suite) throws BuildException {
118         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Tests run: ");
119         sb.append(suite.runCount());
120         sb.append(", Failures: ");
121         sb.append(suite.failureCount());
122         sb.append(", Errors: ");
123         sb.append(suite.errorCount());
124         sb.append(", Time elapsed: ");
125         sb.append(nf.format(suite.getRunTime() / 1000.0));
126         sb.append(" sec");
127         sb.append(StringUtils.LINE_SEP);
128
129         // append the err and output streams to the log
130
if (systemOutput != null && systemOutput.length() > 0) {
131             sb.append("------------- Standard Output ---------------")
132                 .append(StringUtils.LINE_SEP)
133                 .append(systemOutput)
134                 .append("------------- ---------------- ---------------")
135                 .append(StringUtils.LINE_SEP);
136         }
137
138         if (systemError != null && systemError.length() > 0) {
139             sb.append("------------- Standard Error -----------------")
140                 .append(StringUtils.LINE_SEP)
141                 .append(systemError)
142                 .append("------------- ---------------- ---------------")
143                 .append(StringUtils.LINE_SEP);
144         }
145
146         sb.append(StringUtils.LINE_SEP);
147
148         if (out != null) {
149             try {
150                 out.write(sb.toString().getBytes());
151                 wri.close();
152                 out.write(inner.toString().getBytes());
153                 out.flush();
154             } catch (IOException JavaDoc ioex) {
155                 throw new BuildException("Unable to write output", ioex);
156             } finally {
157                 if (out != System.out && out != System.err) {
158                     FileUtils.close(out);
159                 }
160             }
161         }
162     }
163
164     /**
165      * Interface TestListener.
166      *
167      * <p>A new Test is started.
168      * @param t the test.
169      */

170     public void startTest(Test t) {
171         testStarts.put(t, new Long JavaDoc(System.currentTimeMillis()));
172         failed.put(t, Boolean.FALSE);
173     }
174
175     /**
176      * Interface TestListener.
177      *
178      * <p>A Test is finished.
179      * @param test the test.
180      */

181     public void endTest(Test test) {
182         if (Boolean.TRUE.equals(failed.get(test))) {
183             return;
184         }
185         synchronized (wri) {
186             wri.print("Testcase: "
187                       + JUnitVersionHelper.getTestCaseName(test));
188             Long JavaDoc l = (Long JavaDoc) testStarts.get(test);
189             double seconds = 0;
190             // can be null if an error occurred in setUp
191
if (l != null) {
192                 seconds =
193                     (System.currentTimeMillis() - l.longValue()) / 1000.0;
194             }
195
196             wri.println(" took " + nf.format(seconds) + " sec");
197         }
198     }
199
200     /**
201      * Interface TestListener for JUnit &lt;= 3.4.
202      *
203      * <p>A Test failed.
204      * @param test the test.
205      * @param t the exception.
206      */

207     public void addFailure(Test test, Throwable JavaDoc t) {
208         formatError("\tFAILED", test, t);
209     }
210
211     /**
212      * Interface TestListener for JUnit &gt; 3.4.
213      *
214      * <p>A Test failed.
215      * @param test the test.
216      * @param t the assertion that failed.
217      */

218     public void addFailure(Test test, AssertionFailedError t) {
219         addFailure(test, (Throwable JavaDoc) t);
220     }
221
222     /**
223      * Interface TestListener.
224      *
225      * <p>An error occurred while running the test.
226      * @param test the test.
227      * @param t the exception.
228      */

229     public void addError(Test test, Throwable JavaDoc t) {
230         formatError("\tCaused an ERROR", test, t);
231     }
232
233     private void formatError(String JavaDoc type, Test test, Throwable JavaDoc t) {
234         synchronized (wri) {
235             if (test != null) {
236                 endTest(test);
237                 failed.put(test, Boolean.TRUE);
238             }
239
240             wri.println(type);
241             wri.println(t.getMessage());
242             String JavaDoc strace = JUnitTestRunner.getFilteredTrace(t);
243             wri.print(strace);
244             wri.println("");
245         }
246     }
247
248 } // PlainJUnitResultFormatter
249
Popular Tags