Search in sources :

Example 41 with FooService

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

the class TestManagedServiceFactoryTestForImmediate method testCreationAndReconfiguration.

@Test
public void testCreationAndReconfiguration() {
    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();
    // 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);
    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)

Example 42 with FooService

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

the class TestManagedServiceFactoryTestForServices method testDelayedCreationAndReconfiguration2.

@Test
public void testDelayedCreationAndReconfiguration2() {
    factory.stop();
    // The reconfiguration happens before the service invocation
    Configuration configuration = null;
    try {
        configuration = admin.createFactoryConfiguration("CA-ConfigurableProvider", "?");
    } 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();
    System.out.println("PID : " + pid);
    assertNull("check no instance", osgiHelper.getServiceObject(Architecture.class.getName(), "(architecture.instance=" + pid + ")"));
    factory.start();
    // 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 no object", 0, ((PrimitiveInstanceDescription) architecture.getInstanceDescription()).getCreatedObjects().length);
    // Wait for the processing of the first configuration.
    grace();
    props.put("message", "message2");
    try {
        configuration.update(props);
        // Update the configuration ...
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // architecture = (Architecture) osgiHelper.getServiceObject( Architecture.class.getName(), "(architecture.instance="+pid+")");
    assertEquals("Check no object -2", 0, ((PrimitiveInstanceDescription) architecture.getInstanceDescription()).getCreatedObjects().length);
    // Invoke
    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", "message2", mes);
    assertEquals("Assert count", 1, count);
    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)

Example 43 with FooService

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

the class TestManagedServiceTestForImmediate method testCreationAndReconfiguration3.

@Test
public void testCreationAndReconfiguration3() {
    // The configuration exists before the instance creation.
    // Update
    Configuration configuration;
    try {
        configuration = admin.getConfiguration(msp, getTestBundle().getLocation());
        Dictionary prc = configuration.getProperties();
        if (prc == null) {
            prc = new Properties();
        }
        prc.put("message", "message2");
        configuration.update(prc);
        grace();
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    Properties props = new Properties();
    props.put("managed.service.pid", msp);
    props.put("message", "message");
    ComponentInstance instance = null;
    try {
        instance = factImm.createComponentInstance(props);
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    assertNotNull("FS availability", ref);
    FooService fs = (FooService) bc.getService(ref);
    Properties p = fs.fooProps();
    String mes = p.getProperty("message");
    // int count = ((Integer) p.get("count")).intValue();
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    // Already reconfigured.
    assertEquals("Check message", "message2", mes);
    // Reconfiguration
    try {
        configuration = admin.getConfiguration(msp);
        Dictionary prc = configuration.getProperties();
        if (prc == null) {
            prc = new Properties();
        }
        prc.put("message", "message3");
        configuration.update(prc);
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    instance.dispose();
    // Recreation of the instance.
    props = new Properties();
    props.put("managed.service.pid", msp);
    props.put("message", "message");
    instance = null;
    try {
        instance = factImm.createComponentInstance(props);
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Wait until the configuration is delivered.
    grace();
    ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    assertNotNull("FS availability", ref);
    fs = (FooService) bc.getService(ref);
    p = fs.fooProps();
    mes = p.getProperty("message");
    // count = ((Integer) p.get("count")).intValue();
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    // Already reconfigured.
    assertEquals("Check message", "message3", mes);
    // assertEquals("Check count", 1, count);
    instance.dispose();
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Properties(java.util.Properties) IOException(java.io.IOException) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 44 with FooService

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

the class TestManagedServiceTestForImmediate method testFactoryCreationAndReconfigurationUsingManagedService.

@Test
public void testFactoryCreationAndReconfigurationUsingManagedService() {
    Properties props = new Properties();
    // Exposes a ManagedService.
    props.put("managed.service.pid", msp);
    props.put("message", "message");
    ComponentInstance instance = null;
    try {
        instance = factImm.createComponentInstance(props);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    assertNotNull("FS availability", ref);
    FooService fs = (FooService) bc.getService(ref);
    Properties p = fs.fooProps();
    String mes = p.getProperty("message");
    int count = (Integer) p.get("count");
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    assertEquals("Check message", "message", mes);
    assertEquals("Check count", 1, count);
    // Update
    Configuration configuration;
    try {
        configuration = admin.getConfiguration(msp, getTestBundle().getLocation());
        Dictionary prc = configuration.getProperties();
        if (prc == null) {
            prc = new Properties();
        }
        prc.put("message", "message2");
        configuration.update(prc);
        System.err.println("updated ? ");
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    assertNotNull("FS availability", ref);
    fs = (FooService) bc.getService(ref);
    p = fs.fooProps();
    mes = p.getProperty("message");
    count = (Integer) p.get("count");
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    assertEquals("Check message", "message2", mes);
    assertEquals("Check count", 2, count);
    instance.dispose();
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Properties(java.util.Properties) IOException(java.io.IOException) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 45 with FooService

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

the class TestManagedServiceTestForService method testConfigurationPushedBeforeInstantiationUsingFactoryAndReconfiguration.

@Test
public void testConfigurationPushedBeforeInstantiationUsingFactoryAndReconfiguration() {
    // The configuration exists before the instance creation.
    // Update
    Configuration configuration;
    try {
        configuration = admin.getConfiguration(msp, getTestBundle().getLocation());
        Dictionary prc = configuration.getProperties();
        if (prc == null) {
            prc = new Properties();
        }
        prc.put("message", "message2");
        configuration.update(prc);
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    Properties props = new Properties();
    props.put("managed.service.pid", msp);
    props.put("message", "message");
    ComponentInstance instance = null;
    try {
        instance = factSvc.createComponentInstance(props);
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
    assertEquals("Check no object", 0, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    assertNotNull("FS availability", ref);
    FooService fs = (FooService) bc.getService(ref);
    Properties p = fs.fooProps();
    String mes = p.getProperty("message");
    int count = (Integer) p.get("count");
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    // Already reconfigured.
    assertEquals("Check message", "message2", mes);
    assertEquals("Check count", 1, count);
    // Reconfiguration
    try {
        configuration = admin.getConfiguration(msp, getTestBundle().getLocation());
        Dictionary prc = configuration.getProperties();
        if (prc == null) {
            prc = new Properties();
        }
        prc.put("message", "message3");
        configuration.update(prc);
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    instance.dispose();
    // Recreation of the instance.
    props = new Properties();
    props.put("managed.service.pid", msp);
    props.put("message", "message");
    instance = null;
    try {
        instance = factSvc.createComponentInstance(props);
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
    assertEquals("Check no object", 0, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    assertNotNull("FS availability", ref);
    fs = (FooService) bc.getService(ref);
    p = fs.fooProps();
    mes = p.getProperty("message");
    count = (Integer) p.get("count");
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    // Already reconfigured.
    assertEquals("Check message", "message3", mes);
    assertEquals("Check count", 1, count);
    instance.dispose();
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Properties(java.util.Properties) IOException(java.io.IOException) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) 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