1 package org.junit.internal.runners; 2 3 import java.io.PrintStream ; 4 import java.text.NumberFormat ; 5 6 import org.junit.runner.Description; 7 import org.junit.runner.Result; 8 import org.junit.runner.notification.Failure; 9 import org.junit.runner.notification.RunListener; 10 11 public class TextListener extends RunListener { 12 13 private final PrintStream fWriter; 14 15 public TextListener() { 16 this(System.out); 17 } 18 19 public TextListener(PrintStream writer) { 20 this.fWriter= writer; 21 } 22 23 @Override 24 public void testRunFinished(Result result) { 25 printHeader(result.getRunTime()); 26 printFailures(result); 27 printFooter(result); 28 } 29 30 @Override 31 public void testStarted(Description description) { 32 fWriter.append('.'); 33 } 34 35 @Override 36 public void testFailure(Failure failure) { 37 fWriter.append('E'); 38 } 39 40 @Override 41 public void testIgnored(Description description) { 42 fWriter.append('I'); 43 } 44 45 48 49 private PrintStream getWriter() { 50 return fWriter; 51 } 52 53 protected void printHeader(long runTime) { 54 getWriter().println(); 55 getWriter().println("Time: " + elapsedTimeAsString(runTime)); 56 } 57 58 protected void printFailures(Result result) { 59 if (result.getFailureCount() == 0) 60 return; 61 if (result.getFailureCount() == 1) 62 getWriter().println("There was " + result.getFailureCount() + " failure:"); 63 else 64 getWriter().println("There were " + result.getFailureCount() + " failures:"); 65 int i= 1; 66 for (Failure each : result.getFailures()) 67 printFailure(each, i++); 68 } 69 70 protected void printFailure(Failure failure, int count) { 71 printFailureHeader(failure, count); 72 printFailureTrace(failure); 73 } 74 75 protected void printFailureHeader(Failure failure, int count) { 76 getWriter().println(count + ") " + failure.getTestHeader()); 77 } 78 79 protected void printFailureTrace(Failure failure) { 80 getWriter().print(failure.getTrace()); 81 } 82 83 protected void printFooter(Result result) { 84 if (result.wasSuccessful()) { 85 getWriter().println(); 86 getWriter().print("OK"); 87 getWriter().println(" (" + result.getRunCount() + " test" + (result.getRunCount() == 1 ? "" : "s") + ")"); 88 89 } else { 90 getWriter().println(); 91 getWriter().println("FAILURES!!!"); 92 getWriter().println("Tests run: " + result.getRunCount() + ", Failures: " + result.getFailureCount()); 93 } 94 getWriter().println(); 95 } 96 97 101 protected String elapsedTimeAsString(long runTime) { 102 return NumberFormat.getInstance().format((double) runTime / 1000); 103 } 104 } 105 | Popular Tags |