Search in sources :

Example 1 with ServiceRegistry

use of org.apache.felix.ipojo.context.ServiceRegistry in project felix by apache.

the class ServiceRegistryTest method testGetService.

@Test
public void testGetService() {
    ComponentInstance im = new FakeComponent();
    ComponentInstance im2 = new FakeComponent();
    ServiceRegistry registry = new ServiceRegistry(getContext());
    try {
        assertNull("Check that there is no available service", registry.getServiceReferences(null, null));
    } catch (InvalidSyntaxException e) {
        fail("Cannot query the registry : " + e.getMessage());
    }
    Properties props = new Properties();
    props.put("foo", "bar");
    ServiceRegistration reg1 = registry.registerService(im, BarService.class.getName(), new barProvider(), props);
    ServiceReference ref = registry.getServiceReference(BarService.class.getName());
    assertNotNull("Check ref not null", ref);
    assertEquals("Test property", ref.getProperty("foo"), "bar");
    BarService bar = (BarService) registry.getService(im2, ref);
    assertTrue("Test invocation", bar.bar());
    reg1.unregister();
    ref = registry.getServiceReference(BarService.class.getName());
    assertNull("Check ref null", ref);
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) ServiceRegistry(org.apache.felix.ipojo.context.ServiceRegistry) Properties(java.util.Properties) BarService(org.apache.felix.ipojo.runtime.core.services.BarService) Test(org.junit.Test)

Example 2 with ServiceRegistry

use of org.apache.felix.ipojo.context.ServiceRegistry in project felix by apache.

the class ServiceRegistryTest method testRegistrationAndListener.

@Test
public void testRegistrationAndListener() {
    ComponentInstance im = new FakeComponent();
    ComponentInstance im2 = new FakeComponent();
    ServiceRegistry registry = new ServiceRegistry(getContext());
    assertNotNull("Assert registry not null", registry);
    svcListener all = new svcListener();
    try {
        assertNull("Check that there is no available service", registry.getServiceReferences(null, null));
    } catch (InvalidSyntaxException e) {
        fail("Cannot query the registry : " + e.getMessage());
    }
    registry.addServiceListener(all);
    ServiceRegistration reg1 = registry.registerService(im, BarService.class.getName(), new barProvider(), null);
    try {
        assertEquals("Check number of registred service", 1, registry.getServiceReferences(null, null).length);
    } catch (InvalidSyntaxException e) {
        fail("Cannot query the registry : " + e.getMessage());
    }
    ServiceRegistration reg2 = registry.registerService(im2, BarService.class.getName(), new barProvider(), null);
    try {
        assertEquals("Check number of registred service", 2, registry.getServiceReferences(null, null).length);
    } catch (InvalidSyntaxException e) {
        fail("Cannot query the registry : " + e.getMessage());
    }
    assertEquals("Check the number of registration", 2, all.registration);
    Dictionary<String, String> props = new Hashtable<String, String>();
    props.put("foo", "bar");
    reg1.setProperties(props);
    assertEquals("Check the number of modification", 1, all.modification);
    reg1.unregister();
    assertEquals("Check the number of unregistration", 1, all.unregistration);
    reg2.setProperties(props);
    assertEquals("Check the number of modification", 2, all.modification);
    reg2.unregister();
    assertEquals("Check the number of unregistration", 2, all.unregistration);
    registry.removeServiceListener(all);
}
Also used : Hashtable(java.util.Hashtable) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) ServiceRegistry(org.apache.felix.ipojo.context.ServiceRegistry) BarService(org.apache.felix.ipojo.runtime.core.services.BarService) Test(org.junit.Test)

Example 3 with ServiceRegistry

use of org.apache.felix.ipojo.context.ServiceRegistry in project felix by apache.

the class ServiceRegistryTest method testGetFilteredService.

@Test
public void testGetFilteredService() {
    ComponentInstance im = new FakeComponent();
    ComponentInstance im2 = new FakeComponent();
    ServiceRegistry registry = new ServiceRegistry(getContext());
    try {
        assertNull("Check that there is no available service", registry.getServiceReferences(null, null));
    } catch (InvalidSyntaxException e) {
        fail("Cannot query the registry : " + e.getMessage());
    }
    Properties props = new Properties();
    props.put("foo", "bar");
    ServiceRegistration reg1 = registry.registerService(im, BarService.class.getName(), new barProvider(), props);
    ServiceRegistration reg2 = registry.registerService(im2, BarService.class.getName(), new barProvider(), null);
    ServiceReference[] ref = null;
    try {
        ref = registry.getServiceReferences(BarService.class.getName(), "(foo=bar)");
    } catch (InvalidSyntaxException e) {
        fail("Registry query fail : " + e.getMessage());
    }
    assertNotNull("Check ref not null", ref);
    assertEquals("Check ref count", ref.length, 1);
    assertEquals("Test property", ref[0].getProperty("foo"), "bar");
    BarService bar = (BarService) registry.getService(im2, ref[0]);
    assertTrue("Test invocation", bar.bar());
    ref = null;
    reg1.unregister();
    try {
        ref = registry.getServiceReferences(BarService.class.getName(), "(bar=foo)");
    } catch (InvalidSyntaxException e) {
        fail("Registry query fail : " + e.getMessage());
    }
    assertNull("Check ref null", ref);
    reg2.unregister();
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) ServiceRegistry(org.apache.felix.ipojo.context.ServiceRegistry) Properties(java.util.Properties) BarService(org.apache.felix.ipojo.runtime.core.services.BarService) Test(org.junit.Test)

Example 4 with ServiceRegistry

use of org.apache.felix.ipojo.context.ServiceRegistry in project felix by apache.

the class ServiceRegistryTest method testRegistrationAndFilter.

@Test
public void testRegistrationAndFilter() {
    ComponentInstance im = new FakeComponent();
    ComponentInstance im2 = new FakeComponent();
    ServiceRegistry registry = new ServiceRegistry(getContext());
    svcListener filtered = new svcListener();
    try {
        assertNull("Check that there is no available service", registry.getServiceReferences(null, null));
    } catch (InvalidSyntaxException e) {
        fail("Cannot query the registry : " + e.getMessage());
    }
    registry.addServiceListener(filtered, "(foo=bar)");
    Dictionary<String, String> props = new Hashtable<String, String>();
    props.put("foo", "bar");
    ServiceRegistration reg1 = registry.registerService(im, BarService.class.getName(), new barProvider(), props);
    try {
        assertEquals("Check number of registred service", 1, registry.getServiceReferences(null, null).length);
    } catch (InvalidSyntaxException e) {
        fail("Cannot query the registry : " + e.getMessage());
    }
    ServiceRegistration reg2 = registry.registerService(im2, BarService.class.getName(), new barProvider(), null);
    try {
        assertEquals("Check number of registred service", 2, registry.getServiceReferences(null, null).length);
    } catch (InvalidSyntaxException e) {
        fail("Cannot query the registry : " + e.getMessage());
    }
    assertEquals("Check the number of registration", 1, filtered.registration);
    reg2.setProperties(props);
    assertEquals("Check the number of modification", 1, filtered.modification);
    // Follow the OSGi semantics of filters
    reg1.unregister();
    reg2.unregister();
    assertEquals("Check the number of unregistration", 2, filtered.unregistration);
    registry.removeServiceListener(filtered);
}
Also used : Hashtable(java.util.Hashtable) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) ServiceRegistry(org.apache.felix.ipojo.context.ServiceRegistry) BarService(org.apache.felix.ipojo.runtime.core.services.BarService) Test(org.junit.Test)

Aggregations

ComponentInstance (org.apache.felix.ipojo.ComponentInstance)4 ServiceRegistry (org.apache.felix.ipojo.context.ServiceRegistry)4 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)4 Test (org.junit.Test)4 Hashtable (java.util.Hashtable)2 Properties (java.util.Properties)2