KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.NumberFormat JavaDoc;
24 import junit.framework.AssertionFailedError;
25 import junit.framework.Test;
26 import org.apache.tools.ant.BuildException;
27
28 /**
29  * Prints short summary output of the test to Ant's logging system.
30  *
31  */

32
33 public class SummaryJUnitResultFormatter
34     implements JUnitResultFormatter, JUnitTaskMirror.SummaryJUnitResultFormatterMirror {
35
36     /**
37      * Formatter for timings.
38      */

39     private NumberFormat JavaDoc nf = NumberFormat.getInstance();
40     /**
41      * OutputStream to write to.
42      */

43     private OutputStream JavaDoc out;
44
45     private boolean withOutAndErr = false;
46     private String JavaDoc systemOutput = null;
47     private String JavaDoc systemError = null;
48
49     /**
50      * Empty
51      */

52     public SummaryJUnitResultFormatter() {
53     }
54     /**
55      * The testsuite started.
56      * @param suite the testsuite.
57      */

58     public void startTestSuite(JUnitTest suite) {
59         String JavaDoc newLine = System.getProperty("line.separator");
60         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Running ");
61         sb.append(suite.getName());
62         sb.append(newLine);
63
64         try {
65             out.write(sb.toString().getBytes());
66             out.flush();
67         } catch (IOException JavaDoc ioex) {
68             throw new BuildException("Unable to write summary output", ioex);
69         }
70     }
71     /**
72      * Empty
73      * @param t not used.
74      */

75     public void startTest(Test t) {
76     }
77     /**
78      * Empty
79      * @param test not used.
80      */

81     public void endTest(Test test) {
82     }
83     /**
84      * Empty
85      * @param test not used.
86      * @param t not used.
87      */

88     public void addFailure(Test test, Throwable JavaDoc t) {
89     }
90     /**
91      * Interface TestListener for JUnit > 3.4.
92      *
93      * <p>A Test failed.
94      * @param test not used.
95      * @param t not used.
96      */

97     public void addFailure(Test test, AssertionFailedError t) {
98         addFailure(test, (Throwable JavaDoc) t);
99     }
100     /**
101      * Empty
102      * @param test not used.
103      * @param t not used.
104      */

105     public void addError(Test test, Throwable JavaDoc t) {
106     }
107
108     /** {@inheritDoc}. */
109     public void setOutput(OutputStream JavaDoc out) {
110         this.out = out;
111     }
112
113     /** {@inheritDoc}. */
114     public void setSystemOutput(String JavaDoc out) {
115         systemOutput = out;
116     }
117
118     /** {@inheritDoc}. */
119     public void setSystemError(String JavaDoc err) {
120         systemError = err;
121     }
122
123     /**
124      * Should the output to System.out and System.err be written to
125      * the summary.
126      * @param value if true write System.out and System.err to the summary.
127      */

128     public void setWithOutAndErr(boolean value) {
129         withOutAndErr = value;
130     }
131
132     /**
133      * The whole testsuite ended.
134      * @param suite the testsuite.
135      * @throws BuildException if there is an error.
136      */

137     public void endTestSuite(JUnitTest suite) throws BuildException {
138         String JavaDoc newLine = System.getProperty("line.separator");
139         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Tests run: ");
140         sb.append(suite.runCount());
141         sb.append(", Failures: ");
142         sb.append(suite.failureCount());
143         sb.append(", Errors: ");
144         sb.append(suite.errorCount());
145         sb.append(", Time elapsed: ");
146         sb.append(nf.format(suite.getRunTime() / 1000.0));
147         sb.append(" sec");
148         sb.append(newLine);
149
150         if (withOutAndErr) {
151             if (systemOutput != null && systemOutput.length() > 0) {
152                 sb.append("Output:").append(newLine).append(systemOutput)
153                     .append(newLine);
154             }
155
156             if (systemError != null && systemError.length() > 0) {
157                 sb.append("Error: ").append(newLine).append(systemError)
158                     .append(newLine);
159             }
160         }
161
162         try {
163             out.write(sb.toString().getBytes());
164             out.flush();
165         } catch (IOException JavaDoc ioex) {
166             throw new BuildException("Unable to write summary output", ioex);
167         } finally {
168             if (out != System.out && out != System.err) {
169                 try {
170                     out.close();
171                 } catch (IOException JavaDoc e) {
172                     // ignore
173
}
174             }
175         }
176     }
177 }
178
Popular Tags