Search in sources :

Example 16 with PrimitiveInstanceDescription

use of org.apache.felix.ipojo.PrimitiveInstanceDescription 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)

Example 17 with PrimitiveInstanceDescription

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

the class TestGetComponentInstance method testGetComponentInstance.

/**
 * Check if a pojo can correctly be cast in POJO.
 * Check the getComponentInstance method.
 */
@Test
public void testGetComponentInstance() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException {
    String factName = "Manipulation-FooProviderType-1";
    String compName = "FooProvider-1";
    ServiceReference ref = null;
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider-1", fact);
    Properties props = new Properties();
    props.put("instance.name", compName);
    ComponentInstance ci = null;
    try {
        ci = fact.createComponentInstance(props);
    } catch (Exception e1) {
        fail(e1.getMessage());
    }
    assertEquals("Check instance name", compName, ci.getInstanceName());
    // Get a FooService provider
    ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), compName);
    assertNotNull("FS not available", ref);
    // Get foo object
    FooService fs = (FooService) osgiHelper.getServiceObject(ref);
    // Cast to POJO
    Pojo pojo = (Pojo) fs;
    Field im = fs.getClass().getDeclaredField("__IM");
    assertNotNull(im);
    im.setAccessible(true);
    assertNotNull(im.get(fs));
    Method method = fs.getClass().getMethod("getComponentInstance");
    assertNotNull(method);
    // GetComponentInstance
    ComponentInstance instance = pojo.getComponentInstance();
    assertNotNull(instance);
    assertEquals("Check component instance name", instance.getInstanceName(), compName);
    assertEquals("Check component factory name", instance.getFactory().getName(), factName);
    assertNotNull("Instance description not null", instance.getInstanceDescription());
    PrimitiveInstanceDescription id = (PrimitiveInstanceDescription) instance.getInstanceDescription();
    assertTrue("Check instance state", id.getState() == ComponentInstance.VALID);
    assertEquals("Check created pojo count", id.getCreatedObjects().length, 1);
    assertEquals("Check instance description name", id.getName(), compName);
    ci.dispose();
    // Check that there is no more FooService
    ref = osgiHelper.getServiceReference(FooService.class.getName());
    assertNull("FS available, but component instance stopped", ref);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Field(java.lang.reflect.Field) Pojo(org.apache.felix.ipojo.Pojo) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Method(java.lang.reflect.Method) Properties(java.util.Properties) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) BaseTest(org.ow2.chameleon.testing.helpers.BaseTest)

Example 18 with PrimitiveInstanceDescription

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

the class TestPOJOCreation method testImmediateCreation.

/**
 * Check immediate creation.
 */
@Test
public void testImmediateCreation() {
    assertEquals("Check that one object is created ", 1, ((PrimitiveInstanceDescription) immeArch.getInstanceDescription()).getCreatedObjects().length);
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), ci_immediate.getInstanceName());
    assertNotNull("Check that a FooService from " + ci_immediate.getInstanceName() + " is available", ref);
    FooService fs = (FooService) osgiHelper.getServiceObject(ref);
    assertTrue("Check the FooService invocation", fs.foo());
    assertEquals("Check the creation of 1 object", 1, ((PrimitiveInstanceDescription) immeArch.getInstanceDescription()).getCreatedObjects().length);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) BaseTest(org.ow2.chameleon.testing.helpers.BaseTest)

Example 19 with PrimitiveInstanceDescription

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

the class TestPOJOCreation method testLazyCreationSingletonM.

/**
 * Check creation through a factory method.
 * (lazy & singleton creation)
 */
@Test
public void testLazyCreationSingletonM() {
    assertEquals("Check that no objects are created ", 0, ((PrimitiveInstanceDescription) lazzyArchSingM.getInstanceDescription()).getCreatedObjects().length);
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), ci_lazzy_singM.getInstanceName());
    assertNotNull("Check that a FooService from " + ci_lazzy_singM.getInstanceName() + " is available", ref);
    FooService fs = (FooService) osgiHelper.getServiceObject(ref);
    FooService fs2 = (FooService) osgiHelper.getServiceObject(ref);
    assertTrue("Check the FooService invocation", fs.foo());
    assertTrue("Check the FooService invocation", fs2.foo());
    assertEquals("Check the creation of 1 object", 1, ((PrimitiveInstanceDescription) lazzyArchSingM.getInstanceDescription()).getCreatedObjects().length);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) BaseTest(org.ow2.chameleon.testing.helpers.BaseTest)

Example 20 with PrimitiveInstanceDescription

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

the class TestPOJOCreation method testLazyCreationSeveral.

/**
 * Check lazy and "several" creation.
 */
@Test
public void testLazyCreationSeveral() {
    assertEquals("Check that no objects are created ", 0, ((PrimitiveInstanceDescription) lazzyArchSev.getInstanceDescription()).getCreatedObjects().length);
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), ci_lazzy_sev.getInstanceName());
    assertNotNull("Check that a FooService from " + ci_lazzy_sev.getInstanceName() + " is available", ref);
    FooService fs = (FooService) osgiHelper.getServiceObject(ref);
    FooService fs2 = (FooService) osgiHelper.getServiceObject(ref);
    assertTrue("Check the FooService invocation", fs.foo());
    assertTrue("Check the FooService invocation-2", fs2.foo());
    assertEquals("Check the creation of 1 object", 1, ((PrimitiveInstanceDescription) lazzyArchSev.getInstanceDescription()).getCreatedObjects().length);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) BaseTest(org.ow2.chameleon.testing.helpers.BaseTest)

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