OsgiServiceList extends OsgiServiceCollection<E> implements List<E>, RandomAccess { @Override public ListIterator<E> listIterator() { return listIterator(0); } OsgiServiceList(BundleContext context, String filter, ClassLoader classLoader,
        OsgiProxyCreator proxyCreator); @Override @SuppressWarnings("unchecked") E get(int index); @Override int indexOf(Object o); @Override int lastIndexOf(Object o); @Override ListIterator<E> listIterator(); @Override ListIterator<E> listIterator(final int index); @Override List<E> subList(int fromIndex, int toIndex); @Override E remove(int index); @Override E set(int index, Object o); @Override void add(int index, Object o); @Override boolean addAll(int index, Collection c);  }
@Test @SuppressWarnings("unchecked") public void testAddWhileListIterating() throws Exception { OsgiServiceList list = createList("(a=b)"); list.startTracking(); assertThat(list.isEmpty(), equalTo(true)); assertThat(list.size(), equalTo(0)); registry.registerService( Collection.class.getName(), new ArrayList<Object>(), new Hashtable(Collections.singletonMap("a", "b"))); assertThat(list.isEmpty(), equalTo(false)); assertThat(list.size(), equalTo(1)); Iterator<?> iterator = list.listIterator(); assertThat(iterator.hasNext(), equalTo(true)); assertThat(iterator.next(), notNullValue()); assertThat(iterator.hasNext(), equalTo(false)); registry.registerService( Collection.class.getName(), new ArrayList<Object>(), new Hashtable(Collections.singletonMap("a", "b"))); assertThat(list.isEmpty(), equalTo(false)); assertThat(list.size(), equalTo(2)); assertThat(iterator.hasNext(), equalTo(true)); assertThat(iterator.next(), notNullValue()); assertThat(iterator.hasNext(), equalTo(false)); }
@Test @SuppressWarnings("unchecked") public void testRemoveWhileListIterating() throws Exception { OsgiServiceList list = createList("(a=b)"); list.startTracking(); registry.registerService( Collection.class.getName(), new ArrayList<Object>(), new Hashtable(Collections.singletonMap("a", "b"))); ServiceRegistration registration = registry.registerService( Collection.class.getName(), new ArrayList<Object>(), new Hashtable(Collections.singletonMap("a", "b"))); assertThat(list.isEmpty(), equalTo(false)); assertThat(list.size(), equalTo(2)); Iterator<?> iterator = list.listIterator(); assertThat(iterator.hasNext(), equalTo(true)); assertThat(iterator.next(), notNullValue()); registration.unregister(); assertThat(iterator.hasNext(), equalTo(false)); assertThat(list.isEmpty(), equalTo(false)); assertThat(list.size(), equalTo(1)); }