001: /* SummaryFormatter.java */
002:
003: // //////////////////////////////////////////////////////////////////
004: // MODIFY TO EXTEND BaseFormatter ? /////////////////////////////////
005: // //////////////////////////////////////////////////////////////////
006: package org.quilt.reports;
007:
008: import java.text.NumberFormat;
009: import java.io.IOException;
010: import java.io.OutputStream;
011:
012: import junit.framework.AssertionFailedError;
013: import junit.framework.Test;
014:
015: import org.apache.tools.ant.BuildException;
016:
017: import org.quilt.framework.*;
018: import org.quilt.runner.Runner;
019:
020: public class SummaryFormatter implements Formatter {
021:
022: private boolean filtertrace = false;
023: private Runner runner = null;
024:
025: private NumberFormat nf = NumberFormat.getInstance();
026: private OutputStream out;
027:
028: private boolean withOutAndErr = false;
029: private String systemOutput = null;
030: private String systemError = null;
031:
032: public SummaryFormatter() {
033: }
034:
035: // FORMATTER INTERFACE //////////////////////////////////////////
036:
037: /** Called at the end of the Ant/Quilt test run. */
038: public void endTestSuite(QuiltTest qt) throws BuildException {
039: StringBuffer sb = new StringBuffer("Tests run: "
040: + qt.runCount() + ", Failures: " + qt.failureCount()
041: + ", Errors: " + qt.errorCount() + ", Time elapsed: "
042: + nf.format(qt.getRunTime() / 1000.0) + " sec\n");
043:
044: if (withOutAndErr) {
045: if (systemOutput != null && systemOutput.length() > 0) {
046: sb.append("Output:\n" + systemOutput + "\n");
047: }
048: if (systemError != null && systemError.length() > 0) {
049: sb.append("Error:\n " + systemError + "\n");
050: }
051: }
052:
053: try {
054: out.write(sb.toString().getBytes());
055: out.flush();
056: } catch (IOException e) {
057: throw new BuildException("Unable to write summary output",
058: e);
059: } finally {
060: if (out != System.out && out != System.err) {
061: try {
062: out.close();
063: } catch (IOException e) {
064: }
065: }
066: }
067: }
068:
069: /** Enable filtering of Ant/Quilt/JUnit lines from stack traces. */
070: public void setFiltertrace(boolean b) {
071: filtertrace = b; // NEVER USED
072: }
073:
074: /** Where to direct output. */
075: public void setOutput(OutputStream out) {
076: this .out = out;
077: }
078:
079: /** Select the runner to be used. */
080: public void setRunner(Runner testrunner) {
081: runner = testrunner;
082: }
083:
084: /** Where to send system error output. */
085: public void setSystemError(String err) {
086: systemError = err;
087: }
088:
089: /** Where to send standard output. */
090: public void setSystemOutput(String out) {
091: systemOutput = out;
092: }
093:
094: /** Called at start of Ant/Quilt test run. */
095: public void startTestSuite(QuiltTest suite) {
096: }
097:
098: // INTERFACE TESTLISTENER ///////////////////////////////////////
099: /** An unexpected error occurred. */
100: public void addError(Test test, Throwable t) {
101: }
102:
103: /** A failure occurred. */
104: public void addFailure(Test test, Throwable t) {
105: }
106:
107: /** A failure occurred. */
108: public void addFailure(Test test, AssertionFailedError t) {
109: addFailure(test, (Throwable) t);
110: }
111:
112: /** Called at end of JUnit test. */
113: public void endTest(Test test) {
114: }
115:
116: /** Called at beginning of JUnit test. */
117: public void startTest(Test t) {
118: }
119:
120: // OTHER METHODS ////////////////////////////////////////////////
121: /**
122: * Ant-compatible method determining whether System.out and
123: * System.err should be echoed to the summary report.
124: */
125: public void setWithOutAndErr(boolean value) {
126: withOutAndErr = value;
127: }
128: }
|