Search in sources :

Example 1 with PojoA

use of org.apache.aries.blueprint.pojos.PojoA in project aries by apache.

the class WiringTest method testConstructor.

public void testConstructor() throws Exception {
    ComponentDefinitionRegistryImpl registry = parse("/test-constructor.xml");
    Repository repository = new TestBlueprintContainer(registry).getRepository();
    Object obj1 = repository.create("pojoA");
    assertNotNull(obj1);
    assertTrue(obj1 instanceof PojoA);
    PojoA pojoa = (PojoA) obj1;
    Object obj2 = repository.create("pojoB");
    testPojoB(obj2, URI.create("urn:myuri"), 10);
    assertEquals(obj2, pojoa.getPojob());
    assertEquals(new BigInteger("10"), pojoa.getNumber());
    Object obj3 = repository.create("pojoC");
    testPojoB(obj3, URI.create("urn:myuri-static"), 15);
    Object obj4 = repository.create("pojoD");
    testPojoB(obj4, URI.create("urn:myuri-static"), 15);
    Object obj5 = repository.create("pojoE");
    testPojoB(obj5, URI.create("urn:myuri-dynamic"), 20);
    Object obj6 = repository.create("multipleInt");
    testMultiple(obj6, null, 123, null);
    Object obj7 = repository.create("multipleInteger");
    testMultiple(obj7, null, -1, new Integer(123));
    Object obj8 = repository.create("multipleString");
    testMultiple(obj8, "123", -1, null);
    // TODO: check the below tests when the incoherence between TCK / spec is solved
    //        try {
    //            graph.create("multipleStringConvertable");
    //            fail("Did not throw exception");
    //        } catch (RuntimeException e) {
    //            // we expect exception
    //        }
    Object obj10 = repository.create("multipleFactory1");
    testMultiple(obj10, null, 1234, null);
    Object obj11 = repository.create("multipleFactory2");
    testMultiple(obj11, "helloCreate-boolean", -1, null);
    try {
        repository.create("multipleFactoryNull");
        fail("Did not throw exception");
    } catch (RuntimeException e) {
    // we expect exception 
    // TODO: check the exception string?
    }
    Object obj12 = repository.create("multipleFactoryTypedNull");
    testMultiple(obj12, "hello-boolean", -1, null);
    Object obj13 = repository.create("mapConstruction");
    Map<String, String> constructionMap = new HashMap<String, String>();
    constructionMap.put("a", "b");
    testMultiple(obj13, constructionMap);
    Object obj14 = repository.create("propsConstruction");
    Properties constructionProperties = new Properties();
    constructionProperties.put("a", "b");
    testMultiple(obj14, constructionProperties);
    Object obja = repository.create("mapConstructionWithDefaultType");
    Map<String, Date> mapa = new HashMap<String, Date>();
    // Months are 0-indexed
    Calendar calendar = new GregorianCalendar(2012, 0, 6);
    calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
    mapa.put("date", new Date(calendar.getTimeInMillis()));
    testMultiple(obja, mapa);
    Object objc = repository.create("mapConstructionWithTypedEntries");
    Map mapc = new HashMap();
    mapc.put("boolean", Boolean.TRUE);
    mapc.put("double", 1.23);
    mapc.put("date", new Date(calendar.getTimeInMillis()));
    testMultiple(objc, mapc);
    Object objb = repository.create("mapConstructionWithNonDefaultTypedEntries");
    Map mapb = new HashMap();
    mapb.put("boolean", Boolean.TRUE);
    mapb.put("double", 3.45);
    mapb.put("otherdouble", 10.2);
    testMultiple(objb, mapb);
    Object objd = repository.create("mapConstructionWithNonDefaultTypedKeys");
    Map mapd = new HashMap();
    mapd.put(Boolean.TRUE, "boolean");
    mapd.put(42.42, "double");
    testMultiple(objd, mapd);
    BeanF obj15 = (BeanF) repository.create("booleanWrapped");
    assertNotNull(obj15.getWrapped());
    assertEquals(false, (boolean) obj15.getWrapped());
    assertNull(obj15.getPrim());
// TODO: check the below tests when the incoherence between TCK / spec is solved
//        BeanF obj16 = (BeanF) graph.create("booleanPrim");
//        assertNotNull(obj16.getPrim());
//        assertEquals(false, (boolean) obj16.getPrim());
//        assertNull(obj16.getWrapped());
}
Also used : HashMap(java.util.HashMap) PojoA(org.apache.aries.blueprint.pojos.PojoA) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) BeanF(org.apache.aries.blueprint.pojos.BeanF) GregorianCalendar(java.util.GregorianCalendar) Properties(java.util.Properties) Date(java.util.Date) 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) ComponentDefinitionRegistryImpl(org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with PojoA

use of org.apache.aries.blueprint.pojos.PojoA 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)

Aggregations

BigInteger (java.math.BigInteger)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BlueprintRepository (org.apache.aries.blueprint.container.BlueprintRepository)2 Repository (org.apache.aries.blueprint.di.Repository)2 ComponentDefinitionRegistryImpl (org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl)2 PojoA (org.apache.aries.blueprint.pojos.PojoA)2 MyObject (org.apache.aries.blueprint.pojos.PojoGenerics2.MyObject)2 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 List (java.util.List)1 Properties (java.util.Properties)1 ServiceRecipe (org.apache.aries.blueprint.container.ServiceRecipe)1 MapRecipe (org.apache.aries.blueprint.di.MapRecipe)1 Recipe (org.apache.aries.blueprint.di.Recipe)1 BeanF (org.apache.aries.blueprint.pojos.BeanF)1 PojoB (org.apache.aries.blueprint.pojos.PojoB)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1