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.IOException;
022: import java.io.OutputStream;
023: import java.text.NumberFormat;
024: import junit.framework.AssertionFailedError;
025: import junit.framework.Test;
026: import org.apache.tools.ant.BuildException;
027:
028: /**
029: * Prints short summary output of the test to Ant's logging system.
030: *
031: */
032:
033: public class SummaryJUnitResultFormatter implements
034: JUnitResultFormatter,
035: JUnitTaskMirror.SummaryJUnitResultFormatterMirror {
036:
037: /**
038: * Formatter for timings.
039: */
040: private NumberFormat nf = NumberFormat.getInstance();
041: /**
042: * OutputStream to write to.
043: */
044: private OutputStream out;
045:
046: private boolean withOutAndErr = false;
047: private String systemOutput = null;
048: private String systemError = null;
049:
050: /**
051: * Empty
052: */
053: public SummaryJUnitResultFormatter() {
054: }
055:
056: /**
057: * The testsuite started.
058: * @param suite the testsuite.
059: */
060: public void startTestSuite(JUnitTest suite) {
061: String newLine = System.getProperty("line.separator");
062: StringBuffer sb = new StringBuffer("Running ");
063: sb.append(suite.getName());
064: sb.append(newLine);
065:
066: try {
067: out.write(sb.toString().getBytes());
068: out.flush();
069: } catch (IOException ioex) {
070: throw new BuildException("Unable to write summary output",
071: ioex);
072: }
073: }
074:
075: /**
076: * Empty
077: * @param t not used.
078: */
079: public void startTest(Test t) {
080: }
081:
082: /**
083: * Empty
084: * @param test not used.
085: */
086: public void endTest(Test test) {
087: }
088:
089: /**
090: * Empty
091: * @param test not used.
092: * @param t not used.
093: */
094: public void addFailure(Test test, Throwable t) {
095: }
096:
097: /**
098: * Interface TestListener for JUnit > 3.4.
099: *
100: * <p>A Test failed.
101: * @param test not used.
102: * @param t not used.
103: */
104: public void addFailure(Test test, AssertionFailedError t) {
105: addFailure(test, (Throwable) t);
106: }
107:
108: /**
109: * Empty
110: * @param test not used.
111: * @param t not used.
112: */
113: public void addError(Test test, Throwable t) {
114: }
115:
116: /** {@inheritDoc}. */
117: public void setOutput(OutputStream out) {
118: this .out = out;
119: }
120:
121: /** {@inheritDoc}. */
122: public void setSystemOutput(String out) {
123: systemOutput = out;
124: }
125:
126: /** {@inheritDoc}. */
127: public void setSystemError(String err) {
128: systemError = err;
129: }
130:
131: /**
132: * Should the output to System.out and System.err be written to
133: * the summary.
134: * @param value if true write System.out and System.err to the summary.
135: */
136: public void setWithOutAndErr(boolean value) {
137: withOutAndErr = value;
138: }
139:
140: /**
141: * The whole testsuite ended.
142: * @param suite the testsuite.
143: * @throws BuildException if there is an error.
144: */
145: public void endTestSuite(JUnitTest suite) throws BuildException {
146: String newLine = System.getProperty("line.separator");
147: StringBuffer sb = new StringBuffer("Tests run: ");
148: sb.append(suite.runCount());
149: sb.append(", Failures: ");
150: sb.append(suite.failureCount());
151: sb.append(", Errors: ");
152: sb.append(suite.errorCount());
153: sb.append(", Time elapsed: ");
154: sb.append(nf.format(suite.getRunTime() / 1000.0));
155: sb.append(" sec");
156: sb.append(newLine);
157:
158: if (withOutAndErr) {
159: if (systemOutput != null && systemOutput.length() > 0) {
160: sb.append("Output:").append(newLine).append(
161: systemOutput).append(newLine);
162: }
163:
164: if (systemError != null && systemError.length() > 0) {
165: sb.append("Error: ").append(newLine)
166: .append(systemError).append(newLine);
167: }
168: }
169:
170: try {
171: out.write(sb.toString().getBytes());
172: out.flush();
173: } catch (IOException ioex) {
174: throw new BuildException("Unable to write summary output",
175: ioex);
176: } finally {
177: if (out != System.out && out != System.err) {
178: try {
179: out.close();
180: } catch (IOException e) {
181: // ignore
182: }
183: }
184: }
185: }
186: }
|