• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package junit.textui;
3 
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;
8 
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;
15 
16 public class ResultPrinter implements TestListener {
17 	PrintStream fWriter;
18 	int fColumn= 0;
19 
ResultPrinter(PrintStream writer)20 	public ResultPrinter(PrintStream writer) {
21 		fWriter= writer;
22 	}
23 
24 	/* API for use by textui.TestRunner
25 	 */
26 
print(TestResult result, long runTime)27 	synchronized void print(TestResult result, long runTime) {
28 		printHeader(runTime);
29 	    printErrors(result);
30 	    printFailures(result);
31 	    printFooter(result);
32 	}
33 
printWaitPrompt()34 	void printWaitPrompt() {
35 		getWriter().println();
36 		getWriter().println("<RETURN> to continue");
37 	}
38 
39 	/* Internal methods
40 	 */
41 
printHeader(long runTime)42 	protected void printHeader(long runTime) {
43 		getWriter().println();
44 		getWriter().println("Time: "+elapsedTimeAsString(runTime));
45 	}
46 
printErrors(TestResult result)47 	protected void printErrors(TestResult result) {
48 		printDefects(result.errors(), result.errorCount(), "error");
49 	}
50 
printFailures(TestResult result)51 	protected void printFailures(TestResult result) {
52 		printDefects(result.failures(), result.failureCount(), "failure");
53 	}
54 
printDefects(Enumeration booBoos, int count, String type)55 	protected void printDefects(Enumeration booBoos, int count, String type) {
56 		if (count == 0) return;
57 		if (count == 1)
58 			getWriter().println("There was " + count + " " + type + ":");
59 		else
60 			getWriter().println("There were " + count + " " + type + "s:");
61 		for (int i= 1; booBoos.hasMoreElements(); i++) {
62 			printDefect((TestFailure) booBoos.nextElement(), i);
63 		}
64 	}
65 
printDefect(TestFailure booBoo, int count)66 	public void printDefect(TestFailure booBoo, int count) { // only public for testing purposes
67 		printDefectHeader(booBoo, count);
68 		printDefectTrace(booBoo);
69 	}
70 
printDefectHeader(TestFailure booBoo, int count)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());
75 	}
76 
printDefectTrace(TestFailure booBoo)77 	protected void printDefectTrace(TestFailure booBoo) {
78 		getWriter().print(BaseTestRunner.getFilteredTrace(booBoo.trace()));
79 	}
80 
printFooter(TestResult result)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") + ")");
86 
87 		} else {
88 			getWriter().println();
89 			getWriter().println("FAILURES!!!");
90 			getWriter().println("Tests run: "+result.runCount()+
91 				         ",  Failures: "+result.failureCount()+
92 				         ",  Errors: "+result.errorCount());
93 		}
94 	    getWriter().println();
95 	}
96 
97 
98 	/**
99 	 * Returns the formatted string of the elapsed time.
100 	 * Duplicated from BaseTestRunner. Fix it.
101 	 */
elapsedTimeAsString(long runTime)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);
106 	}
107 
getWriter()108 	public PrintStream getWriter() {
109 		return fWriter;
110 	}
111 	/**
112 	 * @see junit.framework.TestListener#addError(Test, Throwable)
113 	 */
addError(Test test, Throwable t)114 	public void addError(Test test, Throwable t) {
115 		getWriter().print("E");
116 	}
117 
118 	/**
119 	 * @see junit.framework.TestListener#addFailure(Test, AssertionFailedError)
120 	 */
addFailure(Test test, AssertionFailedError t)121 	public void addFailure(Test test, AssertionFailedError t) {
122 		getWriter().print("F");
123 	}
124 
125 	/**
126 	 * @see junit.framework.TestListener#endTest(Test)
127 	 */
endTest(Test test)128 	public void endTest(Test test) {
129 	}
130 
131 	/**
132 	 * @see junit.framework.TestListener#startTest(Test)
133 	 */
startTest(Test test)134 	public void startTest(Test test) {
135 		getWriter().print(".");
136 		if (fColumn++ >= 40) {
137 			getWriter().println();
138 			fColumn= 0;
139 		}
140 	}
141 
142 }
143