Search in sources :

Example 36 with CheckService

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

the class TestInjectingContextAndProperties method testWhenPropertyIsFirst.

@Test
public void testWhenPropertyIsFirst() {
    BundleContext bc = osgiHelper.getBundle("org.apache.felix.ipojo").getBundleContext();
    Properties configuration = new Properties();
    configuration.put("instance.bundle.context", bc);
    configuration.put("message", "hello");
    ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core" + ".components.mix.MixWithProperties1", configuration);
    CheckService check = ipojoHelper.getServiceObjectByName(CheckService.class, instance.getInstanceName());
    assertNotNull(check);
    BundleContext context = (BundleContext) check.map().get("context");
    assertNotNull(context);
    assertEquals(getTestBundle().getSymbolicName(), context.getBundle().getSymbolicName());
    assertEquals("hello", check.map().get("message"));
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 37 with CheckService

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

the class TestImmediateCallbackSeveralFactories method testCallback.

@Test
public void testCallback() {
    // Check instance is invalid
    ServiceReference arch_ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), instance.getInstanceName());
    assertNotNull("Check architecture availability", arch_ref);
    PrimitiveInstanceDescription id_dep = (PrimitiveInstanceDescription) ((Architecture) osgiHelper.getRawServiceObject(arch_ref)).getInstanceDescription();
    assertTrue("Check instance invalidity - 1", id_dep.getState() == ComponentInstance.INVALID);
    assertEquals("Check pojo count - 1", id_dep.getCreatedObjects().length, 0);
    // Start fooprovider
    fooProvider.start();
    // Check instance validity
    // id_dep = ((Architecture) getContext().getService(arch_ref)).getInstanceDescription();
    assertTrue("Check instance validity - 1", id_dep.getState() == ComponentInstance.VALID);
    // Check service providing
    ServiceReference cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    CheckService cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
    assertTrue("check CheckService invocation", cs.check());
    assertEquals("Check pojo count - 2", id_dep.getCreatedObjects().length, 1);
    // Check int property
    Integer index = (Integer) (cs.getProps().get("int"));
    Integer count = (Integer) (cs.getProps().get("count"));
    assertEquals("Check int property - 1 (" + index + ")", index.intValue(), 1);
    assertEquals("Check count property - 1 (" + count + ")", count.intValue(), 1);
    fooProvider.stop();
    // id_dep = ((Architecture) getContext().getService(arch_ref)).getInstanceDescription();
    assertTrue("Check instance invalidity - 2", id_dep.getState() == ComponentInstance.INVALID);
    assertEquals("Check pojo count - 3", id_dep.getCreatedObjects().length, 1);
    fooProvider.start();
    // Check instance validity
    // id_dep = ((Architecture) getContext().getService(arch_ref)).getInstanceDescription();
    assertTrue("Check instance validity - 2", id_dep.getState() == ComponentInstance.VALID);
    // Check service providing
    cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
    assertTrue("check CheckService invocation", cs.check());
    // Check int property
    index = (Integer) (cs.getProps().get("int"));
    count = (Integer) (cs.getProps().get("count"));
    assertEquals("Check int property - 2 (" + index + ")", index.intValue(), 3);
    assertEquals("Check count property - 2 (" + count + ")", count.intValue(), 1);
    assertEquals("Check pojo count - 4 ", id_dep.getCreatedObjects().length, 1);
}
Also used : Architecture(org.apache.felix.ipojo.architecture.Architecture) CallbackCheckService(org.apache.felix.ipojo.runtime.core.services.CallbackCheckService) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 38 with CheckService

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

the class TestParentCallback method testCallback.

@Test
public void testCallback() {
    // Check instance is invalid
    ServiceReference arch_ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), instance.getInstanceName());
    assertNotNull("Check architecture availability", arch_ref);
    PrimitiveInstanceDescription id_dep = (PrimitiveInstanceDescription) ((Architecture) osgiHelper.getRawServiceObject(arch_ref)).getInstanceDescription();
    assertTrue("Check instance invalidity - 1", id_dep.getState() == ComponentInstance.INVALID);
    // Start fooprovider
    fooProvider.start();
    // Check instance validity
    // id_dep = ((Architecture) osgiHelper.getServiceObject(arch_ref)).getInstanceDescription();
    assertTrue("Check instance validity - 1", id_dep.getState() == ComponentInstance.VALID);
    // Check service providing
    ServiceReference cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    // Check int property
    assertEquals("Check pojo count - 2", id_dep.getCreatedObjects().length, 1);
    fooProvider.stop();
    // id_dep = ((Architecture) osgiHelper.getServiceObject(arch_ref)).getInstanceDescription();
    assertTrue("Check instance invalidity - 2", id_dep.getState() == ComponentInstance.INVALID);
    fooProvider.start();
    // Check instance validity
    // id_dep = ((Architecture) osgiHelper.getServiceObject(arch_ref)).getInstanceDescription();
    assertTrue("Check instance validity - 2", id_dep.getState() == ComponentInstance.VALID);
    // Check service providing
    cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    CheckService cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
    assertTrue("check CheckService invocation", cs.check());
    // Clean up
    bc.ungetService(arch_ref);
    bc.ungetService(cs_ref);
}
Also used : Architecture(org.apache.felix.ipojo.architecture.Architecture) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 39 with CheckService

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

the class TestConfigurableLifeCycleController method testInValidThenValid.

@Test
public void testInValidThenValid() throws Exception {
    Properties props = new Properties();
    props.put("instance.name", "under1");
    props.put("state", "false");
    under = factory.createComponentInstance(props);
    // The instance should now be invalid
    assertFalse("Check service availability -2", ipojoHelper.isServiceAvailableByName(CheckService.class.getName(), "under1"));
    // Reconfigure the instance
    // Bar is a bad conf
    props.put("state", "true");
    try {
        factory.reconfigure(props);
    } catch (Exception e) {
        fail("The reconfiguration is not unacceptable and seems unacceptable : " + props);
    }
    // Object[] objects = Utils.getServiceObjects(context, Architecture.class.getName(), null);
    // for (int i = 0; i < objects.length; i++) {
    // Architecture a = (Architecture) objects[i];
    // System.out.println(a.getInstanceDescription().getDescription());
    // }
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), "under1");
    assertNotNull("Check service availability -1", ref);
    CheckService cs = (CheckService) osgiHelper.getRawServiceObject(ref);
    assertTrue("Check state 1", cs.check());
    bc.ungetService(ref);
    // Reconfigure the instance
    // Bar is a bad conf
    props.put("state", "false");
    try {
        factory.reconfigure(props);
    } catch (Exception e) {
        fail("The reconfiguration is not unacceptable and seems unacceptable : " + props);
    }
    // The instance should now be invalid
    assertFalse("Check service availability -2", ipojoHelper.isServiceAvailableByName(CheckService.class.getName(), "under1"));
    // Reconfigure the instance with a valid configuration
    props.put("state", "true");
    try {
        factory.reconfigure(props);
    } catch (Exception e) {
        fail("The reconfiguration is not unacceptable and seems unacceptable (2) : " + props);
    }
    ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), "under1");
    assertNotNull("Check service availability -3", ref);
    cs = (CheckService) osgiHelper.getRawServiceObject(ref);
    assertTrue("Check state 2", cs.check());
    bc.ungetService(ref);
    under.dispose();
}
Also used : Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 40 with CheckService

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

the class TestConfigurableLifeCycleController method testValidThenInvalid.

@Test
public void testValidThenInvalid() throws UnacceptableConfiguration, MissingHandlerException, ConfigurationException {
    Properties props = new Properties();
    props.put("instance.name", "under1");
    props.put("state", "true");
    under = factory.createComponentInstance(props);
    // The conf is correct, the PS must be provided
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), "under1");
    assertNotNull("Check service availability -1", ref);
    CheckService cs = (CheckService) osgiHelper.getRawServiceObject(ref);
    assertTrue("Check state 1", cs.check());
    bc.ungetService(ref);
    // Reconfigure the instance
    // Bar is a bad conf
    props.put("state", "false");
    try {
        factory.reconfigure(props);
    } catch (Exception e) {
        fail("The reconfiguration is not unacceptable and seems unacceptable : " + props);
    }
    // The instance should now be invalid
    assertFalse("Check service availability -2", ipojoHelper.isServiceAvailableByName(CheckService.class.getName(), "under1"));
    // Reconfigure the instance with a valid configuration
    props.put("state", "true");
    try {
        factory.reconfigure(props);
    } catch (Exception e) {
        fail("The reconfiguration is not unacceptable and seems unacceptable (2) : " + props);
    }
    ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), "under1");
    assertNotNull("Check service availability -3", ref);
    cs = (CheckService) osgiHelper.getRawServiceObject(ref);
    assertTrue("Check state 2", cs.check());
    bc.ungetService(ref);
    under.dispose();
}
Also used : Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)124 Test (org.junit.Test)122 ServiceReference (org.osgi.framework.ServiceReference)90 Properties (java.util.Properties)87 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)50 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)25 BundleContext (org.osgi.framework.BundleContext)20 Factory (org.apache.felix.ipojo.Factory)11 ServiceContext (org.apache.felix.ipojo.ServiceContext)10 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)6 Architecture (org.apache.felix.ipojo.architecture.Architecture)6 Hashtable (java.util.Hashtable)5 BaseTest (org.ow2.chameleon.testing.helpers.BaseTest)4 ProvidedServiceDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription)2 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)2 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)2 CallbackCheckService (org.apache.felix.ipojo.runtime.core.services.CallbackCheckService)2 Before (org.junit.Before)2 List (java.util.List)1