View Javadoc

1   package junitour;
2   
3   import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
4   import junit.framework.Test;
5   import junit.framework.AssertionFailedError;
6   
7   import org.apache.tools.ant.BuildException;
8   
9   import java.io.IOException;
10  import java.io.OutputStream;
11  import java.text.NumberFormat;
12  
13  
14  /**
15   * wrapper for SummaryJUnitResultFormatter which also handles the
16   * JUnitour - unittests in endTestSuite to show the incomplete test count.
17   * <br/>
18   * Based on the ANT Junit Formatter
19   * {@link org.apache.tools.ant.taskdefs.optional.junit.SummaryJUnitResultFormatter}
20   * from ANT 1.5.3.1 (not very spectacular).
21   * <br/>
22   * The most important fact is that we have to do some corrections on the
23   * statistical values from the suite which also count failures and errors,
24   * when they are incompletes, too.
25   * 
26   * <p/>
27   * <br/>created: by hostlows, at 30.10.2003/ 17:15:01
28   * <pre>
29   * $Log$
30   * Revision 1.2  2004/07/08 01:47:50  hostlows
31   * cleanup, new build, license changed to mozilla license...
32   *
33   * Revision 1.1  2003/11/25 21:37:23  hostlows
34   * made it ant-compatible
35   *
36   * </pre>
37   * 
38   * @author $Author: hostlows $
39   * @author created by <a href="mailto:hostlows@users.sf.net">Robert Hostlowsky</a>
40   * @version $Revision: 26 $
41   * @see #endTestSuite(org.apache.tools.ant.taskdefs.optional.junit.JUnitTest)
42   * @see org.apache.tools.ant.taskdefs.optional.junit.SummaryJUnitResultFormatter#endTestSuite(org.apache.tools.ant.taskdefs.optional.junit.JUnitTest)
43   */
44  public class UnitourSummaryResultFormatter implements org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter {
45  
46    /** Formatter for timings. */
47    private NumberFormat nf = NumberFormat.getInstance();
48    private boolean _WithOutAndErr;
49    private OutputStream _out;
50    private String _systemOutput;
51    private String _systemError;
52    private int currentSuiteIncompleteCounter;
53    private int suiteErrorCompensationCounter;
54    private int suiteFailureCompensationCounter;
55  
56    public UnitourSummaryResultFormatter() {
57      _out = System.out;
58    }
59  
60    public void set_output(OutputStream _out) {
61      this._out = _out;
62    }
63  
64    public void setSystemOutput(String _out) {
65      this._systemOutput = _out;
66    }
67  
68    public void setSystemError(String err) {
69      this._systemError = err;
70    }
71  
72    public void setWith_outAndErr(boolean value) {
73      this._WithOutAndErr = value;
74    }
75  
76    public void addFailure(Test test, Throwable t) {
77      if (junitour.UnitTestIncompleteError.isTestIncompleteError(t)) {
78        currentSuiteIncompleteCounter++;
79        suiteFailureCompensationCounter++;
80      }
81    }
82  
83    public void addError(Test test, Throwable t) {
84      if (junitour.UnitTestIncompleteError.isTestIncompleteError(t)) {
85        currentSuiteIncompleteCounter++;
86        suiteErrorCompensationCounter++;
87      }
88    }
89  
90    public void addFailure(Test test, AssertionFailedError assertionFailedError) {
91      this.addFailure(test, (Throwable) assertionFailedError);
92    }
93  
94    public void endTest(Test test) {
95    }
96  
97    public void startTest(Test test) {
98    }
99  
100   public void startTestSuite(JUnitTest suite) {
101     currentSuiteIncompleteCounter = 0;
102     suiteFailureCompensationCounter = 0;
103     suiteErrorCompensationCounter = 0;
104   }
105 
106   public void endTestSuite(JUnitTest suite) throws BuildException {
107 
108     String newLine = System.getProperty("line.separator");
109     StringBuffer sb = new StringBuffer("Tests run: ");
110     sb.append(suite.runCount());
111     sb.append(", Failures: ");
112     sb.append(suite.failureCount() - suiteFailureCompensationCounter);
113     sb.append(", Errors: ");
114     sb.append(suite.errorCount() - suiteErrorCompensationCounter);
115     sb.append(", Incomplete: ");
116     sb.append(currentSuiteIncompleteCounter);
117     sb.append(", Time elapsed: ");
118     sb.append(nf.format(suite.getRunTime() / 1000.0));
119     sb.append(" sec");
120     sb.append(newLine);
121 
122     if (_WithOutAndErr) {
123       if (_systemOutput != null && _systemOutput.length() > 0) {
124         sb.append("_output:").append(newLine).append(_systemOutput)
125                 .append(newLine);
126       }
127 
128       if (_systemError != null && _systemError.length() > 0) {
129         sb.append("Error: ").append(newLine).append(_systemError)
130                 .append(newLine);
131       }
132     }
133 
134     try {
135       _out.write(sb.toString().getBytes());
136       _out.flush();
137     }
138     catch (IOException ioex) {
139       throw new BuildException("Unable to write summary _output", ioex);
140     }
141     finally {
142       if (_out != System.out && _out != System.err) {
143         try {
144           _out.close();
145         }
146         catch (IOException e) {
147         }
148       }
149     }
150 
151   }
152 
153   public void setOutput(OutputStream out) {
154     _out = out;
155   }
156 
157 
158 }