Skip to content

Commit

Permalink
Add tests for MATCHING_RESOURCE TestResource issue
Browse files Browse the repository at this point in the history
  • Loading branch information
snazy authored and geoand committed Nov 4, 2024
1 parent 0358242 commit 8c596db
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, String> initArgs) {
this.argument = requireNonNull(initArgs.get("resource.arg"));
}

@Override
public Map<String, String> 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 {
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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 {
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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 {
}
}
Original file line number Diff line number Diff line change
@@ -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("SomeResource1", null, "test-one"));
}
}
Original file line number Diff line number Diff line change
@@ -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, "SomeResource2", "test-two"));
}
}

0 comments on commit 8c596db

Please sign in to comment.