Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian W. Damus2014-03-14 15:02:32 -0400
committerChristian W. Damus2014-03-14 15:07:40 -0400
commitd25fd775ab2c111532c29074c6152c06c1991d8a (patch)
treed03fe3ed5b5555d3cd0789a2a0e391d7b201da8b /tests/junit
parent0d1438a6bce3f67bcba4793c5943d6c3db5b8b43 (diff)
downloadorg.eclipse.papyrus-d25fd775ab2c111532c29074c6152c06c1991d8a.tar.gz
org.eclipse.papyrus-d25fd775ab2c111532c29074c6152c06c1991d8a.tar.xz
org.eclipse.papyrus-d25fd775ab2c111532c29074c6152c06c1991d8a.zip
430023: [Papyrus Tests] Papyrus shall propose different levels of testing, to allow test-driven development
https://bugs.eclipse.org/bugs/show_bug.cgi?id=430023 Add support for the @Conditional annotation to the ClassificationRunner.
Diffstat (limited to 'tests/junit')
-rw-r--r--tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/classification/ClassificationRunner.java65
-rw-r--r--tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/ConditionRule.java18
2 files changed, 75 insertions, 8 deletions
diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/classification/ClassificationRunner.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/classification/ClassificationRunner.java
index 34b8867d2cc..24e7c324dd4 100644
--- a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/classification/ClassificationRunner.java
+++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/classification/ClassificationRunner.java
@@ -1,5 +1,5 @@
/*****************************************************************************
- * Copyright (c) 2014 CEA LIST.
+ * Copyright (c) 2014 CEA LIST and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -8,9 +8,13 @@
*
* Contributors:
* Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
+ * Christian W. Damus (CEA) - add support for conditional tests
+ *
*****************************************************************************/
package org.eclipse.papyrus.junit.utils.classification;
+import org.eclipse.papyrus.junit.utils.rules.ConditionRule;
+import org.eclipse.papyrus.junit.utils.rules.Conditional;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
@@ -18,7 +22,7 @@ import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
/**
- * A Test Runner which is aware of Classification-related annotations.
+ * A Test Runner which is aware of Classification-related annotations and {@link Conditional @Conditional} tests.
*
* It ignores the test methods according to their annotations, and the policy defined
* in {@link ClassificationConfig}
@@ -26,19 +30,22 @@ import org.junit.runners.model.InitializationError;
*
* @see {@link ClassificationConfig}
* @see {@link TestCategory}
+ * @see Conditional
*
* @author Camille Letavernier
*
*/
public class ClassificationRunner extends BlockJUnit4ClassRunner {
+ private final ThreadLocal<Object> preparedTest = new ThreadLocal<Object>();
+
public ClassificationRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
- if(ClassificationConfig.shouldRun(method.getAnnotations())) {
+ if(ClassificationConfig.shouldRun(method.getAnnotations()) && conditionSatisfied(method)) {
super.runChild(method, notifier);
} else {
Description description = describeChild(method);
@@ -46,4 +53,56 @@ public class ClassificationRunner extends BlockJUnit4ClassRunner {
}
}
+ @Override
+ protected Object createTest() throws Exception {
+ // Look for a prepared test instance
+ Object result = preparedTest.get();
+ if(result != null) {
+ // We won't need this test instance again
+ clearPreparedTest();
+ } else {
+ result = basicCreateTest();
+ }
+
+ return result;
+ }
+
+ protected final Object basicCreateTest() throws Exception {
+ return super.createTest();
+ }
+
+ protected final Object prepareTest() throws Exception {
+ // Prepare the test instance and stash it to return on the next invocation
+ Object result = basicCreateTest();
+ preparedTest.set(result);
+ return result;
+ }
+
+ protected final void clearPreparedTest() {
+ preparedTest.remove();
+ }
+
+ private boolean conditionSatisfied(FrameworkMethod method) {
+ boolean result = true;
+
+ // Does this test declare some precondition?
+ Conditional conditional = method.getAnnotation(Conditional.class);
+ if(conditional != null) {
+ try {
+ // We need the test instance to invoke the condition on it, so prepare it now
+ Object test = prepareTest();
+ result = ConditionRule.testCondition(method.getMethod().getDeclaringClass(), conditional, test);
+ } catch (Throwable t) {
+ // If we couldn't create the test, then we should just ignore it
+ result = false;
+ } finally {
+ if(!result) {
+ // We won't be running the test, so forget the prepared instance (if any)
+ clearPreparedTest();
+ }
+ }
+ }
+
+ return result;
+ }
}
diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/ConditionRule.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/ConditionRule.java
index 961d38552ce..46dac013ec3 100644
--- a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/ConditionRule.java
+++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/ConditionRule.java
@@ -19,6 +19,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
+import org.eclipse.papyrus.junit.utils.classification.ClassificationRunner;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.rules.MethodRule;
@@ -27,13 +28,20 @@ import org.junit.runners.model.Statement;
/**
+ * <p>
* A JUnit {@linkplain Rule rule} that skips tests annotated as {@linkplain Conditional @Conditional} when their {@linkplain Condition conditions} are
* not satisfied. This model is compatible with the JUnit execution and reporting in the Papyrus Hudson build, which does not understand
* {@linkplain Assume#assumeThat(Object, org.hamcrest.Matcher) assumption failures} and reports them as test failures.
+ * </p>
+ * <p>
+ * <b>Note</b> that this is not ideal, as tests will be reported in the JDT's JUnit view as passes, when in fact they should be reported as skipped.
+ * It is better to use the {@link ClassificationRunner} if possible (such as when not using some other test runner).
+ * </p>
*
* @see Conditional
* @see Condition
* @see Rule
+ * @see ClassificationRunner
*/
public class ConditionRule implements MethodRule {
@@ -53,7 +61,7 @@ public class ConditionRule implements MethodRule {
};
}
- protected boolean testCondition(Class<?> testClass, Conditional conditional, Object test) {
+ public static boolean testCondition(Class<?> testClass, Conditional conditional, Object test) {
boolean result = true;
if(conditional != null) {
@@ -73,7 +81,7 @@ public class ConditionRule implements MethodRule {
return result;
}
- protected Method findConditionMethod(Class<?> testClass, Conditional conditional) {
+ static Method findConditionMethod(Class<?> testClass, Conditional conditional) {
Method result = null;
for(Method next : testClass.getDeclaredMethods()) {
@@ -87,7 +95,7 @@ public class ConditionRule implements MethodRule {
return result;
}
- protected Field findConditionField(Class<?> testClass, Conditional conditional) {
+ static Field findConditionField(Class<?> testClass, Conditional conditional) {
Field result = null;
for(Field next : testClass.getDeclaredFields()) {
@@ -101,7 +109,7 @@ public class ConditionRule implements MethodRule {
return result;
}
- protected boolean evaluateCondition(Method conditionMethod, Object test) {
+ static boolean evaluateCondition(Method conditionMethod, Object test) {
boolean result = true;
try {
@@ -116,7 +124,7 @@ public class ConditionRule implements MethodRule {
return result;
}
- protected boolean evaluateCondition(Field conditionField, Object test) {
+ static boolean evaluateCondition(Field conditionField, Object test) {
boolean result = true;
try {

Back to the top