From 9d5307f88614af5286e1e91a33ff3d6b745fe769 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 5 Sep 2024 09:42:04 +0300 Subject: [PATCH] Fix handling of @WithTestResource and global resources Fixes: #43034 --- .../io/quarkus/test/common/TestResourceManager.java | 9 ++------- .../test/common/TestResourceManagerReloadTest.java | 12 ++++++++++++ .../java/io/quarkus/test/junit/TestResourceUtil.java | 8 ++++++-- 3 files changed, 20 insertions(+), 9 deletions(-) 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 144c987aaaf44..9a40ff79e4ef1 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 @@ -522,13 +522,13 @@ public static boolean testResourcesRequireReload(Set // now we need to check whether the sets contain the exact same MATCHING_RESOURCE test resources - Set inExistingAndNotNext = onlyMatchingResourceItems(existing); + Set inExistingAndNotNext = new HashSet<>(existing); inExistingAndNotNext.removeAll(next); if (!inExistingAndNotNext.isEmpty()) { return true; } - Set inNextAndNotExisting = onlyMatchingResourceItems(next); + Set inNextAndNotExisting = new HashSet<>(next); inNextAndNotExisting.removeAll(existing); if (!inNextAndNotExisting.isEmpty()) { return true; @@ -538,11 +538,6 @@ public static boolean testResourcesRequireReload(Set return false; } - private static Set onlyMatchingResourceItems(Set set) { - return set.stream().filter(i -> i.scope == MATCHING_RESOURCES).collect( - Collectors.toSet()); - } - private static boolean hasRestrictedToClassScope(Set existing) { for (TestResourceComparisonInfo info : existing) { if (info.scope == RESTRICTED_TO_CLASS) { 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 97b70119a1ba8..a4748969f9120 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 @@ -72,4 +72,16 @@ public void differentMultipleMatchingResource() { new TestResourceComparisonInfo("test2", MATCHING_RESOURCES), new TestResourceComparisonInfo("TEST", MATCHING_RESOURCES)))); } + + @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)))); + } } 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 2e3dbfcab7e85..975699c137ab8 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 @@ -43,9 +43,13 @@ static Set existingTestResources return Collections.emptySet(); } Closeable closeable = state.testResourceManager; - if (closeable instanceof TestResourceManager testResourceManager) { + if (closeable == null) { + return Collections.emptySet(); + } + // we can't compare with instanceof because of the different CL + if (TestResourceManager.class.getName().equals(closeable.getClass().getName())) { return TestResourceManagerReflections - .testResourceComparisonInfo(testResourceManager); + .testResourceComparisonInfo(closeable); } return Collections.emptySet(); }