use of org.osgi.framework.hooks.service.EventHook in project felix by apache.
the class EventDispatcherTest method testFireServiceEvent.
public void testFireServiceEvent() {
final Bundle b1 = getMockBundle();
final Bundle b2 = getMockBundle();
final Bundle b3 = getMockBundle();
final Bundle b4 = getMockBundle();
final Set calledHooks = new HashSet();
final EventHook eh1 = new EventHook() {
public void event(ServiceEvent event, Collection contexts) {
calledHooks.add(this);
}
};
final EventHook eh2 = new EventHook() {
public void event(ServiceEvent event, Collection contexts) {
calledHooks.add(this);
for (Iterator it = contexts.iterator(); it.hasNext(); ) {
BundleContext bc = (BundleContext) it.next();
if (bc.getBundle() == b1) {
it.remove();
}
if (bc.getBundle() == b2) {
it.remove();
}
}
}
};
Logger logger = new Logger();
ServiceRegistry registry = new ServiceRegistry(logger, null);
registry.registerService(b4, new String[] { EventHook.class.getName() }, eh1, new Hashtable());
registry.registerService(b4, new String[] { EventHook.class.getName() }, eh2, new Hashtable());
// -- Set up event dispatcher
EventDispatcher ed = new EventDispatcher(logger, registry);
// -- Register some listeners
final List fired = Collections.synchronizedList(new ArrayList());
ServiceListener sl1 = new ServiceListener() {
public void serviceChanged(ServiceEvent arg0) {
fired.add(this);
}
};
ed.addListener(b1.getBundleContext(), ServiceListener.class, sl1, null);
ServiceListener sl2 = new ServiceListener() {
public void serviceChanged(ServiceEvent arg0) {
fired.add(this);
}
};
ed.addListener(b2.getBundleContext(), ServiceListener.class, sl2, null);
ServiceListener sl3 = new ServiceListener() {
public void serviceChanged(ServiceEvent arg0) {
fired.add(this);
}
};
ed.addListener(b3.getBundleContext(), ServiceListener.class, sl3, null);
// --- make the invocation
ServiceReference sr = EasyMock.createNiceMock(ServiceReference.class);
EasyMock.expect(sr.getProperty(Constants.OBJECTCLASS)).andReturn(new String[] { "java.lang.String" }).anyTimes();
sr.isAssignableTo(b1, String.class.getName());
EasyMock.expectLastCall().andReturn(Boolean.TRUE).anyTimes();
sr.isAssignableTo(b2, String.class.getName());
EasyMock.expectLastCall().andReturn(Boolean.TRUE).anyTimes();
sr.isAssignableTo(b3, String.class.getName());
EasyMock.expectLastCall().andReturn(Boolean.TRUE).anyTimes();
EasyMock.replay(new Object[] { sr });
ServiceEvent event = new ServiceEvent(ServiceEvent.REGISTERED, sr);
assertEquals("Precondition failed", 0, fired.size());
Felix framework = new Felix(new HashMap());
ed.fireServiceEvent(event, null, framework);
assertEquals(1, fired.size());
assertSame(sl3, fired.iterator().next());
assertEquals(2, calledHooks.size());
assertTrue(calledHooks.contains(eh1));
assertTrue(calledHooks.contains(eh2));
}
use of org.osgi.framework.hooks.service.EventHook in project felix by apache.
the class ServiceRegistryTest method testRegisterEventHookService.
public void testRegisterEventHookService() {
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);
EventHook hook = new EventHook() {
@Override
public void event(ServiceEvent event, Collection contexts) {
}
};
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[] { EventHook.class.getName() }, hook, new Hashtable());
assertEquals(1, sr.getHookRegistry().getHooks(EventHook.class).size());
assertTrue(sr.getHookRegistry().getHooks(EventHook.class).iterator().next() instanceof ServiceReference);
assertSame(reg.getReference(), sr.getHookRegistry().getHooks(EventHook.class).iterator().next());
assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.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());
}
use of org.osgi.framework.hooks.service.EventHook 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());
}
Aggregations