Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove redundant copy & paste from ParentRunner

  • Loading branch information...
commit d37168919e1f425caaadafcafa5224b08c29a90c 1 parent 4b48d9b
@stephenc authored
View
88 assumes/src/main/java/org/junit/contrib/assumes/Corollaries.java
@@ -1,9 +1,22 @@
+/*
+ * The copyright holders of this work license this file to You under
+ * the Apache License, Version 2.0 (the "License"); you may not use this
+ * file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.junit.contrib.assumes;
-import org.junit.Ignore;
import org.junit.internal.AssumptionViolatedException;
import org.junit.internal.runners.model.EachTestNotifier;
-import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.NoTestsRemainException;
@@ -25,7 +38,6 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
-import java.util.Map;
import java.util.Set;
/**
@@ -33,11 +45,11 @@
*/
public class Corollaries extends BlockJUnit4ClassRunner {
- private Sorter fSorter= Sorter.NULL;
+ private Sorter fSorter = Sorter.NULL;
private List<FrameworkMethod> fFilteredChildren;
- private RunnerScheduler fScheduler= new RunnerScheduler() {
+ private RunnerScheduler fScheduler = new RunnerScheduler() {
public void schedule(Runnable childStatement) {
childStatement.run();
}
@@ -59,9 +71,10 @@ public Corollaries(Class<?> klass) throws InitializationError {
@Override
public void sort(Sorter sorter) {
- fSorter= sorter;
- for (FrameworkMethod each : getFilteredChildren())
+ fSorter = sorter;
+ for (FrameworkMethod each : getFilteredChildren()) {
sortChild(each);
+ }
Collections.sort(getFilteredChildren(), comparator());
assumptionSort(getFilteredChildren());
}
@@ -78,20 +91,6 @@ public int compare(FrameworkMethod o1, FrameworkMethod o2) {
};
}
- @Override
- public void run(final RunNotifier notifier) {
- EachTestNotifier testNotifier= new EachTestNotifier(notifier, getDescription());
- try {
- Statement statement= classBlock(notifier);
- statement.evaluate();
- } catch (AssumptionViolatedException e) {
- testNotifier.fireTestIgnored();
- } catch (StoppedByUserException e) {
- throw e;
- } catch (Throwable e) {
- testNotifier.addFailure(e);
- }
- }
/**
* Returns a {@link Statement}: Call {@link #runChild(Object, RunNotifier)}
* on each object returned by {@link #getChildren()} (subject to any imposed
@@ -124,12 +123,13 @@ public void testIgnored(Description description) throws Exception {
}
};
notifier.addListener(l);
- for (final FrameworkMethod each : getFilteredChildren())
- fScheduler.schedule(new Runnable() {
+ for (final FrameworkMethod each : getFilteredChildren()) {
+ fScheduler.schedule(new Runnable() {
public void run() {
Corollaries.this.runChild(each, notifier, invalidAssumptions);
}
});
+ }
fScheduler.finished();
}
@@ -137,7 +137,7 @@ protected void runChild(final FrameworkMethod method, RunNotifier notifier, Set<
Assumes assumptions = method.getAnnotation(Assumes.class);
boolean invalidAssumption = false;
if (assumptions != null) {
- for (String assumption: assumptions.value()) {
+ for (String assumption : assumptions.value()) {
if (invalidAssumptions.contains(assumption)) {
invalidAssumption = true;
break;
@@ -180,29 +180,30 @@ private void assumptionSort(List<FrameworkMethod> methods) {
}
}
}
- Collections.sort(getFilteredChildren(), new AssumptionComparator());
}
@Override
public Description getDescription() {
- Description description= Description.createSuiteDescription(getName(),
+ Description description = Description.createSuiteDescription(getName(),
getTestClass().getAnnotations());
- for (FrameworkMethod child : getFilteredChildren())
+ for (FrameworkMethod child : getFilteredChildren()) {
description.addChild(describeChild(child));
+ }
return description;
}
public void filter(Filter filter) throws NoTestsRemainException {
for (Iterator<FrameworkMethod> iter = getFilteredChildren().iterator(); iter.hasNext(); ) {
FrameworkMethod each = iter.next();
- if (shouldRun(filter, each))
+ if (shouldRun(filter, each)) {
try {
filter.apply(each);
} catch (NoTestsRemainException e) {
iter.remove();
}
- else
+ } else {
iter.remove();
+ }
}
if (getFilteredChildren().isEmpty()) {
throw new NoTestsRemainException();
@@ -221,33 +222,4 @@ public void setScheduler(RunnerScheduler scheduler) {
this.fScheduler = scheduler;
}
- private class AssumptionComparator implements Comparator<FrameworkMethod> {
-
- public int compare(FrameworkMethod o1, FrameworkMethod o2) {
- if (assumed(o2.getAnnotation(Assumes.class), o1.getName())) {
- if (!assumed(o1.getAnnotation(Assumes.class), o2.getName())) {
- return -1;
- }
- } else {
- if (assumed(o1.getAnnotation(Assumes.class), o2.getName())) {
- return 1;
- }
- }
- return 0;
- }
-
- private boolean assumed(Assumes assumes, String predicate) {
- if (assumes == null) {
- return false;
- }
- for (String s : assumes.value()) {
- if (s.equals(predicate)) {
- return true;
- }
- }
- return false;
- }
-
- }
-
}
View
254 assumes/src/test/java/org/junit/contrib/assumes/AssumesTest.java
@@ -1,6 +1,22 @@
+/*
+ * The copyright holders of this work license this file to You under
+ * the Apache License, Version 2.0 (the "License"); you may not use this
+ * file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.junit.contrib.assumes;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.Description;
@@ -8,11 +24,13 @@
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
+import org.junit.runner.manipulation.Filter;
import java.util.Comparator;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import static org.junit.Assume.assumeThat;
@RunWith(Enclosed.class)
public class AssumesTest {
@@ -64,15 +82,18 @@ public void runsInReverse() throws Exception {
public void a() {
log += "a";
}
+
@Test
public void b() {
log += "b";
}
+
@Test
public void c() {
log += "c";
assertThat(true, is(false));
}
+
@Test
public void d() {
log += "d";
@@ -111,15 +132,18 @@ public void runsInReverse() throws Exception {
public void a() {
log += "a";
}
+
@Test
public void b() {
log += "b";
}
+
@Test
public void c() {
log += "c";
assertThat(true, is(false));
}
+
@Test
public void d() {
log += "d";
@@ -159,15 +183,18 @@ public void runsInReverse() throws Exception {
public void a() {
log += "a";
}
+
@Test
@Assumes("c")
public void b() {
log += "b";
}
+
@Test
public void c() {
log += "c";
}
+
@Test
public void d() {
log += "d";
@@ -193,14 +220,17 @@ public void doesNotInfiniteLoopInReverse() throws Exception {
@Test
public void a() {
}
+
@Test
@Assumes("c")
public void b() {
}
+
@Test
@Assumes("d")
public void c() {
}
+
@Test
@Assumes("b")
public void d() {
@@ -226,14 +256,17 @@ public void doesNotInfiniteLoopInReverse() throws Exception {
@Test
public void a() {
}
+
@Test
@Assumes("c")
public void b() {
}
+
@Test
@Assumes("b")
public void c() {
}
+
@Test
public void d() {
}
@@ -272,16 +305,195 @@ public void runsInReverse() throws Exception {
public void a() {
log += "a";
}
+
@Test
@Assumes("c")
public void b() {
log += "b";
}
+
@Test
public void c() {
log += "c";
assertThat(true, is(false));
}
+
+ @Test
+ public void d() {
+ log += "d";
+ }
+ }
+ }
+
+ public static class UntestedAssumptionsAreAssumedTrue {
+ private static String log;
+
+ @Before
+ public void setUp() {
+ log = "";
+ }
+
+ @Test
+ public void runsInOrder() throws Exception {
+ Request request = Request.classes(Artifact.class).sortWith(forward()).filterWith(new Filter() {
+ @Override
+ public boolean shouldRun(Description description) {
+ return !"c".equals(description.getMethodName());
+ }
+
+ @Override
+ public String describe() {
+ return "Anything but c";
+ }
+ });
+ Result result = new JUnitCore().run(request);
+ assertThat(log, is("abd"));
+ assertThat(result.getFailureCount(), is(0));
+ assertThat(result.getIgnoreCount(), is(0));
+ }
+
+ @Test
+ public void runsInReverse() throws Exception {
+ Request request = Request.classes(Artifact.class).sortWith(backward()).filterWith(new Filter() {
+ @Override
+ public boolean shouldRun(Description description) {
+ return !"c".equals(description.getMethodName());
+ }
+
+ @Override
+ public String describe() {
+ return "Anything but c";
+ }
+ });
+ Result result = new JUnitCore().run(request);
+ assertThat(log, is("dba"));
+ assertThat(result.getFailureCount(), is(0));
+ assertThat(result.getIgnoreCount(), is(0));
+ }
+
+ @RunWith(Corollaries.class)
+ public static class Artifact {
+ @Test
+ public void a() {
+ log += "a";
+ }
+
+ @Test
+ @Assumes("c")
+ public void b() {
+ log += "b";
+ }
+
+ @Test
+ public void c() {
+ log += "c";
+ assertThat(true, is(false));
+ }
+
+ @Test
+ public void d() {
+ log += "d";
+ }
+ }
+ }
+
+ public static class IgnoredAssumptionsAreSkipped {
+ private static String log;
+
+ @Before
+ public void setUp() {
+ log = "";
+ }
+
+ @Test
+ public void runsInOrder() throws Exception {
+ Request request = Request.classes(Artifact.class).sortWith(forward());
+ Result result = new JUnitCore().run(request);
+ assertThat(log, is("ad"));
+ assertThat(result.getFailureCount(), is(0));
+ assertThat(result.getIgnoreCount(), is(2));
+ }
+
+ @Test
+ public void runsInReverse() throws Exception {
+ Request request = Request.classes(Artifact.class).sortWith(backward());
+ Result result = new JUnitCore().run(request);
+ assertThat(log, is("da"));
+ assertThat(result.getFailureCount(), is(0));
+ assertThat(result.getIgnoreCount(), is(2));
+ }
+
+ @RunWith(Corollaries.class)
+ public static class Artifact {
+ @Test
+ public void a() {
+ log += "a";
+ }
+
+ @Test
+ @Assumes("c")
+ public void b() {
+ log += "b";
+ }
+
+ @Test
+ @Ignore("for now")
+ public void c() {
+ log += "c";
+ }
+
+ @Test
+ public void d() {
+ log += "d";
+ }
+ }
+ }
+
+ public static class UntestedAssumptionsAreSkipped {
+ private static String log;
+
+ @Before
+ public void setUp() {
+ log = "";
+ }
+
+ @Test
+ public void runsInOrder() throws Exception {
+ Request request = Request.classes(Artifact.class).sortWith(forward());
+ Result result = new JUnitCore().run(request);
+ assertThat(log, is("acd"));
+ assertThat(result.getFailureCount(), is(0));
+ assertThat(result.getIgnoreCount(), is(1));
+ }
+
+ @Test
+ public void runsInReverse() throws Exception {
+ Request request = Request.classes(Artifact.class).sortWith(backward());
+ Result result = new JUnitCore().run(request);
+ assertThat(log, is("dca"));
+ assertThat(result.getFailureCount(), is(0));
+ assertThat(result.getIgnoreCount(), is(1));
+ }
+
+ @RunWith(Corollaries.class)
+ public static class Artifact {
+ @Test
+ public void a() {
+ log += "a";
+ }
+
+ @Test
+ @Assumes("c")
+ public void b() {
+ log += "b";
+ }
+
+ @Test
+ public void c() {
+ log += "c";
+ assumeThat(true, is(false));
+ }
+
@Test
public void d() {
log += "d";
@@ -321,46 +533,56 @@ public void runsInReverse() throws Exception {
public void a() {
log += "a";
}
+
@Test
@Assumes("c")
public void b() {
log += "b";
}
+
@Test
public void c() {
log += "c";
}
+
@Test
- @Assumes({"k","h"})
+ @Assumes({"k", "h"})
public void d() {
log += "d";
}
+
@Test
public void e() {
log += "e";
}
+
@Test
@Assumes("c")
public void f() {
log += "f";
}
+
@Test
public void g() {
log += "g";
}
+
@Test
- @Assumes({"c","b"})
+ @Assumes({"c", "b"})
public void h() {
log += "h";
}
+
@Test
public void i() {
log += "i";
}
+
@Test
public void j() {
log += "j";
}
+
@Test
public void k() {
log += "k";
@@ -400,47 +622,57 @@ public void runsInReverse() throws Exception {
public void a() {
log += "a";
}
+
@Test
@Assumes("c")
public void b() {
log += "b";
}
+
@Test
public void c() {
log += "c";
assertThat(true, is(false));
}
+
@Test
- @Assumes({"k","h"})
+ @Assumes({"k", "h"})
public void d() {
log += "d";
}
+
@Test
public void e() {
log += "e";
}
+
@Test
@Assumes("c")
public void f() {
log += "f";
}
+
@Test
public void g() {
log += "g";
}
+
@Test
- @Assumes({"c","b"})
+ @Assumes({"c", "b"})
public void h() {
log += "h";
}
+
@Test
public void i() {
log += "i";
}
+
@Test
public void j() {
log += "j";
}
+
@Test
public void k() {
log += "k";
@@ -480,47 +712,57 @@ public void runsInReverse() throws Exception {
public void a() {
log += "a";
}
+
@Test
@Assumes("c")
public void b() {
log += "b";
assertThat(true, is(false));
}
+
@Test
public void c() {
log += "c";
}
+
@Test
- @Assumes({"k","h"})
+ @Assumes({"k", "h"})
public void d() {
log += "d";
}
+
@Test
public void e() {
log += "e";
}
+
@Test
@Assumes("c")
public void f() {
log += "f";
}
+
@Test
public void g() {
log += "g";
}
+
@Test
- @Assumes({"c","b"})
+ @Assumes({"c", "b"})
public void h() {
log += "h";
}
+
@Test
public void i() {
log += "i";
}
+
@Test
public void j() {
log += "j";
}
+
@Test
public void k() {
log += "k";
Please sign in to comment.
Something went wrong with that request. Please try again.