Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InjectMock#convertScopes() - support annotations added by extensions #29378

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.it.mockbean;

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

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;

@QuarkusTest
class GreetingSingletonResourceMockTest {

// resteasy-reactive adds @Singleton automatically
@InjectMock(convertScopes = true)
GreetingResourceSingleton greetingResourceSingleton;

@Test
public void testAutoScope() {
Mockito.when(greetingResourceSingleton.greet()).thenReturn("Ahoj");
assertEquals("Ahoj", greetingResourceSingleton.greet());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import org.jboss.jandex.MethodInfo;

import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem;
import io.quarkus.arc.deployment.BeanDefiningAnnotationBuildItem;
import io.quarkus.arc.deployment.CustomScopeAnnotationsBuildItem;
import io.quarkus.arc.processor.Annotations;
import io.quarkus.arc.processor.AnnotationsTransformer;
import io.quarkus.arc.processor.DotNames;
import io.quarkus.builder.BuildChainBuilder;
Expand Down Expand Up @@ -52,6 +55,16 @@ public void execute(BuildContext context) {
return;
}

CustomScopeAnnotationsBuildItem scopes = context.consume(CustomScopeAnnotationsBuildItem.class);
// A bean defining annotation cannot be bound to multiple default scopes
Set<DotName> singletonBeanDefiningAnnotations = new HashSet<>();
for (BeanDefiningAnnotationBuildItem annotation : context
.consumeMulti(BeanDefiningAnnotationBuildItem.class)) {
if (DotNames.SINGLETON.equals(annotation.getDefaultScope())) {
singletonBeanDefiningAnnotations.add(annotation.getName());
}
}

// TODO: this annotation transformer is too simplistic and should be replaced
// by whatever build item comes out of the implementation
// of https://github.com/quarkusio/quarkus/issues/16572
Expand All @@ -67,14 +80,17 @@ public void transform(TransformationContext transformationContext) {
if (target.kind() == AnnotationTarget.Kind.CLASS) { // scope on bean case
ClassInfo classInfo = target.asClass();
if (isMatchingBean(classInfo)) {
if (classInfo.classAnnotation(DotNames.SINGLETON) != null) {
if (Annotations.contains(transformationContext.getAnnotations(), DotNames.SINGLETON)
|| hasSingletonBeanDefiningAnnotation(transformationContext)) {
replaceSingletonWithApplicationScoped(transformationContext);
}
}
} else if (target.kind() == AnnotationTarget.Kind.METHOD) { // CDI producer case
MethodInfo methodInfo = target.asMethod();
if ((methodInfo.annotation(DotNames.PRODUCES) != null)
&& (methodInfo.annotation(DotNames.SINGLETON) != null)) {
&& (Annotations.contains(transformationContext.getAnnotations(),
DotNames.SINGLETON)
|| hasSingletonBeanDefiningAnnotation(transformationContext))) {
DotName returnType = methodInfo.returnType().name();
if (mockTypes.contains(returnType)) {
replaceSingletonWithApplicationScoped(transformationContext);
Expand Down Expand Up @@ -105,9 +121,20 @@ private boolean isMatchingBean(ClassInfo classInfo) {
}
return false;
}

private boolean hasSingletonBeanDefiningAnnotation(TransformationContext transformationContext) {
if (singletonBeanDefiningAnnotations.isEmpty()
|| scopes.isScopeIn(transformationContext.getAnnotations())) {
return false;
}
return Annotations.containsAny(transformationContext.getAnnotations(),
singletonBeanDefiningAnnotations);
}

}));
}
}).produces(AnnotationsTransformerBuildItem.class).build();
}).produces(AnnotationsTransformerBuildItem.class).consumes(CustomScopeAnnotationsBuildItem.class)
.consumes(BeanDefiningAnnotationBuildItem.class).build();
}
};
}
Expand Down