aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/rules/RuleChain.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/junit/rules/RuleChain.java')
-rw-r--r--src/main/java/org/junit/rules/RuleChain.java50
1 files changed, 17 insertions, 33 deletions
diff --git a/src/main/java/org/junit/rules/RuleChain.java b/src/main/java/org/junit/rules/RuleChain.java
index bf93aae..f43d8f5 100644
--- a/src/main/java/org/junit/rules/RuleChain.java
+++ b/src/main/java/org/junit/rules/RuleChain.java
@@ -4,34 +4,26 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
-import org.junit.Rule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
- * The {@code RuleChain} can be used for creating composite rules. You create a
+ * The RuleChain rule allows ordering of TestRules. You create a
* {@code RuleChain} with {@link #outerRule(TestRule)} and subsequent calls of
* {@link #around(TestRule)}:
*
* <pre>
- * public abstract class CompositeRules {
- * public static TestRule extendedLogging() {
- * return RuleChain.outerRule(new LoggingRule("outer rule"))
- * .around(new LoggingRule("middle rule"))
- * .around(new LoggingRule("inner rule"));
- * }
- * }
- * </pre>
- *
- * <pre>
- * public class UseRuleChain {
- * &#064;Rule
- * public final TestRule extendedLogging = CompositeRules.extendedLogging();
+ * public static class UseRuleChain {
+ * &#064;Rule
+ * public RuleChain chain= RuleChain
+ * .outerRule(new LoggingRule("outer rule")
+ * .around(new LoggingRule("middle rule")
+ * .around(new LoggingRule("inner rule");
*
- * &#064;Test
- * public void example() {
- * assertTrue(true);
- * }
+ * &#064;Test
+ * public void example() {
+ * assertTrue(true);
+ * }
* }
* </pre>
*
@@ -46,13 +38,6 @@ import org.junit.runners.model.Statement;
* finished outer rule
* </pre>
*
- * In older versions of JUnit (before 4.13) {@code RuleChain} was used for
- * ordering rules. We recommend to not use it for this purpose anymore. You can
- * use the attribute {@code order} of the annotation {@link Rule#order() Rule}
- * or {@link org.junit.ClassRule#order() ClassRule} for ordering rules.
- *
- * @see org.junit.Rule#order()
- * @see org.junit.ClassRule#order()
* @since 4.10
*/
public class RuleChain implements TestRule {
@@ -87,17 +72,13 @@ public class RuleChain implements TestRule {
}
/**
- * Create a new {@code RuleChain}, which encloses the given {@link TestRule} with
+ * Create a new {@code RuleChain}, which encloses the {@code nextRule} with
* the rules of the current {@code RuleChain}.
*
- * @param enclosedRule the rule to enclose; must not be {@code null}.
+ * @param enclosedRule the rule to enclose.
* @return a new {@code RuleChain}.
- * @throws NullPointerException if the argument {@code enclosedRule} is {@code null}
*/
public RuleChain around(TestRule enclosedRule) {
- if (enclosedRule == null) {
- throw new NullPointerException("The enclosed rule must not be null");
- }
List<TestRule> rulesOfNewChain = new ArrayList<TestRule>();
rulesOfNewChain.add(enclosedRule);
rulesOfNewChain.addAll(rulesStartingWithInnerMost);
@@ -108,6 +89,9 @@ public class RuleChain implements TestRule {
* {@inheritDoc}
*/
public Statement apply(Statement base, Description description) {
- return new RunRules(base, rulesStartingWithInnerMost, description);
+ for (TestRule each : rulesStartingWithInnerMost) {
+ base = each.apply(base, description);
+ }
+ return base;
}
} \ No newline at end of file