1 package junit.framework; 2 3 import java.util.ArrayList ; 4 import java.util.Collections ; 5 import java.util.Enumeration ; 6 import java.util.List ; 7 8 17 public class TestResult extends Object { 18 protected List <TestFailure> fFailures; 19 protected List <TestFailure> fErrors; 20 protected List <TestListener> fListeners; 21 protected int fRunTests; 22 private boolean fStop; 23 24 public TestResult() { 25 fFailures= new ArrayList <TestFailure>(); 26 fErrors= new ArrayList <TestFailure>(); 27 fListeners= new ArrayList <TestListener>(); 28 fRunTests= 0; 29 fStop= false; 30 } 31 35 public synchronized void addError(Test test, Throwable t) { 36 fErrors.add(new TestFailure(test, t)); 37 for (TestListener each : cloneListeners()) 38 each.addError(test, t); 39 } 40 44 public synchronized void addFailure(Test test, AssertionFailedError t) { 45 fFailures.add(new TestFailure(test, t)); 46 for (TestListener each : cloneListeners()) 47 each.addFailure(test, t); 48 } 49 52 public synchronized void addListener(TestListener listener) { 53 fListeners.add(listener); 54 } 55 58 public synchronized void removeListener(TestListener listener) { 59 fListeners.remove(listener); 60 } 61 64 private synchronized List <TestListener> cloneListeners() { 65 List <TestListener> result= new ArrayList <TestListener>(); 66 result.addAll(fListeners); 67 return result; 68 } 69 72 public void endTest(Test test) { 73 for (TestListener each : cloneListeners()) 74 each.endTest(test); 75 } 76 79 public synchronized int errorCount() { 80 return fErrors.size(); 81 } 82 85 public synchronized Enumeration <TestFailure> errors() { 86 return Collections.enumeration(fErrors); 87 } 88 89 90 93 public synchronized int failureCount() { 94 return fFailures.size(); 95 } 96 99 public synchronized Enumeration <TestFailure> failures() { 100 return Collections.enumeration(fFailures); 101 } 102 103 106 protected void run(final TestCase test) { 107 startTest(test); 108 Protectable p= new Protectable() { 109 public void protect() throws Throwable { 110 test.runBare(); 111 } 112 }; 113 runProtected(test, p); 114 115 endTest(test); 116 } 117 120 public synchronized int runCount() { 121 return fRunTests; 122 } 123 126 public void runProtected(final Test test, Protectable p) { 127 try { 128 p.protect(); 129 } 130 catch (AssertionFailedError e) { 131 addFailure(test, e); 132 } 133 catch (ThreadDeath e) { throw e; 135 } 136 catch (Throwable e) { 137 addError(test, e); 138 } 139 } 140 143 public synchronized boolean shouldStop() { 144 return fStop; 145 } 146 149 public void startTest(Test test) { 150 final int count= test.countTestCases(); 151 synchronized(this) { 152 fRunTests+= count; 153 } 154 for (TestListener each : cloneListeners()) 155 each.startTest(test); 156 } 157 160 public synchronized void stop() { 161 fStop= true; 162 } 163 166 public synchronized boolean wasSuccessful() { 167 return failureCount() == 0 && errorCount() == 0; 168 } 169 } | Popular Tags |