### Question:
SortedList extends TransformedList<E,E> { @Override public Iterator<E> iterator() { return new SortedListIterator(); } SortedList(EventList<E> source);  SortedList(EventList<E> source, Comparator<? super E> comparator); static SortedList<E> create(EventList<E> source); void setMode(int mode); int getMode(); @Override void listChanged(ListEvent<E> listChanges); Comparator<? super E> getComparator(); void setComparator(Comparator<? super E> comparator); @Override int indexOf(Object object); @Override int lastIndexOf(Object object); int sortIndex(Object object); int lastSortIndex(Object object); @Deprecated int indexOfSimulated(Object object); @Override boolean contains(Object object); @Override Iterator<E> iterator(); static final int STRICT_SORT_ORDER; static final int AVOID_MOVING_ELEMENTS; }### Answer:
@Test public void testIteratorOnEmptySortedList() { Iterator i = sortedList.iterator(); assertEquals(false, i.hasNext()); try { i.next(); fail("An expected Exception was not thrown."); } catch(NoSuchElementException e) { } catch(Exception e) { fail("The following Exception was not expected:\n" + e); } i = sortedList.iterator(); try { i.next(); fail("An expected Exception was not thrown."); } catch(NoSuchElementException e) { } catch(Exception e) { fail("The following Exception was not expected:\n" + e); } }
@Test public void testIteratorReadOnly() { sortedList.add("Riders"); sortedList.add("Stampeders"); sortedList.add("Bombers"); sortedList.add("Eskimos"); sortedList.add("Argos"); sortedList.add("Ti-Cats"); sortedList.add("Renegades"); sortedList.add("Als"); String[] expected = {"Als", "Argos", "Bombers", "Eskimos", "Renegades", "Riders", "Stampeders", "Ti-Cats"}; int counter = 0; for(Iterator i = sortedList.iterator(); i.hasNext(); counter++) { assertEquals(expected[counter], i.next()); } assertEquals(expected.length, counter); }
@Test public void testIteratorRemoves() { sortedList.add("Riders"); sortedList.add("Stampeders"); sortedList.add("Bombers"); sortedList.add("Eskimos"); sortedList.add("Argos"); sortedList.add("Ti-Cats"); sortedList.add("Renegades"); sortedList.add("Als"); String[] expected = {"Als", "Argos", "Bombers", "Eskimos", "Renegades", "Riders", "Stampeders", "Ti-Cats"}; Iterator i = sortedList.iterator(); try { i.remove(); fail("An expected Exception was not thrown."); } catch(NoSuchElementException e) { } int counter = 0; for(i = sortedList.iterator(); i.hasNext(); counter++) { assertEquals(expected[counter], i.next()); i.remove(); try { i.remove(); fail("An expected Exception was not thrown."); } catch(NoSuchElementException e) { } } assertEquals(expected.length, counter); assertEquals(0, sortedList.size()); try { i.remove(); fail("An expected Exception was not thrown."); } catch(NoSuchElementException e) { } }
@Test public void testIteratorIsConsistent() { ListConsistencyListener.install(sortedList); unsortedList.addAll(Arrays.asList("d", "c", "a", "b")); Iterator<Comparable> iterator = sortedList.iterator(); assertEquals("a", iterator.next()); assertEquals("a", sortedList.get(0)); assertEquals("b", iterator.next()); assertEquals("b", sortedList.get(1)); assertEquals("c", iterator.next()); assertEquals("c", sortedList.get(2)); assertEquals("d", iterator.next()); assertEquals("d", sortedList.get(3)); }