Search in sources :

Example 11 with ComponentDefinitionRegistryImpl

use of org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl in project aries by apache.

the class WiringTest method testGenerics.

public void testGenerics() throws Exception {
    ComponentDefinitionRegistryImpl registry = parse("/test-generics.xml");
    Repository repository = new TestBlueprintContainer(registry).getRepository();
    List<Integer> expectedList = new ArrayList<Integer>();
    expectedList.add(new Integer(10));
    expectedList.add(new Integer(20));
    expectedList.add(new Integer(50));
    Set<Long> expectedSet = new HashSet<Long>();
    expectedSet.add(new Long(1000));
    expectedSet.add(new Long(2000));
    expectedSet.add(new Long(5000));
    Map<Short, Boolean> expectedMap = new HashMap<Short, Boolean>();
    expectedMap.put(new Short((short) 1), Boolean.TRUE);
    expectedMap.put(new Short((short) 2), Boolean.FALSE);
    expectedMap.put(new Short((short) 5), Boolean.TRUE);
    Object obj;
    PojoGenerics pojo;
    obj = repository.create("method");
    assertTrue(obj instanceof PojoGenerics);
    pojo = (PojoGenerics) obj;
    assertEquals(expectedList, pojo.getList());
    assertEquals(expectedSet, pojo.getSet());
    assertEquals(expectedMap, pojo.getMap());
    obj = repository.create("constructorList");
    assertTrue(obj instanceof PojoGenerics);
    pojo = (PojoGenerics) obj;
    assertEquals(expectedList, pojo.getList());
    obj = repository.create("constructorSet");
    assertTrue(obj instanceof PojoGenerics);
    pojo = (PojoGenerics) obj;
    assertEquals(expectedSet, pojo.getSet());
    obj = repository.create("constructorMap");
    assertTrue(obj instanceof PojoGenerics);
    pojo = (PojoGenerics) obj;
    assertEquals(expectedMap, pojo.getMap());
    obj = repository.create("genericPojo");
    assertTrue(obj instanceof Primavera);
    assertEquals("string", ((Primavera) obj).prop);
    obj = repository.create("doubleGenericPojo");
    assertTrue(obj instanceof Primavera);
    assertEquals("stringToo", ((Primavera) obj).prop);
}
Also used : PojoGenerics(org.apache.aries.blueprint.pojos.PojoGenerics) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) Repository(org.apache.aries.blueprint.di.Repository) BlueprintRepository(org.apache.aries.blueprint.container.BlueprintRepository) MyObject(org.apache.aries.blueprint.pojos.PojoGenerics2.MyObject) Primavera(org.apache.aries.blueprint.pojos.Primavera) ComponentDefinitionRegistryImpl(org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl) HashSet(java.util.HashSet)

Example 12 with ComponentDefinitionRegistryImpl

use of org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl in project aries by apache.

the class WiringTest method testWiring.

public void testWiring() throws Exception {
    ComponentDefinitionRegistryImpl registry = parse("/test-wiring.xml");
    Repository repository = new TestBlueprintContainer(registry).getRepository();
    Object obj1 = repository.create("pojoA");
    assertNotNull(obj1);
    assertTrue(obj1 instanceof PojoA);
    PojoA pojoa = (PojoA) obj1;
    // test singleton scope
    assertTrue(obj1 == repository.create("pojoA"));
    Object obj2 = repository.create("pojoB");
    assertNotNull(obj2);
    assertTrue(obj2 instanceof PojoB);
    PojoB pojob = (PojoB) obj2;
    assertNotNull(pojoa.getPojob());
    assertNotNull(pojoa.getPojob().getUri());
    assertNotNull(pojoa.getList());
    assertEquals("list value", pojoa.getList().get(0));
    assertEquals(new Integer(55), pojoa.getList().get(2));
    assertEquals(URI.create("http://geronimo.apache.org"), pojoa.getList().get(3));
    Object c0 = pojoa.getList().get(1);
    Object c1 = pojoa.getList().get(4);
    assertNotNull(c0);
    assertNotNull(c1);
    assertEquals(PojoB.class, c0.getClass());
    assertEquals(PojoB.class, c1.getClass());
    assertNotSame(c0, c1);
    assertNotNull(pojoa.getArray());
    assertEquals("list value", pojoa.getArray()[0]);
    assertEquals(pojob, pojoa.getArray()[1]);
    assertEquals(new Integer(55), pojoa.getArray()[2]);
    assertEquals(URI.create("http://geronimo.apache.org"), pojoa.getArray()[3]);
    assertNotNull(pojoa.getSet());
    assertTrue(pojoa.getSet().contains("set value"));
    assertTrue(pojoa.getSet().contains(pojob.getUri()));
    assertTrue(pojoa.getSet().contains(URI.create("http://geronimo.apache.org")));
    assertNotNull(pojoa.getMap());
    assertEquals("val", pojoa.getMap().get("key"));
    assertEquals(pojob, pojoa.getMap().get(pojob));
    assertEquals(URI.create("http://geronimo.apache.org"), pojoa.getMap().get(new Integer(5)));
    assertNotNull(pojoa.getProps());
    assertEquals("value1", pojoa.getProps().get("key1"));
    assertEquals("value2", pojoa.getProps().get("2"));
    assertEquals("bar", pojoa.getProps().get("foo"));
    assertNotNull(pojoa.getNumber());
    assertEquals(new BigInteger("10"), pojoa.getNumber());
    assertNotNull(pojoa.getIntArray());
    assertEquals(3, pojoa.getIntArray().length);
    assertEquals(1, pojoa.getIntArray()[0]);
    assertEquals(50, pojoa.getIntArray()[1]);
    assertEquals(100, pojoa.getIntArray()[2]);
    assertNotNull(pojoa.getNumberArray());
    assertEquals(4, pojoa.getNumberArray().length);
    assertEquals(new Integer(1), pojoa.getNumberArray()[0]);
    assertEquals(new BigInteger("50"), pojoa.getNumberArray()[1]);
    assertEquals(new Long(100), pojoa.getNumberArray()[2]);
    assertEquals(new Integer(200), pojoa.getNumberArray()[3]);
    // test init-method
    assertEquals(true, pojob.getInitCalled());
    // test service
    Object obj3 = repository.create("service1");
    assertNotNull(obj3);
    assertTrue(obj3 instanceof ServiceRegistration);
    ExecutionContext.Holder.setContext((ExecutionContext) repository);
    for (Recipe r : ((ServiceRecipe) repository.getRecipe("service1")).getDependencies()) {
        if (r instanceof MapRecipe) {
            Map m = (Map) r.create();
            assertEquals("value1", m.get("key1"));
            assertEquals("value2", m.get("key2"));
            assertTrue(m.get("key3") instanceof List);
        }
    }
    ExecutionContext.Holder.setContext(null);
    // tests 'prototype' scope
    Object obj4 = repository.create("pojoC");
    assertNotNull(obj4);
    assertTrue(obj4 != repository.create("pojoC"));
    repository.destroy();
    // test destroy-method
    assertEquals(true, pojob.getDestroyCalled());
}
Also used : PojoB(org.apache.aries.blueprint.pojos.PojoB) ServiceRecipe(org.apache.aries.blueprint.container.ServiceRecipe) MapRecipe(org.apache.aries.blueprint.di.MapRecipe) Recipe(org.apache.aries.blueprint.di.Recipe) PojoA(org.apache.aries.blueprint.pojos.PojoA) MapRecipe(org.apache.aries.blueprint.di.MapRecipe) ServiceRecipe(org.apache.aries.blueprint.container.ServiceRecipe) BigInteger(java.math.BigInteger) Repository(org.apache.aries.blueprint.di.Repository) BlueprintRepository(org.apache.aries.blueprint.container.BlueprintRepository) BigInteger(java.math.BigInteger) MyObject(org.apache.aries.blueprint.pojos.PojoGenerics2.MyObject) ArrayList(java.util.ArrayList) List(java.util.List) ComponentDefinitionRegistryImpl(org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl) HashMap(java.util.HashMap) Map(java.util.Map) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 13 with ComponentDefinitionRegistryImpl

use of org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl in project aries by apache.

the class WiringTest method testFieldInjection.

public void testFieldInjection() throws Exception {
    ComponentDefinitionRegistryImpl registry = parse("/test-wiring.xml");
    Repository repository = new TestBlueprintContainer(registry).getRepository();
    Object fiTestBean = repository.create("FITestBean");
    assertNotNull(fiTestBean);
    assertTrue(fiTestBean instanceof FITestBean);
    FITestBean bean = (FITestBean) fiTestBean;
    // single field injection
    assertEquals("value", bean.getAttr());
    // prefer setter injection to field injection
    assertEquals("IS_LOWER", bean.getUpperCaseAttr());
    // support cascaded injection 'bean.name' via fields
    assertEquals("aName", bean.getBeanName());
    // fail if field-injection is not specified
    try {
        repository.create("FIFailureTestBean");
        Assert.fail("Expected exception");
    } catch (ComponentDefinitionException cde) {
    }
    // fail if field-injection is false
    try {
        repository.create("FIFailureTest2Bean");
        Assert.fail("Expected exception");
    } catch (ComponentDefinitionException cde) {
    }
}
Also used : Repository(org.apache.aries.blueprint.di.Repository) BlueprintRepository(org.apache.aries.blueprint.container.BlueprintRepository) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) FITestBean(org.apache.aries.blueprint.pojos.FITestBean) MyObject(org.apache.aries.blueprint.pojos.PojoGenerics2.MyObject) ComponentDefinitionRegistryImpl(org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl)

Example 14 with ComponentDefinitionRegistryImpl

use of org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl in project aries by apache.

the class WiringTest method testIdRefs.

public void testIdRefs() throws Exception {
    ComponentDefinitionRegistryImpl registry = parse("/test-bad-id-ref.xml");
    try {
        new TestBlueprintContainer(registry).getRepository();
        fail("Did not throw exception");
    } catch (RuntimeException e) {
    // we expect exception
    // TODO: check error string?
    }
}
Also used : ComponentDefinitionRegistryImpl(org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl)

Example 15 with ComponentDefinitionRegistryImpl

use of org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl in project aries by apache.

the class BeanLoadingTest method testLoadSimpleBeanNested.

public void testLoadSimpleBeanNested() throws Exception {
    ComponentDefinitionRegistryImpl registry = parse("/test-bean-classes.xml");
    Repository repository = new TestBlueprintContainer(registry).getRepository();
    Object obj = repository.create("simpleBeanNested");
    assertNotNull(obj);
    assertTrue(obj instanceof SimpleBean.Nested);
}
Also used : Repository(org.apache.aries.blueprint.di.Repository) SimpleBean(org.apache.aries.blueprint.pojos.SimpleBean) ComponentDefinitionRegistryImpl(org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl)

Aggregations

ComponentDefinitionRegistryImpl (org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl)16 Repository (org.apache.aries.blueprint.di.Repository)12 BlueprintRepository (org.apache.aries.blueprint.container.BlueprintRepository)8 MyObject (org.apache.aries.blueprint.pojos.PojoGenerics2.MyObject)6 HashMap (java.util.HashMap)4 BigInteger (java.math.BigInteger)3 Map (java.util.Map)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 ComponentDefinitionRegistry (org.apache.aries.blueprint.ComponentDefinitionRegistry)2 NamespaceHandlerSet (org.apache.aries.blueprint.parser.NamespaceHandlerSet)2 Parser (org.apache.aries.blueprint.parser.Parser)2 BeanD (org.apache.aries.blueprint.pojos.BeanD)2 PojoA (org.apache.aries.blueprint.pojos.PojoA)2 PojoB (org.apache.aries.blueprint.pojos.PojoB)2 SimpleBean (org.apache.aries.blueprint.pojos.SimpleBean)2 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)2 URL (java.net.URL)1 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1