1
2
3
4 package junit.framework;
5
6 import java.util.Enumeration;
7 import java.util.Vector;
8
9
10
11
12
13
14
15
16
17
18 public class TestResult extends Object {
19 protected Vector fFailures;
20 protected Vector fErrors;
21 protected Vector fListeners;
22 protected int fRunTests;
23 private boolean fStop;
24
25 public TestResult() {
26 fFailures= new Vector();
27 fErrors= new Vector();
28 fListeners= new Vector();
29 fRunTests= 0;
30 fStop= false;
31 }
32
33
34
35
36 public synchronized void addError(Test test, Throwable t) {
37 fErrors.addElement(new TestFailure(test, t));
38 for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) {
39 ((TestListener)e.nextElement()).addError(test, t);
40 }
41 }
42
43
44
45
46 public synchronized void addFailure(Test test, AssertionFailedError t) {
47 fFailures.addElement(new TestFailure(test, t));
48 for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) {
49 ((TestListener)e.nextElement()).addFailure(test, t);
50 }
51 }
52
53
54
55 public synchronized void addListener(TestListener listener) {
56 fListeners.addElement(listener);
57 }
58
59
60
61 public synchronized void removeListener(TestListener listener) {
62 fListeners.removeElement(listener);
63 }
64
65
66
67 private synchronized Vector cloneListeners() {
68 Vector clone = new Vector();
69 for (int i = 0; i < fListeners.size(); i++) {
70 clone.addElement(fListeners.elementAt(i));
71 }
72 return clone;
73 }
74
75
76
77 public void endTest(Test test) {
78 for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) {
79 ((TestListener)e.nextElement()).endTest(test);
80 }
81 }
82
83
84
85 public synchronized int errorCount() {
86 return fErrors.size();
87 }
88
89
90
91 public synchronized Enumeration errors() {
92 return fErrors.elements();
93 }
94
95
96
97 public synchronized int failureCount() {
98 return fFailures.size();
99 }
100
101
102
103 public synchronized Enumeration failures() {
104 return fFailures.elements();
105 }
106
107
108
109 protected void run(final TestCase test) {
110 startTest(test);
111 Protectable p= new Protectable() {
112 public void protect() throws Throwable {
113 test.runBare();
114 }
115 };
116 runProtected(test, p);
117
118 endTest(test);
119 }
120
121
122
123 public synchronized int runCount() {
124 return fRunTests;
125 }
126
127
128
129 public void runProtected(final Test test, Protectable p) {
130 try {
131 p.protect();
132 }
133 catch (AssertionFailedError e) {
134 addFailure(test, e);
135 }
136 catch (Throwable e) {
137 addError(test, e);
138 }
139 }
140
141
142
143 public synchronized boolean shouldStop() {
144 return fStop;
145 }
146
147
148
149 public void startTest(Test test) {
150 final int count= test.countTestCases();
151 synchronized(this) {
152 fRunTests+= count;
153 }
154 for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) {
155 ((TestListener)e.nextElement()).startTest(test);
156 }
157 }
158
159
160
161 public synchronized void stop() {
162 fStop= true;
163 }
164
165
166
167 public synchronized boolean wasSuccessful() {
168 return failureCount() == 0 && errorCount() == 0;
169 }
170 }