1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.junit;
11
12 import java.text.MessageFormat;
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
15
16 import org.junit.rules.TestRule;
17 import org.junit.runner.Description;
18 import org.junit.runners.model.Statement;
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 public class RepeatRule implements TestRule {
47
48 private static final Logger LOG = Logger
49 .getLogger(RepeatRule.class.getName());
50
51
52
53
54
55 public static class RepeatedTestException extends RuntimeException {
56 private static final long serialVersionUID = 1L;
57
58
59
60
61
62
63
64
65 public RepeatedTestException(String message) {
66 super(message);
67 }
68
69
70
71
72
73
74
75
76
77 public RepeatedTestException(String message, Throwable cause) {
78 super(message, cause);
79 }
80 }
81
82 private static class RepeatStatement extends Statement {
83
84 private final int repetitions;
85
86 private boolean abortOnFailure;
87
88 private final Statement statement;
89
90 private RepeatStatement(int repetitions, boolean abortOnFailure,
91 Statement statement) {
92 this.repetitions = repetitions;
93 this.abortOnFailure = abortOnFailure;
94 this.statement = statement;
95 }
96
97 @Override
98 public void evaluate() throws Throwable {
99 int failures = 0;
100 for (int i = 0; i < repetitions; i++) {
101 try {
102 statement.evaluate();
103 } catch (Throwable e) {
104 failures += 1;
105 RepeatedTestException ex = new RepeatedTestException(
106 MessageFormat.format(
107 "Repeated test failed when run for the {0}. time",
108 Integer.valueOf(i + 1)),
109 e);
110 LOG.log(Level.SEVERE, ex.getMessage(), ex);
111 if (abortOnFailure) {
112 throw ex;
113 }
114 }
115 }
116 if (failures > 0) {
117 RepeatedTestException e = new RepeatedTestException(
118 MessageFormat.format(
119 "Test failed {0} times out of {1} repeated executions",
120 Integer.valueOf(failures),
121 Integer.valueOf(repetitions)));
122 LOG.log(Level.SEVERE, e.getMessage(), e);
123 throw e;
124 }
125 }
126 }
127
128
129 @Override
130 public Statement apply(Statement statement, Description description) {
131 Statement result = statement;
132 Repeat repeat = description.getAnnotation(Repeat.class);
133 if (repeat != null) {
134 int n = repeat.n();
135 boolean abortOnFailure = repeat.abortOnFailure();
136 result = new RepeatStatement(n, abortOnFailure, statement);
137 }
138 return result;
139 }
140 }