Skip to content

Commit

Permalink
ArC: fix BeanConfiguratorBase#read()
Browse files Browse the repository at this point in the history
- fixes #39169
  • Loading branch information
mkouba committed Mar 5, 2024
1 parent 90762d5 commit 3aecefb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.quarkus.arc.test.synthetic;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.lang.reflect.Method;
import java.util.List;
import java.util.function.Consumer;

import jakarta.enterprise.context.ApplicationScoped;
Expand All @@ -14,6 +15,7 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem.ExtendedBeanConfigurator;
import io.quarkus.builder.BuildChainBuilder;
Expand Down Expand Up @@ -46,20 +48,31 @@ public void execute(BuildContext context) {
// We need to use reflection due to some class loading problems
Object recorderProxy = bytecodeRecorder.getRecordingProxy(TestRecorder.class);
try {
Method test = recorderProxy.getClass().getDeclaredMethod("test");
Object proxy = test.invoke(recorderProxy);
ExtendedBeanConfigurator configurator = SyntheticBeanBuildItem.configure(SynthBean.class)
Method test = recorderProxy.getClass().getDeclaredMethod("test", String.class);

Object proxy1 = test.invoke(recorderProxy, "ok");
ExtendedBeanConfigurator configurator1 = SyntheticBeanBuildItem.configure(SynthBean.class)
.scope(ApplicationScoped.class)
.identifier("ok")
.unremovable();
// No creator
assertThrows(IllegalStateException.class,
() -> configurator.done());
() -> configurator1.done());
// Not a returned proxy
assertThrows(IllegalArgumentException.class,
() -> configurator.runtimeProxy(new SynthBean()));
context.produce(configurator
.runtimeProxy(proxy)
() -> configurator1.runtimeProxy(new SynthBean()));
context.produce(configurator1
.runtimeProxy(proxy1)
.done());

// Register a synthetic bean with same types and qualifiers but different identifier
context.produce(SyntheticBeanBuildItem.configure(SynthBean.class)
.scope(ApplicationScoped.class)
.identifier("nok")
.unremovable()
.runtimeProxy(test.invoke(recorderProxy, "nok"))
.done());

} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -73,19 +86,22 @@ public void execute(BuildContext context) {
@Recorder
public static class TestRecorder {

public SynthBean test() {
public SynthBean test(String val) {
SynthBean bean = new SynthBean();
bean.setValue("ok");
bean.setValue(val);
return bean;
}

}

@Test
public void testBeans() {
SynthBean bean = Arc.container().instance(SynthBean.class).get();
assertNotNull(bean);
assertEquals("ok", bean.getValue());
List<InstanceHandle<SynthBean>> beans = Arc.container().listAll(SynthBean.class);
assertEquals(2, beans.size());
for (InstanceHandle<SynthBean> handle : beans) {
String val = handle.get().getValue();
assertTrue("ok".equals(val) || "nok".equals(val));
}
}

@Vetoed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ protected BeanConfiguratorBase(DotName implClazz) {
*/
public THIS read(BeanConfiguratorBase<?, ?> base) {
super.read(base);
identifier = base.identifier;
types.clear();
types.addAll(base.types);
qualifiers.clear();
Expand Down

0 comments on commit 3aecefb

Please sign in to comment.