-
Notifications
You must be signed in to change notification settings - Fork 5
/
RepeatTest.java
69 lines (56 loc) · 1.67 KB
/
RepeatTest.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
package org.libex.test.rules;
import static org.hamcrest.MatcherAssert.*;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.ThreadSafe;
import org.hamcrest.number.OrderingComparison;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.libex.test.annotation.Repeat;
/**
* Rule used to repeat tests marked with the {@link Repeat} annotation
*
* @author John Butler
*
* @see Repeat
*/
@ThreadSafe
@ParametersAreNonnullByDefault
public class RepeatTest implements TestRule {
public static RepeatTest findRepeats() {
return new RepeatTest();
}
private RepeatTest() {
super();
}
@Override
public Statement apply(final Statement statement, Description description) {
return new StatementRepeater(statement, description);
}
private final class StatementRepeater extends Statement {
private final Statement statement;
private final int repeatCount;
private StatementRepeater(Statement statement, Description description) {
super();
this.statement = statement;
Repeat repeat = description.getAnnotation(Repeat.class);
this.repeatCount = getRepeatCount(repeat);
}
private final int getRepeatCount(@Nullable Repeat repeat) {
int count = 1;
if (repeat != null) {
count = repeat.count();
}
assertThat("Repeat count must be > 0", count,
OrderingComparison.greaterThan(0));
return count;
}
@Override
public void evaluate() throws Throwable {
for (int i = 0; i < repeatCount; i++) {
statement.evaluate();
}
}
}
}