diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SharedResource.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SharedResource.java new file mode 100644 index 0000000000000..ff77182a0f830 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SharedResource.java @@ -0,0 +1,44 @@ +package io.quarkus.it.extension.testresources; + +import static java.util.Objects.requireNonNull; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.Map; + +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +public class SharedResource implements QuarkusTestResourceLifecycleManager { + private String argument; + + @Override + public void init(Map initArgs) { + this.argument = requireNonNull(initArgs.get("resource.arg")); + } + + @Override + public Map start() { + System.err.println(getClass().getSimpleName() + " start with arg '" + argument + "'"); + return Map.of(); + } + + @Override + public void stop() { + System.err.println(getClass().getSimpleName() + " stop"); + } + + @Override + public void inject(TestInjector testInjector) { + testInjector.injectIntoFields(argument, + new TestInjector.AnnotatedAndMatchesType(SharedResourceAnnotation.class, String.class)); + } + + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public @interface SharedResourceAnnotation { + } +} diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SomeResource1.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SomeResource1.java new file mode 100644 index 0000000000000..7c0d4c8b6c7b6 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SomeResource1.java @@ -0,0 +1,35 @@ +package io.quarkus.it.extension.testresources; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.Map; + +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +public class SomeResource1 implements QuarkusTestResourceLifecycleManager { + @Override + public Map start() { + System.err.println(getClass().getSimpleName() + " start"); + return Map.of(); + } + + @Override + public void stop() { + System.err.println(getClass().getSimpleName() + " stop"); + } + + @Override + public void inject(TestInjector testInjector) { + testInjector.injectIntoFields(getClass().getSimpleName(), + new TestInjector.AnnotatedAndMatchesType(Resource1Annotation.class, String.class)); + } + + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public @interface Resource1Annotation { + } +} diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SomeResource2.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SomeResource2.java new file mode 100644 index 0000000000000..66cfb038e9b1b --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SomeResource2.java @@ -0,0 +1,35 @@ +package io.quarkus.it.extension.testresources; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.Map; + +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +public class SomeResource2 implements QuarkusTestResourceLifecycleManager { + @Override + public Map start() { + System.err.println(getClass().getSimpleName() + " start"); + return Map.of(); + } + + @Override + public void stop() { + System.err.println(getClass().getSimpleName() + " stop"); + } + + @Override + public void inject(TestInjector testInjector) { + testInjector.injectIntoFields(getClass().getSimpleName(), + new TestInjector.AnnotatedAndMatchesType(Resource2Annotation.class, String.class)); + } + + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public @interface Resource2Annotation { + } +} diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/WithResourcesPoliciesFirstTest.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/WithResourcesPoliciesFirstTest.java new file mode 100644 index 0000000000000..477fef0c94af4 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/WithResourcesPoliciesFirstTest.java @@ -0,0 +1,31 @@ +package io.quarkus.it.extension.testresources; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.common.ResourceArg; +import io.quarkus.test.common.TestResourceScope; +import io.quarkus.test.common.WithTestResource; +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +@WithTestResource(value = SomeResource1.class, scope = TestResourceScope.MATCHING_RESOURCES) +@WithTestResource(value = SharedResource.class, scope = TestResourceScope.MATCHING_RESOURCES, initArgs = { + @ResourceArg(name = "resource.arg", value = "test-one") }) +public class WithResourcesPoliciesFirstTest { + @SomeResource1.Resource1Annotation + String resource1; + @SomeResource2.Resource2Annotation + String resource2; + @SharedResource.SharedResourceAnnotation + String sharedResource; + + @Test + public void checkOnlyResource1started() { + assertThat(Arrays.asList(resource1, resource2, sharedResource)).isEqualTo( + Arrays.asList("SomeResource1", null, "test-one")); + } +} diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/WithResourcesPoliciesSecondTest.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/WithResourcesPoliciesSecondTest.java new file mode 100644 index 0000000000000..2e4215154fba4 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/WithResourcesPoliciesSecondTest.java @@ -0,0 +1,31 @@ +package io.quarkus.it.extension.testresources; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.common.ResourceArg; +import io.quarkus.test.common.TestResourceScope; +import io.quarkus.test.common.WithTestResource; +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +@WithTestResource(value = SomeResource2.class, scope = TestResourceScope.MATCHING_RESOURCES) +@WithTestResource(value = SharedResource.class, scope = TestResourceScope.MATCHING_RESOURCES, initArgs = { + @ResourceArg(name = "resource.arg", value = "test-two") }) +public class WithResourcesPoliciesSecondTest { + @SomeResource1.Resource1Annotation + String resource1; + @SomeResource2.Resource2Annotation + String resource2; + @SharedResource.SharedResourceAnnotation + String sharedResource; + + @Test + public void checkOnlyResource1started() { + assertThat(Arrays.asList(resource1, resource2, sharedResource)).isEqualTo( + Arrays.asList(null, "SomeResource2", "test-two")); + } +} diff --git a/test-framework/common/src/main/java/io/quarkus/test/common/TestResourceManager.java b/test-framework/common/src/main/java/io/quarkus/test/common/TestResourceManager.java index 9a40ff79e4ef1..ca18f00b0a814 100644 --- a/test-framework/common/src/main/java/io/quarkus/test/common/TestResourceManager.java +++ b/test-framework/common/src/main/java/io/quarkus/test/common/TestResourceManager.java @@ -108,7 +108,7 @@ public TestResourceManager(Class testClass, this.testResourceComparisonInfo = new HashSet<>(); for (TestResourceClassEntry uniqueEntry : uniqueEntries) { testResourceComparisonInfo.add(new TestResourceComparisonInfo( - uniqueEntry.testResourceLifecycleManagerClass().getName(), uniqueEntry.getScope())); + uniqueEntry.testResourceLifecycleManagerClass().getName(), uniqueEntry.getScope(), uniqueEntry.args)); } Set remainingUniqueEntries = initParallelTestResources(uniqueEntries); @@ -326,7 +326,8 @@ public static Set testResourceCo } Set result = new HashSet<>(uniqueEntries.size()); for (TestResourceClassEntry entry : uniqueEntries) { - result.add(new TestResourceComparisonInfo(entry.testResourceLifecycleManagerClass().getName(), entry.getScope())); + result.add(new TestResourceComparisonInfo(entry.testResourceLifecycleManagerClass().getName(), entry.getScope(), + entry.args)); } return result; } @@ -439,15 +440,15 @@ private static void addTestResourceEntry(QuarkusTestResource quarkusTestResource private static Collection findTestResourceInstancesOfClass(Class testClass, IndexView index) { // collect all test supertypes for matching per-test targets - Set testClasses = new HashSet<>(); + Set currentTestClassHierarchy = new HashSet<>(); Class current = testClass; while (current != Object.class) { - testClasses.add(current.getName()); + currentTestClassHierarchy.add(current.getName()); current = current.getSuperclass(); } current = testClass.getEnclosingClass(); while (current != null) { - testClasses.add(current.getName()); + currentTestClassHierarchy.add(current.getName()); current = current.getEnclosingClass(); } @@ -455,7 +456,7 @@ private static Collection findTestResourceInstancesOfClass(C for (DotName testResourceClasses : List.of(WITH_TEST_RESOURCE, QUARKUS_TEST_RESOURCE)) { for (AnnotationInstance annotation : index.getAnnotations(testResourceClasses)) { - if (keepTestResourceAnnotation(annotation, annotation.target().asClass(), testClasses)) { + if (keepTestResourceAnnotation(annotation, annotation.target().asClass(), currentTestClassHierarchy)) { testResourceAnnotations.add(annotation); } } @@ -466,7 +467,8 @@ private static Collection findTestResourceInstancesOfClass(C for (AnnotationInstance annotation : index.getAnnotations(testResourceListClasses)) { for (AnnotationInstance nestedAnnotation : annotation.value().asNestedArray()) { // keep the list target - if (keepTestResourceAnnotation(nestedAnnotation, annotation.target().asClass(), testClasses)) { + if (keepTestResourceAnnotation(nestedAnnotation, annotation.target().asClass(), + currentTestClassHierarchy)) { testResourceAnnotations.add(nestedAnnotation); } } @@ -477,21 +479,22 @@ private static Collection findTestResourceInstancesOfClass(C } private static boolean keepTestResourceAnnotation(AnnotationInstance annotation, ClassInfo targetClass, - Set testClasses) { + Set currentTestClassHierarchy) { if (targetClass.isAnnotation()) { // meta-annotations have already been handled in collectMetaAnnotations return false; } if (restrictToAnnotatedClass(annotation)) { - return testClasses.contains(targetClass.name().toString('.')); + return currentTestClassHierarchy.contains(targetClass.name().toString('.')); } return true; } private static boolean restrictToAnnotatedClass(AnnotationInstance annotation) { - return TestResourceClassEntryHandler.determineScope(annotation) == RESTRICTED_TO_CLASS; + return TestResourceClassEntryHandler.determineScope(annotation) == RESTRICTED_TO_CLASS + || TestResourceClassEntryHandler.determineScope(annotation) == MATCHING_RESOURCES; } /** @@ -516,7 +519,7 @@ public static boolean testResourcesRequireReload(Set return false; } - if (hasRestrictedToClassScope(existing) || hasRestrictedToClassScope(next)) { + if (anyResourceRestrictedToClass(existing) || anyResourceRestrictedToClass(next)) { return true; } @@ -538,8 +541,8 @@ public static boolean testResourcesRequireReload(Set return false; } - private static boolean hasRestrictedToClassScope(Set existing) { - for (TestResourceComparisonInfo info : existing) { + private static boolean anyResourceRestrictedToClass(Set testResources) { + for (TestResourceComparisonInfo info : testResources) { if (info.scope == RESTRICTED_TO_CLASS) { return true; } @@ -603,7 +606,8 @@ public TestResourceScope getScope() { } } - public record TestResourceComparisonInfo(String testResourceLifecycleManagerClass, TestResourceScope scope) { + public record TestResourceComparisonInfo(String testResourceLifecycleManagerClass, TestResourceScope scope, + Map args) { } diff --git a/test-framework/common/src/test/java/io/quarkus/test/common/TestResourceManagerReloadTest.java b/test-framework/common/src/test/java/io/quarkus/test/common/TestResourceManagerReloadTest.java index a4748969f9120..782f0d9ad2076 100644 --- a/test-framework/common/src/test/java/io/quarkus/test/common/TestResourceManagerReloadTest.java +++ b/test-framework/common/src/test/java/io/quarkus/test/common/TestResourceManagerReloadTest.java @@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Collections; +import java.util.Map; import java.util.Set; import org.junit.jupiter.api.Test; @@ -22,66 +23,87 @@ public void emptyResources() { @Test public void differentCount() { assertTrue(testResourcesRequireReload(Collections.emptySet(), - Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS)))); + Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS, Map.of())))); - assertTrue(testResourcesRequireReload(Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS)), + assertTrue(testResourcesRequireReload(Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS, Map.of())), Collections.emptySet())); } @Test public void sameSingleRestrictedToClassResource() { assertTrue(testResourcesRequireReload( - Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS)), - Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS)))); + Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS, Map.of())), + Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS, Map.of())))); } @Test public void sameSingleMatchingResource() { assertFalse(testResourcesRequireReload( - Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES)), - Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES)))); + Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of())), + Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of())))); + } + + @Test + public void sameSingleMatchingResourceWithArgs() { + assertFalse(testResourcesRequireReload( + Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of("a", "b"))), + Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of("a", "b"))))); + } + + @Test + public void sameSingleResourceDifferentArgs() { + assertTrue(testResourcesRequireReload( + Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of())), + Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of("a", "b"))))); + } + + @Test + public void sameSingleResourceDifferentArgValues() { + assertTrue(testResourcesRequireReload( + Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of("a", "b"))), + Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of("a", "c"))))); } @Test public void differentSingleMatchingResource() { assertTrue(testResourcesRequireReload( - Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES)), - Set.of(new TestResourceComparisonInfo("test2", MATCHING_RESOURCES)))); + Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of())), + Set.of(new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of())))); } @Test public void sameMultipleMatchingResource() { assertFalse(testResourcesRequireReload( Set.of( - new TestResourceComparisonInfo("test", MATCHING_RESOURCES), - new TestResourceComparisonInfo("test2", MATCHING_RESOURCES), - new TestResourceComparisonInfo("test3", GLOBAL)), - Set.of(new TestResourceComparisonInfo("test3", GLOBAL), - new TestResourceComparisonInfo("test2", MATCHING_RESOURCES), - new TestResourceComparisonInfo("test", MATCHING_RESOURCES)))); + new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of()), + new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()), + new TestResourceComparisonInfo("test3", GLOBAL, Map.of())), + Set.of(new TestResourceComparisonInfo("test3", GLOBAL, Map.of()), + new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()), + new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of())))); } @Test public void differentMultipleMatchingResource() { assertTrue(testResourcesRequireReload( Set.of( - new TestResourceComparisonInfo("test", MATCHING_RESOURCES), - new TestResourceComparisonInfo("test2", MATCHING_RESOURCES), - new TestResourceComparisonInfo("test3", GLOBAL)), - Set.of(new TestResourceComparisonInfo("test3", GLOBAL), - new TestResourceComparisonInfo("test2", MATCHING_RESOURCES), - new TestResourceComparisonInfo("TEST", MATCHING_RESOURCES)))); + new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of()), + new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()), + new TestResourceComparisonInfo("test3", GLOBAL, Map.of())), + Set.of(new TestResourceComparisonInfo("test3", GLOBAL, Map.of()), + new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()), + new TestResourceComparisonInfo("TEST", MATCHING_RESOURCES, Map.of())))); } @Test public void differentGlobalMultipleMatchingResource() { assertTrue(testResourcesRequireReload( Set.of( - new TestResourceComparisonInfo("test", MATCHING_RESOURCES), - new TestResourceComparisonInfo("test2", MATCHING_RESOURCES), - new TestResourceComparisonInfo("test4", GLOBAL)), - Set.of(new TestResourceComparisonInfo("test3", GLOBAL), - new TestResourceComparisonInfo("test2", MATCHING_RESOURCES), - new TestResourceComparisonInfo("TEST", MATCHING_RESOURCES)))); + new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of()), + new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()), + new TestResourceComparisonInfo("test4", GLOBAL, Map.of())), + Set.of(new TestResourceComparisonInfo("test3", GLOBAL, Map.of()), + new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()), + new TestResourceComparisonInfo("TEST", MATCHING_RESOURCES, Map.of())))); } } diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractTestWithCallbacksExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractTestWithCallbacksExtension.java index c48fdc5624a9e..0299c686e134d 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractTestWithCallbacksExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractTestWithCallbacksExtension.java @@ -100,7 +100,7 @@ protected void invokeAfterAllCallbacks(Class clazz, Object testContext) throw invokeCallbacks(afterAllCallbacks, "afterAll", clazz, testContext); } - protected void populateCallbacks(ClassLoader classLoader) throws ClassNotFoundException { + protected static void clearCallbacks() { beforeClassCallbacks = new ArrayList<>(); afterConstructCallbacks = new ArrayList<>(); beforeEachCallbacks = new ArrayList<>(); @@ -108,6 +108,10 @@ protected void populateCallbacks(ClassLoader classLoader) throws ClassNotFoundEx afterTestCallbacks = new ArrayList<>(); afterEachCallbacks = new ArrayList<>(); afterAllCallbacks = new ArrayList<>(); + } + + protected void populateCallbacks(ClassLoader classLoader) throws ClassNotFoundException { + clearCallbacks(); ServiceLoader quarkusTestBeforeClassLoader = ServiceLoader .load(Class.forName(QuarkusTestBeforeClassCallback.class.getName(), false, classLoader), classLoader); diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestExtensionState.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestExtensionState.java index 0983096593d22..5c51ae0d062f4 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestExtensionState.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestExtensionState.java @@ -8,11 +8,11 @@ public class IntegrationTestExtensionState extends QuarkusTestExtensionState { - private Map sysPropRestore; + private final Map sysPropRestore; public IntegrationTestExtensionState(TestResourceManager testResourceManager, Closeable resource, - Map sysPropRestore) { - super(testResourceManager, resource); + Runnable clearCallbacks, Map sysPropRestore) { + super(testResourceManager, resource, clearCallbacks); this.sysPropRestore = sysPropRestore; } diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java index c2c6d43c99ac2..1dd9e846a844f 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java @@ -13,12 +13,10 @@ import static io.quarkus.test.junit.IntegrationTestUtil.handleDevServices; import static io.quarkus.test.junit.IntegrationTestUtil.readQuarkusArtifactProperties; import static io.quarkus.test.junit.IntegrationTestUtil.startLauncher; -import static io.quarkus.test.junit.TestResourceUtil.testResourcesRequireReload; import static io.quarkus.test.junit.TestResourceUtil.TestResourceManagerReflections.copyEntriesFromProfile; import java.io.Closeable; import java.io.File; -import java.io.IOException; import java.lang.reflect.Field; import java.nio.file.Path; import java.time.Duration; @@ -108,7 +106,6 @@ public void beforeTestExecution(ExtensionContext context) throws Exception { } else { throwBootFailureException(); - return; } } @@ -305,7 +302,7 @@ public void close() throws Throwable { Closeable resource = new IntegrationTestExtensionStateResource(launcher, devServicesLaunchResult.getCuratedApplication()); IntegrationTestExtensionState state = new IntegrationTestExtensionState(testResourceManager, resource, - sysPropRestore); + AbstractTestWithCallbacksExtension::clearCallbacks, sysPropRestore); testHttpEndpointProviders = TestHttpEndpointProvider.load(); return state; @@ -467,7 +464,7 @@ public IntegrationTestExtensionStateResource(ArtifactLauncher launcher, } @Override - public void close() throws IOException { + public void close() { if (launcher != null) { try { launcher.close(); 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 33e6a6175196d..4d9c48d8ca2ba 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 @@ -3,7 +3,6 @@ import static io.quarkus.test.junit.IntegrationTestUtil.activateLogging; import java.io.Closeable; -import java.io.IOException; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.reflect.Constructor; @@ -272,7 +271,7 @@ public Thread newThread(Runnable r) { Closeable shutdownTask = new Closeable() { @Override - public void close() throws IOException { + public void close() { TracingHandler.quarkusStopping(); try { runningQuarkusApplication.close(); @@ -295,8 +294,7 @@ public void close() throws IOException { } } }; - ExtensionState state = new ExtensionState(testResourceManager, shutdownTask); - return state; + return new ExtensionState(testResourceManager, shutdownTask, AbstractTestWithCallbacksExtension::clearCallbacks); } catch (Throwable e) { if (!InitialConfigurator.DELAYED_HANDLER.isActivated()) { activateLogging(); @@ -1176,12 +1174,12 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con public static class ExtensionState extends QuarkusTestExtensionState { - public ExtensionState(Closeable testResourceManager, Closeable resource) { - super(testResourceManager, resource); + public ExtensionState(Closeable testResourceManager, Closeable resource, Runnable clearCallbacks) { + super(testResourceManager, resource, clearCallbacks); } @Override - protected void doClose() throws IOException { + protected void doClose() { ClassLoader old = Thread.currentThread().getContextClassLoader(); if (runningQuarkusApplication != null) { Thread.currentThread().setContextClassLoader(runningQuarkusApplication.getClassLoader()); diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtensionState.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtensionState.java index 6024cb50f400c..121bf20220c0d 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtensionState.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtensionState.java @@ -15,11 +15,13 @@ public class QuarkusTestExtensionState implements ExtensionContext.Store.Closeab protected final Closeable testResourceManager; protected final Closeable resource; private final Thread shutdownHook; + private final Runnable clearCallbacks; private Throwable testErrorCause; - public QuarkusTestExtensionState(Closeable testResourceManager, Closeable resource) { + public QuarkusTestExtensionState(Closeable testResourceManager, Closeable resource, Runnable clearCallbacks) { this.testResourceManager = testResourceManager; this.resource = resource; + this.clearCallbacks = clearCallbacks; this.shutdownHook = new Thread(new Runnable() { @Override public void run() { @@ -40,6 +42,7 @@ public Throwable getTestErrorCause() { public void close() throws IOException { if (closed.compareAndSet(false, true)) { doClose(); + clearCallbacks.run(); try { Runtime.getRuntime().removeShutdownHook(shutdownHook); diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/TestResourceUtil.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/TestResourceUtil.java index 975699c137ab8..eb1fea1f4ff62 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/TestResourceUtil.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/TestResourceUtil.java @@ -192,8 +192,10 @@ static Set testResourceCompariso if (originalTestResourceScope != null) { testResourceScope = TestResourceScope.valueOf(originalTestResourceScope.toString()); } + Object originalArgs = entry.getClass().getMethod("args").invoke(entry); + Map args = (Map) originalArgs; result.add(new TestResourceManager.TestResourceComparisonInfo(testResourceLifecycleManagerClass, - testResourceScope)); + testResourceScope, args)); } return result;