Skip to content

Commit 241fafd

Browse files
committed
Eliminate duplicated code to improve safety
1 parent af01441 commit 241fafd

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/ExecutionListener.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayDeque;
44
import java.util.Deque;
55
import java.util.Optional;
6+
import java.util.function.Consumer;
67

78
import org.junit.platform.engine.TestExecutionResult;
89
import org.junit.platform.engine.TestSource;
@@ -23,42 +24,43 @@ public class ExecutionListener implements TestExecutionListener {
2324

2425
@Override
2526
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);
4028
}
4129

4230
@Override
4331
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
4437
Optional<TestSource> oSource = testIdentifier.getSource();
4538
if (oSource.isPresent()) {
4639
TestSource source = oSource.get();
4740
if (source instanceof ClassSource cs) {
4841
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
5043
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);
5645
}
5746
}
5847
}
5948
}
6049

6150
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
6252
return classLoader instanceof QuarkusClassLoader;
6353
}
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+
6466
}

0 commit comments

Comments
 (0)