Search in sources :

Example 81 with FooService

use of org.apache.felix.ipojo.runtime.core.services.FooService in project felix by apache.

the class TestUpdatedNoArgMethodAndConfigAdmin method testDynamicString.

@Test
public void testDynamicString() throws InterruptedException, IOException {
    ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
    assertNotNull("Check FS availability", fooRef);
    String fooP = (String) fooRef.getProperty("foo");
    Integer barP = (Integer) fooRef.getProperty("bar");
    String bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality", fooP, "foo");
    assertEquals("Check bar equality", barP, new Integer(2));
    assertEquals("Check baz equality", bazP, "baz");
    ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
    assertNotNull("Check Configuration Admin availability", admin);
    Configuration configuration = admin.getConfiguration(instance.getInstanceName(), getTestBundle().getLocation());
    // Configuration of baz
    Dictionary<String, Object> conf = new Hashtable<String, Object>();
    conf.put("baz", "zab");
    conf.put("foo", "oof");
    conf.put("bar", "0");
    // Asynchronous dispatching of the configuration
    configuration.update(conf);
    grace();
    // Recheck props
    fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
    fooP = (String) fooRef.getProperty("foo");
    barP = (Integer) fooRef.getProperty("bar");
    bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality", fooP, "oof");
    assertEquals("Check bar equality", barP, new Integer(0));
    assertEquals("Check baz equality", bazP, "zab");
    // Check field value
    FooService fs = (FooService) osgiHelper.getRawServiceObject(fooRef);
    Properties p = fs.fooProps();
    fooP = (String) p.get("foo");
    barP = (Integer) p.get("bar");
    assertEquals("Check foo field equality", fooP, "oof");
    assertEquals("Check bar field equality", barP, new Integer(0));
    Integer updated = (Integer) fs.fooProps().get("updated");
    assertEquals("Check updated", 1, updated.intValue());
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) Properties(java.util.Properties) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 82 with FooService

use of org.apache.felix.ipojo.runtime.core.services.FooService in project felix by apache.

the class TestUpdatedNoArgMethodAndManagedService method testStaticInstance1.

@Test
public void testStaticInstance1() {
    ServiceReference fooRef = osgiHelper.waitForService(FooService.class.getName(), "(instance.name=" + instance1.getInstanceName() + ")", 5000);
    assertNotNull("Check FS availability", fooRef);
    String fooP = (String) fooRef.getProperty("foo");
    Integer barP = (Integer) fooRef.getProperty("bar");
    String bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality -1", fooP, "foo");
    assertEquals("Check bar equality -1", barP, new Integer(2));
    assertEquals("Check baz equality -1", bazP, "baz");
    ServiceReference msRef = osgiHelper.getServiceReferenceByPID(ManagedService.class.getName(), "FooProvider-3");
    assertNotNull("Check ManagedServiceFactory availability", msRef);
    // Configuration of baz
    Dictionary<String, Object> conf = new Hashtable<String, Object>();
    conf.put("baz", "zab");
    conf.put("bar", new Integer(2));
    conf.put("foo", "foo");
    ManagedService ms = (ManagedService) osgiHelper.getRawServiceObject(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    // Re-check props
    fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance1.getInstanceName());
    fooP = (String) fooRef.getProperty("foo");
    barP = (Integer) fooRef.getProperty("bar");
    bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality -2", fooP, "foo");
    assertEquals("Check bar equality -2", barP, new Integer(2));
    assertEquals("Check baz equality -2", bazP, "zab");
    // Get Service
    FooService fs = (FooService) osgiHelper.getRawServiceObject(fooRef);
    Integer updated = (Integer) fs.fooProps().get("updated");
    assertEquals("Check updated", 1, updated.intValue());
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ManagedService(org.osgi.service.cm.ManagedService) ConfigurationException(org.osgi.service.cm.ConfigurationException) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 83 with FooService

use of org.apache.felix.ipojo.runtime.core.services.FooService in project felix by apache.

the class TestDynamicProps method testProperties2.

@Test
public void testProperties2() {
    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");
    Boolean boolProp = (Boolean) sr.getProperty("boolean");
    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", boolProp, new Boolean(false));
    assertEquals("Check strProp equality", strProp, new String("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");
        }
    }
    // Invoke
    FooService fs = (FooService) osgiHelper.getRawServiceObject(sr);
    assertTrue("invoke fs", fs.foo());
    // Re-check the property (change)
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(3));
    assertEquals("Check longProp equality", boolProp, new Boolean(true));
    assertEquals("Check strProp equality", strProp, new String("foo"));
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar", "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");
        }
    }
    fs = null;
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 84 with FooService

use of org.apache.felix.ipojo.runtime.core.services.FooService in project felix by apache.

the class TestDynamicPropsReconfiguration method testFactoryReconfString.

@Test
public void testFactoryReconfString() {
    ServiceReference sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    Integer intProp = (Integer) sr.getProperty("int");
    Boolean boolProp = (Boolean) sr.getProperty("boolean");
    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(0));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "");
    assertNotNull("Check strAProp not nullity", strAProp);
    String[] v = new String[0];
    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[0];
    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-Dyn2");
    Factory fact = (Factory) osgiHelper.getRawServiceObject(fact_ref);
    Properties p3 = new Properties();
    p3.put("instance.name", "FooProvider-3");
    p3.put("int", "1");
    p3.put("boolean", "true");
    p3.put("string", "foo");
    p3.put("strAProp", "{foo, bar, baz}");
    p3.put("intAProp", "{1, 2, 3}");
    try {
        fact.reconfigure(p3);
    } catch (Exception e) {
        fail("Unable to reconfigure the instance with : " + p3);
    }
    sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(1));
    assertEquals("Check longProp equality", boolProp, new Boolean(true));
    assertEquals("Check strProp equality", strProp, new String("foo"));
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar", "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[] { 1, 2, 3 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
    // Invoke
    FooService fs = (FooService) osgiHelper.getRawServiceObject(sr);
    assertTrue("invoke fs", fs.foo());
    // Re-check the property (change)
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(2));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "foo");
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar" };
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNull("Check intAProp hiding (no value)", intAProp);
    // Reconfiguration
    fact_ref = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "PS-FooProviderType-Dyn2");
    fact = (Factory) osgiHelper.getRawServiceObject(fact_ref);
    p3 = new Properties();
    p3.put("instance.name", "FooProvider-3");
    p3.put("int", "1");
    p3.put("boolean", "true");
    p3.put("string", "foo");
    p3.put("strAProp", "{foo, bar, baz}");
    p3.put("intAProp", "{ 1, 2, 3}");
    try {
        fact.reconfigure(p3);
    } catch (Exception e) {
        fail("Unable to reconfigure the instance with : " + p3);
    }
    sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(1));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "foo");
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar", "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[] { 1, 2, 3 };
    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 85 with FooService

use of org.apache.felix.ipojo.runtime.core.services.FooService in project felix by apache.

the class TestDynamicPropsReconfiguration method testReconfiguraitonUsingManagedServiceWithStrings.

@Test
public void testReconfiguraitonUsingManagedServiceWithStrings() throws IOException, InterruptedException {
    ServiceReference sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    Integer intProp = (Integer) sr.getProperty("int");
    Boolean boolProp = (Boolean) sr.getProperty("boolean");
    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(0));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "");
    assertNotNull("Check strAProp not nullity", strAProp);
    String[] v = new String[0];
    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[0];
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
    // Reconfiguration
    ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
    Configuration configuration = admin.getConfiguration("FooProvider-3", "?");
    Dictionary<String, Object> p3 = new Hashtable<String, Object>();
    p3.put("int", "1");
    p3.put("boolean", "true");
    p3.put("string", "foo");
    p3.put("strAProp", "{foo, bar, baz}");
    p3.put("intAProp", "{ 1, 2, 3}");
    configuration.update(p3);
    TimeUtils.grace(200);
    sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(1));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "foo");
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar", "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[] { 1, 2, 3 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
    // Invoke
    FooService fs = (FooService) osgiHelper.getRawServiceObject(sr);
    assertTrue("invoke fs", fs.foo());
    // Re-check the property (change)
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(2));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "foo");
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar" };
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNull("Check intAProp hidding (no value)", intAProp);
    // Reconfiguration
    p3 = new Hashtable<String, Object>();
    p3.put("int", "1");
    p3.put("boolean", "true");
    p3.put("string", "foo");
    p3.put("strAProp", "{foo, bar, baz}");
    p3.put("intAProp", "{ 1, 2, 3}");
    configuration.update(p3);
    TimeUtils.grace(200);
    sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(1));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "foo");
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar", "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[] { 1, 2, 3 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Test(org.junit.Test)

Aggregations

FooService (org.apache.felix.ipojo.runtime.core.services.FooService)123 Test (org.junit.Test)121 ServiceReference (org.osgi.framework.ServiceReference)110 Properties (java.util.Properties)68 Configuration (org.osgi.service.cm.Configuration)33 Hashtable (java.util.Hashtable)30 Dictionary (java.util.Dictionary)28 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)25 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)24 ServiceContext (org.apache.felix.ipojo.ServiceContext)21 IOException (java.io.IOException)19 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)17 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)15 ConfigurationException (org.osgi.service.cm.ConfigurationException)13 ManagedService (org.osgi.service.cm.ManagedService)13 BaseTest (org.ow2.chameleon.testing.helpers.BaseTest)13 Architecture (org.apache.felix.ipojo.architecture.Architecture)11 Factory (org.apache.felix.ipojo.Factory)8 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)5 FooProviderType1 (org.apache.felix.ipojo.runtime.core.components.FooProviderType1)3