Search in sources :

Example 76 with WeldContainer

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);
    }
}
Also used : Type(java.lang.reflect.Type) WeldContainer(org.jboss.weld.environment.se.WeldContainer) ArrayList(java.util.ArrayList) List(java.util.List) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 77 with WeldContainer

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());
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 78 with WeldContainer

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);
    }
}
Also used : WeldEvent(org.jboss.weld.events.WeldEvent) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 79 with WeldContainer

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());
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 80 with WeldContainer

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());
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Aggregations

WeldContainer (org.jboss.weld.environment.se.WeldContainer)115 Weld (org.jboss.weld.environment.se.Weld)100 Test (org.junit.Test)98 CountDownLatch (java.util.concurrent.CountDownLatch)8 Bean (javax.enterprise.inject.spi.Bean)8 BeanManager (javax.enterprise.inject.spi.BeanManager)8 Type (java.lang.reflect.Type)7 URLClassLoader (java.net.URLClassLoader)7 ArrayList (java.util.ArrayList)7 URL (java.net.URL)6 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)6 Fabric8Extension (io.fabric8.cdi.Fabric8Extension)4 ParameterizedType (java.lang.reflect.ParameterizedType)4 List (java.util.List)4 CreationalContext (javax.enterprise.context.spi.CreationalContext)4 BeanManagerImpl (org.jboss.weld.manager.BeanManagerImpl)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 PrintStream (java.io.PrintStream)3 ServiceRegistry (org.jboss.weld.bootstrap.api.ServiceRegistry)3