1. package junit.textui;
  2. import java.io.PrintStream;
  3. import java.text.NumberFormat;
  4. import java.util.Enumeration;
  5. import junit.framework.AssertionFailedError;
  6. import junit.framework.Test;
  7. import junit.framework.TestFailure;
  8. import junit.framework.TestListener;
  9. import junit.framework.TestResult;
  10. import junit.runner.BaseTestRunner;
  11. public class ResultPrinter implements TestListener {
  12. PrintStream fWriter;
  13. int fColumn= 0;
  14. public ResultPrinter(PrintStream writer) {
  15. fWriter= writer;
  16. }
  17. /* API for use by textui.TestRunner
  18. */
  19. synchronized void print(TestResult result, long runTime) {
  20. printHeader(runTime);
  21. printErrors(result);
  22. printFailures(result);
  23. printFooter(result);
  24. }
  25. void printWaitPrompt() {
  26. getWriter().println();
  27. getWriter().println("<RETURN> to continue");
  28. }
  29. /* Internal methods
  30. */
  31. protected void printHeader(long runTime) {
  32. getWriter().println();
  33. getWriter().println("Time: "+elapsedTimeAsString(runTime));
  34. }
  35. protected void printErrors(TestResult result) {
  36. printDefects(result.errors(), result.errorCount(), "error");
  37. }
  38. protected void printFailures(TestResult result) {
  39. printDefects(result.failures(), result.failureCount(), "failure");
  40. }
  41. protected void printDefects(Enumeration booBoos, int count, String type) {
  42. if (count == 0) return;
  43. if (count == 1)
  44. getWriter().println("There was " + count + " " + type + ":");
  45. else
  46. getWriter().println("There were " + count + " " + type + "s:");
  47. for (int i= 1; booBoos.hasMoreElements(); i++) {
  48. printDefect((TestFailure) booBoos.nextElement(), i);
  49. }
  50. }
  51. public void printDefect(TestFailure booBoo, int count) { // only public for testing purposes
  52. printDefectHeader(booBoo, count);
  53. printDefectTrace(booBoo);
  54. }
  55. protected void printDefectHeader(TestFailure booBoo, int count) {
  56. // I feel like making this a println, then adding a line giving the throwable a chance to print something
  57. // before we get to the stack trace.
  58. getWriter().print(count + ") " + booBoo.failedTest());
  59. }
  60. protected void printDefectTrace(TestFailure booBoo) {
  61. getWriter().print(BaseTestRunner.getFilteredTrace(booBoo.trace()));
  62. }
  63. protected void printFooter(TestResult result) {
  64. if (result.wasSuccessful()) {
  65. getWriter().println();
  66. getWriter().print("OK");
  67. getWriter().println (" (" + result.runCount() + " test" + (result.runCount() == 1 ? "": "s") + ")");
  68. } else {
  69. getWriter().println();
  70. getWriter().println("FAILURES!!!");
  71. getWriter().println("Tests run: "+result.runCount()+
  72. ", Failures: "+result.failureCount()+
  73. ", Errors: "+result.errorCount());
  74. }
  75. getWriter().println();
  76. }
  77. /**
  78. * Returns the formatted string of the elapsed time.
  79. * Duplicated from BaseTestRunner. Fix it.
  80. */
  81. protected String elapsedTimeAsString(long runTime) {
  82. return NumberFormat.getInstance().format((double)runTime1000);
  83. }
  84. public PrintStream getWriter() {
  85. return fWriter;
  86. }
  87. /**
  88. * @see junit.framework.TestListener#addError(Test, Throwable)
  89. */
  90. public void addError(Test test, Throwable t) {
  91. getWriter().print("E");
  92. }
  93. /**
  94. * @see junit.framework.TestListener#addFailure(Test, AssertionFailedError)
  95. */
  96. public void addFailure(Test test, AssertionFailedError t) {
  97. getWriter().print("F");
  98. }
  99. /**
  100. * @see junit.framework.TestListener#endTest(Test)
  101. */
  102. public void endTest(Test test) {
  103. }
  104. /**
  105. * @see junit.framework.TestListener#startTest(Test)
  106. */
  107. public void startTest(Test test) {
  108. getWriter().print(".");
  109. if (fColumn++ >= 40) {
  110. getWriter().println();
  111. fColumn= 0;
  112. }
  113. }
  114. }