diff options
author | Pete Bentley <prb@google.com> | 2021-03-03 15:09:30 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-03-03 15:09:30 +0000 |
commit | 6b74b10fa73184d2d997583b5eded4a23324b64d (patch) | |
tree | 10923530939981dd1dc5f44ceb6316fb9442428a /src/main/java/org/junit/rules/RuleChain.java | |
parent | e4ffdd500e7ca130574414d7ecf208a147167249 (diff) | |
parent | a9a7715d84046efd231e038b0dc2f551daf61701 (diff) | |
download | junit-6b74b10fa73184d2d997583b5eded4a23324b64d.tar.gz |
Merge changes I578a2676,I4b37c2d0,Id1e2d638,I1ebe37da,I6135799c am: b6446bec0a am: fcd81b3e3f am: c7a6d4ec7d am: a9a7715d84
Original change: https://android-review.googlesource.com/c/platform/external/junit/+/1613132
MUST ONLY BE SUBMITTED BY AUTOMERGER
Change-Id: I70a6b61e69982b66be9f6d7c2e7df5afc91bf382
Diffstat (limited to 'src/main/java/org/junit/rules/RuleChain.java')
-rw-r--r-- | src/main/java/org/junit/rules/RuleChain.java | 50 |
1 files changed, 33 insertions, 17 deletions
diff --git a/src/main/java/org/junit/rules/RuleChain.java b/src/main/java/org/junit/rules/RuleChain.java index f43d8f5..bf93aae 100644 --- a/src/main/java/org/junit/rules/RuleChain.java +++ b/src/main/java/org/junit/rules/RuleChain.java @@ -4,26 +4,34 @@ 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 RuleChain rule allows ordering of TestRules. You create a + * The {@code RuleChain} can be used for creating composite rules. You create a * {@code RuleChain} with {@link #outerRule(TestRule)} and subsequent calls of * {@link #around(TestRule)}: * * <pre> - * public static class UseRuleChain { - * @Rule - * public RuleChain chain= RuleChain - * .outerRule(new LoggingRule("outer rule") - * .around(new LoggingRule("middle rule") - * .around(new LoggingRule("inner rule"); + * 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 { + * @Rule + * public final TestRule extendedLogging = CompositeRules.extendedLogging(); * - * @Test - * public void example() { - * assertTrue(true); - * } + * @Test + * public void example() { + * assertTrue(true); + * } * } * </pre> * @@ -38,6 +46,13 @@ 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 { @@ -72,13 +87,17 @@ public class RuleChain implements TestRule { } /** - * Create a new {@code RuleChain}, which encloses the {@code nextRule} with + * Create a new {@code RuleChain}, which encloses the given {@link TestRule} with * the rules of the current {@code RuleChain}. * - * @param enclosedRule the rule to enclose. + * @param enclosedRule the rule to enclose; must not be {@code null}. * @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); @@ -89,9 +108,6 @@ public class RuleChain implements TestRule { * {@inheritDoc} */ public Statement apply(Statement base, Description description) { - for (TestRule each : rulesStartingWithInnerMost) { - base = each.apply(base, description); - } - return base; + return new RunRules(base, rulesStartingWithInnerMost, description); } }
\ No newline at end of file |