1 2package junit.textui; 3 4import java.io.PrintStream; 5// The following line was removed for compatibility with Android libraries. 6//import java.text.NumberFormat; 7import java.util.Enumeration; 8 9import junit.framework.AssertionFailedError; 10import junit.framework.Test; 11import junit.framework.TestFailure; 12import junit.framework.TestListener; 13import junit.framework.TestResult; 14import junit.runner.BaseTestRunner; 15 16public class ResultPrinter implements TestListener { 17 PrintStream fWriter; 18 int fColumn= 0; 19 20 public ResultPrinter(PrintStream writer) { 21 fWriter= writer; 22 } 23 24 /* API for use by textui.TestRunner 25 */ 26 27 synchronized void print(TestResult result, long runTime) { 28 printHeader(runTime); 29 printErrors(result); 30 printFailures(result); 31 printFooter(result); 32 } 33 34 void printWaitPrompt() { 35 getWriter().println(); 36 getWriter().println("<RETURN> to continue"); 37 } 38 39 /* Internal methods 40 */ 41 42 protected void printHeader(long runTime) { 43 getWriter().println(); 44 getWriter().println("Time: "+elapsedTimeAsString(runTime)); 45 } 46 47 protected void printErrors(TestResult result) { 48 printDefects(result.errors(), result.errorCount(), "error"); 49 } 50 51 protected void printFailures(TestResult result) { 52 printDefects(result.failures(), result.failureCount(), "failure"); 53 } 54 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 66 public void printDefect(TestFailure booBoo, int count) { // only public for testing purposes 67 printDefectHeader(booBoo, count); 68 printDefectTrace(booBoo); 69 } 70 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 77 protected void printDefectTrace(TestFailure booBoo) { 78 getWriter().print(BaseTestRunner.getFilteredTrace(booBoo.trace())); 79 } 80 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 */ 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 108 public PrintStream getWriter() { 109 return fWriter; 110 } 111 /** 112 * @see junit.framework.TestListener#addError(Test, Throwable) 113 */ 114 public void addError(Test test, Throwable t) { 115 getWriter().print("E"); 116 } 117 118 /** 119 * @see junit.framework.TestListener#addFailure(Test, AssertionFailedError) 120 */ 121 public void addFailure(Test test, AssertionFailedError t) { 122 getWriter().print("F"); 123 } 124 125 /** 126 * @see junit.framework.TestListener#endTest(Test) 127 */ 128 public void endTest(Test test) { 129 } 130 131 /** 132 * @see junit.framework.TestListener#startTest(Test) 133 */ 134 public void startTest(Test test) { 135 getWriter().print("."); 136 if (fColumn++ >= 40) { 137 getWriter().println(); 138 fColumn= 0; 139 } 140 } 141 142} 143