Search in sources :

Example 1 with ListenerHook

use of org.osgi.framework.hooks.service.ListenerHook in project felix by apache.

the class ServiceRegistryTest method testRegisterCombinedService.

public void testRegisterCombinedService() {
    MockControl control = MockControl.createNiceControl(Bundle.class);
    Bundle b = (Bundle) control.getMock();
    control.replay();
    MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
    BundleContext c = (BundleContext) controlContext.getMock();
    controlContext.expectAndReturn(c.getBundle(), b);
    controlContext.replay();
    ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
    class CombinedService implements ListenerHook, FindHook, EventHook, Runnable {

        @Override
        public void added(Collection listeners) {
        }

        @Override
        public void removed(Collection listener) {
        }

        @Override
        public void find(BundleContext context, String name, String filter, boolean allServices, Collection references) {
        }

        @Override
        public void event(ServiceEvent event, Collection contexts) {
        }

        @Override
        public void run() {
        }
    }
    CombinedService hook = new CombinedService();
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { Runnable.class.getName(), ListenerHook.class.getName(), FindHook.class.getName(), EventHook.class.getName() }, hook, new Hashtable());
    assertEquals(1, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(ListenerHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals(1, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(EventHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals(1, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(FindHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    sr.unregisterService(b, reg);
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
Also used : FindHook(org.osgi.framework.hooks.service.FindHook) Bundle(org.osgi.framework.Bundle) ListenerHook(org.osgi.framework.hooks.service.ListenerHook) Hashtable(java.util.Hashtable) EventHook(org.osgi.framework.hooks.service.EventHook) MockControl(org.easymock.MockControl) ServiceEvent(org.osgi.framework.ServiceEvent) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with ListenerHook

use of org.osgi.framework.hooks.service.ListenerHook in project felix by apache.

the class ServiceRegistryTest method testRegisterListenerHookService.

public void testRegisterListenerHookService() {
    MockControl control = MockControl.createNiceControl(Bundle.class);
    Bundle b = (Bundle) control.getMock();
    control.replay();
    MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
    BundleContext c = (BundleContext) controlContext.getMock();
    controlContext.expectAndReturn(c.getBundle(), b);
    controlContext.replay();
    ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
    ListenerHook hook = new ListenerHook() {

        @Override
        public void added(Collection listeners) {
        }

        @Override
        public void removed(Collection listener) {
        }
    };
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { ListenerHook.class.getName() }, hook, new Hashtable());
    assertEquals(1, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(ListenerHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    sr.unregisterService(b, reg);
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
Also used : MockControl(org.easymock.MockControl) Bundle(org.osgi.framework.Bundle) ListenerHook(org.osgi.framework.hooks.service.ListenerHook) Hashtable(java.util.Hashtable) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

Collection (java.util.Collection)2 Hashtable (java.util.Hashtable)2 MockControl (org.easymock.MockControl)2 Bundle (org.osgi.framework.Bundle)2 BundleContext (org.osgi.framework.BundleContext)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 ListenerHook (org.osgi.framework.hooks.service.ListenerHook)2 ServiceEvent (org.osgi.framework.ServiceEvent)1 EventHook (org.osgi.framework.hooks.service.EventHook)1 FindHook (org.osgi.framework.hooks.service.FindHook)1