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.
ArC - introduce AutoAddScopeBuildItem#priority
- a build item with higher priority takes precedence - also do not add scope if already added by another build item
- Loading branch information
Showing
4 changed files
with
175 additions
and
9 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
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
80 changes: 80 additions & 0 deletions
80
...ns/arc/deployment/src/test/java/io/quarkus/arc/test/autoscope/AutoScopeBuildItemTest.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,80 @@ | ||
package io.quarkus.arc.test.autoscope; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.UUID; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.enterprise.inject.Instance; | ||
import javax.inject.Inject; | ||
|
||
import org.jboss.logging.Logger; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.deployment.AutoAddScopeBuildItem; | ||
import io.quarkus.arc.processor.BuiltinScope; | ||
import io.quarkus.builder.BuildContext; | ||
import io.quarkus.builder.BuildStep; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class AutoScopeBuildItemTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(SimpleBean.class)) | ||
.addBuildChainCustomizer(b -> { | ||
b.addBuildStep(new BuildStep() { | ||
@Override | ||
public void execute(BuildContext context) { | ||
context.produce(AutoAddScopeBuildItem.builder().match((clazz, annotations, index) -> { | ||
return clazz.name().toString().equals(SimpleBean.class.getName()); | ||
}).defaultScope(BuiltinScope.DEPENDENT) | ||
.scopeAlreadyAdded((scope, reason) -> { | ||
// We cant's pass the state directly to AutoScopeBuildItemTest because it's loaded by a different classloader | ||
Logger.getLogger("AutoScopeBuildItemTest").info(scope + ":" + reason); | ||
}).build()); | ||
} | ||
}).produces(AutoAddScopeBuildItem.class).build(); | ||
b.addBuildStep(new BuildStep() { | ||
@Override | ||
public void execute(BuildContext context) { | ||
context.produce(AutoAddScopeBuildItem.builder().match((clazz, annotations, index) -> { | ||
return clazz.name().toString().equals(SimpleBean.class.getName()); | ||
}).defaultScope(BuiltinScope.SINGLETON).priority(10).reason("Foo!").build()); | ||
} | ||
}).produces(AutoAddScopeBuildItem.class).build(); | ||
}).setLogRecordPredicate(log -> "AutoScopeBuildItemTest".equals(log.getLoggerName())) | ||
.assertLogRecords(records -> { | ||
assertEquals(1, records.size()); | ||
assertEquals("javax.inject.Singleton:Foo!", records.get(0).getMessage()); | ||
}); | ||
|
||
@Inject | ||
Instance<SimpleBean> instance; | ||
|
||
@Test | ||
public void testBean() { | ||
assertTrue(instance.isResolvable()); | ||
// The scope should be @Singleton | ||
assertEquals(instance.get().ping(), instance.get().ping()); | ||
} | ||
|
||
static class SimpleBean { | ||
|
||
private String id; | ||
|
||
public String ping() { | ||
return id; | ||
} | ||
|
||
@PostConstruct | ||
void init() { | ||
id = UUID.randomUUID().toString(); | ||
} | ||
|
||
} | ||
|
||
} |