Search in sources :

Example 6 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestStaticPropsReconfiguration method testReconfFactory2.

@Test
public void testReconfFactory2() {
    ServiceReference sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-2");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    Integer intProp = (Integer) sr.getProperty("int");
    Long longProp = (Long) sr.getProperty("long");
    String strProp = (String) sr.getProperty("string");
    String[] strAProp = (String[]) sr.getProperty("strAProp");
    int[] intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(4));
    assertEquals("Check longProp equality", longProp, new Long(42));
    assertEquals("Check strProp equality", strProp, "bar");
    assertNotNull("Check strAProp not nullity", strAProp);
    String[] v = new String[] { "bar", "foo" };
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNotNull("Check intAProp not nullity", intAProp);
    int[] v2 = new int[] { 1, 2, 3 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
    // Reconfiguration
    ServiceReference fact_ref = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "PS-FooProviderType-2");
    Properties reconf = new Properties();
    reconf.put("instance.name", "FooProvider-2");
    reconf.put("int", 5);
    reconf.put("long", (long) 43);
    reconf.put("string", "toto");
    reconf.put("strAProp", new String[] { "foo", "baz" });
    reconf.put("intAProp", new int[] { 3, 2, 1 });
    Factory fact = (Factory) osgiHelper.getRawServiceObject(fact_ref);
    try {
        fact.reconfigure(reconf);
    } catch (Exception e) {
        fail("Configuration non acceptable : " + reconf);
    }
    // Check service properties after the reconfiguration
    intProp = (Integer) sr.getProperty("int");
    longProp = (Long) sr.getProperty("long");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality after reconfiguration", intProp, new Integer(5));
    assertEquals("Check longProp equality after reconfiguration", longProp, new Long(43));
    assertEquals("Check strProp equality after reconfiguration", strProp, "toto");
    assertNotNull("Check strAProp not nullity after reconfiguration", strAProp);
    v = new String[] { "foo", "baz" };
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNotNull("Check intAProp not nullity", intAProp);
    v2 = new int[] { 3, 2, 1 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 7 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestStaticPropsReconfiguration method testReconfFactory1.

@Test
public void testReconfFactory1() {
    ServiceReference sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-1");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    Integer intProp = (Integer) sr.getProperty("int");
    Long longProp = (Long) sr.getProperty("long");
    String strProp = (String) sr.getProperty("string");
    String[] strAProp = (String[]) sr.getProperty("strAProp");
    int[] intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(2));
    assertEquals("Check longProp equality", longProp, new Long(40));
    assertEquals("Check strProp equality", strProp, new String("foo"));
    assertNotNull("Check strAProp not nullity", strAProp);
    String[] v = new String[] { "foo", "bar" };
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNotNull("Check intAProp not nullity", intAProp);
    int[] v2 = new int[] { 1, 2, 3 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
    // Reconfiguration
    ServiceReference fact_ref = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "PS-FooProviderType-2");
    Dictionary reconf = new Properties();
    reconf.put("instance.name", "FooProvider-1");
    reconf.put("int", new Integer(5));
    reconf.put("long", new Long(43));
    reconf.put("string", new String("toto"));
    reconf.put("strAProp", new String[] { "foo", "baz" });
    reconf.put("intAProp", new int[] { 3, 2, 1 });
    Factory fact = (Factory) osgiHelper.getRawServiceObject(fact_ref);
    try {
        fact.reconfigure(reconf);
    } catch (Exception e) {
        fail("Configuration non acceptable : " + reconf);
    }
    sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-1");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties after the reconfiguration
    intProp = (Integer) sr.getProperty("int");
    longProp = (Long) sr.getProperty("long");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality after reconfiguration", intProp, new Integer(5));
    assertEquals("Check longProp equality after reconfiguration", longProp, new Long(43));
    assertEquals("Check strProp equality after reconfiguration", strProp, new String("toto"));
    assertNotNull("Check strAProp not nullity after reconfiguration", strAProp);
    v = new String[] { "foo", "baz" };
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNotNull("Check intAProp not nullity", intAProp);
    v2 = new int[] { 3, 2, 1 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Dictionary(java.util.Dictionary) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 8 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestMethodProperties method setUp.

@Before
public void setUp() {
    Factory fact = ipojoHelper.getFactory("CONFIG-MethodConfigurableCheckService");
    Properties props = new Properties();
    props.put("instance.name", "under-test");
    props.put("b", "1");
    props.put("s", "1");
    props.put("i", "1");
    props.put("l", "1");
    props.put("d", "1");
    props.put("f", "1");
    props.put("c", "a");
    props.put("bool", "true");
    props.put("bs", "{1,2,3}");
    props.put("ss", "{1,2,3}");
    props.put("is", "{1,2,3}");
    props.put("ls", "{1,2,3}");
    props.put("ds", "{1,2,3}");
    props.put("fs", "{1,2,3}");
    props.put("cs", "{a,b,c}");
    props.put("bools", "{true,true,true}");
    props.put("string", "foo");
    props.put("strings", "{foo, bar, baz}");
    try {
        instance = fact.createComponentInstance(props);
    } catch (Exception e) {
        fail("Cannot create the under-test instance : " + e.getMessage());
    }
    Properties props2 = new Properties();
    props2.put("instance.name", "under-test-2");
    props2.put("b", new Byte("1"));
    props2.put("s", new Short("1"));
    props2.put("i", new Integer("1"));
    props2.put("l", new Long("1"));
    props2.put("d", new Double("1"));
    props2.put("f", new Float("1"));
    props2.put("c", new Character('a'));
    props2.put("bool", new Boolean(true));
    props2.put("bs", new byte[] { 1, 2, 3 });
    props2.put("ss", new short[] { 1, 2, 3 });
    props2.put("is", new int[] { 1, 2, 3 });
    props2.put("ls", new long[] { 1, 2, 3 });
    props2.put("ds", new double[] { 1, 2, 3 });
    props2.put("fs", new float[] { 1, 2, 3 });
    props2.put("cs", new char[] { 'a', 'b', 'c' });
    props2.put("bools", new boolean[] { true, true, true });
    props2.put("string", "foo");
    props2.put("strings", new String[] { "foo", "bar", "baz" });
    try {
        instance2 = fact.createComponentInstance(props2);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Cannot create the under-test instance 2 : " + e.getMessage());
    }
    try {
        instance3 = fact.createComponentInstance(null);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Cannot create the under-test instance 3 : " + e.getMessage());
    }
}
Also used : Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) Before(org.junit.Before)

Example 9 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestFactoryProperties method testProps.

@Test
public void testProps() {
    ServiceReference ref1 = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "Factories-FooProviderType-Dyn2");
    assertNotNull("The factory is available", ref1);
    PropertyDescription[] pd = (PropertyDescription[]) ref1.getProperty("component.properties");
    assertEquals("Check property list size", pd.length, 5);
    // P0
    assertEquals("0) Check name", "int", pd[0].getName());
    assertEquals("0) Check type", "int", pd[0].getType());
    assertEquals("0) Check value", "4", pd[0].getValue());
    // P1
    assertEquals("1) Check name", "boolean", pd[1].getName());
    assertEquals("1) Check type", "boolean", pd[1].getType());
    assertNull("1) Check value", pd[1].getValue());
    // P2
    assertEquals("2) Check name", "string", pd[2].getName());
    assertEquals("2) Check type", String.class.getName(), pd[2].getType());
    assertNull("2) Check value", pd[2].getValue());
    // P3
    assertEquals("3) Check name", "strAProp", pd[3].getName());
    assertEquals("3) Check type", "java.lang.String[]", pd[3].getType());
    assertNull("3) Check value", pd[3].getValue());
    // P4
    assertEquals("4) Check name", "intAProp", pd[4].getName());
    assertEquals("4) Check type", "int[]", pd[4].getType());
}
Also used : PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription) Factory(org.apache.felix.ipojo.Factory) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 10 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestFactoryProperties method testDoubleExposition.

@Test
public void testDoubleExposition() {
    ServiceReference ref1 = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "Factories-FooBarProviderType-1");
    assertNotNull("The factory is available", ref1);
    String[] spec = (String[]) ref1.getProperty("component.providedServiceSpecifications");
    assertEquals("Check array length", spec.length, 2);
    assertContains("Check spec 1", spec, FooService.class.getName());
    assertContains("Check spec 2", spec, BarService.class.getName());
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Factory(org.apache.felix.ipojo.Factory) BarService(org.apache.felix.ipojo.runtime.core.services.BarService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

Factory (org.apache.felix.ipojo.Factory)111 Properties (java.util.Properties)88 Test (org.junit.Test)71 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)53 Before (org.junit.Before)33 ServiceReference (org.osgi.framework.ServiceReference)29 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)17 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)17 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)14 ServiceContext (org.apache.felix.ipojo.ServiceContext)13 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)11 IOException (java.io.IOException)6 HandlerFactory (org.apache.felix.ipojo.HandlerFactory)6 Architecture (org.apache.felix.ipojo.architecture.Architecture)4 HandlerDescription (org.apache.felix.ipojo.architecture.HandlerDescription)4 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)4 CompositeInstanceDescription (org.apache.felix.ipojo.composite.CompositeInstanceDescription)4 CompositeManager (org.apache.felix.ipojo.composite.CompositeManager)4 ProvidedServiceDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription)4 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)4