LocalDiscovery implements BundleListener { @Reference void bindListener(ServiceReference<EndpointEventListener> endpointListenerRef, EndpointEventListener endpointListener) { List<String> filters = StringPlus.normalize(endpointListenerRef.getProperty(EndpointEventListener.ENDPOINT_LISTENER_SCOPE)); if (filters.isEmpty()) { return; } synchronized (listenerToFilters) { listenerToFilters.put(endpointListener, filters); for (String filter : filters) { Collection<EndpointEventListener> listeners = filterToListeners.computeIfAbsent(filter, k -> new ArrayList<>()); listeners.add(endpointListener); } } triggerCallbacks(filters, endpointListener); } LocalDiscovery(); @Activate void activate(BundleContext context); @Deactivate void deactivate(BundleContext context); void processExistingBundles(Bundle[] bundles); void bundleChanged(BundleEvent be);  }
@Test public void testRegisterTracker() throws Exception { LocalDiscovery ld = new LocalDiscovery(); final Map<String, Object> props = new Hashtable<>(); props.put(EndpointEventListener.ENDPOINT_LISTENER_SCOPE, "(objectClass=Aaaa)"); @SuppressWarnings("unchecked") ServiceReference<EndpointEventListener> sr = EasyMock.createMock(ServiceReference.class); EasyMock.expect(sr.getPropertyKeys()).andReturn(props.keySet().toArray(new String[] {})).anyTimes(); EasyMock.expect(sr.getProperty(EasyMock.anyObject())).andAnswer(new IAnswer<Object>() { public Object answer() throws Throwable { return props.get(EasyMock.getCurrentArguments()[0]); } }).anyTimes(); EasyMock.replay(sr); EndpointEventListener endpointListener = EasyMock.createMock(EndpointEventListener.class); EasyMock.replay(endpointListener); assertEquals("Precondition failed", 0, ld.listenerToFilters.size()); assertEquals("Precondition failed", 0, ld.filterToListeners.size()); ld.bindListener(sr, endpointListener); assertEquals(1, ld.listenerToFilters.size()); assertEquals(Collections.singletonList("(objectClass=Aaaa)"), ld.listenerToFilters.get(endpointListener)); assertEquals(1, ld.filterToListeners.size()); assertEquals(Collections.singletonList(endpointListener), ld.filterToListeners.get("(objectClass=Aaaa)")); @SuppressWarnings("unchecked") ServiceReference<EndpointEventListener> sr2 = EasyMock.createMock(ServiceReference.class); EasyMock.expect(sr2.getPropertyKeys()).andReturn(props.keySet().toArray(new String[] {})).anyTimes(); EasyMock.expect(sr2.getProperty(EasyMock.anyObject())).andAnswer(new IAnswer<Object>() { public Object answer() throws Throwable { return props.get(EasyMock.getCurrentArguments()[0]); } }).anyTimes(); EasyMock.replay(sr2); EndpointEventListener endpointListener2 = EasyMock.createMock(EndpointEventListener.class); EasyMock.replay(endpointListener2); ld.bindListener(sr2, endpointListener2); assertEquals(2, ld.listenerToFilters.size()); assertEquals(Collections.singletonList("(objectClass=Aaaa)"), ld.listenerToFilters.get(endpointListener)); assertEquals(Collections.singletonList("(objectClass=Aaaa)"), ld.listenerToFilters.get(endpointListener2)); assertEquals(1, ld.filterToListeners.size()); List<EndpointEventListener> endpointListeners12 = Arrays.asList(endpointListener, endpointListener2); assertEquals(endpointListeners12, ld.filterToListeners.get("(objectClass=Aaaa)")); final Map<String, Object> props2 = new Hashtable<>(); props2.put(EndpointEventListener.ENDPOINT_LISTENER_SCOPE, Arrays.asList("(objectClass=X)", "(objectClass=Y)")); @SuppressWarnings("unchecked") ServiceReference<EndpointEventListener> sr3 = EasyMock.createMock(ServiceReference.class); EasyMock.expect(sr3.getPropertyKeys()).andReturn(props2.keySet().toArray(new String[] {})).anyTimes(); EasyMock.expect(sr3.getProperty(EasyMock.anyObject())).andAnswer(new IAnswer<Object>() { public Object answer() throws Throwable { return props2.get(EasyMock.getCurrentArguments()[0]); } }).anyTimes(); EasyMock.replay(sr3); EndpointEventListener endpointListener3 = EasyMock.createMock(EndpointEventListener.class); EasyMock.replay(endpointListener3); ld.bindListener(sr3, endpointListener3); assertEquals(3, ld.listenerToFilters.size()); assertEquals(Collections.singletonList("(objectClass=Aaaa)"), ld.listenerToFilters.get(endpointListener)); assertEquals(Collections.singletonList("(objectClass=Aaaa)"), ld.listenerToFilters.get(endpointListener2)); assertEquals(Arrays.asList("(objectClass=X)", "(objectClass=Y)"), ld.listenerToFilters.get(endpointListener3)); assertEquals(3, ld.filterToListeners.size()); assertEquals(endpointListeners12, ld.filterToListeners.get("(objectClass=Aaaa)")); assertEquals(Collections.singletonList(endpointListener3), ld.filterToListeners.get("(objectClass=X)")); assertEquals(Collections.singletonList(endpointListener3), ld.filterToListeners.get("(objectClass=Y)")); }