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());
}
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());
}
}
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());
}
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());
}
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);
}
Aggregations