1
+ /*
2
+ * The copyright holders of this work license this file to You under
3
+ * the Apache License, Version 2.0 (the "License"); you may not use this
4
+ * file except in compliance with the License. You may obtain a copy of
5
+ * the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
1
16
package org .junit .contrib .assumes ;
2
17
3
- import org .junit .Ignore ;
4
18
import org .junit .internal .AssumptionViolatedException ;
5
19
import org .junit .internal .runners .model .EachTestNotifier ;
6
- import org .junit .rules .TestRule ;
7
20
import org .junit .runner .Description ;
8
21
import org .junit .runner .manipulation .Filter ;
9
22
import org .junit .runner .manipulation .NoTestsRemainException ;
25
38
import java .util .HashSet ;
26
39
import java .util .Iterator ;
27
40
import java .util .List ;
28
- import java .util .Map ;
29
41
import java .util .Set ;
30
42
31
43
/**
32
44
* A runner which is aware of the {@link Assumes} of its {@link org.junit.Test}s
33
45
*/
34
46
public class Corollaries extends BlockJUnit4ClassRunner {
35
47
36
- private Sorter fSorter = Sorter .NULL ;
48
+ private Sorter fSorter = Sorter .NULL ;
37
49
38
50
private List <FrameworkMethod > fFilteredChildren ;
39
51
40
- private RunnerScheduler fScheduler = new RunnerScheduler () {
52
+ private RunnerScheduler fScheduler = new RunnerScheduler () {
41
53
public void schedule (Runnable childStatement ) {
42
54
childStatement .run ();
43
55
}
@@ -59,9 +71,10 @@ public Corollaries(Class<?> klass) throws InitializationError {
59
71
60
72
@ Override
61
73
public void sort (Sorter sorter ) {
62
- fSorter = sorter ;
63
- for (FrameworkMethod each : getFilteredChildren ())
74
+ fSorter = sorter ;
75
+ for (FrameworkMethod each : getFilteredChildren ()) {
64
76
sortChild (each );
77
+ }
65
78
Collections .sort (getFilteredChildren (), comparator ());
66
79
assumptionSort (getFilteredChildren ());
67
80
}
@@ -78,20 +91,6 @@ public int compare(FrameworkMethod o1, FrameworkMethod o2) {
78
91
};
79
92
}
80
93
81
- @ Override
82
- public void run (final RunNotifier notifier ) {
83
- EachTestNotifier testNotifier = new EachTestNotifier (notifier , getDescription ());
84
- try {
85
- Statement statement = classBlock (notifier );
86
- statement .evaluate ();
87
- } catch (AssumptionViolatedException e ) {
88
- testNotifier .fireTestIgnored ();
89
- } catch (StoppedByUserException e ) {
90
- throw e ;
91
- } catch (Throwable e ) {
92
- testNotifier .addFailure (e );
93
- }
94
- }
95
94
/**
96
95
* Returns a {@link Statement}: Call {@link #runChild(Object, RunNotifier)}
97
96
* on each object returned by {@link #getChildren()} (subject to any imposed
@@ -124,20 +123,21 @@ public void testIgnored(Description description) throws Exception {
124
123
}
125
124
};
126
125
notifier .addListener (l );
127
- for (final FrameworkMethod each : getFilteredChildren ())
128
- fScheduler .schedule (new Runnable () {
126
+ for (final FrameworkMethod each : getFilteredChildren ()) {
127
+ fScheduler .schedule (new Runnable () {
129
128
public void run () {
130
129
Corollaries .this .runChild (each , notifier , invalidAssumptions );
131
130
}
132
131
});
132
+ }
133
133
fScheduler .finished ();
134
134
}
135
135
136
136
protected void runChild (final FrameworkMethod method , RunNotifier notifier , Set <String > invalidAssumptions ) {
137
137
Assumes assumptions = method .getAnnotation (Assumes .class );
138
138
boolean invalidAssumption = false ;
139
139
if (assumptions != null ) {
140
- for (String assumption : assumptions .value ()) {
140
+ for (String assumption : assumptions .value ()) {
141
141
if (invalidAssumptions .contains (assumption )) {
142
142
invalidAssumption = true ;
143
143
break ;
@@ -180,29 +180,30 @@ private void assumptionSort(List<FrameworkMethod> methods) {
180
180
}
181
181
}
182
182
}
183
- Collections .sort (getFilteredChildren (), new AssumptionComparator ());
184
183
}
185
184
186
185
@ Override
187
186
public Description getDescription () {
188
- Description description = Description .createSuiteDescription (getName (),
187
+ Description description = Description .createSuiteDescription (getName (),
189
188
getTestClass ().getAnnotations ());
190
- for (FrameworkMethod child : getFilteredChildren ())
189
+ for (FrameworkMethod child : getFilteredChildren ()) {
191
190
description .addChild (describeChild (child ));
191
+ }
192
192
return description ;
193
193
}
194
194
195
195
public void filter (Filter filter ) throws NoTestsRemainException {
196
196
for (Iterator <FrameworkMethod > iter = getFilteredChildren ().iterator (); iter .hasNext (); ) {
197
197
FrameworkMethod each = iter .next ();
198
- if (shouldRun (filter , each ))
198
+ if (shouldRun (filter , each )) {
199
199
try {
200
200
filter .apply (each );
201
201
} catch (NoTestsRemainException e ) {
202
202
iter .remove ();
203
203
}
204
- else
204
+ } else {
205
205
iter .remove ();
206
+ }
206
207
}
207
208
if (getFilteredChildren ().isEmpty ()) {
208
209
throw new NoTestsRemainException ();
@@ -221,33 +222,4 @@ public void setScheduler(RunnerScheduler scheduler) {
221
222
this .fScheduler = scheduler ;
222
223
}
223
224
224
- private class AssumptionComparator implements Comparator <FrameworkMethod > {
225
-
226
- public int compare (FrameworkMethod o1 , FrameworkMethod o2 ) {
227
- if (assumed (o2 .getAnnotation (Assumes .class ), o1 .getName ())) {
228
- if (!assumed (o1 .getAnnotation (Assumes .class ), o2 .getName ())) {
229
- return -1 ;
230
- }
231
- } else {
232
- if (assumed (o1 .getAnnotation (Assumes .class ), o2 .getName ())) {
233
- return 1 ;
234
- }
235
- }
236
- return 0 ;
237
- }
238
-
239
- private boolean assumed (Assumes assumes , String predicate ) {
240
- if (assumes == null ) {
241
- return false ;
242
- }
243
- for (String s : assumes .value ()) {
244
- if (s .equals (predicate )) {
245
- return true ;
246
- }
247
- }
248
- return false ;
249
- }
250
-
251
- }
252
-
253
225
}
0 commit comments