4 import java.io.PrintStream;
5 // The following line was removed for compatibility with Android libraries.
6 //import java.text.NumberFormat;
7 import java.util.Enumeration;
9 import junit.framework.AssertionFailedError;
10 import junit.framework.Test;
11 import junit.framework.TestFailure;
12 import junit.framework.TestListener;
13 import junit.framework.TestResult;
14 import junit.runner.BaseTestRunner;
16 public class ResultPrinter implements TestListener {
20 public ResultPrinter(PrintStream writer) {
24 /* API for use by textui.TestRunner
27 synchronized void print(TestResult result, long runTime) {
30 printFailures(result);
34 void printWaitPrompt() {
35 getWriter().println();
36 getWriter().println("<RETURN> to continue");
42 protected void printHeader(long runTime) {
43 getWriter().println();
44 getWriter().println("Time: "+elapsedTimeAsString(runTime));
47 protected void printErrors(TestResult result) {
48 printDefects(result.errors(), result.errorCount(), "error");
51 protected void printFailures(TestResult result) {
52 printDefects(result.failures(), result.failureCount(), "failure");
55 protected void printDefects(Enumeration booBoos, int count, String type) {
56 if (count == 0) return;
58 getWriter().println("There was " + count + " " + type + ":");
60 getWriter().println("There were " + count + " " + type + "s:");
61 for (int i= 1; booBoos.hasMoreElements(); i++) {
62 printDefect((TestFailure) booBoos.nextElement(), i);
66 public void printDefect(TestFailure booBoo, int count) { // only public for testing purposes
67 printDefectHeader(booBoo, count);
68 printDefectTrace(booBoo);
71 protected void printDefectHeader(TestFailure booBoo, int count) {
72 // I feel like making this a println, then adding a line giving the throwable a chance to print something
73 // before we get to the stack trace.
74 getWriter().print(count + ") " + booBoo.failedTest());
77 protected void printDefectTrace(TestFailure booBoo) {
78 getWriter().print(BaseTestRunner.getFilteredTrace(booBoo.trace()));
81 protected void printFooter(TestResult result) {
82 if (result.wasSuccessful()) {
83 getWriter().println();
84 getWriter().print("OK");
85 getWriter().println (" (" + result.runCount() + " test" + (result.runCount() == 1 ? "": "s") + ")");
88 getWriter().println();
89 getWriter().println("FAILURES!!!");
90 getWriter().println("Tests run: "+result.runCount()+
91 ", Failures: "+result.failureCount()+
92 ", Errors: "+result.errorCount());
94 getWriter().println();
99 * Returns the formatted string of the elapsed time.
100 * Duplicated from BaseTestRunner. Fix it.
102 protected String elapsedTimeAsString(long runTime) {
103 // The following line was altered for compatibility with
104 // Android libraries.
105 return Double.toString((double)runTime/1000);
108 public PrintStream getWriter() {
112 * @see junit.framework.TestListener#addError(Test, Throwable)
114 public void addError(Test test, Throwable t) {
115 getWriter().print("E");
119 * @see junit.framework.TestListener#addFailure(Test, AssertionFailedError)
121 public void addFailure(Test test, AssertionFailedError t) {
122 getWriter().print("F");
126 * @see junit.framework.TestListener#endTest(Test)
128 public void endTest(Test test) {
132 * @see junit.framework.TestListener#startTest(Test)
134 public void startTest(Test test) {
135 getWriter().print(".");
136 if (fColumn++ >= 40) {
137 getWriter().println();