001: package junit.textui;
002:
003: import java.io.PrintStream;
004: import java.text.NumberFormat;
005: import java.util.Enumeration;
006:
007: import junit.framework.AssertionFailedError;
008: import junit.framework.Test;
009: import junit.framework.TestFailure;
010: import junit.framework.TestListener;
011: import junit.framework.TestResult;
012: import junit.runner.BaseTestRunner;
013:
014: public class ResultPrinter implements TestListener {
015: PrintStream fWriter;
016: int fColumn = 0;
017:
018: public ResultPrinter(PrintStream writer) {
019: fWriter = writer;
020: }
021:
022: /* API for use by textui.TestRunner
023: */
024:
025: synchronized void print(TestResult result, long runTime) {
026: printHeader(runTime);
027: printErrors(result);
028: printFailures(result);
029: printFooter(result);
030: }
031:
032: void printWaitPrompt() {
033: getWriter().println();
034: getWriter().println("<RETURN> to continue");
035: }
036:
037: /* Internal methods
038: */
039:
040: protected void printHeader(long runTime) {
041: getWriter().println();
042: getWriter().println("Time: " + elapsedTimeAsString(runTime));
043: }
044:
045: protected void printErrors(TestResult result) {
046: printDefects(result.errors(), result.errorCount(), "error");
047: }
048:
049: protected void printFailures(TestResult result) {
050: printDefects(result.failures(), result.failureCount(),
051: "failure");
052: }
053:
054: protected void printDefects(Enumeration<TestFailure> booBoos,
055: int count, String type) {
056: if (count == 0)
057: return;
058: if (count == 1)
059: getWriter()
060: .println("There was " + count + " " + type + ":");
061: else
062: getWriter().println(
063: "There were " + count + " " + type + "s:");
064: for (int i = 1; booBoos.hasMoreElements(); i++) {
065: printDefect(booBoos.nextElement(), i);
066: }
067: }
068:
069: public void printDefect(TestFailure booBoo, int count) { // only public for testing purposes
070: printDefectHeader(booBoo, count);
071: printDefectTrace(booBoo);
072: }
073:
074: protected void printDefectHeader(TestFailure booBoo, int count) {
075: // I feel like making this a println, then adding a line giving the throwable a chance to print something
076: // before we get to the stack trace.
077: getWriter().print(count + ") " + booBoo.failedTest());
078: }
079:
080: protected void printDefectTrace(TestFailure booBoo) {
081: getWriter().print(
082: BaseTestRunner.getFilteredTrace(booBoo.trace()));
083: }
084:
085: protected void printFooter(TestResult result) {
086: if (result.wasSuccessful()) {
087: getWriter().println();
088: getWriter().print("OK");
089: getWriter()
090: .println(
091: " ("
092: + result.runCount()
093: + " test"
094: + (result.runCount() == 1 ? ""
095: : "s") + ")");
096:
097: } else {
098: getWriter().println();
099: getWriter().println("FAILURES!!!");
100: getWriter().println(
101: "Tests run: " + result.runCount() + ", Failures: "
102: + result.failureCount() + ", Errors: "
103: + result.errorCount());
104: }
105: getWriter().println();
106: }
107:
108: /**
109: * Returns the formatted string of the elapsed time.
110: * Duplicated from BaseTestRunner. Fix it.
111: */
112: protected String elapsedTimeAsString(long runTime) {
113: return NumberFormat.getInstance().format(
114: (double) runTime / 1000);
115: }
116:
117: public PrintStream getWriter() {
118: return fWriter;
119: }
120:
121: /**
122: * @see junit.framework.TestListener#addError(Test, Throwable)
123: */
124: public void addError(Test test, Throwable t) {
125: getWriter().print("E");
126: }
127:
128: /**
129: * @see junit.framework.TestListener#addFailure(Test, AssertionFailedError)
130: */
131: public void addFailure(Test test, AssertionFailedError t) {
132: getWriter().print("F");
133: }
134:
135: /**
136: * @see junit.framework.TestListener#endTest(Test)
137: */
138: public void endTest(Test test) {
139: }
140:
141: /**
142: * @see junit.framework.TestListener#startTest(Test)
143: */
144: public void startTest(Test test) {
145: getWriter().print(".");
146: if (fColumn++ >= 40) {
147: getWriter().println();
148: fColumn = 0;
149: }
150: }
151:
152: }
|