Skip to content

Commit

Permalink
Merge pull request quarkusio#17941 from geoand/quarkusio#17930
Browse files Browse the repository at this point in the history
Fix issue with custom test annotation and @QuarkusTestResource.List
  • Loading branch information
FroMage authored Jun 17, 2021
2 parents 9422b14 + 1310aae commit ea6c9b4
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.it.extension;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.enterprise.inject.Stereotype;

import io.quarkus.test.common.QuarkusTestResource;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@QuarkusTestResource.List({
@QuarkusTestResource(LifecycleManager.class)
})
@Stereotype
public @interface CustomResourceWithList {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.it.extension;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@CustomResourceWithList
@QuarkusTest
public class EndTestWithList {

@Test
public void test1() {
assertTrue(Counter.endCounter.get() <= 1);
}

@Test
public void test2() {
assertTrue(Counter.endCounter.get() <= 1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.it.extension;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@CustomResourceWithList
@QuarkusTest
public class StartTestWithList {

@Test
public void test1() {
assertTrue(Counter.startCounter.get() <= 1);
}

@Test
public void test2() {
assertTrue(Counter.startCounter.get() <= 1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -301,31 +301,17 @@ private Set<TestResourceClassEntry> getUniqueTestResourceClassEntries(Class<?> t

private void collectMetaAnnotations(Class<?> testClassFromTCCL, Set<TestResourceClassEntry> uniqueEntries) {
while (!testClassFromTCCL.getName().equals("java.lang.Object")) {
for (Annotation reflAnnotation : testClassFromTCCL.getAnnotations()) {
for (Annotation annotationAnnotation : reflAnnotation.annotationType().getAnnotations()) {
if (annotationAnnotation.annotationType() == QuarkusTestResource.class) {
QuarkusTestResource testResource = (QuarkusTestResource) annotationAnnotation;

// NOTE: we don't need to check restrictToAnnotatedClass because by design config-based annotations
// are not discovered outside the test class, so they're restricted
Class<? extends QuarkusTestResourceLifecycleManager> testResourceClass = testResource.value();

ResourceArg[] argsAnnotationValue = testResource.initArgs();
Map<String, String> args;
if (argsAnnotationValue.length == 0) {
args = Collections.emptyMap();
} else {
args = new HashMap<>();
for (ResourceArg arg : argsAnnotationValue) {
args.put(arg.name(), arg.value());
}
for (Annotation metaAnnotation : testClassFromTCCL.getAnnotations()) {
for (Annotation ann : metaAnnotation.annotationType().getAnnotations()) {
if (ann.annotationType() == QuarkusTestResource.class) {
addTestResourceEntry((QuarkusTestResource) ann, metaAnnotation, uniqueEntries);
hasPerTestResources = true;
break;
} else if (ann.annotationType() == QuarkusTestResource.List.class) {
for (QuarkusTestResource quarkusTestResource : ((QuarkusTestResource.List) ann).value()) {
addTestResourceEntry(quarkusTestResource, metaAnnotation, uniqueEntries);
}

boolean isParallel = testResource.parallel();

hasPerTestResources = true;
uniqueEntries.add(new TestResourceClassEntry(testResourceClass, args, reflAnnotation, isParallel));

break;
}
}
Expand All @@ -334,6 +320,27 @@ private void collectMetaAnnotations(Class<?> testClassFromTCCL, Set<TestResource
}
}

private void addTestResourceEntry(QuarkusTestResource quarkusTestResource, Annotation originalAnnotation,
Set<TestResourceClassEntry> uniqueEntries) {

// NOTE: we don't need to check restrictToAnnotatedClass because by design config-based annotations
// are not discovered outside the test class, so they're restricted
Class<? extends QuarkusTestResourceLifecycleManager> testResourceClass = quarkusTestResource.value();

ResourceArg[] argsAnnotationValue = quarkusTestResource.initArgs();
Map<String, String> args;
if (argsAnnotationValue.length == 0) {
args = Collections.emptyMap();
} else {
args = new HashMap<>();
for (ResourceArg arg : argsAnnotationValue) {
args.put(arg.name(), arg.value());
}
}
uniqueEntries
.add(new TestResourceClassEntry(testResourceClass, args, originalAnnotation, quarkusTestResource.parallel()));
}

@SuppressWarnings("unchecked")
private Class<? extends QuarkusTestResourceLifecycleManager> loadTestResourceClassFromTCCL(String className) {
try {
Expand Down

0 comments on commit ea6c9b4

Please sign in to comment.