1 21 package db4ounit; 22 23 import java.io.IOException ; 24 import java.io.Writer ; 25 26 public class TestRunner { 27 28 private TestSuiteBuilder _suiteBuilder; 29 30 public TestRunner(TestSuite suite) { 31 if (null == suite) throw new IllegalArgumentException ("suite"); 32 _suiteBuilder = new NullTestSuiteBuilder(suite); 33 } 34 35 public TestRunner(TestSuiteBuilder builder) { 36 if (null == builder) throw new IllegalArgumentException ("suite"); 37 _suiteBuilder = builder; 38 } 39 40 public TestRunner(Class clazz) { 41 this(new ReflectionTestSuiteBuilder(clazz)); 42 } 43 44 public int run() { 45 return run(true); 46 } 47 48 private int run(boolean printLabels) { 49 TestSuite suite = buildTestSuite(); 50 if (null == suite) return 1; 51 52 TestResult result = new TestResult(printLabels); 53 result.runStarted(); 54 suite.run(result); 55 result.runFinished(); 56 report(result); 57 return result.failures().size(); 58 } 59 60 private TestSuite buildTestSuite() { 61 try { 62 return _suiteBuilder.build(); 63 } catch (Exception x) { 64 report(x); 65 } 66 return null; 67 } 68 69 private void report(Exception x) { 70 Writer stdout = TestPlatform.getStdOut(); 71 TestPlatform.printStackTrace(stdout, x); 72 } 73 74 private void report(TestResult result) { 75 try { 76 java.io.Writer stdout = TestPlatform.getStdOut(); 77 result.print(stdout); 78 stdout.flush(); 79 } catch (IOException e) { 80 } 81 } 82 } 83 | Popular Tags |