From 478b7870d98ba270a95b480f8e4cf8bdea262216 Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Thu, 21 May 2020 16:45:10 +1000 Subject: [PATCH] Make sure TCCL is restored in @QuarkusTest Fixes #9273 --- .../io/quarkus/it/main/EnabledIfTest.java | 4 +- .../test/junit/QuarkusTestExtension.java | 55 +++++++++++++------ 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/integration-tests/main/src/test/java/io/quarkus/it/main/EnabledIfTest.java b/integration-tests/main/src/test/java/io/quarkus/it/main/EnabledIfTest.java index e807c0d5d5f31..18f16327bc542 100644 --- a/integration-tests/main/src/test/java/io/quarkus/it/main/EnabledIfTest.java +++ b/integration-tests/main/src/test/java/io/quarkus/it/main/EnabledIfTest.java @@ -15,9 +15,9 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtensionContext; -import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.QuarkusTestExtension; -@QuarkusTest +@ExtendWith(value = { QuarkusTestExtension.class, EnabledIfTest.EnabledIfCondition.class }) public class EnabledIfTest { @Test diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java index fd85a0e7c6137..dc89fc507ee71 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java @@ -200,6 +200,10 @@ public void run() { e.addSuppressed(ex); } throw e; + } finally { + if (originalCl != null) { + Thread.currentThread().setContextClassLoader(originalCl); + } } } @@ -237,10 +241,15 @@ public void afterEach(ExtensionContext context) throws Exception { afterEachCallback.getClass().getMethod("afterEach", Object.class).invoke(afterEachCallback, actualTestInstance); } boolean nativeImageTest = isNativeTest(context); - runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName()) - .getDeclaredMethod("clearURL").invoke(null); - runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName()) - .getDeclaredMethod("tearDown", boolean.class).invoke(null, nativeImageTest); + ClassLoader original = setCCL(runningQuarkusApplication.getClassLoader()); + try { + runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName()) + .getDeclaredMethod("clearURL").invoke(null); + runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName()) + .getDeclaredMethod("tearDown", boolean.class).invoke(null, nativeImageTest); + } finally { + setCCL(original); + } } } @@ -254,17 +263,22 @@ public void beforeEach(ExtensionContext context) throws Exception { return; } if (!failedBoot) { - pushMockContext(); - for (Object beforeEachCallback : beforeEachCallbacks) { - beforeEachCallback.getClass().getMethod("beforeEach", Object.class).invoke(beforeEachCallback, - actualTestInstance); - } - boolean nativeImageTest = isNativeTest(context); - if (runningQuarkusApplication != null) { - runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName()) - .getDeclaredMethod("setURL", boolean.class).invoke(null, false); - runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName()) - .getDeclaredMethod("setup", boolean.class).invoke(null, nativeImageTest); + ClassLoader original = setCCL(runningQuarkusApplication.getClassLoader()); + try { + pushMockContext(); + for (Object beforeEachCallback : beforeEachCallbacks) { + beforeEachCallback.getClass().getMethod("beforeEach", Object.class).invoke(beforeEachCallback, + actualTestInstance); + } + boolean nativeImageTest = isNativeTest(context); + if (runningQuarkusApplication != null) { + runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName()) + .getDeclaredMethod("setURL", boolean.class).invoke(null, false); + runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName()) + .getDeclaredMethod("setup", boolean.class).invoke(null, nativeImageTest); + } + } finally { + setCCL(original); } } else { if (firstException != null) { @@ -310,7 +324,6 @@ public void beforeAll(ExtensionContext context) throws Exception { ensureStarted(context); if (runningQuarkusApplication != null) { pushMockContext(); - setCCL(runningQuarkusApplication.getClassLoader()); } } @@ -380,11 +393,15 @@ public T interceptTestClassConstructor(Invocation invocation, // We do this here as well, because when @TestInstance(Lifecycle.PER_CLASS) is used on a class, // interceptTestClassConstructor is called before beforeAll, meaning that the TCCL will not be set correctly // (for any test other than the first) unless this is done + old = null; if (runningQuarkusApplication != null) { - setCCL(runningQuarkusApplication.getClassLoader()); + old = setCCL(runningQuarkusApplication.getClassLoader()); } initTestState(extensionContext, state); + if (old != null) { + setCCL(old); + } return result; } @@ -477,10 +494,10 @@ private Object runExtensionMethod(ReflectiveInvocationContext invocation throws Throwable { Method newMethod = null; + ClassLoader old = setCCL(runningQuarkusApplication.getClassLoader()); try { Class c = Class.forName(extensionContext.getRequiredTestClass().getName(), true, Thread.currentThread().getContextClassLoader()); - ; while (c != Object.class) { if (c.getName().equals(invocationContext.getExecutable().getDeclaringClass().getName())) { try { @@ -522,6 +539,8 @@ private Object runExtensionMethod(ReflectiveInvocationContext invocation throw e.getCause(); } catch (IllegalAccessException | ClassNotFoundException e) { throw new RuntimeException(e); + } finally { + setCCL(old); } }