-
Notifications
You must be signed in to change notification settings - Fork 3
/
ValueProviderExtension.java
153 lines (130 loc) · 6.07 KB
/
ValueProviderExtension.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
package com.tngtech.valueprovider;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.tngtech.valueprovider.ValueProviderExtension.TestMethodCycleState.BEFORE_FIRST_CYCLE;
import static com.tngtech.valueprovider.ValueProviderExtension.TestMethodCycleState.CYCLE_COMLETED;
import static com.tngtech.valueprovider.ValueProviderExtension.TestMethodCycleState.CYCLE_STARTED;
import static java.lang.System.identityHashCode;
public class ValueProviderExtension implements
BeforeAllCallback, AfterAllCallback,
BeforeEachCallback, AfterEachCallback,
TestExecutionExceptionHandler,
InvocationInterceptor {
private static final Logger logger = LoggerFactory.getLogger(ValueProviderExtension.class);
/**
* Required to deal with @{@link Disabled}
* test-methods, as the test-class is instantiated for them
* (i.e. {@link #interceptTestClassConstructor(Invocation, ReflectiveInvocationContext, ExtensionContext)} invoked),
* but neither of {@link #beforeEach(ExtensionContext)} nor esp. {@link #afterEach(ExtensionContext)} is invoked, but
* either interceptTestClassConstructor() for the next test-method,
* or afterAll() when the disabled test-method was the last to be executed
*/
enum TestMethodCycleState {
BEFORE_FIRST_CYCLE,
CYCLE_STARTED,
CYCLE_COMLETED
}
private TestMethodCycleState testMethodCycleState = BEFORE_FIRST_CYCLE;
@Override
public void beforeAll(ExtensionContext context) {
logger.debug("{} beforeAll {}",
identityHashCode(this), getTestClassName(context));
startTestClassCycle();
}
@Override
public <T> T interceptTestClassConstructor(Invocation<T> invocation, ReflectiveInvocationContext<Constructor<T>> invocationContext,
ExtensionContext extensionContext) throws Throwable {
logger.debug("{} interceptTestClassConstructor {}",
identityHashCode(this), buildQualifiedTestMethodName(extensionContext));
ensureStaticInitializationOfTestClass(extensionContext);
startTestMethodCycle();
return invocation.proceed();
}
private void ensureStaticInitializationOfTestClass(ExtensionContext extensionContext) throws ClassNotFoundException {
Class<?> testClass = getTestClass(extensionContext);
// the following statement may seem meaningless,
// but as tests show, depending on the static code of a test,
// the testClass is NOT necessarily initialized at this stage of test execution
// e.g. a @BeforeAll method ensures testClass initialization, static code does NOT
Class.forName(testClass.getName());
}
@Override
public void beforeEach(ExtensionContext context) {
logger.debug("{} beforeEach {}",
identityHashCode(this), buildQualifiedTestMethodName(context));
}
@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
logger.debug("{} handleTestExecutionException {}",
identityHashCode(this), buildQualifiedTestMethodName(context));
// Note: handleTestExecutionException() is invoked BEFORE afterEach, i.e. BEFORE seed is reset,
// so that the correct seed values appear in the failure message
throwable.addSuppressed(new ValueProviderException());
throw throwable;
}
@Override
public void afterEach(ExtensionContext context) {
logger.debug("{} afterEach {}",
identityHashCode(this), buildQualifiedTestMethodName(context));
finishTestMethodCycle();
}
@Override
public void afterAll(ExtensionContext context) {
logger.debug("{} afterAll {}",
identityHashCode(this), getTestClassName(context));
finishTestClassCycle();
}
private void startTestClassCycle() {
ValueProviderFactory.startTestClassCycle();
resetTestMethodCycleState();
}
private void startTestMethodCycle() {
finishTestMethodCycleIfNecessary();
ValueProviderFactory.startTestMethodCycle();
testMethodCycleState = CYCLE_STARTED;
}
private void finishTestClassCycle() {
finishTestMethodCycleIfNecessary();
ValueProviderFactory.finishTestClassCycle();
resetTestMethodCycleState();
}
private void finishTestMethodCycleIfNecessary() {
if (testMethodCycleState == CYCLE_STARTED) {
// was started, but not finished due to @Disabled test-method
finishTestMethodCycle();
}
}
private void finishTestMethodCycle() {
ValueProviderFactory.finishTestMethodCycle();
testMethodCycleState = CYCLE_COMLETED;
}
private void resetTestMethodCycleState() {
testMethodCycleState = BEFORE_FIRST_CYCLE;
}
static String buildQualifiedTestMethodName(ExtensionContext context) {
return String.format("%s.%s", getTestClassName(context), getTestMethodName(context));
}
private static String getTestClassName(ExtensionContext context) {
return getTestClass(context).getSimpleName();
}
private static Class<?> getTestClass(ExtensionContext context) {
return context.getTestClass()
.orElseThrow(() -> new IllegalArgumentException("cannot determine test class"));
}
private static String getTestMethodName(ExtensionContext context) {
return context.getTestMethod()
.map(Method::getName)
.orElse("<unknown>");
}
}