001: /* PlainFormatter */
002:
003: package org.quilt.reports;
004:
005: import org.apache.tools.ant.taskdefs.optional.junit.*;
006: import org.apache.tools.ant.BuildException;
007:
008: import java.io.OutputStream;
009: import java.io.StringWriter;
010: import java.io.PrintWriter;
011: import java.io.IOException;
012: import java.text.NumberFormat;
013: import java.util.Hashtable;
014:
015: import junit.framework.AssertionFailedError;
016: import junit.framework.Test;
017:
018: import org.quilt.runner.Runner;
019: import org.quilt.framework.*;
020:
021: /** Plain text output of JUnit test results. */
022:
023: public class PlainFormatter extends BaseFormatter {
024:
025: private Hashtable testStarts = new Hashtable();
026: private Hashtable failed = new Hashtable();
027:
028: /** No-arg constructor. */
029: public PlainFormatter() {
030: results = new StringWriter();
031: resultWriter = new PrintWriter(results);
032: }
033:
034: // INTERFACE FORMATTER //////////////////////////////////////////
035:
036: public void endTestSuite(QuiltTest qt) throws BuildException {
037: StringBuffer sb = new StringBuffer("Testsuite: " + qt.getName()
038: + "\nTests run: " + qt.runCount() + ", Failures: "
039: + qt.failureCount() + ", Errors: " + qt.errorCount()
040: + ", Time elapsed: "
041: + numberFormat.format(qt.getRunTime() / 1000.0)
042: + " sec\n");
043:
044: if (systemOutput != null && systemOutput.length() > 0) {
045: sb
046: .append("------------- Standard Output ----------------\n"
047: + systemOutput
048: + "------------- ---------------- ---------------\n");
049: }
050:
051: if (systemError != null && systemError.length() > 0) {
052: sb
053: .append("------------- Standard Error -----------------\n"
054: + systemError
055: + "------------- ---------------- ---------------\n");
056: }
057:
058: sb.append("\n");
059:
060: if (out != null) {
061: try {
062: out.write(sb.toString().getBytes());
063: resultWriter.close();
064: out.write(results.toString().getBytes());
065: out.flush();
066: } catch (IOException ioex) {
067: throw new BuildException("Unable to write output", ioex);
068: } finally {
069: if (out != System.out && out != System.err) {
070: try {
071: out.close();
072: } catch (IOException e) {
073: }
074: }
075: }
076: }
077: }
078:
079: // INTERFACE TESTLISTENER ///////////////////////////////////////
080: public void startTest(Test t) {
081: testStarts.put(t, new Long(System.currentTimeMillis()));
082: failed.put(t, Boolean.FALSE);
083: }
084:
085: public void endTest(Test test) {
086: synchronized (resultWriter) {
087: // requires JUnit 3.7 or later
088: resultWriter.print("Testcase: " + getTestName(test));
089:
090: // remember that the hash holds objects, not primitives
091: if (Boolean.TRUE.equals(failed.get(test))) {
092: return;
093: }
094: Long t0 = (Long) testStarts.get(test);
095: double seconds = 0;
096: // can be null if an error occured in setUp
097: if (t0 != null) {
098: seconds = (System.currentTimeMillis() - t0.longValue()) / 1000.0;
099: }
100:
101: resultWriter.println(" took "
102: + numberFormat.format(seconds) + " sec");
103: }
104: }
105:
106: public void addFailure(Test test, Throwable t) {
107: formatError("\tFAILED", test, t);
108: }
109:
110: public void addFailure(Test test, AssertionFailedError t) {
111: addFailure(test, (Throwable) t);
112: }
113:
114: public void addError(Test test, Throwable t) {
115: formatError("\tCaused an ERROR", test, t);
116: }
117:
118: // OTHER METHODS ////////////////////////////////////////////////
119: private void formatError(String type, Test test, Throwable t) {
120: synchronized (resultWriter) {
121: if (test != null) {
122: endTest(test);
123: failed.put(test, Boolean.TRUE);
124: }
125:
126: resultWriter.println(type);
127: resultWriter.println(t.getMessage());
128: String strace = runner.getFilteredTrace(t, filtertrace);
129: resultWriter.print(strace);
130: resultWriter.println("");
131: }
132: }
133: }
|