aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/rules/RuleChain.java
blob: 8af3c0571f3db01ffb8331029969aa647f08513e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
 * 
 */
package org.junit.rules;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/**
 * 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 static class UseRuleChain {
 * 	&#064;Rule
 * 	public TestRule chain= RuleChain
 * 	                       .outerRule(new LoggingRule("outer rule")
 * 	                       .around(new LoggingRule("middle rule")
 * 	                       .around(new LoggingRule("inner rule");
 * 
 * 	&#064;Test
 * 	public void example() {
 * 		assertTrue(true);
 * 	}
 * }
 * </pre>
 * 
 * writes the log
 * 
 * <pre>
 * starting outer rule
 * starting middle rule
 * starting inner rule
 * finished inner rule
 * finished middle rule
 * finished outer rule
 * </pre>
 */
public class RuleChain implements TestRule {
	private static final RuleChain EMPTY_CHAIN= new RuleChain(
			Collections.<TestRule> emptyList());

	private List<TestRule> rulesStartingWithInnerMost;

	/**
	 * Returns a {@code RuleChain} without a {@link TestRule}. This method may
	 * be the starting point of a {@code RuleChain}.
	 * 
	 * @return a {@code RuleChain} without a {@link TestRule}.
	 */
	public static RuleChain emptyRuleChain() {
		return EMPTY_CHAIN;
	}

	/**
	 * Returns a {@code RuleChain} with a single {@link TestRule}. This method
	 * is the usual starting point of a {@code RuleChain}.
	 * 
	 * @param outerRule
	 *            the outer rule of the {@code RuleChain}.
	 * @return a {@code RuleChain} with a single {@link TestRule}.
	 */
	public static RuleChain outerRule(TestRule outerRule) {
		return emptyRuleChain().around(outerRule);
	}

	private RuleChain(List<TestRule> rules) {
		this.rulesStartingWithInnerMost= rules;
	}

	/**
	 * Create a new {@code RuleChain}, which encloses the {@code nextRule} with
	 * the rules of the current {@code RuleChain}.
	 * 
	 * @param enclosedRule
	 *            the rule to enclose.
	 * @return a new {@code RuleChain}.
	 */
	public RuleChain around(TestRule enclosedRule) {
		List<TestRule> rulesOfNewChain= new ArrayList<TestRule>();
		rulesOfNewChain.add(enclosedRule);
		rulesOfNewChain.addAll(rulesStartingWithInnerMost);
		return new RuleChain(rulesOfNewChain);
	}

	/**
	 * {@inheritDoc}
	 */
	public Statement apply(Statement base, Description description) {
		for (TestRule each : rulesStartingWithInnerMost)
			base= each.apply(base, description);
		return base;
	}
}