KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quilt > reports > PlainFormatter


1 /* PlainFormatter */
2
3 package org.quilt.reports;
4
5 import org.apache.tools.ant.taskdefs.optional.junit.*;
6 import org.apache.tools.ant.BuildException;
7
8 import java.io.OutputStream JavaDoc;
9 import java.io.StringWriter JavaDoc;
10 import java.io.PrintWriter JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.text.NumberFormat JavaDoc;
13 import java.util.Hashtable JavaDoc;
14
15 import junit.framework.AssertionFailedError;
16 import junit.framework.Test;
17
18 import org.quilt.runner.Runner;
19 import org.quilt.framework.*;
20
21 /** Plain text output of JUnit test results. */
22
23 public class PlainFormatter extends BaseFormatter {
24
25     private Hashtable JavaDoc testStarts = new Hashtable JavaDoc();
26     private Hashtable JavaDoc failed = new Hashtable JavaDoc();
27
28     /** No-arg constructor. */
29     public PlainFormatter() {
30         results = new StringWriter JavaDoc();
31         resultWriter = new PrintWriter JavaDoc(results);
32     }
33
34     // INTERFACE FORMATTER //////////////////////////////////////////
35

36     public void endTestSuite(QuiltTest qt) throws BuildException {
37         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Testsuite: " + qt.getName()
38             + "\nTests run: " + qt.runCount()
39             + ", Failures: " + qt.failureCount()
40             + ", Errors: " + qt.errorCount()
41             + ", Time elapsed: " + numberFormat.format(
42                                                 qt.getRunTime() / 1000.0)
43             + " sec\n");
44         
45         if (systemOutput != null && systemOutput.length() > 0) {
46             sb.append(
47                   "------------- Standard Output ----------------\n"
48                 + systemOutput
49                 + "------------- ---------------- ---------------\n");
50         }
51         
52         if (systemError != null && systemError.length() > 0) {
53             sb.append(
54                   "------------- Standard Error -----------------\n"
55                 + systemError
56                 + "------------- ---------------- ---------------\n");
57         }
58
59         sb.append("\n");
60
61         if (out != null) {
62             try {
63                 out.write(sb.toString().getBytes());
64                 resultWriter.close();
65                 out.write(results.toString().getBytes());
66                 out.flush();
67             } catch (IOException JavaDoc ioex) {
68                 throw new BuildException("Unable to write output", ioex);
69             } finally {
70                 if (out != System.out && out != System.err) {
71                     try {
72                         out.close();
73                     } catch (IOException JavaDoc e) {}
74                 }
75             }
76         }
77     }
78
79     // INTERFACE TESTLISTENER ///////////////////////////////////////
80
public void startTest(Test t) {
81         testStarts.put(t, new Long JavaDoc(System.currentTimeMillis()));
82         failed.put(t, Boolean.FALSE);
83     }
84
85     public void endTest(Test test) {
86         synchronized (resultWriter) {
87             // requires JUnit 3.7 or later
88
resultWriter.print("Testcase: " + getTestName(test));
89
90             // remember that the hash holds objects, not primitives
91
if (Boolean.TRUE.equals(failed.get(test))) {
92                 return;
93             }
94             Long JavaDoc t0 = (Long JavaDoc) testStarts.get(test);
95             double seconds = 0;
96             // can be null if an error occured in setUp
97
if (t0 != null) {
98                 seconds =
99                     (System.currentTimeMillis() - t0.longValue()) / 1000.0;
100             }
101             
102             resultWriter.println(" took "
103                                 + numberFormat.format(seconds) + " sec");
104         }
105     }
106     public void addFailure(Test test, Throwable JavaDoc t) {
107         formatError("\tFAILED", test, t);
108     }
109     public void addFailure(Test test, AssertionFailedError t) {
110         addFailure(test, (Throwable JavaDoc) t);
111     }
112     public void addError(Test test, Throwable JavaDoc t) {
113         formatError("\tCaused an ERROR", test, t);
114     }
115     // OTHER METHODS ////////////////////////////////////////////////
116
private void formatError(String JavaDoc type, Test test, Throwable JavaDoc t) {
117         synchronized (resultWriter) {
118             if (test != null) {
119                 endTest(test);
120                 failed.put(test, Boolean.TRUE);
121             }
122
123             resultWriter.println(type);
124             resultWriter.println(t.getMessage());
125             String JavaDoc strace = runner.getFilteredTrace(t, filtertrace);
126             resultWriter.print(strace);
127             resultWriter.println("");
128         }
129     }
130 }
131
Popular Tags