source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

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