/
ClassTestDescriptor.java
271 lines (220 loc) · 10.2 KB
/
ClassTestDescriptor.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.jupiter.engine.descriptor;
import static org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findAfterAllMethods;
import static org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findAfterEachMethods;
import static org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findBeforeAllMethods;
import static org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findBeforeEachMethods;
import static org.junit.platform.commons.meta.API.Usage.Internal;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ContainerExtensionContext;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestExtensionContext;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
import org.junit.jupiter.engine.execution.AfterEachMethodAdapter;
import org.junit.jupiter.engine.execution.BeforeEachMethodAdapter;
import org.junit.jupiter.engine.execution.ConditionEvaluator;
import org.junit.jupiter.engine.execution.ExecutableInvoker;
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
import org.junit.jupiter.engine.execution.TestInstanceProvider;
import org.junit.jupiter.engine.execution.ThrowableCollector;
import org.junit.jupiter.engine.extension.ExtensionRegistry;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.meta.API;
import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.TestTag;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.JavaClassSource;
/**
* {@link TestDescriptor} for tests based on Java classes.
*
* <h3>Default Display Names</h3>
*
* <p>The default display name for a top-level or nested static test class is
* the fully qualified name of the class with the package name and leading dot
* (".") removed.
*
* @since 5.0
*/
@API(Internal)
public class ClassTestDescriptor extends JupiterTestDescriptor {
private static final ConditionEvaluator conditionEvaluator = new ConditionEvaluator();
private static final ExecutableInvoker executableInvoker = new ExecutableInvoker();
private final String displayName;
private final Class<?> testClass;
private final List<Method> beforeAllMethods;
private final List<Method> afterAllMethods;
private final List<Method> beforeEachMethods;
private final List<Method> afterEachMethods;
public ClassTestDescriptor(UniqueId uniqueId, Class<?> testClass) {
super(uniqueId);
this.testClass = Preconditions.notNull(testClass, "Class must not be null");
this.displayName = determineDisplayName(testClass);
this.beforeAllMethods = findBeforeAllMethods(testClass);
this.afterAllMethods = findAfterAllMethods(testClass);
this.beforeEachMethods = findBeforeEachMethods(testClass);
this.afterEachMethods = findAfterEachMethods(testClass);
setSource(new JavaClassSource(testClass));
}
// --- TestDescriptor ------------------------------------------------------
@Override
public Set<TestTag> getTags() {
return getTags(this.testClass);
}
@Override
public final String getDisplayName() {
return this.displayName;
}
public final Class<?> getTestClass() {
return this.testClass;
}
@Override
public final boolean isTest() {
return false;
}
@Override
public final boolean isContainer() {
return true;
}
@Override
protected String generateDefaultDisplayName() {
String name = this.testClass.getName();
int index = name.lastIndexOf('.');
return name.substring(index + 1);
}
// --- Node ----------------------------------------------------------------
@Override
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) {
ExtensionRegistry registry = populateNewExtensionRegistryFromExtendWith(this.testClass,
context.getExtensionRegistry());
registerBeforeEachMethodAdapters(registry);
registerAfterEachMethodAdapters(registry);
ContainerExtensionContext containerExtensionContext = new ClassBasedContainerExtensionContext(
context.getExtensionContext(), context.getExecutionListener(), this);
// @formatter:off
return context.extend()
.withTestInstanceProvider(testInstanceProvider(context, registry, containerExtensionContext))
.withExtensionRegistry(registry)
.withExtensionContext(containerExtensionContext)
.build();
// @formatter:on
}
@Override
public SkipResult shouldBeSkipped(JupiterEngineExecutionContext context) throws Exception {
ConditionEvaluationResult evaluationResult = conditionEvaluator.evaluateForContainer(
context.getExtensionRegistry(), context.getConfigurationParameters(),
(ContainerExtensionContext) context.getExtensionContext());
if (evaluationResult.isDisabled()) {
return SkipResult.skip(evaluationResult.getReason().orElse("<unknown>"));
}
return SkipResult.doNotSkip();
}
@Override
public JupiterEngineExecutionContext before(JupiterEngineExecutionContext context) throws Exception {
ExtensionRegistry registry = context.getExtensionRegistry();
ContainerExtensionContext extensionContext = (ContainerExtensionContext) context.getExtensionContext();
ThrowableCollector throwableCollector = context.getThrowableCollector();
invokeBeforeAllCallbacks(registry, extensionContext, throwableCollector);
if (throwableCollector.isEmpty()) {
context.beforeAllMethodsExecuted(true);
invokeBeforeAllMethods(registry, extensionContext, throwableCollector);
}
throwableCollector.assertEmpty();
return context;
}
@Override
public void after(JupiterEngineExecutionContext context) throws Exception {
ExtensionRegistry registry = context.getExtensionRegistry();
ContainerExtensionContext extensionContext = (ContainerExtensionContext) context.getExtensionContext();
ThrowableCollector throwableCollector = context.getThrowableCollector();
if (context.beforeAllMethodsExecuted()) {
invokeAfterAllMethods(registry, extensionContext, throwableCollector);
}
invokeAfterAllCallbacks(registry, extensionContext, throwableCollector);
throwableCollector.assertEmpty();
}
protected TestInstanceProvider testInstanceProvider(JupiterEngineExecutionContext parentExecutionContext,
ExtensionRegistry registry, ExtensionContext extensionContext) {
return () -> {
Constructor<?> constructor = ReflectionUtils.getDeclaredConstructor(this.testClass);
Object instance = executableInvoker.invoke(constructor, extensionContext, registry);
invokeTestInstancePostProcessors(instance, registry, extensionContext);
return instance;
};
}
protected void invokeTestInstancePostProcessors(Object instance, ExtensionRegistry registry,
ExtensionContext context) {
// @formatter:off
registry.stream(TestInstancePostProcessor.class)
.forEach(extension -> executeAndMaskThrowable(() -> extension.postProcessTestInstance(instance, context)));
// @formatter:on
}
private void invokeBeforeAllCallbacks(ExtensionRegistry registry, ContainerExtensionContext context,
ThrowableCollector throwableCollector) {
for (BeforeAllCallback callback : registry.toList(BeforeAllCallback.class)) {
throwableCollector.execute(() -> callback.beforeAll(context));
if (throwableCollector.isNotEmpty()) {
break;
}
}
}
private void invokeBeforeAllMethods(ExtensionRegistry registry, ContainerExtensionContext context,
ThrowableCollector throwableCollector) {
for (Method method : this.beforeAllMethods) {
throwableCollector.execute(() -> executableInvoker.invoke(method, context, registry));
if (throwableCollector.isNotEmpty()) {
break;
}
}
}
private void invokeAfterAllMethods(ExtensionRegistry registry, ContainerExtensionContext context,
ThrowableCollector throwableCollector) {
this.afterAllMethods.forEach(
method -> throwableCollector.execute(() -> executableInvoker.invoke(method, context, registry)));
}
private void invokeAfterAllCallbacks(ExtensionRegistry registry, ContainerExtensionContext context,
ThrowableCollector throwableCollector) {
registry.reverseStream(AfterAllCallback.class)//
.forEach(extension -> throwableCollector.execute(() -> extension.afterAll(context)));
}
private void registerBeforeEachMethodAdapters(ExtensionRegistry registry) {
registerMethodsAsExtensions(this.beforeEachMethods, registry, this::synthesizeBeforeEachMethodAdapter);
}
private void registerAfterEachMethodAdapters(ExtensionRegistry registry) {
registerMethodsAsExtensions(this.afterEachMethods, registry, this::synthesizeAfterEachMethodAdapter);
}
private void registerMethodsAsExtensions(List<Method> methods, ExtensionRegistry registry,
BiFunction<ExtensionRegistry, Method, Extension> extensionSynthesizer) {
methods.forEach(method -> registry.registerExtension(extensionSynthesizer.apply(registry, method), method));
}
private BeforeEachMethodAdapter synthesizeBeforeEachMethodAdapter(ExtensionRegistry registry, Method method) {
return extensionContext -> invokeMethodInTestExtensionContext(method, extensionContext, registry);
}
private AfterEachMethodAdapter synthesizeAfterEachMethodAdapter(ExtensionRegistry registry, Method method) {
return extensionContext -> invokeMethodInTestExtensionContext(method, extensionContext, registry);
}
private void invokeMethodInTestExtensionContext(Method method, TestExtensionContext context,
ExtensionRegistry registry) {
Object instance = ReflectionUtils.getOuterInstance(context.getTestInstance(),
method.getDeclaringClass()).orElseThrow(
() -> new JUnitException("Failed to find instance for method: " + method.toGenericString()));
executableInvoker.invoke(method, instance, context, registry);
}
}