View Javadoc

1   /*
2    * Copyright  2000-2004 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  
18  package org.woopi.ant.taskdefs.junit;
19  import java.io.IOException;
20  import java.io.OutputStream;
21  import java.io.PrintWriter;
22  import java.io.StringWriter;
23  import java.text.NumberFormat;
24  import java.util.Hashtable;
25  import junit.framework.AssertionFailedError;
26  import junit.framework.Test;
27  import org.apache.tools.ant.BuildException;
28  
29  
30  /***
31   * Prints plain text output of the test to a specified Writer.
32   *
33   */
34  
35  public class PlainJUnitResultFormatter implements JUnitResultFormatter {
36  
37      /***
38       * Formatter for timings.
39       */
40      private NumberFormat nf = NumberFormat.getInstance();
41      /***
42       * Timing helper.
43       */
44      private Hashtable testStarts = new Hashtable();
45      /***
46       * Where to write the log to.
47       */
48      private OutputStream out;
49      /***
50       * Helper to store intermediate output.
51       */
52      private StringWriter inner;
53      /***
54       * Convenience layer on top of {@link #inner inner}.
55       */
56      private PrintWriter wri;
57      /***
58       * Suppress endTest if testcase failed.
59       */
60      private Hashtable failed = new Hashtable();
61  
62      private String systemOutput = null;
63      private String systemError = null;
64  
65      public PlainJUnitResultFormatter() {
66          inner = new StringWriter();
67          wri = new PrintWriter(inner);
68      }
69  
70      public void setOutput(OutputStream out) {
71          this.out = out;
72      }
73  
74      public void setSystemOutput(String out) {
75          systemOutput = out;
76      }
77  
78      public void setSystemError(String err) {
79          systemError = err;
80      }
81  
82      /***
83       * Empty.
84       */
85      public void startTestSuite(JUnitTest suite) {
86      }
87  
88      /***
89       * The whole testsuite ended.
90       */
91      public void endTestSuite(JUnitTest suite) throws BuildException {
92          String newLine = System.getProperty("line.separator");
93          StringBuffer sb = new StringBuffer("Testsuite: ");
94          sb.append(suite.getName());
95          sb.append(newLine);
96          sb.append("Tests run: ");
97          sb.append(suite.runCount());
98          sb.append(", Failures: ");
99          sb.append(suite.failureCount());
100         sb.append(", Errors: ");
101         sb.append(suite.errorCount());
102         sb.append(", Time elapsed: ");
103         sb.append(nf.format(suite.getRunTime() / 1000.0));
104         sb.append(" sec");
105         sb.append(newLine);
106 
107         // append the err and output streams to the log
108         if (systemOutput != null && systemOutput.length() > 0) {
109             sb.append("------------- Standard Output ---------------")
110                 .append(newLine)
111                 .append(systemOutput)
112                 .append("------------- ---------------- ---------------")
113                 .append(newLine);
114         }
115 
116         if (systemError != null && systemError.length() > 0) {
117             sb.append("------------- Standard Error -----------------")
118                 .append(newLine)
119                 .append(systemError)
120                 .append("------------- ---------------- ---------------")
121                 .append(newLine);
122         }
123 
124         sb.append(newLine);
125 
126         if (out != null) {
127             try {
128                 out.write(sb.toString().getBytes());
129                 wri.close();
130                 out.write(inner.toString().getBytes());
131                 out.flush();
132             } catch (IOException ioex) {
133                 throw new BuildException("Unable to write output", ioex);
134             } finally {
135                 if (out != System.out && out != System.err) {
136                     try {
137                         out.close();
138                     } catch (IOException e) {
139                         // ignore
140                     }
141                 }
142             }
143         }
144     }
145 
146     /***
147      * Interface TestListener.
148      *
149      * <p>A new Test is started.
150      */
151     public void startTest(Test t) {
152         testStarts.put(t, new Long(System.currentTimeMillis()));
153         failed.put(t, Boolean.FALSE);
154     }
155 
156     /***
157      * Interface TestListener.
158      *
159      * <p>A Test is finished.
160      */
161     public void endTest(Test test) {
162         if (Boolean.TRUE.equals(failed.get(test))) {
163             return;
164         }
165         synchronized (wri) {
166             wri.print("Testcase: "
167                       + JUnitVersionHelper.getTestCaseName(test));
168             Long l = (Long) testStarts.get(test);
169             double seconds = 0;
170             // can be null if an error occurred in setUp
171             if (l != null) {
172                 seconds =
173                     (System.currentTimeMillis() - l.longValue()) / 1000.0;
174             }
175 
176             wri.println(" took " + nf.format(seconds) + " sec");
177         }
178     }
179 
180     /***
181      * Interface TestListener for JUnit &lt;= 3.4.
182      *
183      * <p>A Test failed.
184      */
185     public void addFailure(Test test, Throwable t) {
186         formatError("\tFAILED", test, t);
187     }
188 
189     /***
190      * Interface TestListener for JUnit &gt; 3.4.
191      *
192      * <p>A Test failed.
193      */
194     public void addFailure(Test test, AssertionFailedError t) {
195         addFailure(test, (Throwable) t);
196     }
197 
198     /***
199      * Interface TestListener.
200      *
201      * <p>An error occurred while running the test.
202      */
203     public void addError(Test test, Throwable t) {
204         formatError("\tCaused an ERROR", test, t);
205     }
206 
207     private void formatError(String type, Test test, Throwable t) {
208         synchronized (wri) {
209             if (test != null) {
210                 endTest(test);
211                 failed.put(test, Boolean.TRUE);
212             }
213 
214             wri.println(type);
215             wri.println(t.getMessage());
216             String strace = JUnitTestRunner.getFilteredTrace(t);
217             wri.print(strace);
218             wri.println("");
219         }
220     }
221 
222 } // PlainJUnitResultFormatter