Search in sources :

Example 6 with BeanService

use of org.apache.aries.cdi.test.interfaces.BeanService in project aries by apache.

the class EventsTests method testEventsGetSent.

public void testEventsGetSent() throws Exception {
    BeanManager beanManager = cdiContainer.getBeanManager();
    assertNotNull(beanManager);
    @SuppressWarnings("serial") Set<Bean<?>> beans = beanManager.getBeans(Object.class, new AnnotationLiteral<CdiEventObserverQualifier>() {
    });
    Bean<?> bean = beanManager.resolve(beans);
    CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
    Object bcb = beanManager.getReference(bean, Object.class, ctx);
    assertNotNull(bcb);
    @SuppressWarnings("unchecked") BeanService<List<CdiEvent>> bti = (BeanService<List<CdiEvent>>) bcb;
    List<CdiEvent> list = bti.get();
    assertNotNull(list);
    assertEquals(3, list.size());
}
Also used : Bean(javax.enterprise.inject.spi.Bean) List(java.util.List) CdiEvent(org.osgi.service.cdi.CdiEvent) BeanManager(javax.enterprise.inject.spi.BeanManager) CdiEventObserverQualifier(org.apache.aries.cdi.test.interfaces.CdiEventObserverQualifier) BeanService(org.apache.aries.cdi.test.interfaces.BeanService)

Example 7 with BeanService

use of org.apache.aries.cdi.test.interfaces.BeanService in project aries by apache.

the class CdiBeanTests method testBundleContextInjection.

public void testBundleContextInjection() throws Exception {
    BeanManager beanManager = cdiContainer.getBeanManager();
    assertNotNull(beanManager);
    @SuppressWarnings("serial") Set<Bean<?>> beans = beanManager.getBeans(Object.class, new AnnotationLiteral<BundleContextBeanQualifier>() {
    });
    Bean<?> bean = beanManager.resolve(beans);
    CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
    Object bcb = beanManager.getReference(bean, Object.class, ctx);
    assertNotNull(bcb);
    @SuppressWarnings("unchecked") BeanService<BundleContext> bti = (BeanService<BundleContext>) bcb;
    assertNotNull(bti.get());
    assertTrue(bti.get() instanceof BundleContext);
}
Also used : BundleContextBeanQualifier(org.apache.aries.cdi.test.interfaces.BundleContextBeanQualifier) BeanManager(javax.enterprise.inject.spi.BeanManager) BeanService(org.apache.aries.cdi.test.interfaces.BeanService) Bean(javax.enterprise.inject.spi.Bean) BundleContext(org.osgi.framework.BundleContext)

Example 8 with BeanService

use of org.apache.aries.cdi.test.interfaces.BeanService in project aries by apache.

the class CdiBeanTests method testBeanAsServiceWithProperties.

@SuppressWarnings("unchecked")
public void testBeanAsServiceWithProperties() throws Exception {
    Iterator<ServiceReference<BeanService>> iterator = bundleContext.getServiceReferences(BeanService.class, String.format("(objectClass=*.%s)", "ServiceWithProperties")).iterator();
    assertTrue(iterator.hasNext());
    ServiceReference<BeanService> serviceReference = iterator.next();
    assertNotNull(serviceReference);
    BeanService bean = bundleContext.getService(serviceReference);
    assertNotNull(bean);
    assertEquals("test.value.b2", serviceReference.getProperty("test.key.b2"));
    assertTrue(serviceReference.getProperty("p.Boolean") instanceof Boolean);
    assertTrue(serviceReference.getProperty("p.Boolean.array") instanceof Boolean[]);
    assertEquals(2, ((Boolean[]) serviceReference.getProperty("p.Boolean.array")).length);
    assertTrue(serviceReference.getProperty("p.Boolean.list") instanceof List);
    assertEquals(2, ((List<Boolean>) serviceReference.getProperty("p.Boolean.list")).size());
    assertTrue(serviceReference.getProperty("p.Boolean.set") instanceof Set);
    assertEquals(2, ((Set<Boolean>) serviceReference.getProperty("p.Boolean.set")).size());
    assertTrue(serviceReference.getProperty("p.Byte") instanceof Byte);
    assertTrue(serviceReference.getProperty("p.Byte.array") instanceof Byte[]);
    assertEquals(2, ((Byte[]) serviceReference.getProperty("p.Byte.array")).length);
    assertTrue(serviceReference.getProperty("p.Byte.list") instanceof List);
    assertEquals(2, ((List<Byte>) serviceReference.getProperty("p.Byte.list")).size());
    assertTrue(serviceReference.getProperty("p.Byte.set") instanceof Set);
    assertEquals(2, ((Set<Byte>) serviceReference.getProperty("p.Byte.set")).size());
    assertTrue(serviceReference.getProperty("p.Character") instanceof Character);
    assertTrue(serviceReference.getProperty("p.Character.array") instanceof Character[]);
    assertEquals(2, ((Character[]) serviceReference.getProperty("p.Character.array")).length);
    assertTrue(serviceReference.getProperty("p.Character.list") instanceof List);
    assertEquals(2, ((List<Character>) serviceReference.getProperty("p.Character.list")).size());
    assertTrue(serviceReference.getProperty("p.Character.set") instanceof Set);
    assertEquals(2, ((Set<Character>) serviceReference.getProperty("p.Character.set")).size());
    assertTrue(serviceReference.getProperty("p.Double") instanceof Double);
    assertTrue(serviceReference.getProperty("p.Double.array") instanceof Double[]);
    assertEquals(2, ((Double[]) serviceReference.getProperty("p.Double.array")).length);
    assertTrue(serviceReference.getProperty("p.Double.list") instanceof List);
    assertEquals(2, ((List<Double>) serviceReference.getProperty("p.Double.list")).size());
    assertTrue(serviceReference.getProperty("p.Double.set") instanceof Set);
    assertEquals(2, ((Set<Double>) serviceReference.getProperty("p.Double.set")).size());
    assertTrue(serviceReference.getProperty("p.Float") instanceof Float);
    assertTrue(serviceReference.getProperty("p.Float.array") instanceof Float[]);
    assertEquals(2, ((Float[]) serviceReference.getProperty("p.Float.array")).length);
    assertTrue(serviceReference.getProperty("p.Float.list") instanceof List);
    assertEquals(2, ((List<Float>) serviceReference.getProperty("p.Float.list")).size());
    assertTrue(serviceReference.getProperty("p.Float.set") instanceof Set);
    assertEquals(2, ((Set<Float>) serviceReference.getProperty("p.Float.set")).size());
    assertTrue(serviceReference.getProperty("p.Integer") instanceof Integer);
    assertTrue(serviceReference.getProperty("p.Integer.array") instanceof Integer[]);
    assertEquals(2, ((Integer[]) serviceReference.getProperty("p.Integer.array")).length);
    assertTrue(serviceReference.getProperty("p.Integer.list") instanceof List);
    assertEquals(2, ((List<Integer>) serviceReference.getProperty("p.Integer.list")).size());
    assertTrue(serviceReference.getProperty("p.Integer.set") instanceof Set);
    assertEquals(2, ((Set<Integer>) serviceReference.getProperty("p.Integer.set")).size());
    assertTrue(serviceReference.getProperty("p.Long") instanceof Long);
    assertTrue(serviceReference.getProperty("p.Long.array") instanceof Long[]);
    assertEquals(2, ((Long[]) serviceReference.getProperty("p.Long.array")).length);
    assertTrue(serviceReference.getProperty("p.Long.list") instanceof List);
    assertEquals(2, ((List<Long>) serviceReference.getProperty("p.Long.list")).size());
    assertTrue(serviceReference.getProperty("p.Long.set") instanceof Set);
    assertEquals(2, ((Set<Long>) serviceReference.getProperty("p.Long.set")).size());
    assertTrue(serviceReference.getProperty("p.Short") instanceof Short);
    assertTrue(serviceReference.getProperty("p.Short.array") instanceof Short[]);
    assertEquals(2, ((Short[]) serviceReference.getProperty("p.Short.array")).length);
    assertTrue(serviceReference.getProperty("p.Short.list") instanceof List);
    assertEquals(2, ((List<Short>) serviceReference.getProperty("p.Short.list")).size());
    assertTrue(serviceReference.getProperty("p.Short.set") instanceof Set);
    assertEquals(2, ((Set<Short>) serviceReference.getProperty("p.Short.set")).size());
    assertTrue(serviceReference.getProperty("p.String") instanceof String);
    assertTrue(serviceReference.getProperty("p.String.array") instanceof String[]);
    assertEquals(2, ((String[]) serviceReference.getProperty("p.String.array")).length);
    assertTrue(serviceReference.getProperty("p.String.list") instanceof List);
    assertEquals(2, ((List<String>) serviceReference.getProperty("p.String.list")).size());
    assertTrue(serviceReference.getProperty("p.String.set") instanceof Set);
    assertEquals(2, ((Set<String>) serviceReference.getProperty("p.String.set")).size());
    // glubInteger = 45, gooString = "green"
    assertTrue(serviceReference.getProperty("glub.integer") instanceof Integer);
    assertEquals(45, ((Integer) serviceReference.getProperty("glub.integer")).intValue());
    assertTrue(serviceReference.getProperty("goo.string") instanceof String);
    assertEquals("green", ((String) serviceReference.getProperty("goo.string")));
}
Also used : Set(java.util.Set) ServiceReference(org.osgi.framework.ServiceReference) List(java.util.List) BeanService(org.apache.aries.cdi.test.interfaces.BeanService)

Example 9 with BeanService

use of org.apache.aries.cdi.test.interfaces.BeanService in project aries by apache.

the class CdiBeanTests method testMethodInjectedService.

public void testMethodInjectedService() throws Exception {
    Iterator<ServiceReference<BeanService>> iterator = bundleContext.getServiceReferences(BeanService.class, String.format("(objectClass=*.%s)", "MethodInjectedService")).iterator();
    assertTrue(iterator.hasNext());
    ServiceReference<BeanService> serviceReference = iterator.next();
    assertNotNull(serviceReference);
    BeanService bean = bundleContext.getService(serviceReference);
    assertNotNull(bean);
    assertEquals("PREFIXMETHOD", bean.doSomething());
}
Also used : BeanService(org.apache.aries.cdi.test.interfaces.BeanService) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

BeanService (org.apache.aries.cdi.test.interfaces.BeanService)9 ServiceReference (org.osgi.framework.ServiceReference)4 Bean (javax.enterprise.inject.spi.Bean)3 BeanManager (javax.enterprise.inject.spi.BeanManager)3 Bundle (org.osgi.framework.Bundle)3 CdiContainer (org.osgi.service.cdi.CdiContainer)3 Hashtable (java.util.Hashtable)2 List (java.util.List)2 Callable (java.util.concurrent.Callable)2 Filter (org.osgi.framework.Filter)2 Configuration (org.osgi.service.cm.Configuration)2 ServiceTracker (org.osgi.util.tracker.ServiceTracker)2 Set (java.util.Set)1 BundleContextBeanQualifier (org.apache.aries.cdi.test.interfaces.BundleContextBeanQualifier)1 CdiEventObserverQualifier (org.apache.aries.cdi.test.interfaces.CdiEventObserverQualifier)1 BundleContext (org.osgi.framework.BundleContext)1 CdiEvent (org.osgi.service.cdi.CdiEvent)1