1 21 package db4ounit; 22 23 import java.io.IOException ; 24 import java.io.Writer ; 25 26 import db4ounit.util.StopWatch; 27 28 public class TestResult extends Printable { 29 30 private TestFailureCollection _failures = new TestFailureCollection(); 31 32 private int _testCount = 0; 33 34 private final StopWatch _watch = new StopWatch(); 35 36 private final Writer _stdout; 37 38 public TestResult(boolean printLabels) { 39 _stdout = printLabels ? TestPlatform.getStdOut() : null; 40 } 41 42 public TestResult() { 43 this(false); 44 } 45 46 public void testStarted(Test test) throws IOException { 47 ++_testCount; 48 printLabel(test.getLabel()); 49 } 50 51 private void printLabel(String label) throws IOException { 52 if (null != _stdout) { 53 _stdout.write(label + "\n"); 54 _stdout.flush(); 55 } 56 } 57 58 public void testFailed(Test test, Throwable failure) { 59 _failures.add(new TestFailure(test, failure)); 60 } 61 62 public boolean green() { 63 return _failures.size() == 0; 64 } 65 66 public TestFailureCollection failures() { 67 return _failures; 68 } 69 70 public void print(Writer writer) throws IOException { 71 if (green()) { 72 writer.write("GREEN (" + _testCount + " tests) - " + elapsedString() + "\n"); 73 return; 74 } 75 writer.write("RED (" + _failures.size() +" out of " + _testCount + " tests failed) - " + elapsedString() + "\n"); 76 _failures.print(writer); 77 } 78 79 private String elapsedString() { 80 return _watch.toString(); 81 } 82 83 public int assertions() { 84 return 0; 85 } 86 87 public void runStarted() { 88 _watch.start(); 89 } 90 91 public void runFinished() { 92 _watch.stop(); 93 } 94 } 95 | Popular Tags |