Skip to content

Commit

Permalink
Make sure TCCL is restored in @QuarkusTest
Browse files Browse the repository at this point in the history
Fixes #9273
  • Loading branch information
stuartwdouglas committed May 21, 2020
1 parent 8254c62 commit 478b787
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ public void run() {
e.addSuppressed(ex);
}
throw e;
} finally {
if (originalCl != null) {
Thread.currentThread().setContextClassLoader(originalCl);
}
}
}

Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -310,7 +324,6 @@ public void beforeAll(ExtensionContext context) throws Exception {
ensureStarted(context);
if (runningQuarkusApplication != null) {
pushMockContext();
setCCL(runningQuarkusApplication.getClassLoader());
}
}

Expand Down Expand Up @@ -380,11 +393,15 @@ public <T> T interceptTestClassConstructor(Invocation<T> 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;
}

Expand Down Expand Up @@ -477,10 +494,10 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> 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 {
Expand Down Expand Up @@ -522,6 +539,8 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocation
throw e.getCause();
} catch (IllegalAccessException | ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
setCCL(old);
}
}

Expand Down

0 comments on commit 478b787

Please sign in to comment.