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
9
10
11
12
13
14
15
16
17 public class TestResult {
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
32
33
34
35
36 public synchronized void addError(Test test, Throwable e) {
37 fErrors.add(new TestFailure(test, e));
38 for (TestListener each : cloneListeners()) {
39 each.addError(test, e);
40 }
41 }
42
43
44
45
46
47 public synchronized void addFailure(Test test, AssertionFailedError e) {
48 fFailures.add(new TestFailure(test, e));
49 for (TestListener each : cloneListeners()) {
50 each.addFailure(test, e);
51 }
52 }
53
54
55
56
57 public synchronized void addListener(TestListener listener) {
58 fListeners.add(listener);
59 }
60
61
62
63
64 public synchronized void removeListener(TestListener listener) {
65 fListeners.remove(listener);
66 }
67
68
69
70
71 private synchronized List<TestListener> cloneListeners() {
72 List<TestListener> result = new ArrayList<TestListener>();
73 result.addAll(fListeners);
74 return result;
75 }
76
77
78
79
80 public void endTest(Test test) {
81 for (TestListener each : cloneListeners()) {
82 each.endTest(test);
83 }
84 }
85
86
87
88
89 public synchronized int errorCount() {
90 return fErrors.size();
91 }
92
93
94
95
96 public synchronized Enumeration<TestFailure> errors() {
97 return Collections.enumeration(fErrors);
98 }
99
100
101
102
103
104 public synchronized int failureCount() {
105 return fFailures.size();
106 }
107
108
109
110
111 public synchronized Enumeration<TestFailure> failures() {
112 return Collections.enumeration(fFailures);
113 }
114
115
116
117
118 protected void run(final TestCase test) {
119 startTest(test);
120 Protectable p = new Protectable() {
121 public void protect() throws Throwable {
122 test.runBare();
123 }
124 };
125 runProtected(test, p);
126
127 endTest(test);
128 }
129
130
131
132
133 public synchronized int runCount() {
134 return fRunTests;
135 }
136
137
138
139
140 public void runProtected(final Test test, Protectable p) {
141 try {
142 p.protect();
143 } catch (AssertionFailedError e) {
144 addFailure(test, e);
145 } catch (ThreadDeath e) {
146 throw e;
147 } catch (Throwable e) {
148 addError(test, e);
149 }
150 }
151
152
153
154
155 public synchronized boolean shouldStop() {
156 return fStop;
157 }
158
159
160
161
162 public void startTest(Test test) {
163 final int count = test.countTestCases();
164 synchronized (this) {
165 fRunTests += count;
166 }
167 for (TestListener each : cloneListeners()) {
168 each.startTest(test);
169 }
170 }
171
172
173
174
175 public synchronized void stop() {
176 fStop = true;
177 }
178
179
180
181
182 public synchronized boolean wasSuccessful() {
183 return failureCount() == 0 && errorCount() == 0;
184 }
185 }