11
11
import static org .hibernate .testing .bytecode .enhancement .extension .engine .BytecodeEnhancedClassUtils .enhanceTestClass ;
12
12
import static org .junit .platform .commons .util .AnnotationUtils .findAnnotation ;
13
13
14
+ import java .lang .reflect .Constructor ;
15
+ import java .lang .reflect .InvocationHandler ;
16
+ import java .lang .reflect .InvocationTargetException ;
14
17
import java .lang .reflect .Method ;
18
+ import java .lang .reflect .Proxy ;
15
19
import java .util .Arrays ;
16
20
import java .util .HashSet ;
17
21
import java .util .List ;
18
22
import java .util .Map ;
19
23
import java .util .Objects ;
20
24
import java .util .Optional ;
21
25
import java .util .Set ;
22
- import java .util .function .Function ;
23
- import java .util .function .Predicate ;
24
- import java .util .function .Supplier ;
25
26
26
27
import org .hibernate .testing .bytecode .enhancement .extension .BytecodeEnhanced ;
27
- import org .hibernate .testing .junit4 .BaseUnitTestCase ;
28
28
import org .junit .jupiter .api .Assertions ;
29
- import org .junit .jupiter .api .ClassOrderer ;
30
29
import org .junit .jupiter .api .DisplayNameGenerator ;
31
- import org .junit .jupiter .api .MethodOrderer ;
32
- import org .junit .jupiter .api .TestInstance ;
33
- import org .junit .jupiter .api .extension .ExecutionCondition ;
34
30
import org .junit .jupiter .api .extension .ExtensionContext ;
35
- import org .junit .jupiter .api .io .CleanupMode ;
36
- import org .junit .jupiter .api .io .TempDirFactory ;
37
- import org .junit .jupiter .api .parallel .ExecutionMode ;
38
31
import org .junit .jupiter .engine .JupiterTestEngine ;
39
32
import org .junit .jupiter .engine .config .JupiterConfiguration ;
40
33
import org .junit .jupiter .engine .descriptor .ClassBasedTestDescriptor ;
44
37
import org .junit .jupiter .engine .descriptor .TestTemplateTestDescriptor ;
45
38
import org .junit .jupiter .engine .execution .JupiterEngineExecutionContext ;
46
39
import org .junit .platform .engine .EngineDiscoveryRequest ;
40
+ import org .junit .platform .engine .EngineExecutionListener ;
47
41
import org .junit .platform .engine .ExecutionRequest ;
48
42
import org .junit .platform .engine .TestDescriptor ;
49
43
import org .junit .platform .engine .UniqueId ;
@@ -143,9 +137,13 @@ private void replaceWithEnhanced(Class<?> enhanced, ClassBasedTestDescriptor des
143
137
Set <? extends TestDescriptor > children , TestDescriptor parent , String [] testEnhancedClasses ,
144
138
Object enhancementContextId )
145
139
throws NoSuchMethodException {
146
- DelegatingJupiterConfiguration configuration = new DelegatingJupiterConfiguration ( jc , enhancementContextId );
140
+ final JupiterConfiguration configuration = (JupiterConfiguration ) Proxy .newProxyInstance (
141
+ BytecodeEnhancedTestEngine .class .getClassLoader (),
142
+ new Class [] { JupiterConfiguration .class },
143
+ new JupiterConfigurationInvocationHandler ( jc , enhancementContextId )
144
+ );
147
145
148
- ClassTestDescriptor updated = new ClassTestDescriptor (
146
+ final ClassTestDescriptor updated = new ClassTestDescriptor (
149
147
convertUniqueId ( descriptor .getUniqueId (), enhancementContextId ),
150
148
enhanced ,
151
149
configuration
@@ -210,6 +208,24 @@ private Method findMethodReplacement(ClassTestDescriptor updated, Method testMet
210
208
211
209
@ Override
212
210
protected JupiterEngineExecutionContext createExecutionContext (ExecutionRequest request ) {
211
+ try {
212
+ // Try constructing the JupiterEngineExecutionContext the way it is done in 5.13+
213
+ final Class <?> storeFacadeClass =
214
+ Class .forName ( "org.junit.jupiter.engine.descriptor.LauncherStoreFacade" );
215
+ final Method getStore = ExecutionRequest .class .getMethod ( "getStore" );
216
+ final Constructor <?> storeConstructor = storeFacadeClass .getConstructor ( getStore .getReturnType () );
217
+ final Constructor <JupiterEngineExecutionContext > constructor = JupiterEngineExecutionContext .class
218
+ .getConstructor ( EngineExecutionListener .class , JupiterConfiguration .class , storeFacadeClass );
219
+ return constructor .newInstance (
220
+ request .getEngineExecutionListener (),
221
+ this .getJupiterConfiguration ( request ),
222
+ storeConstructor .newInstance ( getStore .invoke ( request ) )
223
+ );
224
+ }
225
+ catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e ) {
226
+ // Ignore errors as they are probably due to version mismatches and try the 5.12 way
227
+ }
228
+
213
229
return new JupiterEngineExecutionContext (
214
230
request .getEngineExecutionListener (),
215
231
this .getJupiterConfiguration ( request )
@@ -237,111 +253,51 @@ public Context(ExecutionRequest request) {
237
253
}
238
254
}
239
255
240
- private static class DelegatingJupiterConfiguration implements JupiterConfiguration {
256
+ private static class JupiterConfigurationInvocationHandler implements InvocationHandler {
241
257
private final JupiterConfiguration configuration ;
242
- private final DelegatingDisplayNameGenerator displayNameGenerator ;
258
+ private final DisplayNameGenerator displayNameGenerator ;
243
259
244
- private DelegatingJupiterConfiguration (JupiterConfiguration configuration , Object id ) {
260
+ private JupiterConfigurationInvocationHandler (JupiterConfiguration configuration , Object id ) {
245
261
this .configuration = configuration ;
246
- displayNameGenerator = new DelegatingDisplayNameGenerator (
247
- configuration .getDefaultDisplayNameGenerator (),
248
- id
262
+ displayNameGenerator = (DisplayNameGenerator ) Proxy .newProxyInstance (
263
+ BytecodeEnhancedTestEngine .class .getClassLoader (),
264
+ new Class []{ DisplayNameGenerator .class },
265
+ new DisplayNameGeneratorInvocationHandler ( configuration .getDefaultDisplayNameGenerator (), id )
249
266
);
250
267
}
251
268
252
269
@ Override
253
- public Optional <String > getRawConfigurationParameter (String s ) {
254
- return configuration .getRawConfigurationParameter ( s );
255
- }
256
-
257
- @ Override
258
- public <T > Optional <T > getRawConfigurationParameter (String s , Function <String , T > function ) {
259
- return configuration .getRawConfigurationParameter ( s , function );
260
- }
261
-
262
- @ Override
263
- public boolean isParallelExecutionEnabled () {
264
- return configuration .isParallelExecutionEnabled ();
265
- }
266
-
267
- @ Override
268
- public boolean isExtensionAutoDetectionEnabled () {
269
- return configuration .isExtensionAutoDetectionEnabled ();
270
- }
271
-
272
- @ Override
273
- public ExecutionMode getDefaultExecutionMode () {
274
- return configuration .getDefaultExecutionMode ();
275
- }
276
-
277
- @ Override
278
- public ExecutionMode getDefaultClassesExecutionMode () {
279
- return configuration .getDefaultClassesExecutionMode ();
280
- }
281
-
282
- @ Override
283
- public TestInstance .Lifecycle getDefaultTestInstanceLifecycle () {
284
- return configuration .getDefaultTestInstanceLifecycle ();
285
- }
286
-
287
- @ Override
288
- public Predicate <ExecutionCondition > getExecutionConditionFilter () {
289
- return configuration .getExecutionConditionFilter ();
290
- }
291
-
292
- @ Override
293
- public DisplayNameGenerator getDefaultDisplayNameGenerator () {
294
- return displayNameGenerator ;
295
- }
296
-
297
- @ Override
298
- public Optional <MethodOrderer > getDefaultTestMethodOrderer () {
299
- return configuration .getDefaultTestMethodOrderer ();
300
- }
301
-
302
- @ Override
303
- public Optional <ClassOrderer > getDefaultTestClassOrderer () {
304
- return configuration .getDefaultTestClassOrderer ();
305
- }
306
-
307
- @ Override
308
- public CleanupMode getDefaultTempDirCleanupMode () {
309
- return configuration .getDefaultTempDirCleanupMode ();
310
- }
311
-
312
- @ Override
313
- public Supplier <TempDirFactory > getDefaultTempDirFactorySupplier () {
314
- return configuration .getDefaultTempDirFactorySupplier ();
270
+ public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
271
+ if ( "getDefaultDisplayNameGenerator" .equals ( method .getName () ) ) {
272
+ return displayNameGenerator ;
273
+ }
274
+ else {
275
+ return method .invoke ( configuration , args );
276
+ }
315
277
}
316
278
}
317
279
318
- private static class DelegatingDisplayNameGenerator implements DisplayNameGenerator {
280
+ private static class DisplayNameGeneratorInvocationHandler implements InvocationHandler {
319
281
320
282
private final DisplayNameGenerator delegate ;
321
283
private final Object id ;
322
284
323
- private DelegatingDisplayNameGenerator (DisplayNameGenerator delegate , Object id ) {
285
+ private DisplayNameGeneratorInvocationHandler (DisplayNameGenerator delegate , Object id ) {
324
286
this .delegate = delegate ;
325
287
this .id = id ;
326
288
}
327
289
328
- @ Override
329
- public String generateDisplayNameForClass (Class <?> aClass ) {
330
- return prefix () + delegate .generateDisplayNameForClass ( aClass );
331
- }
332
-
333
290
private String prefix () {
334
291
return "Enhanced" + ( id == null ? "" : "[" + id + "]" ) + ":" ;
335
292
}
336
293
337
294
@ Override
338
- public String generateDisplayNameForNestedClass (Class <?> aClass ) {
339
- return prefix () + delegate .generateDisplayNameForNestedClass ( aClass );
340
- }
341
-
342
- @ Override
343
- public String generateDisplayNameForMethod (Class <?> aClass , Method method ) {
344
- return prefix () + delegate .generateDisplayNameForMethod ( aClass , method );
295
+ public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
296
+ final Object result = method .invoke ( delegate , args );
297
+ if ( method .getDeclaringClass () == DisplayNameGenerator .class ) {
298
+ return prefix () + result ;
299
+ }
300
+ return result ;
345
301
}
346
302
}
347
303
@@ -350,9 +306,7 @@ private static class EnhancementWorkedCheckMethodTestDescriptor extends TestMeth
350
306
private final boolean enhanced ;
351
307
private final String [] classes ;
352
308
353
- public EnhancementWorkedCheckMethodTestDescriptor (UniqueId uniqueId , Class <?> testClass ,
354
- JupiterConfiguration configuration ,
355
- boolean enhanced , String [] classes ) {
309
+ public EnhancementWorkedCheckMethodTestDescriptor (UniqueId uniqueId , Class <?> testClass , JupiterConfiguration configuration , boolean enhanced , String [] classes ) {
356
310
super (
357
311
prepareId ( uniqueId , testMethod ( enhanced ) ),
358
312
testClass , testMethod ( enhanced ),
0 commit comments