use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class WeldEventTest method testEventSelectByType.
@Test
public void testEventSelectByType() {
try (WeldContainer container = new Weld().initialize()) {
BeanInjectingEvents bean = container.select(BeanInjectingEvents.class).get();
// prepare some Types
Type firstType = SomeInterface.class;
Type secondType = SomeOtherBean.class;
Type listFooType = new TypeLiteral<List<Foo>>() {
}.getType();
Type listWildcardType = new TypeLiteral<List<?>>() {
}.getType();
// verify invalid behaviour - selecting from Event<X> where X != Object
try {
// using Type from SomeInterface -> SomeOtherBean
bean.getEventSomeInterface().select(secondType);
Assert.fail();
} catch (IllegalStateException e) {
// expected
}
try {
// using Type from List<Object> -> List<?>
bean.getEventListObject().select(listWildcardType);
Assert.fail();
} catch (IllegalStateException e) {
// expected
}
// verify valid behaviour, following selections should work
bean.getEventObject().select(firstType);
bean.getEventObject().select(secondType);
bean.getEventObject().select(listFooType);
bean.getEventObject().select(listWildcardType);
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class WeldEventTest method testEventObservability.
@Test
public void testEventObservability() {
// test few basic fire/observe combinations, just to make sure WeldEvent can work in place of Event
try (WeldContainer container = new Weld().initialize()) {
BeanInjectingEvents bean = container.select(BeanInjectingEvents.class).get();
ObservingBean observer = container.select(ObservingBean.class).get();
// WeldEvent<Foo>
observer.reset();
bean.getEventFoo().fire(new Foo());
Assert.assertTrue(observer.isFooObserved());
// WeldEvent<List<Object>>
observer.reset();
bean.getEventListObject().fire(new ArrayList<>());
Assert.assertTrue(observer.isListObjectObserved());
Assert.assertTrue(observer.isListObserved());
// select and fire from container.event()
observer.reset();
container.event().select(Bar.class, Dubious.Literal.INSTANCE).fire(new Bar());
Assert.assertTrue(observer.isBarObserved());
// and now with Types
Type someInterfaceType = SomeInterface.class;
Type listFooType = new TypeLiteral<List<Foo>>() {
}.getType();
Type listWildcardType = new TypeLiteral<List<?>>() {
}.getType();
// WeldEvent<Object> -> WeldEvent<SomeInterface> -> fire(SomeOtherBean)
observer.reset();
container.event().<SomeInterface>select(someInterfaceType).fire(container.select(SomeOtherBean.class).get());
Assert.assertTrue(observer.isSomeInterfaceObserved());
Assert.assertTrue(observer.isSomeOtherBeanObserved());
Assert.assertFalse(observer.isSomeTypedBeanObserved());
// WeldEvent<Object> -> WeldEvent<List<?>> -> fire(ArrayList<Object>)
observer.reset();
bean.getEventObject().select(listWildcardType).fire(new ArrayList<Object>());
Assert.assertFalse(observer.isListObjectObserved());
Assert.assertTrue(observer.isListObserved());
// WeldEvent<Object> -> WeldEvent<List<Foo>> -> fire(ArrayList<Foo>)
observer.reset();
bean.getEventObject().select(listFooType).fire(new ArrayList<Foo>());
Assert.assertFalse(observer.isListObjectObserved());
Assert.assertTrue(observer.isListObserved());
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class WeldEventTest method testInstanceOf.
@Test
public void testInstanceOf() {
try (WeldContainer container = new Weld().initialize()) {
BeanInjectingEvents bean = container.select(BeanInjectingEvents.class).get();
// assert that Event is instance of WeldEvent
Assert.assertNotNull(bean.getPlainEvent());
Assert.assertTrue(bean.getPlainEvent() instanceof WeldEvent);
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class ContextEventsFiredForPostConstructTest method contextLifecycleEventFiredForPostConstructCallbackActivation.
@Test
public void contextLifecycleEventFiredForPostConstructCallbackActivation() {
try (WeldContainer container = new Weld().initialize()) {
Alpha alpha = container.select(Alpha.class).get();
alpha.ping();
ContextLifecycleObserver ctxLifecycleObserver = container.select(ContextLifecycleObserver.class).get();
Assert.assertEquals(3, ctxLifecycleObserver.getInitCount().get());
Assert.assertEquals(3, ctxLifecycleObserver.getBeforeDestroyedCount().get());
Assert.assertEquals(3, ctxLifecycleObserver.getDestroyedCount().get());
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class ImplicitScanDisabledTest method testDiscovery.
@Test
public void testDiscovery() {
try (WeldContainer container = new Weld().property(Weld.JAVAX_ENTERPRISE_INJECT_SCAN_IMPLICIT, Boolean.FALSE).initialize()) {
// Beans from an implicit bean archive with no beans.xml file are not registered
assertTrue(container.select(Alpha.class).isUnsatisfied());
Bravo bravo = container.select(Bravo.class).get();
assertNotNull(bravo);
assertEquals(1, bravo.ping());
}
}
Aggregations