-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBranchRule.java
158 lines (148 loc) · 4.18 KB
/
BranchRule.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package junittestbranches;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import org.junit.internal.runners.model.MultipleFailureException;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
/**
* This is a JUnit MethodRule that allows "branching" of the execution of a test method. It supports dead end
* branches ( through {@link #branch(Runnable)}), that cut execution at their end, and merge back ones ( through
* {@link #branchAndMerge(Runnable)}), that continue after the branch point.
*
* Branches can be nested. All branches are visited exactly once, in depth-first order.
*
* @author marciomazza
*/
public class BranchRule implements MethodRule {
/**
* Throwing an Exception seems to be the only control flow option we have to "cut" execution at the end of a
* branch. This exception is used signal this.
*
* It is thrown in {@link #branch(Runnable)} and caught in the loop in
* {@link #apply(Statement, FrameworkMethod, Object)}
*/
private class BranchDoneSignalException extends RuntimeException {
}
private LinkedHashSet<Class<?>> completedBranches;
private boolean oneBranchCompleted;
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
completedBranches = new LinkedHashSet<Class<?>>();
while (true) {
oneBranchCompleted = false;
try {
base.evaluate();
if ( ! oneBranchCompleted) {
break;
}
} catch (BranchDoneSignalException e) {
continue;
} catch (MultipleFailureException multiple) {
// remove our cut signaling exception if necessary
for (Throwable failure : multiple.getFailures()) {
if (BranchDoneSignalException.class.equals(failure.getClass())) {
List<Throwable> withoutSignal = new ArrayList<Throwable>(multiple.getFailures());
withoutSignal.remove(failure);
throw new MultipleFailureException(withoutSignal);
}
}
throw multiple;
}
}
}
};
}
/**
* Executes a {@code Runnable} as a dead end branch, i.e, that cuts execution after its completion.
*
* <pre>
* public class BranchRuleTest {
*
* @Rule
* public BranchRule brancher = new BranchRule();
*
* @Test
* public void simpleBranch() throws Exception {
*
* System.out.println("restart");
* brancher.branch(new Runnable() {
* public void run() {
* System.out.println("BRANCH-END");
* }
* });
* System.out.println("NORMAL-END");
* }
* }
* </pre>
*
* The above test prints this output:
*
* <pre>
* restart
* BRANCH-END
* restart
* NORMAL-END
* </pre>
*
* @param runnable
* a Runnable to be ran as a dead end branch
*/
public void branch(Runnable runnable) {
run(runnable, true);
}
/**
* Executes a {@code Runnable} as a merge back branch, i.e, that continues execution after the branch point.
*
* <pre>
* public class BranchRuleTest {
*
* @Rule
* public BranchRule brancher = new BranchRule();
*
* @Test
* public void simpleBranch() throws Exception {
*
* System.out.println("restart");
* brancher.branchAndMerge(new Runnable() {
* public void run() {
* System.out.println("BRANCH-END");
* }
* });
* System.out.println("NORMAL-END");
* }
* }
* </pre>
*
* The above test prints the following output. Note that the first pass prints both BRANCH-END and NORMAL-END.
*
* <pre>
* restart
* BRANCH-END
* NORMAL-END
* restart
* NORMAL-END
* </pre>
*
* @param runnable
* a Runnable to be ran as a dead end branch
*/
public void branchAndMerge(Runnable runnable) {
run(runnable, false);
}
private void run(Runnable runnable, boolean cut) {
if ( ! completedBranches.contains(runnable.getClass())) {
runnable.run();
if ( ! oneBranchCompleted) {
oneBranchCompleted = true;
completedBranches.add(runnable.getClass());
}
if (cut) {
throw new BranchDoneSignalException();
}
}
}
}