Search in sources :

Example 36 with PrimitiveInstanceDescription

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

the class TestManagedServiceTestForImmediate method testCreationAndReconfiguration2.

@Test
public void testCreationAndReconfiguration2() {
    // The configuration exists before the instance creation.
    // Update
    Configuration configuration = null;
    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 = 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 count1 = ((Integer) p.get("count")).intValue();
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    // Already reconfigured.
    assertEquals("Check message - 1 (" + mes + ")", "message2", mes);
    // assertEquals("Check count", 2, count); // Two : 1) "message" on immediate, "message2" on the reconfiguration,
    // not necessary as the property can be set before the immediate instance creation
    instance.dispose();
    // 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());
    }
    // Recreation of the instance.
    props = new Properties();
    props.put("managed.service.pid", msp);
    props.put("message", "message");
    instance = null;
    try {
        instance = factImm.createComponentInstance(props);
        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");
    // int count = ((Integer) p.get("count")).intValue();
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
    // Already reconfigured.
    assertEquals("Check message already reconfigured", "message3", mes);
    // assertEquals("Check count", count1 + 1, count); // message before the reconfiguration, message3 after the reconfiguration
    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 37 with PrimitiveInstanceDescription

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

the class TestManagedServiceTestForService method testCreationUsingMSFAndReconfigurationUsingManagedService.

@Test
public void testCreationUsingMSFAndReconfigurationUsingManagedService() {
    Configuration conf = null;
    try {
        conf = admin.createFactoryConfiguration(factNameSvc, getTestBundle().getLocation());
        Dictionary props = conf.getProperties();
        if (props == null) {
            props = new Properties();
        }
        props.put("managed.service.pid", msp);
        props.put("message", "message");
        conf.update(props);
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    Architecture arch = (Architecture) osgiHelper.getServiceObject(org.apache.felix.ipojo.architecture.Architecture.class.getName(), "(architecture.instance=" + conf.getPid() + ")");
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), conf.getPid());
    assertEquals("Check no object", 0, ((PrimitiveInstanceDescription) arch.getInstanceDescription()).getCreatedObjects().length);
    assertNotNull("FS availability", ref);
    // arch = (Architecture) osgiHelper.getServiceObject( org.apache.felix.ipojo.architecture.Architecture.class.getName(), "(architecture.instance=" + conf.getPid() + ")");
    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) arch.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);
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // arch = (Architecture) osgiHelper.getServiceObject( org.apache.felix.ipojo.architecture.Architecture.class.getName(), "(architecture.instance=" + conf.getPid() + ")");
    ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), conf.getPid());
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) arch.getInstanceDescription()).getCreatedObjects().length);
    assertNotNull("FS availability", ref);
    // arch = (Architecture) osgiHelper.getServiceObject( org.apache.felix.ipojo.architecture.Architecture.class.getName(), "(architecture.instance=" + conf.getPid() + ")");
    fs = (FooService) bc.getService(ref);
    p = fs.fooProps();
    mes = p.getProperty("message");
    count = (Integer) p.get("count");
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) arch.getInstanceDescription()).getCreatedObjects().length);
    if (mes.equals("message")) {
        System.out.println("Warning, configuration not yet applied");
        assertEquals("Check count - W", 1, count);
    } else {
        assertEquals("Check message", "message2", mes);
        assertEquals("Check count", 2, count);
    }
    try {
        conf.delete();
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : 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) IOException(java.io.IOException) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Test(org.junit.Test)

Example 38 with PrimitiveInstanceDescription

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

the class TestManagedServiceTestForService method testCreationUsingFactoryAndReconfigurationUsingManagedService.

@Test
public void testCreationUsingFactoryAndReconfigurationUsingManagedService() {
    Properties props = new Properties();
    props.put("managed.service.pid", msp);
    props.put("message", "message");
    ComponentInstance instance = null;
    try {
        instance = factSvc.createComponentInstance(props);
    } 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);
    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);
        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);
    if (mes.equals("message")) {
        System.out.println("Warning, configuration not yet applied");
        assertEquals("Check count - W", 1, count);
    } else {
        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 39 with PrimitiveInstanceDescription

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

the class TestManagedServiceTestForService method testConfigurationPushedBeforeInstantiationUsingFactory.

@Test
public void testConfigurationPushedBeforeInstantiationUsingFactory() {
    // 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);
        // listener.waitForEvent(msp, "1");
        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);
    instance.dispose();
    // 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());
    }
    // 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)

Example 40 with PrimitiveInstanceDescription

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

the class TestArchitecture method testIntrospection.

/**
 * Checks the introspection possibilities before and after object creation.
 * Especially check the 'unvalued' case.
 */
@Test
public void testIntrospection() {
    Dictionary<String, String> configuration = new Hashtable<String, String>();
    configuration.put("p5", "v5i");
    configuration.put("p7", "v7i");
    configuration.put("p9", "v9i");
    ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.components.arch.MyComponentToIntrospect", configuration);
    // We don't get the service object until we finished the pre-instantiation tests.
    ServiceReference reference = osgiHelper.waitForService(CheckService.class.getName(), "(instance.name=" + instance.getInstanceName() + ")", 1000);
    assertNotNull(reference);
    PropertyDescription[] properties = ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getProperties();
    assertNotNull(properties);
    // Check the properties.
    assertEquals(getProperty(properties, "p1").getValue(), Property.UNVALUED);
    assertEquals(getProperty(properties, "p2").getValue(), Property.UNVALUED);
    // Default value
    assertEquals(getProperty(properties, "p3").getValue(), "v3");
    // Default value
    assertEquals(getProperty(properties, "p4").getValue(), "v4");
    // Instance value
    assertEquals(getProperty(properties, "p5").getValue(), "v5i");
    assertEquals(getProperty(properties, "p6").getValue(), Property.UNVALUED);
    assertEquals(getProperty(properties, "p6").getValue(), Property.UNVALUED);
    // Instance value
    assertEquals(getProperty(properties, "p7").getValue(), "v7i");
    assertEquals(getProperty(properties, "p8").getValue(), Property.UNVALUED);
    // Instance value
    assertEquals(getProperty(properties, "p9").getValue(), "v9i");
    // Check the propagation
    assertNull(reference.getProperty("p1"));
    assertNull(reference.getProperty("p2"));
    assertEquals(reference.getProperty("p3"), "v3");
    assertEquals(reference.getProperty("p4"), "v4");
    assertEquals(reference.getProperty("p5"), "v5i");
    assertNull(reference.getProperty("p6"));
    assertNull(reference.getProperty("p62"));
    assertEquals(reference.getProperty("p7"), "v7i");
    assertNull(reference.getProperty("p8"));
    assertEquals(reference.getProperty("p9"), "v9i");
    // Trigger instantiation
    assertTrue(((CheckService) context.getService(reference)).check());
    // Check new value.
    assertEquals(getProperty(properties, "p1").getValue(), "v1");
    assertEquals(getProperty(properties, "p2").getValue(), "v2");
    // Default value
    assertEquals(getProperty(properties, "p3").getValue(), "v3");
    // Default value
    assertEquals(getProperty(properties, "p4").getValue(), "v42");
    // Field value
    assertEquals(getProperty(properties, "p5").getValue(), "v52");
    // Specific value used for null
    assertEquals(getProperty(properties, "p6").getValue(), Property.UNVALUED);
    // Specific value used for null
    assertEquals(getProperty(properties, "p62").getValue(), "null");
    // Instance value
    assertEquals(getProperty(properties, "p7").getValue(), "v7i");
    assertEquals(getProperty(properties, "p8").getValue(), Property.UNVALUED);
    // Instance value
    assertEquals(getProperty(properties, "p9").getValue(), "v9i");
// New valued properties are not propagated.
// It avoids having fluctuation ins the service registrations
// To enable this propagation use @ServiceProperty
}
Also used : PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription) Hashtable(java.util.Hashtable) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)40 Test (org.junit.Test)36 ServiceReference (org.osgi.framework.ServiceReference)36 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)25 Architecture (org.apache.felix.ipojo.architecture.Architecture)20 Properties (java.util.Properties)18 IOException (java.io.IOException)16 Dictionary (java.util.Dictionary)16 Configuration (org.osgi.service.cm.Configuration)16 BaseTest (org.ow2.chameleon.testing.helpers.BaseTest)9 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)8 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)6 DependencyHandlerDescription (org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription)5 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)5 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)5 FooService (org.apache.felix.ipojo.runtime.core.test.services.FooService)5 PropertyDescription (org.apache.felix.ipojo.architecture.PropertyDescription)2 DependencyDescription (org.apache.felix.ipojo.handlers.dependency.DependencyDescription)2 ProvidedServiceDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription)2 CallbackCheckService (org.apache.felix.ipojo.runtime.core.services.CallbackCheckService)2