use of javax.enterprise.inject.spi.AfterBeanDiscovery in project core by weld.
the class ContainerLifecyleObserverTest method testExtensionBuilder.
@SuppressWarnings({ "serial" })
@Test
public void testExtensionBuilder() {
ActionSequence.reset();
Extension myExtension = ContainerLifecycleObserver.extensionBuilder().add(beforeBeanDiscovery((e) -> addAction(BeforeBeanDiscovery.class.getSimpleName()))).add(afterTypeDiscovery().notify((e, b) -> {
addAction(AfterTypeDiscovery.class.getSimpleName());
e.addAnnotatedType(b.createAnnotatedType(Charlie.class), Charlie.class.getName());
})).add(afterBeanDiscovery((e) -> {
addAction(AfterBeanDiscovery.class.getSimpleName());
e.addObserverMethod().beanClass(Foo.class).observedType(Foo.class).notifyWith((ctx) -> {
});
e.addBean().beanClass(Integer.class).addType(Integer.class).addQualifier(Juicy.Literal.INSTANCE).createWith((ctx) -> Integer.valueOf(10));
})).add(afterDeploymentValidation((e) -> addAction(AfterDeploymentValidation.class.getSimpleName()))).add(beforeShutdown((e) -> addAction(BeforeShutdown.class.getSimpleName()))).build();
Extension myExtension2 = ContainerLifecycleObserver.extensionBuilder().add(processAnnotatedType().withAnnotations(RequestScoped.class).notify((e) -> e.veto())).add(processBeanAttributes().notify((e) -> addAction(ProcessBeanAttributes.class.getSimpleName()))).add(processSyntheticAnnotatedType(new TypeLiteral<ProcessSyntheticAnnotatedType<?>>() {
}.getType()).notify((e) -> addAction(ProcessSyntheticAnnotatedType.class.getSimpleName()))).add(processInjectionPoint().notify((e) -> addAction(ProcessInjectionPoint.class.getSimpleName()))).add(processProducer().notify((e) -> addAction(ProcessProducer.class.getSimpleName()))).add(processBean().notify((e) -> addAction(ProcessBean.class.getSimpleName()))).add(processManagedBean().notify((e) -> addAction(ProcessManagedBean.class.getSimpleName()))).add(processProducerField().notify((e) -> addAction(ProcessProducerField.class.getSimpleName()))).add(processProducerMethod().notify((e) -> {
// Weld SE defines some producer methods, e.g. ParametersFactory
addAction(ProcessProducerMethod.class.getSimpleName());
})).add(processBeanAttributes().notify((e) -> addAction(ProcessBeanAttributes.class.getSimpleName()))).add(processObserverMethod().notify((e) -> addAction(ProcessObserverMethod.class.getSimpleName()))).add(processObserverMethod(new TypeLiteral<ProcessObserverMethod<String, ?>>() {
}.getType()).notify((e) -> addAction(ProcessObserverMethod.class.getSimpleName() + String.class.getSimpleName()))).add(processSyntheticObserverMethod(new TypeLiteral<ProcessSyntheticObserverMethod<Foo, ?>>() {
}.getType()).notify((e) -> addAction(ProcessSyntheticObserverMethod.class.getSimpleName() + Foo.class.getSimpleName()))).add(processSyntheticBean(new TypeLiteral<ProcessSyntheticBean<Integer>>() {
}.getType()).notify((e) -> addAction(ProcessSyntheticBean.class.getSimpleName() + Integer.class.getSimpleName()))).build();
try (WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class, Bravo.class).addExtension(myExtension).addExtension(myExtension2).initialize()) {
assertTrue(container.select(Foo.class).isUnsatisfied());
assertFalse(container.select(Bravo.class).isUnsatisfied());
Assert.assertEquals(Integer.valueOf(10), container.select(Integer.class, Juicy.Literal.INSTANCE).get());
}
ActionSequence.assertSequenceDataContainsAll(BeforeBeanDiscovery.class, AfterTypeDiscovery.class, AfterBeanDiscovery.class, AfterDeploymentValidation.class, BeforeShutdown.class);
ActionSequence.assertSequenceDataContainsAll(ProcessBeanAttributes.class, ProcessSyntheticAnnotatedType.class, ProcessInjectionPoint.class, ProcessObserverMethod.class, ProcessBeanAttributes.class, ProcessProducer.class);
ActionSequence.assertSequenceDataContainsAll(ProcessObserverMethod.class.getSimpleName() + String.class.getSimpleName(), ProcessSyntheticObserverMethod.class.getSimpleName() + Foo.class.getSimpleName(), ProcessSyntheticBean.class.getSimpleName() + Integer.class.getSimpleName());
ActionSequence.assertSequenceDataContainsAll(ProcessBean.class, ProcessManagedBean.class, ProcessProducerMethod.class, ProcessProducerField.class);
}
use of javax.enterprise.inject.spi.AfterBeanDiscovery in project core by weld.
the class ContainerLifecycleEventInvocationOutsideObserverTest method testAfterBeanDiscovery.
@Test
public void testAfterBeanDiscovery() {
final AfterBeanDiscovery event = extension.getAfterBeanDiscovery();
new Invocation() {
void execute() {
event.addBean(new DummyBean());
}
}.run();
new Invocation() {
void execute() {
event.addContext(new DummyContext());
}
}.run();
new Invocation() {
void execute() {
event.addObserverMethod(new DummyObserverMethod());
}
}.run();
new Invocation() {
void execute() {
event.getAnnotatedType(ContainerLifecycleEventInvocationOutsideObserverTest.class, "foo");
}
}.run();
new Invocation() {
void execute() {
event.getAnnotatedTypes(ContainerLifecycleEventInvocationOutsideObserverTest.class);
}
}.run();
}
use of javax.enterprise.inject.spi.AfterBeanDiscovery in project wildfly-swarm by wildfly-swarm.
the class InterfaceExtensionTest method testAfterBeanDiscovery.
@Test
public void testAfterBeanDiscovery() throws Exception {
ConfigView configView = mock(ConfigView.class);
List<SimpleKey> interfaces = Arrays.asList(new SimpleKey("test"));
when(configView.simpleSubkeys(any(ConfigKey.class))).thenReturn(interfaces);
when(configView.valueOf(any(ConfigKey.class))).thenReturn("192.168.1.1");
ext = new InterfaceExtension(configView);
BeanManager beanManager = mock(BeanManager.class);
AfterBeanDiscovery abd = mock(AfterBeanDiscovery.class);
@SuppressWarnings("unchecked") ArgumentCaptor<CommonBean<Interface>> captor = ArgumentCaptor.forClass(CommonBean.class);
ext.afterBeanDiscovery(abd, beanManager);
verify(abd, times(1)).addBean(captor.capture());
assertThat(captor.getValue().create(null).getName()).isEqualTo("test");
assertThat(captor.getValue().create(null).getExpression()).isEqualTo("192.168.1.1");
}
use of javax.enterprise.inject.spi.AfterBeanDiscovery in project wildfly-swarm by wildfly-swarm.
the class InterfaceExtension method afterBeanDiscovery.
@SuppressWarnings("unused")
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) throws Exception {
List<SimpleKey> configuredInterfaces = this.configView.simpleSubkeys(ROOT);
for (SimpleKey interfaceName : configuredInterfaces) {
Set<Bean<?>> ifaces = beanManager.getBeans(Interface.class, AnyLiteral.INSTANCE);
if (ifaces.stream().noneMatch(e -> e.getQualifiers().stream().anyMatch(anno -> anno instanceof Named && ((Named) anno).value().equals(interfaceName + "-interface")))) {
Interface iface = new Interface(interfaceName.name(), "0.0.0.0");
applyConfiguration(iface);
CommonBean<Interface> interfaceBean = CommonBeanBuilder.newBuilder(Interface.class).beanClass(InterfaceExtension.class).scope(ApplicationScoped.class).addQualifier(AnyLiteral.INSTANCE).addQualifier(new NamedLiteral(interfaceName.name() + "-interface")).createSupplier(() -> iface).addType(Interface.class).addType(Object.class).build();
abd.addBean(interfaceBean);
}
}
}
use of javax.enterprise.inject.spi.AfterBeanDiscovery in project core by weld.
the class ContainerLifecyleObserverTest method testAddContainerLifecycleObserver.
@SuppressWarnings("serial")
@Test
public void testAddContainerLifecycleObserver() {
final AtomicBoolean called = new AtomicBoolean(false);
try (WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class).addContainerLifecycleObserver(processAnnotatedType(new TypeLiteral<ProcessAnnotatedType<Foo>>() {
}.getType()).notify((e) -> e.veto())).addContainerLifecycleObserver(afterBeanDiscovery((e) -> called.set(true))).initialize()) {
assertTrue(called.get());
assertTrue(container.select(Foo.class).isUnsatisfied());
}
}
Aggregations