forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QuarkusComponentTest: register additional annotations transformers
- introduce JaxrsSingletonTransformer to add Singleton to a JAX-RS component that has no scope - resolves quarkusio#35313
- Loading branch information
1 parent
4d6c0cd
commit ef6ce14
Showing
7 changed files
with
248 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...k/junit5-component/src/main/java/io/quarkus/test/component/JaxrsSingletonTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.quarkus.test.component; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.inject.Singleton; | ||
|
||
import org.jboss.jandex.AnnotationTarget.Kind; | ||
import org.jboss.jandex.DotName; | ||
|
||
import io.quarkus.arc.processor.Annotations; | ||
import io.quarkus.arc.processor.AnnotationsTransformer; | ||
import io.quarkus.arc.processor.BuiltinScope; | ||
|
||
/** | ||
* Add {@link Singleton} to a JAX-RS component that has no scope annotation. | ||
*/ | ||
public class JaxrsSingletonTransformer implements AnnotationsTransformer { | ||
|
||
private final List<DotName> ANNOTATIONS = List.of(DotName.createSimple("jakarta.ws.rs.Path"), | ||
DotName.createSimple("jakarta.ws.rs.ApplicationPath"), DotName.createSimple("jakarta.ws.rs.ext.Provider")); | ||
|
||
@Override | ||
public boolean appliesTo(Kind kind) { | ||
return Kind.CLASS == kind; | ||
} | ||
|
||
@Override | ||
public void transform(TransformationContext context) { | ||
// Note that custom scopes are not supported yet | ||
if (BuiltinScope.isIn(context.getAnnotations())) { | ||
return; | ||
} | ||
if (Annotations.containsAny(context.getAnnotations(), ANNOTATIONS)) { | ||
context.transform().add(Singleton.class).done(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
.../junit5-component/src/test/java/io/quarkus/test/component/AnnotationsTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.quarkus.test.component; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.jboss.jandex.DotName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.mockito.Mockito; | ||
|
||
import io.quarkus.arc.processor.AnnotationsTransformer; | ||
import io.quarkus.test.InjectMock; | ||
import io.quarkus.test.component.beans.Charlie; | ||
|
||
public class AnnotationsTransformerTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusComponentTestExtension extension = new QuarkusComponentTestExtension() | ||
.addAnnotationsTransformer(AnnotationsTransformer.appliedToClass() | ||
.whenClass(c -> c.declaredAnnotations().isEmpty() | ||
&& c.annotationsMap().containsKey(DotName.createSimple(Inject.class))) | ||
.thenTransform(t -> t.add(Singleton.class))); | ||
|
||
@Inject | ||
NotABean bean; | ||
|
||
@InjectMock | ||
Charlie charlie; | ||
|
||
@Test | ||
public void testPing() { | ||
Mockito.when(charlie.ping()).thenReturn("foo"); | ||
assertEquals("foo", bean.ping()); | ||
} | ||
|
||
// @Singleton should be added automatically | ||
public static class NotABean { | ||
|
||
@Inject | ||
Charlie charlie; | ||
|
||
public String ping() { | ||
return charlie.ping(); | ||
} | ||
|
||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
...onent/src/test/java/io/quarkus/test/component/declarative/AnnotationsTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.quarkus.test.component.declarative; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.jboss.jandex.AnnotationTarget.Kind; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.DotName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import io.quarkus.arc.processor.AnnotationsTransformer; | ||
import io.quarkus.test.InjectMock; | ||
import io.quarkus.test.component.QuarkusComponentTest; | ||
import io.quarkus.test.component.beans.Charlie; | ||
import io.quarkus.test.component.declarative.AnnotationsTransformerTest.MyAnnotationsTransformer; | ||
|
||
@QuarkusComponentTest(annotationsTransformers = MyAnnotationsTransformer.class) | ||
public class AnnotationsTransformerTest { | ||
|
||
@Inject | ||
NotABean bean; | ||
|
||
@InjectMock | ||
Charlie charlie; | ||
|
||
@Test | ||
public void testPing() { | ||
Mockito.when(charlie.ping()).thenReturn("foo"); | ||
assertEquals("foo", bean.ping()); | ||
} | ||
|
||
// @Singleton should be added automatically | ||
public static class NotABean { | ||
|
||
@Inject | ||
Charlie charlie; | ||
|
||
public String ping() { | ||
return charlie.ping(); | ||
} | ||
|
||
} | ||
|
||
public static class MyAnnotationsTransformer implements AnnotationsTransformer { | ||
|
||
@Override | ||
public boolean appliesTo(Kind kind) { | ||
return Kind.CLASS == kind; | ||
} | ||
|
||
@Override | ||
public void transform(TransformationContext context) { | ||
ClassInfo c = context.getTarget().asClass(); | ||
if (c.declaredAnnotations().isEmpty() | ||
&& c.annotationsMap().containsKey(DotName.createSimple(Inject.class))) { | ||
context.transform().add(Singleton.class).done(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...nt/src/test/java/io/quarkus/test/component/declarative/JaxrsSingletonTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.quarkus.test.component.declarative; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import io.quarkus.test.InjectMock; | ||
import io.quarkus.test.component.QuarkusComponentTest; | ||
import io.quarkus.test.component.beans.Charlie; | ||
|
||
@QuarkusComponentTest | ||
public class JaxrsSingletonTransformerTest { | ||
|
||
@Inject | ||
MyResource resource; | ||
|
||
@InjectMock | ||
Charlie charlie; | ||
|
||
@Test | ||
public void testPing() { | ||
Mockito.when(charlie.ping()).thenReturn("foo"); | ||
assertEquals("foo", resource.ping()); | ||
} | ||
|
||
// @Singleton should be added automatically | ||
@Path("my") | ||
public static class MyResource { | ||
|
||
@Inject | ||
Charlie charlie; | ||
|
||
@GET | ||
public String ping() { | ||
return charlie.ping(); | ||
} | ||
|
||
} | ||
|
||
} |