Search in sources :

Example 41 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class InjectionPointWithTypesAndQualifiersOnlyTest method testInjectionPointWithTypesAndQualifiersOnly.

@Test
public void testInjectionPointWithTypesAndQualifiersOnly() throws IOException {
    @SuppressWarnings("unchecked") Bean<DummyBean> bean = (Bean<DummyBean>) manager.resolve(manager.getBeans(DummyBean.class));
    CreationalContext<DummyBean> ctx = manager.createCreationalContext(bean);
    InjectionPoint ip = new CustomInjectionPoint();
    Object o = manager.getInjectableReference(ip, ctx);
    Assert.assertEquals(5, ((DummyBean) o).getSomeValue());
}
Also used : InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 42 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class PreDestroyTest method testLifecycle.

@Test
@SuppressWarnings({ "unchecked" })
public void testLifecycle(BeanManager bm) throws Exception {
    for (Bean b : bm.getBeans(TestBean.class)) {
        CreationalContext ctx = bm.createCreationalContext(null);
        TestBean testBean = (TestBean) bm.getReference(b, b.getBeanClass(), ctx);
        b.destroy(testBean, ctx);
        Assert.assertEquals("PreDestroy invocation number.", 1, testBean.getCounter());
    }
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 43 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class WeldInternalConstructsTest method testInterceptedProxiedBean.

@Test
public void testInterceptedProxiedBean() {
    InterceptedProxiedBean interceptedBean = holder.getInterceptedProxiedBean();
    // trigger interception and assert it works
    Assert.assertTrue(interceptedBean.ping());
    // should be instance of WeldConstruct and WeldClientProxy
    Assert.assertTrue(interceptedBean instanceof WeldConstruct);
    Assert.assertTrue(interceptedBean instanceof WeldClientProxy);
    // cast to WeldClientProxy and test the methods
    WeldClientProxy wcp = (WeldClientProxy) interceptedBean;
    WeldClientProxy.Metadata cm = wcp.getMetadata();
    Object contextualInstance = cm.getContextualInstance();
    // kind of indirect check that this is the actual contextual instance
    Assert.assertTrue(contextualInstance instanceof InterceptedProxiedBean);
    Assert.assertFalse(contextualInstance instanceof WeldClientProxy);
    // NOTE - contextual instance is still a Weld subclass because of interception/decoration
    Assert.assertTrue(contextualInstance instanceof WeldConstruct);
    Bean<?> bean = cm.getBean();
    Set<Bean<?>> beans = bm.getBeans(InterceptedProxiedBean.class);
    Assert.assertEquals(1, beans.size());
    Assert.assertEquals(((WeldBean) beans.iterator().next()).getIdentifier().asString(), ((WeldBean) bean).getIdentifier().asString());
}
Also used : WeldBean(org.jboss.weld.bean.WeldBean) WeldClientProxy(org.jboss.weld.proxy.WeldClientProxy) WeldConstruct(org.jboss.weld.proxy.WeldConstruct) WeldBean(org.jboss.weld.bean.WeldBean) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 44 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class AlternativeDiscoveryTest method testAllBeanDiscoveryAlternative.

@Test
public void testAllBeanDiscoveryAlternative(Cat representative) {
    BeanManager bm = representative.getBeanManager();
    Set<Bean<?>> beans = bm.getBeans(DogInterface.class);
    assertEquals(2, beans.size());
    assertEquals(AlternativeDog.class, bm.resolve(beans).getBeanClass());
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 45 with Bean

use of javax.enterprise.inject.spi.Bean in project narayana by jbosstm.

the class BeanManagerUtil method createBeanInstance.

@SuppressWarnings("unchecked")
public static <T> T createBeanInstance(Class<T> clazz, BeanManager beanManager) {
    BeanManager classBeanManager = getClassBeanManager(clazz, beanManager);
    Bean<T> bean = (Bean<T>) classBeanManager.resolve(classBeanManager.getBeans(clazz));
    if (bean == null) {
        throw new IllegalStateException("CDI BeanManager cannot find an instance of requested type " + clazz.getName());
    }
    CreationalContext<T> context = classBeanManager.createCreationalContext(bean);
    return (T) classBeanManager.getReference(bean, clazz, context);
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Aggregations

Bean (javax.enterprise.inject.spi.Bean)259 Test (org.junit.Test)119 CreationalContext (javax.enterprise.context.spi.CreationalContext)110 BeanManager (javax.enterprise.inject.spi.BeanManager)76 URL (java.net.URL)68 Path (org.uberfire.backend.vfs.Path)67 KieModuleService (org.kie.workbench.common.services.shared.project.KieModuleService)66 Package (org.guvnor.common.services.project.model.Package)43 HashSet (java.util.HashSet)31 Annotation (java.lang.annotation.Annotation)18 ArrayList (java.util.ArrayList)14 Module (org.guvnor.common.services.project.model.Module)13 AnnotationLiteral (javax.enterprise.util.AnnotationLiteral)11 Type (java.lang.reflect.Type)9 Set (java.util.Set)9 AfterDeploymentValidation (javax.enterprise.inject.spi.AfterDeploymentValidation)9 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)9 Any (javax.enterprise.inject.Any)8 AbstractBuiltInBean (org.jboss.weld.bean.builtin.AbstractBuiltInBean)8 Weld (org.jboss.weld.environment.se.Weld)8