001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.junit;
020:
021: import java.io.OutputStream;
022: import java.io.PrintWriter;
023: import java.io.StringWriter;
024: import java.text.NumberFormat;
025:
026: import junit.framework.AssertionFailedError;
027: import junit.framework.Test;
028:
029: import org.apache.tools.ant.util.FileUtils;
030: import org.apache.tools.ant.util.StringUtils;
031:
032: /**
033: * Prints plain text output of the test to a specified Writer.
034: * Inspired by the PlainJUnitResultFormatter.
035: *
036: * @see FormatterElement
037: * @see PlainJUnitResultFormatter
038: */
039: public class BriefJUnitResultFormatter implements JUnitResultFormatter {
040:
041: /**
042: * Where to write the log to.
043: */
044: private OutputStream out;
045:
046: /**
047: * Used for writing the results.
048: */
049: private PrintWriter output;
050:
051: /**
052: * Used as part of formatting the results.
053: */
054: private StringWriter results;
055:
056: /**
057: * Used for writing formatted results to.
058: */
059: private PrintWriter resultWriter;
060:
061: /**
062: * Formatter for timings.
063: */
064: private NumberFormat numberFormat = NumberFormat.getInstance();
065:
066: /**
067: * Output suite has written to System.out
068: */
069: private String systemOutput = null;
070:
071: /**
072: * Output suite has written to System.err
073: */
074: private String systemError = null;
075:
076: /**
077: * Constructor for BriefJUnitResultFormatter.
078: */
079: public BriefJUnitResultFormatter() {
080: results = new StringWriter();
081: resultWriter = new PrintWriter(results);
082: }
083:
084: /**
085: * Sets the stream the formatter is supposed to write its results to.
086: * @param out the output stream to write to
087: */
088: public void setOutput(OutputStream out) {
089: this .out = out;
090: output = new PrintWriter(out);
091: }
092:
093: /**
094: * @see JUnitResultFormatter#setSystemOutput(String)
095: */
096: /** {@inheritDoc}. */
097: public void setSystemOutput(String out) {
098: systemOutput = out;
099: }
100:
101: /**
102: * @see JUnitResultFormatter#setSystemError(String)
103: */
104: /** {@inheritDoc}. */
105: public void setSystemError(String err) {
106: systemError = err;
107: }
108:
109: /**
110: * The whole testsuite started.
111: * @param suite the test suite
112: */
113: public void startTestSuite(JUnitTest suite) {
114: if (output == null) {
115: return; // Quick return - no output do nothing.
116: }
117: StringBuffer sb = new StringBuffer("Testsuite: ");
118: sb.append(suite.getName());
119: sb.append(StringUtils.LINE_SEP);
120: output.write(sb.toString());
121: output.flush();
122: }
123:
124: /**
125: * The whole testsuite ended.
126: * @param suite the test suite
127: */
128: public void endTestSuite(JUnitTest suite) {
129: StringBuffer sb = new StringBuffer("Tests run: ");
130: sb.append(suite.runCount());
131: sb.append(", Failures: ");
132: sb.append(suite.failureCount());
133: sb.append(", Errors: ");
134: sb.append(suite.errorCount());
135: sb.append(", Time elapsed: ");
136: sb.append(numberFormat.format(suite.getRunTime() / 1000.0));
137: sb.append(" sec");
138: sb.append(StringUtils.LINE_SEP);
139: sb.append(StringUtils.LINE_SEP);
140:
141: // append the err and output streams to the log
142: if (systemOutput != null && systemOutput.length() > 0) {
143: sb
144: .append(
145: "------------- Standard Output ---------------")
146: .append(StringUtils.LINE_SEP)
147: .append(systemOutput)
148: .append(
149: "------------- ---------------- ---------------")
150: .append(StringUtils.LINE_SEP);
151: }
152:
153: if (systemError != null && systemError.length() > 0) {
154: sb
155: .append(
156: "------------- Standard Error -----------------")
157: .append(StringUtils.LINE_SEP)
158: .append(systemError)
159: .append(
160: "------------- ---------------- ---------------")
161: .append(StringUtils.LINE_SEP);
162: }
163:
164: if (output != null) {
165: try {
166: output.write(sb.toString());
167: resultWriter.close();
168: output.write(results.toString());
169: output.flush();
170: } finally {
171: if (out != System.out && out != System.err) {
172: FileUtils.close(out);
173: }
174: }
175: }
176: }
177:
178: /**
179: * A test started.
180: * @param test a test
181: */
182: public void startTest(Test test) {
183: }
184:
185: /**
186: * A test ended.
187: * @param test a test
188: */
189: public void endTest(Test test) {
190: }
191:
192: /**
193: * Interface TestListener for JUnit <= 3.4.
194: *
195: * <p>A Test failed.
196: * @param test a test
197: * @param t the exception thrown by the test
198: */
199: public void addFailure(Test test, Throwable t) {
200: formatError("\tFAILED", test, t);
201: }
202:
203: /**
204: * Interface TestListener for JUnit > 3.4.
205: *
206: * <p>A Test failed.
207: * @param test a test
208: * @param t the assertion failed by the test
209: */
210: public void addFailure(Test test, AssertionFailedError t) {
211: addFailure(test, (Throwable) t);
212: }
213:
214: /**
215: * A test caused an error.
216: * @param test a test
217: * @param error the error thrown by the test
218: */
219: public void addError(Test test, Throwable error) {
220: formatError("\tCaused an ERROR", test, error);
221: }
222:
223: /**
224: * Format the test for printing..
225: * @param test a test
226: * @return the formatted testname
227: */
228: protected String formatTest(Test test) {
229: if (test == null) {
230: return "Null Test: ";
231: } else {
232: return "Testcase: " + test.toString() + ":";
233: }
234: }
235:
236: /**
237: * Format an error and print it.
238: * @param type the type of error
239: * @param test the test that failed
240: * @param error the exception that the test threw
241: */
242: protected synchronized void formatError(String type, Test test,
243: Throwable error) {
244: if (test != null) {
245: endTest(test);
246: }
247:
248: resultWriter.println(formatTest(test) + type);
249: resultWriter.println(error.getMessage());
250: String strace = JUnitTestRunner.getFilteredTrace(error);
251: resultWriter.println(strace);
252: resultWriter.println();
253: }
254: }
|