Skip to content

Commit

Permalink
Merge pull request #31383 from scrocquesel/gh-31359
Browse files Browse the repository at this point in the history
Add support for repeatable annotation-based test resources
  • Loading branch information
geoand authored Feb 28, 2023
2 parents 2f54e52 + b82c7dc commit 4bbc70e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/src/main/asciidoc/getting-started-testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,30 @@ public class KubernetesServerTestResource
}
----

If you want to make the annotation repeatable, the containing annotation type must be annotated with `@QuarkusTestResourceRepeatable`.
For example, this would define a repeatable `@WithRepeatableTestResource` annotation.

[source,java]
----
@QuarkusTestResource(KubernetesServerTestResource.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Repeatable(WithRepeatableTestResource.List.class)
public @interface WithRepeatableTestResource {
String key() default "";
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@QuarkusTestResourceRepeatable(WithRepeatableTestResource.class)
@interface List {
WithRepeatableTestResource[] value();
}
}
----



== Hang Detection

`@QuarkusTest` has support for hang detection to help diagnose any unexpected hangs. If no progress is made for a specified
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.test.common;

import java.lang.annotation.Annotation;
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;

/**
* Used to indicate the type of the <em>repeatable annotation
* type</em> annotated with a {@code QuarkusTestResource} annotations.
*
*/
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface QuarkusTestResourceRepeatable {

/**
* @return The class annotated with a {@code QuarkusTestResource} annotations.
*/
Class<? extends Annotation> value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,17 @@ private void collectMetaAnnotations(Class<?> testClassFromTCCL, Function<Class<?
}
hasPerTestResources = true;
break;
} else if (ann.annotationType() == QuarkusTestResourceRepeatable.class) {
for (Annotation repeatableMetaAnn : testClassFromTCCL
.getAnnotationsByType(((QuarkusTestResourceRepeatable) ann).value())) {
QuarkusTestResource quarkusTestResource = repeatableMetaAnn.annotationType()
.getAnnotation(QuarkusTestResource.class);
if (quarkusTestResource != null) {
addTestResourceEntry(quarkusTestResource, repeatableMetaAnn, uniqueEntries);
hasPerTestResources = true;
}

}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -177,4 +182,55 @@ public int order() {
return 2;
}
}

@Test
void testAnnotationBased() {
TestResourceManager manager = new TestResourceManager(RepeatableAnnotationBasedTestResourcesTest.class);
manager.init("test");
Map<String, String> props = manager.start();
Assertions.assertEquals("value", props.get("annotationkey1"));
Assertions.assertEquals("value", props.get("annotationkey2"));
}

public static class AnnotationBasedQuarkusTestResource
implements QuarkusTestResourceConfigurableLifecycleManager<WithAnnotationBasedTestResource> {

private String key;

@Override
public void init(WithAnnotationBasedTestResource annotation) {
this.key = annotation.key();
}

@Override
public Map<String, String> start() {
Map<String, String> props = new HashMap<>();
props.put(key, "value");
return props;
}

@Override
public void stop() {
}
}

@QuarkusTestResource(AnnotationBasedQuarkusTestResource.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Repeatable(WithAnnotationBasedTestResource.List.class)
public @interface WithAnnotationBasedTestResource {
String key() default "";

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@QuarkusTestResourceRepeatable(WithAnnotationBasedTestResource.class)
@interface List {
WithAnnotationBasedTestResource[] value();
}
}

@WithAnnotationBasedTestResource(key = "annotationkey1")
@WithAnnotationBasedTestResource(key = "annotationkey2")
public static class RepeatableAnnotationBasedTestResourcesTest {
}
}

0 comments on commit 4bbc70e

Please sign in to comment.