From ad71c6d54156fb49a7b97ecd47a0f80352cc956f Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 4 Nov 2024 13:14:03 +0200 Subject: [PATCH] Limit MATCHING_RESOURCES TestResources to the test that declares them --- .../test/common/TestResourceManager.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 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 9a40ff79e4ef1..a66290fbf1295 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 @@ -439,15 +439,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 +455,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 +466,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 +478,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; } /**