-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ArC - introduce the convenient AnnotationsTransfromer.Builder
- Loading branch information
Showing
2 changed files
with
354 additions
and
1 deletion.
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
126 changes: 126 additions & 0 deletions
126
...ava/io/quarkus/arc/test/buildextension/annotations/AnnotationsTransformerBuilderTest.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,126 @@ | ||
package io.quarkus.arc.test.buildextension.annotations; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.AbstractList; | ||
|
||
import javax.enterprise.context.Dependent; | ||
import javax.enterprise.inject.Produces; | ||
import javax.enterprise.inject.Vetoed; | ||
import javax.inject.Inject; | ||
|
||
import org.jboss.jandex.AnnotationTarget.Kind; | ||
import org.jboss.jandex.PrimitiveType; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.ArcContainer; | ||
import io.quarkus.arc.InstanceHandle; | ||
import io.quarkus.arc.processor.AnnotationsTransformer; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class AnnotationsTransformerBuilderTest { | ||
|
||
@RegisterExtension | ||
public ArcTestContainer container = ArcTestContainer.builder() | ||
.beanClasses(Seven.class, One.class, IWantToBeABean.class) | ||
.annotationsTransformers( | ||
AnnotationsTransformer.builder() | ||
.appliesTo(Kind.CLASS) | ||
.containsAny(Dependent.class) | ||
.transform(context -> { | ||
if (context.getTarget().asClass().name().toString().equals(One.class.getName())) { | ||
// Veto bean class One | ||
context.transform().add(Vetoed.class).done(); | ||
} | ||
}).build(), | ||
AnnotationsTransformer.builder() | ||
.appliesTo(Kind.CLASS) | ||
.transform(context -> { | ||
if (context.getTarget().asClass().name().local().equals(IWantToBeABean.class.getSimpleName())) { | ||
context.transform().add(Dependent.class).done(); | ||
} | ||
}).build(), | ||
AnnotationsTransformer.builder() | ||
.appliesTo(Kind.CLASS) | ||
.transform(context -> { | ||
if (context.getTarget().asField().name().equals("seven")) { | ||
context.transform().add(Inject.class).done(); | ||
} | ||
}).build(), | ||
// Add @Produces to a method that returns int and is not annoated with @Simple | ||
AnnotationsTransformer.builder() | ||
.appliesTo(Kind.CLASS) | ||
.containsNone(Simple.class) | ||
.and(context -> context.getTarget().asMethod().returnType().name().equals(PrimitiveType.INT.name())) | ||
.transform(context -> context.transform().add(Produces.class)).build()) | ||
.build(); | ||
|
||
@Test | ||
public void testVetoed() { | ||
ArcContainer arc = Arc.container(); | ||
assertTrue(arc.instance(Seven.class).isAvailable()); | ||
// One is vetoed | ||
assertFalse(arc.instance(One.class).isAvailable()); | ||
assertEquals(Integer.valueOf(7), Integer.valueOf(arc.instance(Seven.class).get().size())); | ||
|
||
// Scope annotation and @Inject are added by transformer | ||
InstanceHandle<IWantToBeABean> iwant = arc.instance(IWantToBeABean.class); | ||
assertTrue(iwant.isAvailable()); | ||
assertEquals(Integer.valueOf(7), Integer.valueOf(iwant.get().size())); | ||
} | ||
|
||
// => add @Dependent here | ||
static class IWantToBeABean { | ||
|
||
// => add @Inject here | ||
Seven seven; | ||
|
||
// => add @Produces here | ||
public int size() { | ||
return seven.size(); | ||
} | ||
|
||
// => do not add @Produces here | ||
@Simple | ||
public int anotherSize() { | ||
return seven.size(); | ||
} | ||
|
||
} | ||
|
||
@Dependent | ||
static class Seven extends AbstractList<Integer> { | ||
|
||
@Override | ||
public Integer get(int index) { | ||
return Integer.valueOf(7); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return 7; | ||
} | ||
|
||
} | ||
|
||
// => add @Vetoed here | ||
@Dependent | ||
static class One extends AbstractList<Integer> { | ||
|
||
@Override | ||
public Integer get(int index) { | ||
return Integer.valueOf(1); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return 1; | ||
} | ||
|
||
} | ||
|
||
} |