KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* BriefFormatter.java */
2
3 package org.quilt.reports;
4
5 import java.io.IOException JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.io.PrintWriter JavaDoc;
8 import java.io.StringWriter JavaDoc;
9 import java.text.NumberFormat JavaDoc;
10
11 import junit.framework.AssertionFailedError;
12 import junit.framework.Test;
13
14 import org.apache.tools.ant.BuildException;
15
16 import org.quilt.framework.*;
17 import org.quilt.runner.Runner;
18
19 public class BriefFormatter extends BaseFormatter {
20
21     /** No-arg constructor. */
22     public BriefFormatter() {
23         results = new StringWriter JavaDoc();
24         resultWriter = new PrintWriter JavaDoc(results);
25     }
26
27     // INTERFACE FORMATTER //////////////////////////////////////////
28

29     /**
30      * End the test suite: overrides basic mehod.
31      */

32     public void endTestSuite(QuiltTest suite) throws BuildException {
33         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Testsuite: " + suite.getName()
34             + "\nTests run: " + suite.runCount()
35             + ", Failures: " + suite.failureCount()
36             + ", Errors: " + suite.errorCount()
37             + ", Time elapsed: "
38                 + numberFormat.format( suite.getRunTime() / 1000.0)
39                 + " sec\n\n" );
40
41         if (systemOutput != null && systemOutput.length() > 0) {
42             sb.append(
43                   "------------- Standard Output ----------------\n"
44                 + systemOutput
45                 + "------------- ---------------- ---------------\n");
46         }
47
48         if (systemError != null && systemError.length() > 0) {
49             sb.append(
50                   "------------- Standard Error -----------------\n"
51                 + systemError
52                 + "------------- ---------------- ---------------\n");
53         }
54
55         if (output != null) {
56             try {
57                 output.write(sb.toString());
58                 resultWriter.close();
59                 output.write(results.toString());
60                 output.flush();
61             } finally {
62                 if (out != System.out &&
63                         out != System.err) {
64                     try {
65                         out.close();
66                     } catch (IOException JavaDoc e) {
67                     }
68                 }
69             }
70         }
71     }
72     
73     // INTERFACE TESTLISTENER ///////////////////////////////////////
74
/**
75      * A test failed - overrides BasicFormattter method.
76      */

77     public void addFailure(Test test, Throwable JavaDoc t) {
78         formatError("\tFAILED", test, t);
79     }
80
81     /**
82      * A test failed - overrides BasicFormattter method. Interface for
83      * JUnit &gt 3.4.
84      */

85     public void addFailure(Test test, AssertionFailedError t) {
86         addFailure(test, (Throwable JavaDoc) t);
87     }
88
89     /**
90      * A test caused an unexpected error.
91      */

92     public void addError(Test test, Throwable JavaDoc error) {
93         formatError("\tCaused an ERROR", test, error);
94     }
95
96     // OTHER METHODS ////////////////////////////////////////////////
97

98     /** Format an error and print it. */
99     private synchronized void formatError(String JavaDoc msg, Test test,
100                                             Throwable JavaDoc error) {
101         if (test == null) {
102             resultWriter.println("Null test: " + msg);
103             
104         } else {
105             endTest(test);
106             resultWriter.println("Testcase: " + test + ": " + msg);
107         }
108         resultWriter.println(error.getMessage());
109         String JavaDoc strace = runner.getFilteredTrace(error, filtertrace);
110         resultWriter.println(strace);
111         resultWriter.println();
112     }
113 }
114
Popular Tags