Skip to content
This repository was archived by the owner on Oct 13, 2020. It is now read-only.

Commit d371689

Browse files
committed
Remove redundant copy & paste from ParentRunner
1 parent 4b48d9b commit d371689

File tree

2 files changed

+278
-64
lines changed

2 files changed

+278
-64
lines changed

assumes/src/main/java/org/junit/contrib/assumes/Corollaries.java

+30-58
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
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+
116
package org.junit.contrib.assumes;
217

3-
import org.junit.Ignore;
418
import org.junit.internal.AssumptionViolatedException;
519
import org.junit.internal.runners.model.EachTestNotifier;
6-
import org.junit.rules.TestRule;
720
import org.junit.runner.Description;
821
import org.junit.runner.manipulation.Filter;
922
import org.junit.runner.manipulation.NoTestsRemainException;
@@ -25,19 +38,18 @@
2538
import java.util.HashSet;
2639
import java.util.Iterator;
2740
import java.util.List;
28-
import java.util.Map;
2941
import java.util.Set;
3042

3143
/**
3244
* A runner which is aware of the {@link Assumes} of its {@link org.junit.Test}s
3345
*/
3446
public class Corollaries extends BlockJUnit4ClassRunner {
3547

36-
private Sorter fSorter= Sorter.NULL;
48+
private Sorter fSorter = Sorter.NULL;
3749

3850
private List<FrameworkMethod> fFilteredChildren;
3951

40-
private RunnerScheduler fScheduler= new RunnerScheduler() {
52+
private RunnerScheduler fScheduler = new RunnerScheduler() {
4153
public void schedule(Runnable childStatement) {
4254
childStatement.run();
4355
}
@@ -59,9 +71,10 @@ public Corollaries(Class<?> klass) throws InitializationError {
5971

6072
@Override
6173
public void sort(Sorter sorter) {
62-
fSorter= sorter;
63-
for (FrameworkMethod each : getFilteredChildren())
74+
fSorter = sorter;
75+
for (FrameworkMethod each : getFilteredChildren()) {
6476
sortChild(each);
77+
}
6578
Collections.sort(getFilteredChildren(), comparator());
6679
assumptionSort(getFilteredChildren());
6780
}
@@ -78,20 +91,6 @@ public int compare(FrameworkMethod o1, FrameworkMethod o2) {
7891
};
7992
}
8093

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-
}
9594
/**
9695
* Returns a {@link Statement}: Call {@link #runChild(Object, RunNotifier)}
9796
* on each object returned by {@link #getChildren()} (subject to any imposed
@@ -124,20 +123,21 @@ public void testIgnored(Description description) throws Exception {
124123
}
125124
};
126125
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() {
129128
public void run() {
130129
Corollaries.this.runChild(each, notifier, invalidAssumptions);
131130
}
132131
});
132+
}
133133
fScheduler.finished();
134134
}
135135

136136
protected void runChild(final FrameworkMethod method, RunNotifier notifier, Set<String> invalidAssumptions) {
137137
Assumes assumptions = method.getAnnotation(Assumes.class);
138138
boolean invalidAssumption = false;
139139
if (assumptions != null) {
140-
for (String assumption: assumptions.value()) {
140+
for (String assumption : assumptions.value()) {
141141
if (invalidAssumptions.contains(assumption)) {
142142
invalidAssumption = true;
143143
break;
@@ -180,29 +180,30 @@ private void assumptionSort(List<FrameworkMethod> methods) {
180180
}
181181
}
182182
}
183-
Collections.sort(getFilteredChildren(), new AssumptionComparator());
184183
}
185184

186185
@Override
187186
public Description getDescription() {
188-
Description description= Description.createSuiteDescription(getName(),
187+
Description description = Description.createSuiteDescription(getName(),
189188
getTestClass().getAnnotations());
190-
for (FrameworkMethod child : getFilteredChildren())
189+
for (FrameworkMethod child : getFilteredChildren()) {
191190
description.addChild(describeChild(child));
191+
}
192192
return description;
193193
}
194194

195195
public void filter(Filter filter) throws NoTestsRemainException {
196196
for (Iterator<FrameworkMethod> iter = getFilteredChildren().iterator(); iter.hasNext(); ) {
197197
FrameworkMethod each = iter.next();
198-
if (shouldRun(filter, each))
198+
if (shouldRun(filter, each)) {
199199
try {
200200
filter.apply(each);
201201
} catch (NoTestsRemainException e) {
202202
iter.remove();
203203
}
204-
else
204+
} else {
205205
iter.remove();
206+
}
206207
}
207208
if (getFilteredChildren().isEmpty()) {
208209
throw new NoTestsRemainException();
@@ -221,33 +222,4 @@ public void setScheduler(RunnerScheduler scheduler) {
221222
this.fScheduler = scheduler;
222223
}
223224

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-
253225
}

0 commit comments

Comments
 (0)