Map<BundleContext, Collection<ListenerInfo>> listeners = new HashMap<BundleContext, Collection<ListenerInfo>>();
listeners.put(client1BC, Collections.<ListenerInfo>emptyList());
// Send the event. It should have no effect because the service doens't match the filter
assertEquals("Precondition", 0, gpc.proxyMap.size());
geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref), listeners);
assertEquals("No proxy should have been created because the service doesn't match the filter", 0, gpc.proxyMap.size());
assertEquals("Nothing should have been removed from the listeners", 1, listeners.size());
long service2ID = 887L;
Dictionary<String, Object> props2 = new Hashtable<String, Object>();
props2.put(Constants.SERVICE_ID, service2ID);
props2.put("a", "b");
props2.put("foo", "bar");
ServiceReference<?> sref2 = mockServiceReference(props2);
ServiceEvent se2 = new ServiceEvent(ServiceEvent.REGISTERED, sref2);
// Send the event to the system bundle and the bundle that contains the hook, should have no effect
Map<BundleContext, Collection<ListenerInfo>> listeners2 = new Hashtable<BundleContext, Collection<ListenerInfo>>();
listeners2.put(frameworkBC, Collections.<ListenerInfo>emptyList());
listeners2.put(hookBC, Collections.<ListenerInfo>emptyList());
geh.event(se2, listeners2);
assertEquals("No proxies to be created for the hook bundle or the system bundle", 0, gpc.proxyMap.size());
assertEquals("Nothing should have been removed from the listeners", 2, listeners2.size());
// This is the first time that a proxy actually does get created
Map<BundleContext, Collection<ListenerInfo>> listeners3 = new HashMap<BundleContext, Collection<ListenerInfo>>();
listeners3.put(client1BC, Collections.<ListenerInfo>emptyList());
geh.event(se2, listeners3);
assertEquals("The service should be hidden from these listeners", 0, listeners3.size());
assertEquals("Proxy should have been created for this client", 1, gpc.proxyMap.size());
assertEquals(new Long(service2ID), gpc.proxyMap.keySet().iterator().next());
// Update the service, now an additional client is interested
props2.put("a", "c"); // Will change the properties of sref
Map<BundleContext, Collection<ListenerInfo>> listeners4 = new HashMap<BundleContext, Collection<ListenerInfo>>();
BundleContext client2BC = mockBundleContext(11);
listeners4.put(client2BC, Collections.<ListenerInfo>emptyList());
listeners4.put(client1BC, Collections.<ListenerInfo>emptyList());
geh.event(new ServiceEvent(ServiceEvent.MODIFIED, sref2), listeners4);
assertEquals("The service should be hidden from these listeners", 0, listeners4.size());
assertEquals("There should not be an additional proxy for client 2", 1, gpc.proxyMap.size());
assertNotNull(gpc.proxyMap.get(service2ID));
long service3ID = 1L;
Dictionary<String, Object> props3 = new Hashtable<String, Object>();
props3.put(Constants.SERVICE_ID, service3ID);
props3.put("foo", "bar");
ServiceReference<?> sref3 = mockServiceReference(props3);
// An event for a new service
Map<BundleContext, Collection<ListenerInfo>> listeners5 = new HashMap<BundleContext, Collection<ListenerInfo>>();
listeners5.put(client1BC, Collections.<ListenerInfo>emptyList());
listeners5.put(client1BC, Collections.<ListenerInfo>emptyList()); // Should be ignored
geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref3), listeners5);
assertEquals("There should be an additional procy for client1 to the new service", 2, gpc.proxyMap.size());
assertEquals("The service should be hidden from these listeners", 0, listeners5.size());
assertNotNull(gpc.proxyMap.get(service2ID));
assertNotNull(gpc.proxyMap.get(service3ID));
}