Skip to content

Commit

Permalink
Merge pull request #29378 from mkouba/injectmock-convert-extension-an…
Browse files Browse the repository at this point in the history
…notations
  • Loading branch information
gastaldi authored Nov 21, 2022
2 parents f914563 + 15c5083 commit 03b2197
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
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

0 comments on commit 03b2197

Please sign in to comment.