001: /* BriefFormatter.java */
002:
003: package org.quilt.reports;
004:
005: import java.io.IOException;
006: import java.io.OutputStream;
007: import java.io.PrintWriter;
008: import java.io.StringWriter;
009: import java.text.NumberFormat;
010:
011: import junit.framework.AssertionFailedError;
012: import junit.framework.Test;
013:
014: import org.apache.tools.ant.BuildException;
015:
016: import org.quilt.framework.*;
017: import org.quilt.runner.Runner;
018:
019: public class BriefFormatter extends BaseFormatter {
020:
021: /** No-arg constructor. */
022: public BriefFormatter() {
023: results = new StringWriter();
024: resultWriter = new PrintWriter(results);
025: }
026:
027: // INTERFACE FORMATTER //////////////////////////////////////////
028:
029: /**
030: * End the test suite: overrides basic mehod.
031: */
032: public void endTestSuite(QuiltTest suite) throws BuildException {
033: StringBuffer sb = new StringBuffer("Testsuite: "
034: + suite.getName() + "\nTests run: " + suite.runCount()
035: + ", Failures: " + suite.failureCount() + ", Errors: "
036: + suite.errorCount() + ", Time elapsed: "
037: + numberFormat.format(suite.getRunTime() / 1000.0)
038: + " sec\n\n");
039:
040: if (systemOutput != null && systemOutput.length() > 0) {
041: sb
042: .append("------------- Standard Output ----------------\n"
043: + systemOutput
044: + "------------- ---------------- ---------------\n");
045: }
046:
047: if (systemError != null && systemError.length() > 0) {
048: sb
049: .append("------------- Standard Error -----------------\n"
050: + systemError
051: + "------------- ---------------- ---------------\n");
052: }
053:
054: if (output != null) {
055: try {
056: output.write(sb.toString());
057: resultWriter.close();
058: output.write(results.toString());
059: output.flush();
060: } finally {
061: if (out != System.out && out != System.err) {
062: try {
063: out.close();
064: } catch (IOException e) {
065: }
066: }
067: }
068: }
069: }
070:
071: // INTERFACE TESTLISTENER ///////////////////////////////////////
072: /**
073: * A test failed - overrides BasicFormattter method.
074: */
075: public void addFailure(Test test, Throwable t) {
076: formatError("\tFAILED", test, t);
077: }
078:
079: /**
080: * A test failed - overrides BasicFormattter method. Interface for
081: * JUnit > 3.4.
082: */
083: public void addFailure(Test test, AssertionFailedError t) {
084: addFailure(test, (Throwable) t);
085: }
086:
087: /**
088: * A test caused an unexpected error.
089: */
090: public void addError(Test test, Throwable error) {
091: formatError("\tCaused an ERROR", test, error);
092: }
093:
094: // OTHER METHODS ////////////////////////////////////////////////
095:
096: /** Format an error and print it. */
097: private synchronized void formatError(String msg, Test test,
098: Throwable error) {
099: if (test == null) {
100: resultWriter.println("Null test: " + msg);
101:
102: } else {
103: endTest(test);
104: resultWriter.println("Testcase: " + test + ": " + msg);
105: }
106: resultWriter.println(error.getMessage());
107: String strace = runner.getFilteredTrace(error, filtertrace);
108: resultWriter.println(strace);
109: resultWriter.println();
110: }
111: }
|