1
1
package org .junit .runner .notification ;
2
2
3
- import static java .util .Arrays .asList ;
4
-
5
3
import java .util .ArrayList ;
6
- import java .util .Collections ;
7
- import java .util .Iterator ;
4
+ import java .util .Arrays ;
5
+ import java .util .Collection ;
6
+ import java .util .LinkedList ;
8
7
import java .util .List ;
9
-
8
+ import java . util . concurrent . ConcurrentLinkedQueue ;
10
9
import org .junit .internal .AssumptionViolatedException ;
11
10
import org .junit .runner .Description ;
12
11
import org .junit .runner .Result ;
21
20
* @since 4.0
22
21
*/
23
22
public class RunNotifier {
24
- private final List <RunListener > fListeners =
25
- Collections .synchronizedList (new ArrayList <RunListener >());
26
- private volatile boolean fPleaseStop = false ;
23
+ private final ConcurrentLinkedQueue <RunListener > fListeners = new ConcurrentLinkedQueue <RunListener >();
24
+ private volatile boolean fPleaseStop = false ;
27
25
28
26
/**
29
27
* Internal use only
@@ -40,32 +38,29 @@ public void removeListener(RunListener listener) {
40
38
}
41
39
42
40
private abstract class SafeNotifier {
43
- private final List <RunListener > fCurrentListeners ;
41
+ private final Collection <RunListener > fCurrentListeners ;
44
42
45
43
SafeNotifier () {
46
44
this (fListeners );
47
45
}
48
46
49
- SafeNotifier (List <RunListener > currentListeners ) {
50
- fCurrentListeners = currentListeners ;
47
+ SafeNotifier (Collection <RunListener > currentListeners ) {
48
+ fCurrentListeners = currentListeners ;
51
49
}
52
50
53
51
void run () {
54
- synchronized (fListeners ) {
55
- List <RunListener > safeListeners = new ArrayList <RunListener >();
56
- List <Failure > failures = new ArrayList <Failure >();
57
- for (Iterator <RunListener > all = fCurrentListeners .iterator (); all
58
- .hasNext (); ) {
59
- try {
60
- RunListener listener = all .next ();
61
- notifyListener (listener );
62
- safeListeners .add (listener );
63
- } catch (Exception e ) {
64
- failures .add (new Failure (Description .TEST_MECHANISM , e ));
65
- }
52
+ int capacity = fCurrentListeners .size ();
53
+ ArrayList <RunListener > safeListeners = new ArrayList <RunListener >(capacity );
54
+ ArrayList <Failure > failures = new ArrayList <Failure >(capacity );
55
+ for (RunListener listener : fCurrentListeners ) {
56
+ try {
57
+ notifyListener (listener );
58
+ safeListeners .add (listener );
59
+ } catch (Exception e ) {
60
+ failures .add (new Failure (Description .TEST_MECHANISM , e ));
66
61
}
67
- fireTestFailures (safeListeners , failures );
68
62
}
63
+ fireTestFailures (safeListeners , failures );
69
64
}
70
65
71
66
abstract protected void notifyListener (RunListener each ) throws Exception ;
@@ -80,8 +75,6 @@ public void fireTestRunStarted(final Description description) {
80
75
protected void notifyListener (RunListener each ) throws Exception {
81
76
each .testRunStarted (description );
82
77
}
83
-
84
- ;
85
78
}.run ();
86
79
}
87
80
@@ -94,8 +87,6 @@ public void fireTestRunFinished(final Result result) {
94
87
protected void notifyListener (RunListener each ) throws Exception {
95
88
each .testRunFinished (result );
96
89
}
97
-
98
- ;
99
90
}.run ();
100
91
}
101
92
@@ -114,8 +105,6 @@ public void fireTestStarted(final Description description) throws StoppedByUserE
114
105
protected void notifyListener (RunListener each ) throws Exception {
115
106
each .testStarted (description );
116
107
}
117
-
118
- ;
119
108
}.run ();
120
109
}
121
110
@@ -125,22 +114,18 @@ protected void notifyListener(RunListener each) throws Exception {
125
114
* @param failure the description of the test that failed and the exception thrown
126
115
*/
127
116
public void fireTestFailure (Failure failure ) {
128
- fireTestFailures (fListeners , asList (failure ));
117
+ fireTestFailures (fListeners , Arrays . asList (failure ));
129
118
}
130
119
131
- private void fireTestFailures (List <RunListener > listeners ,
132
- final List <Failure > failures ) {
120
+ private void fireTestFailures (Collection <RunListener > listeners , final List <Failure > failures ) {
133
121
if (!failures .isEmpty ()) {
134
122
new SafeNotifier (listeners ) {
135
123
@ Override
136
- protected void notifyListener (RunListener listener )
137
- throws Exception {
124
+ protected void notifyListener (RunListener listener ) throws Exception {
138
125
for (Failure each : failures ) {
139
126
listener .testFailure (each );
140
127
}
141
128
}
142
-
143
- ;
144
129
}.run ();
145
130
}
146
131
}
@@ -158,8 +143,6 @@ public void fireTestAssumptionFailed(final Failure failure) {
158
143
protected void notifyListener (RunListener each ) throws Exception {
159
144
each .testAssumptionFailure (failure );
160
145
}
161
-
162
- ;
163
146
}.run ();
164
147
}
165
148
@@ -190,8 +173,6 @@ public void fireTestFinished(final Description description) {
190
173
protected void notifyListener (RunListener each ) throws Exception {
191
174
each .testFinished (description );
192
175
}
193
-
194
- ;
195
176
}.run ();
196
177
}
197
178
@@ -202,13 +183,16 @@ protected void notifyListener(RunListener each) throws Exception {
202
183
* to be shared amongst the many runners involved.
203
184
*/
204
185
public void pleaseStop () {
205
- fPleaseStop = true ;
186
+ fPleaseStop = true ;
206
187
}
207
188
208
189
/**
209
190
* Internal use only. The Result's listener must be first.
210
191
*/
211
192
public void addFirstListener (RunListener listener ) {
212
- fListeners .add (0 , listener );
193
+ LinkedList <RunListener > listeners = new LinkedList <RunListener >(fListeners );
194
+ listeners .addFirst (listener );
195
+ fListeners .clear ();
196
+ fListeners .addAll (listeners );
213
197
}
214
198
}
0 commit comments