-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathValueProviderRule.java
82 lines (69 loc) · 2.73 KB
/
ValueProviderRule.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
package com.tngtech.valueprovider;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.tngtech.valueprovider.ValueProviderFactory.finishTestMethodCycle;
import static com.tngtech.valueprovider.ValueProviderFactory.startTestMethodCycle;
import static java.lang.System.identityHashCode;
/**
* Rule to provide failure reproduction data for {@link ValueProvider}s created during or after
* instantiation of the test class.
*
* @see ValueProviderClassRule
*/
public class ValueProviderRule implements TestRule {
private static final Logger logger = LoggerFactory.getLogger(ValueProviderRule.class);
@SuppressWarnings("WeakerAccess") // is part of public API
public ValueProviderRule() {
logger.debug("Instantiation {}", identityHashCode(this));
startTestMethodCycle();
}
public @Nonnull
Statement apply(@Nonnull final Statement base, @Nonnull final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<>();
startingQuietly(description, errors);
try {
base.evaluate();
} catch (org.junit.internal.AssumptionViolatedException e) {
errors.add(e);
} catch (Throwable e) {
handleFailure(e, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
private void startingQuietly(Description description, List<Throwable> errors) {
try {
logger.debug("{} starting test {}", identityHashCode(this), getTestMethodName(description));
} catch (RuntimeException e) {
errors.add(e);
}
}
private void handleFailure(Throwable e, List<Throwable> errors) {
e.addSuppressed(new ValueProviderException());
errors.add(e);
}
private void finishedQuietly(Description description, List<Throwable> errors) {
try {
logger.debug("{} finished test {}", identityHashCode(this), getTestMethodName(description));
finishTestMethodCycle();
} catch (RuntimeException e) {
errors.add(e);
}
}
private String getTestMethodName(Description description) {
return description.getClassName() + "." + description.getMethodName();
}
}