Skip to content

Commit 478b787

Browse files
Make sure TCCL is restored in @QuarkusTest
Fixes #9273
1 parent 8254c62 commit 478b787

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

integration-tests/main/src/test/java/io/quarkus/it/main/EnabledIfTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import org.junit.jupiter.api.extension.ExtendWith;
1616
import org.junit.jupiter.api.extension.ExtensionContext;
1717

18-
import io.quarkus.test.junit.QuarkusTest;
18+
import io.quarkus.test.junit.QuarkusTestExtension;
1919

20-
@QuarkusTest
20+
@ExtendWith(value = { QuarkusTestExtension.class, EnabledIfTest.EnabledIfCondition.class })
2121
public class EnabledIfTest {
2222

2323
@Test

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

+37-18
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ public void run() {
200200
e.addSuppressed(ex);
201201
}
202202
throw e;
203+
} finally {
204+
if (originalCl != null) {
205+
Thread.currentThread().setContextClassLoader(originalCl);
206+
}
203207
}
204208
}
205209

@@ -237,10 +241,15 @@ public void afterEach(ExtensionContext context) throws Exception {
237241
afterEachCallback.getClass().getMethod("afterEach", Object.class).invoke(afterEachCallback, actualTestInstance);
238242
}
239243
boolean nativeImageTest = isNativeTest(context);
240-
runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName())
241-
.getDeclaredMethod("clearURL").invoke(null);
242-
runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName())
243-
.getDeclaredMethod("tearDown", boolean.class).invoke(null, nativeImageTest);
244+
ClassLoader original = setCCL(runningQuarkusApplication.getClassLoader());
245+
try {
246+
runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName())
247+
.getDeclaredMethod("clearURL").invoke(null);
248+
runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName())
249+
.getDeclaredMethod("tearDown", boolean.class).invoke(null, nativeImageTest);
250+
} finally {
251+
setCCL(original);
252+
}
244253
}
245254
}
246255

@@ -254,17 +263,22 @@ public void beforeEach(ExtensionContext context) throws Exception {
254263
return;
255264
}
256265
if (!failedBoot) {
257-
pushMockContext();
258-
for (Object beforeEachCallback : beforeEachCallbacks) {
259-
beforeEachCallback.getClass().getMethod("beforeEach", Object.class).invoke(beforeEachCallback,
260-
actualTestInstance);
261-
}
262-
boolean nativeImageTest = isNativeTest(context);
263-
if (runningQuarkusApplication != null) {
264-
runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName())
265-
.getDeclaredMethod("setURL", boolean.class).invoke(null, false);
266-
runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName())
267-
.getDeclaredMethod("setup", boolean.class).invoke(null, nativeImageTest);
266+
ClassLoader original = setCCL(runningQuarkusApplication.getClassLoader());
267+
try {
268+
pushMockContext();
269+
for (Object beforeEachCallback : beforeEachCallbacks) {
270+
beforeEachCallback.getClass().getMethod("beforeEach", Object.class).invoke(beforeEachCallback,
271+
actualTestInstance);
272+
}
273+
boolean nativeImageTest = isNativeTest(context);
274+
if (runningQuarkusApplication != null) {
275+
runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName())
276+
.getDeclaredMethod("setURL", boolean.class).invoke(null, false);
277+
runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName())
278+
.getDeclaredMethod("setup", boolean.class).invoke(null, nativeImageTest);
279+
}
280+
} finally {
281+
setCCL(original);
268282
}
269283
} else {
270284
if (firstException != null) {
@@ -310,7 +324,6 @@ public void beforeAll(ExtensionContext context) throws Exception {
310324
ensureStarted(context);
311325
if (runningQuarkusApplication != null) {
312326
pushMockContext();
313-
setCCL(runningQuarkusApplication.getClassLoader());
314327
}
315328
}
316329

@@ -380,11 +393,15 @@ public <T> T interceptTestClassConstructor(Invocation<T> invocation,
380393
// We do this here as well, because when @TestInstance(Lifecycle.PER_CLASS) is used on a class,
381394
// interceptTestClassConstructor is called before beforeAll, meaning that the TCCL will not be set correctly
382395
// (for any test other than the first) unless this is done
396+
old = null;
383397
if (runningQuarkusApplication != null) {
384-
setCCL(runningQuarkusApplication.getClassLoader());
398+
old = setCCL(runningQuarkusApplication.getClassLoader());
385399
}
386400

387401
initTestState(extensionContext, state);
402+
if (old != null) {
403+
setCCL(old);
404+
}
388405
return result;
389406
}
390407

@@ -477,10 +494,10 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocation
477494
throws Throwable {
478495
Method newMethod = null;
479496

497+
ClassLoader old = setCCL(runningQuarkusApplication.getClassLoader());
480498
try {
481499
Class<?> c = Class.forName(extensionContext.getRequiredTestClass().getName(), true,
482500
Thread.currentThread().getContextClassLoader());
483-
;
484501
while (c != Object.class) {
485502
if (c.getName().equals(invocationContext.getExecutable().getDeclaringClass().getName())) {
486503
try {
@@ -522,6 +539,8 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocation
522539
throw e.getCause();
523540
} catch (IllegalAccessException | ClassNotFoundException e) {
524541
throw new RuntimeException(e);
542+
} finally {
543+
setCCL(old);
525544
}
526545
}
527546

0 commit comments

Comments
 (0)