OsgiServiceCollection implements Collection<E> { public void stopTracking() { bundleContext.removeServiceListener(listener); synchronized (lock) { for (E service : services) { listener.serviceChanged( new ServiceEvent(ServiceEvent.UNREGISTERING, ((OsgiProxy) service).getReference())); } } } OsgiServiceCollection(BundleContext bundleContext, String filter, ClassLoader fallbackClassLoader,
             OsgiProxyCreator proxyCreator);  OsgiServiceCollection(BundleContext bundleContext, String filter, ClassLoader fallbackClassLoader,
             OsgiProxyCreator proxyCreator, DynamicCollection<E> backed); void startTracking(); void stopTracking(); @Override Iterator<E> iterator(); @Override int size(); @Override String toString(); @Override boolean remove(Object o); @Override boolean removeAll(Collection c); @Override boolean add(Object o); @Override boolean addAll(Collection c); @Override void clear(); @Override boolean retainAll(Collection c); @Override boolean contains(Object o); @Override boolean containsAll(Collection c); @Override boolean isEmpty(); @Override Object[] toArray(); @SuppressWarnings("SuspiciousToArrayCall") @Override T[] toArray(T[] array);  }
@Test @SuppressWarnings("unchecked") public void testStopTracking() throws Exception { OsgiServiceCollection collection = createCollection("(a=b)"); collection.startTracking(); registry.registerService( Collection.class.getName(), new ArrayList<Object>(), new Hashtable(Collections.singletonMap("a", "b"))); assertThat(collection.isEmpty(), equalTo(false)); assertThat(collection.size(), equalTo(1)); collection.stopTracking(); registry.registerService( Collection.class.getName(), new ArrayList<Object>(), new Hashtable(Collections.singletonMap("a", "b"))); assertThat(collection.isEmpty(), equalTo(true)); assertThat(collection.size(), equalTo(0)); }