3
3
import java .util .ArrayDeque ;
4
4
import java .util .Deque ;
5
5
import java .util .Optional ;
6
+ import java .util .function .Consumer ;
6
7
7
8
import org .junit .platform .engine .TestExecutionResult ;
8
9
import org .junit .platform .engine .TestSource ;
@@ -23,42 +24,43 @@ public class ExecutionListener implements TestExecutionListener {
23
24
24
25
@ Override
25
26
public void executionStarted (TestIdentifier testIdentifier ) {
26
- // This will be called for various levels of containers, only some of which are tests, so check carefully and do not assume
27
- Optional <TestSource > oSource = testIdentifier .getSource ();
28
- if (oSource .isPresent ()) {
29
- TestSource source = oSource .get ();
30
- if (source instanceof ClassSource cs ) {
31
- ClassLoader classLoader = cs .getJavaClass ().getClassLoader ();
32
- // Only adjust the TCCL in cases where we know the QuarkusTestExtension would be about to do it anyway
33
- // We could check annotations, but that would be slow, and the assumption that only Quarkus Tests are loaded with the quarkus classloader should be a fair one
34
- if (isQuarkusTest (classLoader )) {
35
- origCl .push (Thread .currentThread ().getContextClassLoader ());
36
- Thread .currentThread ().setContextClassLoader (classLoader );
37
- }
38
- }
39
- }
27
+ invokeIfTestIsQuarkusTest (testIdentifier , this ::setTCCL );
40
28
}
41
29
42
30
@ Override
43
31
public void executionFinished (TestIdentifier testIdentifier , TestExecutionResult result ) {
32
+ invokeIfTestIsQuarkusTest (testIdentifier , this ::unsetTCCL );
33
+ }
34
+
35
+ private void invokeIfTestIsQuarkusTest (TestIdentifier testIdentifier , Consumer <ClassLoader > consumer ) {
36
+ // This will be called for various levels of containers, only some of which are tests, so check carefully and do not assume
44
37
Optional <TestSource > oSource = testIdentifier .getSource ();
45
38
if (oSource .isPresent ()) {
46
39
TestSource source = oSource .get ();
47
40
if (source instanceof ClassSource cs ) {
48
41
ClassLoader classLoader = cs .getJavaClass ().getClassLoader ();
49
- // Only pop if we meet the conditions under which we pushed
42
+ // Only adjust the TCCL in cases where we know the QuarkusTestExtension would be about to do it anyway
50
43
if (isQuarkusTest (classLoader )) {
51
- ClassLoader cl = origCl .pop ();
52
- if (cl != null ) {
53
- // If execution is parallel this could produce odd results, but if execution is parallel any kind of TCCL manipulation will be ill-fated
54
- Thread .currentThread ().setContextClassLoader (cl );
55
- }
44
+ consumer .accept (classLoader );
56
45
}
57
46
}
58
47
}
59
48
}
60
49
61
50
private static boolean isQuarkusTest (ClassLoader classLoader ) {
51
+ // We could check annotations, but that would be slow, and the assumption that only Quarkus Tests are loaded with the quarkus classloader should be a fair one
62
52
return classLoader instanceof QuarkusClassLoader ;
63
53
}
54
+
55
+ private void setTCCL (ClassLoader classLoader ) {
56
+ origCl .push (Thread .currentThread ().getContextClassLoader ());
57
+ Thread .currentThread ().setContextClassLoader (classLoader );
58
+ }
59
+
60
+ private void unsetTCCL (ClassLoader classLoader ) {
61
+ ClassLoader cl = origCl .pop ();
62
+ // If execution is parallel this stack logic could produce odd results, but if execution is parallel any kind of TCCL manipulation will be ill-fated
63
+ Thread .currentThread ().setContextClassLoader (cl );
64
+ }
65
+
64
66
}
0 commit comments