Search in sources :

Example 36 with FooService

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

the class TestUpdatedNoArgMethodAndConfigAdmin method testStatic.

@Test
public void testStatic() throws IOException, InterruptedException {
    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 -1", fooP, "foo");
    assertEquals("Check bar equality -1", barP, new Integer(2));
    assertEquals("Check baz equality -1", 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("bar", new Integer(2));
    conf.put("foo", "foo");
    // 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 -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) Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 37 with FooService

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

the class TestUpdatedNoArgMethodAndManagedService method testStaticInstance2.

@Test
public void testStaticInstance2() {
    ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.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 -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(), "instanceMSP");
    assertNotNull("Check ManagedService 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);
    }
    // Recheck props
    fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.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());
    conf.put("baz", "zab2");
    conf.put("foo", "oof2");
    conf.put("bar", new Integer(0));
    ms = (ManagedService) osgiHelper.getRawServiceObject(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    updated = (Integer) fs.fooProps().get("updated");
    assertEquals("Check updated -2", 2, 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 38 with FooService

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

the class TestUpdatedNoArgMethodAndManagedService method testDynamicInstance1.

@Test
public void testDynamicInstance1() {
    ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance1.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");
    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("foo", "oof");
    conf.put("bar", new Integer(0));
    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", 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", 1, updated.intValue());
    conf.put("baz", "zab2");
    conf.put("foo", "oof2");
    conf.put("bar", new Integer(0));
    ms = (ManagedService) osgiHelper.getRawServiceObject(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    updated = (Integer) fs.fooProps().get("updated");
    assertEquals("Check updated -2", 2, 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) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 39 with FooService

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

the class TestUpdatedNoArgMethodAndManagedService method testDynamicInstance2.

@Test
public void testDynamicInstance2() {
    ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.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");
    ServiceReference msRef = osgiHelper.getServiceReferenceByPID(ManagedService.class.getName(), "instanceMSP");
    assertNotNull("Check ManagedServiceFactory availability", msRef);
    // Configuration of baz
    Dictionary<String, Object> conf = new Hashtable<String, Object>();
    conf.put("baz", "zab");
    conf.put("foo", "oof");
    conf.put("bar", new Integer(0));
    ManagedService ms = (ManagedService) osgiHelper.getRawServiceObject(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    // Recheck props
    fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.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());
    conf.put("baz", "zab2");
    conf.put("foo", "oof2");
    conf.put("bar", new Integer(0));
    ms = (ManagedService) osgiHelper.getRawServiceObject(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    updated = (Integer) fs.fooProps().get("updated");
    assertEquals("Check updated -2", 2, 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) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 40 with FooService

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

the class TestManagedServiceFactoryTestForImmediate method testDelayedCreationAndReconfiguration.

@Test
public void testDelayedCreationAndReconfiguration() {
    factory.stop();
    Configuration configuration = null;
    try {
        configuration = admin.createFactoryConfiguration("CA-ImmConfigurableProvider", "?");
    } catch (IOException e) {
        fail(e.getMessage());
    }
    Dictionary props = configuration.getProperties();
    if (props == null) {
        props = new Properties();
    }
    props.put("message", "message");
    try {
        configuration.update(props);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    String pid = configuration.getPid();
    // Wait for the processing of the first configuration.
    grace();
    assertNull("check no instance", osgiHelper.getServiceObject(Architecture.class.getName(), "(architecture.instance=" + pid + ")"));
    factory.start();
    // Wait for the processing of the first configuration.
    grace();
    // The instance should be created, wait for the architecture service
    osgiHelper.waitForService(Architecture.class.getName(), "(architecture.instance=" + pid + ")", 1000);
    Architecture architecture = (Architecture) osgiHelper.getServiceObject(Architecture.class.getName(), "(architecture.instance=" + pid + ")");
    assertEquals("Check object", 1, ((PrimitiveInstanceDescription) architecture.getInstanceDescription()).getCreatedObjects().length);
    FooService fs = (FooService) osgiHelper.getServiceObject(FooService.class.getName(), "(instance.name=" + pid + ")");
    Properties p = fs.fooProps();
    String mes = p.getProperty("message");
    int count = (Integer) p.get("count");
    // architecture = (Architecture) osgiHelper.getServiceObject( Architecture.class.getName(), "(architecture.instance="+pid+")");
    assertEquals("Assert Message", "message", mes);
    assertEquals("Assert count", 1, count);
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) architecture.getInstanceDescription()).getCreatedObjects().length);
    props.put("message", "message2");
    try {
        configuration.update(props);
        // Update the configuration ...
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    fs = (FooService) osgiHelper.getServiceObject(FooService.class.getName(), "(instance.name=" + pid + ")");
    p = fs.fooProps();
    mes = p.getProperty("message");
    count = (Integer) p.get("count");
    // architecture = (Architecture) osgiHelper.getServiceObject( Architecture.class.getName(), "(architecture.instance="+pid+")");
    assertEquals("Assert Message", "message2", mes);
    // assertEquals("Assert count", 2, count);
    // This test was removed as the result can be 3.
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) architecture.getInstanceDescription()).getCreatedObjects().length);
    try {
        configuration.delete();
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ServiceReference ref = osgiHelper.getServiceReference(FooService.class.getName(), "(instance.name=" + pid + ")");
    assertNull("Check unavailability", ref);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) Architecture(org.apache.felix.ipojo.architecture.Architecture) IOException(java.io.IOException) Properties(java.util.Properties) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) 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