diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SharedResource.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SharedResource.java new file mode 100644 index 00000000000000..ff77182a0f8306 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SharedResource.java @@ -0,0 +1,44 @@ +package io.quarkus.it.extension.testresources; + +import static java.util.Objects.requireNonNull; + +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; +import java.util.Map; + +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +public class SharedResource implements QuarkusTestResourceLifecycleManager { + private String argument; + + @Override + public void init(Map initArgs) { + this.argument = requireNonNull(initArgs.get("resource.arg")); + } + + @Override + public Map start() { + System.err.println(getClass().getSimpleName() + " start with arg '" + argument + "'"); + return Map.of(); + } + + @Override + public void stop() { + System.err.println(getClass().getSimpleName() + " stop"); + } + + @Override + public void inject(TestInjector testInjector) { + testInjector.injectIntoFields(argument, + new TestInjector.AnnotatedAndMatchesType(SharedResourceAnnotation.class, String.class)); + } + + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public @interface SharedResourceAnnotation { + } +} diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SomeResource1.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SomeResource1.java new file mode 100644 index 00000000000000..7c0d4c8b6c7b65 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SomeResource1.java @@ -0,0 +1,35 @@ +package io.quarkus.it.extension.testresources; + +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; +import java.util.Map; + +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +public class SomeResource1 implements QuarkusTestResourceLifecycleManager { + @Override + public Map start() { + System.err.println(getClass().getSimpleName() + " start"); + return Map.of(); + } + + @Override + public void stop() { + System.err.println(getClass().getSimpleName() + " stop"); + } + + @Override + public void inject(TestInjector testInjector) { + testInjector.injectIntoFields(getClass().getSimpleName(), + new TestInjector.AnnotatedAndMatchesType(Resource1Annotation.class, String.class)); + } + + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public @interface Resource1Annotation { + } +} diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SomeResource2.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SomeResource2.java new file mode 100644 index 00000000000000..66cfb038e9b1b3 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/SomeResource2.java @@ -0,0 +1,35 @@ +package io.quarkus.it.extension.testresources; + +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; +import java.util.Map; + +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +public class SomeResource2 implements QuarkusTestResourceLifecycleManager { + @Override + public Map start() { + System.err.println(getClass().getSimpleName() + " start"); + return Map.of(); + } + + @Override + public void stop() { + System.err.println(getClass().getSimpleName() + " stop"); + } + + @Override + public void inject(TestInjector testInjector) { + testInjector.injectIntoFields(getClass().getSimpleName(), + new TestInjector.AnnotatedAndMatchesType(Resource2Annotation.class, String.class)); + } + + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public @interface Resource2Annotation { + } +} diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/WithResourcesPoliciesFirstTest.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/WithResourcesPoliciesFirstTest.java new file mode 100644 index 00000000000000..5dba4b0f303a96 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/WithResourcesPoliciesFirstTest.java @@ -0,0 +1,31 @@ +package io.quarkus.it.extension.testresources; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.common.ResourceArg; +import io.quarkus.test.common.TestResourceScope; +import io.quarkus.test.common.WithTestResource; +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +@WithTestResource(value = SomeResource1.class, scope = TestResourceScope.MATCHING_RESOURCES) +@WithTestResource(value = SharedResource.class, scope = TestResourceScope.MATCHING_RESOURCES, initArgs = { + @ResourceArg(name = "resource.arg", value = "test-one") }) +public class WithResourcesPoliciesFirstTest { + @SomeResource1.Resource1Annotation + String resource1; + @SomeResource2.Resource2Annotation + String resource2; + @SharedResource.SharedResourceAnnotation + String sharedResource; + + @Test + public void checkOnlyResource1started() { + assertThat(Arrays.asList(resource1, resource2, sharedResource)).isEqualTo( + Arrays.asList("resource1", null, "test-one")); + } +} diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/WithResourcesPoliciesSecondTest.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/WithResourcesPoliciesSecondTest.java new file mode 100644 index 00000000000000..5b296898d4fa69 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/testresources/WithResourcesPoliciesSecondTest.java @@ -0,0 +1,31 @@ +package io.quarkus.it.extension.testresources; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.common.ResourceArg; +import io.quarkus.test.common.TestResourceScope; +import io.quarkus.test.common.WithTestResource; +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +@WithTestResource(value = SomeResource2.class, scope = TestResourceScope.MATCHING_RESOURCES) +@WithTestResource(value = SharedResource.class, scope = TestResourceScope.MATCHING_RESOURCES, initArgs = { + @ResourceArg(name = "resource.arg", value = "test-two") }) +public class WithResourcesPoliciesSecondTest { + @SomeResource1.Resource1Annotation + String resource1; + @SomeResource2.Resource2Annotation + String resource2; + @SharedResource.SharedResourceAnnotation + String sharedResource; + + @Test + public void checkOnlyResource1started() { + assertThat(Arrays.asList(resource1, resource2, sharedResource)).isEqualTo( + Arrays.asList(null, "resource2", "test-two")); + } +}