Search in sources :

Example 91 with CheckService

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

the class TestInjectingComponentAndInstanceContext method testSetterInjectionOfBothContext.

@Test
public void testSetterInjectionOfBothContext() {
    BundleContext bc = osgiHelper.getBundle("org.apache.felix.ipojo").getBundleContext();
    Properties configuration = new Properties();
    configuration.put("instance.bundle.context", bc);
    ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core" + ".components.annotations.ComponentWithTwoSetters", configuration);
    CheckService check = ipojoHelper.getServiceObjectByName(CheckService.class, instance.getInstanceName());
    assertNotNull(check);
    BundleContext context = (BundleContext) check.map().get("component");
    assertNotNull(context);
    assertEquals(getTestBundle().getSymbolicName(), context.getBundle().getSymbolicName());
    context = (BundleContext) check.map().get("instance");
    assertNotNull(context);
    assertEquals(bc, context);
}
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 92 with CheckService

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

the class TestInjectingContextAndProperties method testWhenPropertyIsLast.

@Test
public void testWhenPropertyIsLast() {
    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.MixWithProperties2", 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 93 with CheckService

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

the class TestCallbacks method testWithBoth.

@Test
public void testWithBoth() {
    ComponentInstance ci = ipojoHelper.createComponentInstance("PS-Callbacks-both");
    // Controller set to true.
    osgiHelper.waitForService(FooService.class.getName(), null, 5000);
    osgiHelper.waitForService(CheckService.class.getName(), null, 5000);
    CheckService check = (CheckService) osgiHelper.getServiceObject(CheckService.class.getName(), null);
    assertNotNull(check);
    Integer reg = (Integer) check.getProps().get("registered");
    Integer unreg = (Integer) check.getProps().get("unregistered");
    assertNotNull(reg);
    assertNotNull(unreg);
    assertEquals(new Integer(1), reg);
    assertEquals(new Integer(0), unreg);
    ci.stop();
    reg = (Integer) check.getProps().get("registered");
    unreg = (Integer) check.getProps().get("unregistered");
    assertNotNull(reg);
    assertNotNull(unreg);
    assertEquals(new Integer(1), reg);
    assertEquals(new Integer(1), unreg);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) Test(org.junit.Test)

Example 94 with CheckService

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

the class TestCallbacks method testWithPostRegistrationOnly.

@Test
public void testWithPostRegistrationOnly() {
    ComponentInstance ci = ipojoHelper.createComponentInstance("PS-Callbacks-reg-only");
    // Controller set to true.
    osgiHelper.waitForService(FooService.class.getName(), null, 5000);
    osgiHelper.waitForService(CheckService.class.getName(), null, 5000);
    CheckService check = (CheckService) osgiHelper.getServiceObject(CheckService.class.getName(), null);
    assertNotNull(check);
    Integer reg = (Integer) check.getProps().get("registered");
    Integer unreg = (Integer) check.getProps().get("unregistered");
    assertNotNull(reg);
    assertNotNull(unreg);
    assertEquals(new Integer(1), reg);
    assertEquals(new Integer(0), unreg);
    ci.stop();
    reg = (Integer) check.getProps().get("registered");
    unreg = (Integer) check.getProps().get("unregistered");
    assertNotNull(reg);
    assertNotNull(unreg);
    assertEquals(new Integer(1), reg);
    assertEquals(new Integer(0), unreg);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) Test(org.junit.Test)

Example 95 with CheckService

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

the class TestListeners method testProvidedServiceListener.

/**
 * Test that the listeners registered on the tested instance are called when the instance provided service is
 * registered, updated or unregistered.
 */
@Test
public void testProvidedServiceListener() {
    ComponentInstance ci = ipojoHelper.createComponentInstance("PS-Controller-1-default");
    ProvidedServiceHandlerDescription pshd = (ProvidedServiceHandlerDescription) ci.getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:provides");
    ProvidedServiceDescription ps = getPS(FooService.class.getName(), pshd.getProvidedServices());
    // Controller set to true.
    osgiHelper.waitForService(FooService.class.getName(), null, 5000);
    osgiHelper.waitForService(CheckService.class.getName(), null, 5000);
    CheckService check = (CheckService) osgiHelper.getServiceObject(CheckService.class.getName(), null);
    assertNotNull(check);
    // Register listeners :
    // 1- CountingListener l1
    // 2- ThrowingListener bad
    // 3- TotalCountingListener l2
    // 4- AppendingListener l3
    ProvidedServiceListener l1 = new CountingListener();
    ps.addListener(l1);
    ProvidedServiceListener bad = new ThrowingListener();
    ps.addListener(bad);
    ProvidedServiceListener l2 = new TotalCountingListener();
    ps.addListener(l2);
    ProvidedServiceListener l3 = new AppendingListener();
    ps.addListener(l3);
    // Check initial valued are untouched
    assertEquals(0, registrations);
    assertEquals(0, unregistrations);
    assertEquals(0, updates);
    assertEquals(0, total);
    // Unregister the service and check.
    assertFalse(check.check());
    assertEquals(0, registrations);
    assertEquals(1, unregistrations);
    assertEquals(0, updates);
    assertEquals(1, total);
    // Modify the service while it is unregistered. Nothing should move.
    Hashtable<String, Object> props = new Hashtable<String, Object>();
    props.put("change1", "1");
    ps.addProperties(props);
    assertEquals(0, registrations);
    assertEquals(1, unregistrations);
    assertEquals(0, updates);
    assertEquals(1, total);
    // Register the service and check.
    assertTrue(check.check());
    assertEquals(1, registrations);
    assertEquals(1, unregistrations);
    assertEquals(0, updates);
    assertEquals(2, total);
    // Modify the service while it is REGISTERED
    props.clear();
    props.put("change2", "2");
    ps.addProperties(props);
    assertEquals(1, registrations);
    assertEquals(1, unregistrations);
    assertEquals(1, updates);
    assertEquals(3, total);
    // One more time, just to be sure...
    // Unregister
    assertFalse(check.check());
    assertEquals(1, registrations);
    assertEquals(2, unregistrations);
    assertEquals(1, updates);
    assertEquals(4, total);
    // Register
    assertTrue(check.check());
    assertEquals(2, registrations);
    assertEquals(2, unregistrations);
    assertEquals(1, updates);
    assertEquals(5, total);
    // Unregister the listener
    ps.removeListener(l1);
    ps.removeListener(bad);
    ps.removeListener(l2);
    ps.removeListener(l3);
    // Play with the controller and check that nothing moves
    // Unregister
    assertFalse(check.check());
    assertEquals(2, registrations);
    assertEquals(2, unregistrations);
    assertEquals(1, updates);
    assertEquals(5, total);
    // Register
    assertTrue(check.check());
    assertEquals(2, registrations);
    assertEquals(2, unregistrations);
    assertEquals(1, updates);
    assertEquals(5, total);
    // Modify
    props.clear();
    // Modify
    props.put("change3", "3");
    // Modify
    ps.addProperties(props);
    assertEquals(2, registrations);
    assertEquals(2, unregistrations);
    assertEquals(1, updates);
    assertEquals(5, total);
    // Check that instances contains $total times the ci component instance, and nothing else.
    assertEquals(5, instances.size());
    instances.removeAll(Collections.singleton(ci));
    assertEquals(0, instances.size());
    ci.dispose();
}
Also used : ProvidedServiceListener(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceListener) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ProvidedServiceDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) 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