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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class UnitourSummaryResultFormatter implements org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter {
45
46
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 }