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 public RepeatedTestException(String message) {
65 super(message);
66 }
67
68
69
70
71
72
73
74
75
76 public RepeatedTestException(String message, Throwable cause) {
77 super(message, cause);
78 }
79 }
80
81 private static class RepeatStatement extends Statement {
82
83 private final int repetitions;
84
85 private boolean abortOnFailure;
86
87 private final Statement statement;
88
89 private RepeatStatement(int repetitions, boolean abortOnFailure,
90 Statement statement) {
91 this.repetitions = repetitions;
92 this.abortOnFailure = abortOnFailure;
93 this.statement = statement;
94 }
95
96 @Override
97 public void evaluate() throws Throwable {
98 int failures = 0;
99 for (int i = 0; i < repetitions; i++) {
100 try {
101 statement.evaluate();
102 } catch (Throwable e) {
103 failures += 1;
104 RepeatedTestException ex = new RepeatedTestException(
105 MessageFormat.format(
106 "Repeated test failed when run for the {0}. time",
107 Integer.valueOf(i + 1)),
108 e);
109 LOG.log(Level.SEVERE, ex.getMessage(), ex);
110 if (abortOnFailure) {
111 throw ex;
112 }
113 }
114 }
115 if (failures > 0) {
116 RepeatedTestException e = new RepeatedTestException(
117 MessageFormat.format(
118 "Test failed {0} times out of {1} repeated executions",
119 Integer.valueOf(failures),
120 Integer.valueOf(repetitions)));
121 LOG.log(Level.SEVERE, e.getMessage(), e);
122 throw e;
123 }
124 }
125 }
126
127
128 @Override
129 public Statement apply(Statement statement, Description description) {
130 Statement result = statement;
131 Repeat repeat = description.getAnnotation(Repeat.class);
132 if (repeat != null) {
133 int n = repeat.n();
134 boolean abortOnFailure = repeat.abortOnFailure();
135 result = new RepeatStatement(n, abortOnFailure, statement);
136 }
137 return result;
138 }
139 }