Skip to content

Commit

Permalink
Limit MATCHING_RESOURCES TestResources to the test that declares them
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand authored and snazy committed Nov 4, 2024
1 parent d8f1436 commit ad71c6d
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,23 +439,23 @@ private static void addTestResourceEntry(QuarkusTestResource quarkusTestResource

private static Collection<AnnotationInstance> findTestResourceInstancesOfClass(Class<?> testClass, IndexView index) {
// collect all test supertypes for matching per-test targets
Set<String> testClasses = new HashSet<>();
Set<String> 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();
}

Set<AnnotationInstance> testResourceAnnotations = new LinkedHashSet<>();

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);
}
}
Expand All @@ -466,7 +466,8 @@ private static Collection<AnnotationInstance> 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);
}
}
Expand All @@ -477,21 +478,22 @@ private static Collection<AnnotationInstance> findTestResourceInstancesOfClass(C
}

private static boolean keepTestResourceAnnotation(AnnotationInstance annotation, ClassInfo targetClass,
Set<String> testClasses) {
Set<String> 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;
}

/**
Expand Down

0 comments on commit ad71c6d

Please sign in to comment.