Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add the ability to optionaly enable/disable Ignore annotation at runt…

…ime by adding a set of conditions to an @Ignore annotation.
  • Loading branch information...
commit 32d3af8d29fe278df5da85835389b98245676d02 1 parent b4180bf
@lacostej authored
View
7 src/main/java/junit/framework/JUnit4TestAdapter.java
@@ -3,6 +3,7 @@
import java.util.List;
import org.junit.Ignore;
+import org.junit.internal.IgnoreUtil;
import org.junit.runner.Describable;
import org.junit.runner.Description;
import org.junit.runner.Request;
@@ -55,7 +56,7 @@ public Description getDescription() {
}
private Description removeIgnored(Description description) {
- if (isIgnored(description))
+ if (IgnoreUtil.isIgnored(description))
return Description.EMPTY;
Description result = description.childlessCopy();
for (Description each : description.getChildren()) {
@@ -66,10 +67,6 @@ private Description removeIgnored(Description description) {
return result;
}
- private boolean isIgnored(Description description) {
- return description.getAnnotation(Ignore.class) != null;
- }
-
@Override
public String toString() {
return fNewTestClass.getName();
View
6 src/main/java/org/junit/Ignore.java
@@ -35,5 +35,9 @@
/**
* The optional reason why the test is ignored.
*/
- String value() default "";
+ String value() default "";
+ /**
+ * The optional conditions class whose instance allows to dynamically ignore tests. All conditions must be true for the ignore to have effect.
+ */
+ Class<? extends RuntimeCondition>[] ifTrue() default {};
}
View
16 src/main/java/org/junit/RuntimeCondition.java
@@ -0,0 +1,16 @@
+/**
+ *
+ */
+package org.junit;
+
+import org.junit.runner.Description;
+import org.junit.runners.model.FrameworkMethod;
+
+/**
+ * @author jerome@coffeebreaks.org
+ * @since 2/11/11 8:14 AM
+ */
+public interface RuntimeCondition {
+ boolean isTrue(FrameworkMethod method);
+ boolean isTrue(Description description);
+}
View
58 src/main/java/org/junit/internal/IgnoreUtil.java
@@ -0,0 +1,58 @@
+package org.junit.internal;
+
+import org.junit.Ignore;
+import org.junit.RuntimeCondition;
+import org.junit.runner.Description;
+import org.junit.runners.model.FrameworkMethod;
+
+/**
+ * Created by IntelliJ IDEA.
+ *
+ * @author jerome@coffeebreaks.org
+ * @since 2/11/11 2:18 PM
+ */
+public class IgnoreUtil {
+
+ public static boolean isIgnored(FrameworkMethod method) {
+ Ignore annotation = method.getAnnotation(Ignore.class);
+ return annotation != null && isTrue(annotation.ifTrue(), method);
+ }
+
+ private static boolean isTrue(Class<? extends RuntimeCondition>[] conditions, FrameworkMethod method) {
+ if (conditions == null || conditions.length == 0) {
+ return true;
+ }
+ for(Class<? extends RuntimeCondition> condition : conditions){
+ try{
+ if (! condition.newInstance().isTrue(method)) {
+ return false;
+ }
+ } catch(Exception e){
+ throw new RuntimeException(e);
+ }
+ }
+ return true;
+ }
+
+ public static boolean isIgnored(Description description) {
+ Ignore annotation = description.getAnnotation(Ignore.class);
+ return annotation != null && isTrue(annotation.ifTrue(), description);
+ }
+
+ private static boolean isTrue(Class<? extends RuntimeCondition>[] conditions, Description description) {
+ if (conditions == null || conditions.length == 0) {
+ return true;
+ }
+ for(Class<? extends RuntimeCondition> condition : conditions){
+ try{
+ if (! condition.newInstance().isTrue(description)) {
+ return false;
+ }
+ } catch(Exception e){
+ throw new RuntimeException(e);
+ }
+ }
+ return true;
+ }
+
+}
View
2  src/main/java/org/junit/internal/builders/IgnoredBuilder.java
@@ -4,6 +4,8 @@
package org.junit.internal.builders;
import org.junit.Ignore;
+import org.junit.internal.IgnoreUtil;
+import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runners.model.RunnerBuilder;
View
6 src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java
@@ -8,8 +8,10 @@
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
+import org.junit.RuntimeCondition;
import org.junit.Test;
import org.junit.Test.None;
+import org.junit.internal.IgnoreUtil;
import org.junit.internal.runners.model.ReflectiveCallable;
import org.junit.internal.runners.statements.ExpectException;
import org.junit.internal.runners.statements.Fail;
@@ -64,14 +66,14 @@ public BlockJUnit4ClassRunner(Class<?> klass) throws InitializationError {
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
Description description= describeChild(method);
- if (method.getAnnotation(Ignore.class) != null) {
+ if (IgnoreUtil.isIgnored(method)) {
notifier.fireTestIgnored(description);
} else {
runLeaf(methodBlock(method), description, notifier);
}
}
- @Override
+ @Override
protected Description describeChild(FrameworkMethod method) {
return Description.createTestDescription(getTestClass().getJavaClass(),
testName(method), method.getAnnotations());
View
7 src/test/java/org/junit/tests/description/AnnotatedDescriptionTest.java
@@ -6,8 +6,10 @@
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
+import java.util.ArrayList;
import org.junit.Ignore;
+import org.junit.RuntimeCondition;
import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.Request;
@@ -78,7 +80,10 @@ public void childlessCopyOfDescriptionStillHasAnnotations() {
@Test
public void characterizeCreatingMyOwnAnnotation() {
Annotation annotation= new Ignore() {
- public String value() {
+ public Class<? extends RuntimeCondition>[] ifTrue() {
+ return null; // shortcut... null never
+ }
+ public String value() {
return "message";
}
View
37 src/test/java/org/junit/tests/running/classes/IgnoreWithConditionClassTest.java
@@ -0,0 +1,37 @@
+package org.junit.tests.running.classes;
+
+import org.junit.Ignore;
+import org.junit.RuntimeCondition;
+import org.junit.Test;
+import org.junit.runner.Description;
+import org.junit.runner.JUnitCore;
+import org.junit.runner.Result;
+import org.junit.runners.model.FrameworkMethod;
+
+import static org.junit.Assert.*;
+
+public class IgnoreWithConditionClassTest {
+ public static class AlwaysTrue implements RuntimeCondition {
+ public boolean isTrue(FrameworkMethod method) {
+ return true;
+ }
+ public boolean isTrue(Description description) {
+ return true;
+ }
+ }
+ @Ignore(ifTrue = AlwaysTrue.class) public static class IgnoreMeAlso {
+ @Test public void iFail() {
+ fail();
+ }
+
+ @Test public void iFailToo() {
+ fail();
+ }
+ }
+
+ @Test public void ignoreClass() {
+ Result result= JUnitCore.runClasses(IgnoreMeAlso.class);
+ assertEquals(0, result.getFailureCount());
+ assertEquals(1, result.getIgnoreCount());
+ }
+}
View
20 src/test/java/org/junit/tests/running/methods/TestMethodTest.java
@@ -13,10 +13,13 @@
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
+import org.junit.RuntimeCondition;
import org.junit.Test;
+import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runners.BlockJUnit4ClassRunner;
+import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
public class TestMethodTest {
@@ -92,23 +95,34 @@ private EverythingWrong() {}
return Collections.emptyList();
}
+ public static class MethodStartsWithxxx implements RuntimeCondition {
+ public boolean isTrue(FrameworkMethod method) {
+ return method.getName().startsWith("xxx");
+ }
+ public boolean isTrue(Description description) {
+ return true;
+ }
+ }
+
static public class IgnoredTest {
@Test public void valid() {}
@Ignore @Test public void ignored() {}
@Ignore("For testing purposes") @Test public void withReason() {}
+ @Ignore(ifTrue = MethodStartsWithxxx.class) @Test public void xxxIgnored() {}
+ @Ignore(ifTrue = MethodStartsWithxxx.class) @Test public void yyyNotIgnored() {}
}
@Test public void ignoreRunner() {
JUnitCore runner= new JUnitCore();
Result result= runner.run(IgnoredTest.class);
- assertEquals(2, result.getIgnoreCount());
- assertEquals(1, result.getRunCount());
+ assertEquals(3, result.getIgnoreCount());
+ assertEquals(2, result.getRunCount());
}
@Test public void compatibility() {
TestResult result= new TestResult();
new JUnit4TestAdapter(IgnoredTest.class).run(result);
- assertEquals(1, result.runCount());
+ assertEquals(2, result.runCount());
}
public static class Confused {
Please sign in to comment.
Something went wrong with that request. Please try again.