diff --git a/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java b/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java index a8eb7b4f2..989092a97 100644 --- a/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java @@ -73,9 +73,65 @@ public class IteratorUtilsTest { */ private List collectionOdd = null; + private final Collection emptyCollection = new ArrayList<>(1); + private Iterable iterableA = null; - private final Collection emptyCollection = new ArrayList<>(1); + /** + * Creates a NodeList containing the specified nodes. + */ + private NodeList createNodeList(final Node[] nodes) { + return new NodeList() { + @Override + public int getLength() { + return nodes.length; + } + @Override + public Node item(final int index) { + return nodes[index]; + } + }; + } + + /** + * creates an array of four Node instances, mocked by EasyMock. + */ + private Node[] createNodes() { + final Node node1 = createMock(Node.class); + final Node node2 = createMock(Node.class); + final Node node3 = createMock(Node.class); + final Node node4 = createMock(Node.class); + replay(node1); + replay(node2); + replay(node3); + replay(node4); + + return new Node[]{node1, node2, node3, node4}; +} + + /** + * Gets an immutable Iterator operating on the elements ["a", "b", "c", "d"]. + */ + private Iterator getImmutableIterator() { + final List list = new ArrayList<>(); + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + return IteratorUtils.unmodifiableIterator(list.iterator()); + } + + /** + * Gets an immutable ListIterator operating on the elements ["a", "b", "c", "d"]. + */ + private ListIterator getImmutableListIterator() { + final List list = new ArrayList<>(); + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + return IteratorUtils.unmodifiableListIterator(list.listIterator()); + } @Before public void setUp() { @@ -97,125 +153,6 @@ public class IteratorUtilsTest { collectionOdd = Arrays.asList(1, 3, 5, 7, 9, 11); } - @Test - public void testAsIterable() { - final List list = new ArrayList<>(); - list.add(Integer.valueOf(0)); - list.add(Integer.valueOf(1)); - list.add(Integer.valueOf(2)); - final Iterator iterator = list.iterator(); - - final Iterable iterable = IteratorUtils.asIterable(iterator); - int expected = 0; - for(final Integer actual : iterable) { - assertEquals(expected, actual.intValue()); - ++expected; - } - // insure iteration occurred - assertTrue(expected > 0); - - // single use iterator - assertFalse("should not be able to iterate twice", IteratorUtils.asIterable(iterator).iterator().hasNext()); - } - - @Test - public void testAsIterableNull() { - try { - IteratorUtils.asIterable(null); - fail("Expecting NullPointerException"); - } catch (final NullPointerException ex) { - // success - } - } - - @Test - public void testAsMultipleIterable() { - final List list = new ArrayList<>(); - list.add(Integer.valueOf(0)); - list.add(Integer.valueOf(1)); - list.add(Integer.valueOf(2)); - final Iterator iterator = list.iterator(); - - final Iterable iterable = IteratorUtils.asMultipleUseIterable(iterator); - int expected = 0; - for(final Integer actual : iterable) { - assertEquals(expected, actual.intValue()); - ++expected; - } - // insure iteration occurred - assertTrue(expected > 0); - - // multiple use iterator - expected = 0; - for(final Integer actual : iterable) { - assertEquals(expected, actual.intValue()); - ++expected; - } - // insure iteration occurred - assertTrue(expected > 0); - } - - @Test - public void testAsMultipleIterableNull() { - try { - IteratorUtils.asMultipleUseIterable(null); - fail("Expecting NullPointerException"); - } catch (final NullPointerException ex) { - // success - } - } - - @Test - public void testToList() { - final List list = new ArrayList<>(); - list.add(Integer.valueOf(1)); - list.add("Two"); - list.add(null); - final List result = IteratorUtils.toList(list.iterator()); - assertEquals(list, result); - } - - @Test - public void testToArray() { - final List list = new ArrayList<>(); - list.add(Integer.valueOf(1)); - list.add("Two"); - list.add(null); - final Object[] result = IteratorUtils.toArray(list.iterator()); - assertEquals(list, Arrays.asList(result)); - - try { - IteratorUtils.toArray(null); - fail("Expecting NullPointerException"); - } catch (final NullPointerException ex) { - // success - } - } - - @Test - public void testToArray2() { - final List list = new ArrayList<>(); - list.add("One"); - list.add("Two"); - list.add(null); - final String[] result = IteratorUtils.toArray(list.iterator(), String.class); - assertEquals(list, Arrays.asList(result)); - - try { - IteratorUtils.toArray(list.iterator(), null); - fail("Expecting NullPointerException"); - } catch (final NullPointerException ex) { - // success - } - - try { - IteratorUtils.toArray(null, String.class); - fail("Expecting NullPointerException"); - } catch (final NullPointerException ex) { - // success - } - } - @Test public void testArrayIterator() { final Object[] objArray = {"a", "b", "c"}; @@ -500,29 +437,124 @@ public class IteratorUtilsTest { } } + @Test + public void testAsIterable() { + final List list = new ArrayList<>(); + list.add(Integer.valueOf(0)); + list.add(Integer.valueOf(1)); + list.add(Integer.valueOf(2)); + final Iterator iterator = list.iterator(); - /** - * Gets an immutable Iterator operating on the elements ["a", "b", "c", "d"]. - */ - private Iterator getImmutableIterator() { - final List list = new ArrayList<>(); - list.add("a"); - list.add("b"); - list.add("c"); - list.add("d"); - return IteratorUtils.unmodifiableIterator(list.iterator()); + final Iterable iterable = IteratorUtils.asIterable(iterator); + int expected = 0; + for(final Integer actual : iterable) { + assertEquals(expected, actual.intValue()); + ++expected; + } + // insure iteration occurred + assertTrue(expected > 0); + + // single use iterator + assertFalse("should not be able to iterate twice", IteratorUtils.asIterable(iterator).iterator().hasNext()); + } + + @Test + public void testAsIterableNull() { + try { + IteratorUtils.asIterable(null); + fail("Expecting NullPointerException"); + } catch (final NullPointerException ex) { + // success + } + } + + @Test + public void testAsMultipleIterable() { + final List list = new ArrayList<>(); + list.add(Integer.valueOf(0)); + list.add(Integer.valueOf(1)); + list.add(Integer.valueOf(2)); + final Iterator iterator = list.iterator(); + + final Iterable iterable = IteratorUtils.asMultipleUseIterable(iterator); + int expected = 0; + for(final Integer actual : iterable) { + assertEquals(expected, actual.intValue()); + ++expected; + } + // insure iteration occurred + assertTrue(expected > 0); + + // multiple use iterator + expected = 0; + for(final Integer actual : iterable) { + assertEquals(expected, actual.intValue()); + ++expected; + } + // insure iteration occurred + assertTrue(expected > 0); + } + + + @Test + public void testAsMultipleIterableNull() { + try { + IteratorUtils.asMultipleUseIterable(null); + fail("Expecting NullPointerException"); + } catch (final NullPointerException ex) { + // success + } } /** - * Gets an immutable ListIterator operating on the elements ["a", "b", "c", "d"]. + * Tests methods collatedIterator(...) */ - private ListIterator getImmutableListIterator() { - final List list = new ArrayList<>(); - list.add("a"); - list.add("b"); - list.add("c"); - list.add("d"); - return IteratorUtils.unmodifiableListIterator(list.listIterator()); + @Test + public void testCollatedIterator() { + try { + IteratorUtils.collatedIterator(null, collectionOdd.iterator(), null); + fail("expecting NullPointerException"); + } catch (final NullPointerException npe) { + // expected + } + + try { + IteratorUtils.collatedIterator(null, null, collectionEven.iterator()); + fail("expecting NullPointerException"); + } catch (final NullPointerException npe) { + // expected + } + + // natural ordering + Iterator it = + IteratorUtils.collatedIterator(null, collectionOdd.iterator(), collectionEven.iterator()); + + List result = IteratorUtils.toList(it); + assertEquals(12, result.size()); + + final List combinedList = new ArrayList<>(); + combinedList.addAll(collectionOdd); + combinedList.addAll(collectionEven); + Collections.sort(combinedList); + + assertEquals(combinedList, result); + + it = IteratorUtils.collatedIterator(null, collectionOdd.iterator(), emptyCollection.iterator()); + result = IteratorUtils.toList(it); + assertEquals(collectionOdd, result); + + final Comparator reverseComparator = + ComparatorUtils.reversedComparator(ComparatorUtils.naturalComparator()); + + Collections.reverse(collectionOdd); + Collections.reverse(collectionEven); + Collections.reverse(combinedList); + + it = IteratorUtils.collatedIterator(reverseComparator, + collectionOdd.iterator(), + collectionEven.iterator()); + result = IteratorUtils.toList(it); + assertEquals(combinedList, result); } //----------------------------------------------------------------------- @@ -709,297 +741,28 @@ public class IteratorUtilsTest { } catch (final IllegalStateException ex) {} } - //----------------------------------------------------------------------- - /** - * Test next() and hasNext() for an immutable Iterator. - */ @Test - public void testUnmodifiableIteratorIteration() { - final Iterator iterator = getImmutableIterator(); - - assertTrue(iterator.hasNext()); - - assertEquals("a", iterator.next()); - - assertTrue(iterator.hasNext()); - - assertEquals("b", iterator.next()); - - assertTrue(iterator.hasNext()); - - assertEquals("c", iterator.next()); - - assertTrue(iterator.hasNext()); - - assertEquals("d", iterator.next()); - - assertTrue(!iterator.hasNext()); - } - - /** - * Test next(), hasNext(), previous() and hasPrevious() for an immutable - * ListIterator. - */ - @Test - public void testUnmodifiableListIteratorIteration() { - final ListIterator listIterator = getImmutableListIterator(); - - assertTrue(!listIterator.hasPrevious()); - assertTrue(listIterator.hasNext()); - - assertEquals("a", listIterator.next()); - - assertTrue(listIterator.hasPrevious()); - assertTrue(listIterator.hasNext()); - - assertEquals("b", listIterator.next()); - - assertTrue(listIterator.hasPrevious()); - assertTrue(listIterator.hasNext()); - - assertEquals("c", listIterator.next()); - - assertTrue(listIterator.hasPrevious()); - assertTrue(listIterator.hasNext()); - - assertEquals("d", listIterator.next()); - - assertTrue(listIterator.hasPrevious()); - assertTrue(!listIterator.hasNext()); - - assertEquals("d", listIterator.previous()); - - assertTrue(listIterator.hasPrevious()); - assertTrue(listIterator.hasNext()); - - assertEquals("c", listIterator.previous()); - - assertTrue(listIterator.hasPrevious()); - assertTrue(listIterator.hasNext()); - - assertEquals("b", listIterator.previous()); - - assertTrue(listIterator.hasPrevious()); - assertTrue(listIterator.hasNext()); - - assertEquals("a", listIterator.previous()); - - assertTrue(!listIterator.hasPrevious()); - assertTrue(listIterator.hasNext()); - } - - /** - * Test remove() for an immutable Iterator. - */ - @Test - public void testUnmodifiableIteratorImmutability() { - final Iterator iterator = getImmutableIterator(); - + public void testFind() { + Predicate testPredicate = equalPredicate((Number) 4); + Integer test = IteratorUtils.find(iterableA.iterator(), testPredicate); + assertTrue(test.equals(4)); + testPredicate = equalPredicate((Number) 45); + test = IteratorUtils.find(iterableA.iterator(), testPredicate); + assertTrue(test == null); + assertNull(IteratorUtils.find(null,testPredicate)); try { - iterator.remove(); - // We shouldn't get to here. - fail("remove() should throw an UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // This is correct; ignore the exception. - } - - iterator.next(); - - try { - iterator.remove(); - // We shouldn't get to here. - fail("remove() should throw an UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // This is correct; ignore the exception. - } - - } - - /** - * Test remove() for an immutable ListIterator. - */ - @Test - public void testUnmodifiableListIteratorImmutability() { - final ListIterator listIterator = getImmutableListIterator(); - - try { - listIterator.remove(); - // We shouldn't get to here. - fail("remove() should throw an UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // This is correct; ignore the exception. - } - - try { - listIterator.set("a"); - // We shouldn't get to here. - fail("set(Object) should throw an UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // This is correct; ignore the exception. - } - - try { - listIterator.add("a"); - // We shouldn't get to here. - fail("add(Object) should throw an UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // This is correct; ignore the exception. - } - - listIterator.next(); - - try { - listIterator.remove(); - // We shouldn't get to here. - fail("remove() should throw an UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // This is correct; ignore the exception. - } - - try { - listIterator.set("a"); - // We shouldn't get to here. - fail("set(Object) should throw an UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // This is correct; ignore the exception. - } - - try { - listIterator.add("a"); - // We shouldn't get to here. - fail("add(Object) should throw an UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // This is correct; ignore the exception. - } - } - - /** - * Tests method nodeListIterator(NodeList) - */ - @Test - public void testNodeListIterator() { - final Node[] nodes = createNodes(); - final NodeList nodeList = createNodeList(nodes); - - final Iterator iterator = IteratorUtils.nodeListIterator(nodeList); - int expectedNodeIndex = 0; - for (final Node actual : IteratorUtils.asIterable(iterator)) { - assertEquals(nodes[expectedNodeIndex], actual); - ++expectedNodeIndex; - } - - // insure iteration occurred - assertTrue(expectedNodeIndex > 0); - - // single use iterator - assertFalse("should not be able to iterate twice", IteratorUtils.asIterable(iterator).iterator().hasNext()); - } - /** - * Tests method nodeListIterator(Node) - */ - @Test - public void testNodeIterator() { - final Node[] nodes = createNodes(); - final NodeList nodeList = createNodeList(nodes); - final Node parentNode = createMock(Node.class); - expect(parentNode.getChildNodes()).andStubReturn(nodeList); - replay(parentNode); - - final Iterator iterator = IteratorUtils.nodeListIterator(parentNode); - int expectedNodeIndex = 0; - for (final Node actual : IteratorUtils.asIterable(iterator)) { - assertEquals(nodes[expectedNodeIndex], actual); - ++expectedNodeIndex; - } - - // insure iteration occurred - assertTrue(expectedNodeIndex > 0); - - // single use iterator - assertFalse("should not be able to iterate twice", IteratorUtils.asIterable(iterator).iterator().hasNext()); - } - - /** - * creates an array of four Node instances, mocked by EasyMock. - */ - private Node[] createNodes() { - final Node node1 = createMock(Node.class); - final Node node2 = createMock(Node.class); - final Node node3 = createMock(Node.class); - final Node node4 = createMock(Node.class); - replay(node1); - replay(node2); - replay(node3); - replay(node4); - - return new Node[]{node1, node2, node3, node4}; -} - - /** - * Creates a NodeList containing the specified nodes. - */ - private NodeList createNodeList(final Node[] nodes) { - return new NodeList() { - @Override - public Node item(final int index) { - return nodes[index]; - } - @Override - public int getLength() { - return nodes.length; - } - }; - } - - /** - * Tests methods collatedIterator(...) - */ - @Test - public void testCollatedIterator() { - try { - IteratorUtils.collatedIterator(null, collectionOdd.iterator(), null); + assertNull(IteratorUtils.find(iterableA.iterator(), null)); fail("expecting NullPointerException"); } catch (final NullPointerException npe) { // expected } + } - try { - IteratorUtils.collatedIterator(null, null, collectionEven.iterator()); - fail("expecting NullPointerException"); - } catch (final NullPointerException npe) { - // expected - } - - // natural ordering - Iterator it = - IteratorUtils.collatedIterator(null, collectionOdd.iterator(), collectionEven.iterator()); - - List result = IteratorUtils.toList(it); - assertEquals(12, result.size()); - - final List combinedList = new ArrayList<>(); - combinedList.addAll(collectionOdd); - combinedList.addAll(collectionEven); - Collections.sort(combinedList); - - assertEquals(combinedList, result); - - it = IteratorUtils.collatedIterator(null, collectionOdd.iterator(), emptyCollection.iterator()); - result = IteratorUtils.toList(it); - assertEquals(collectionOdd, result); - - final Comparator reverseComparator = - ComparatorUtils.reversedComparator(ComparatorUtils.naturalComparator()); - - Collections.reverse(collectionOdd); - Collections.reverse(collectionEven); - Collections.reverse(combinedList); - - it = IteratorUtils.collatedIterator(reverseComparator, - collectionOdd.iterator(), - collectionEven.iterator()); - result = IteratorUtils.toList(it); - assertEquals(combinedList, result); + @Test + public void testFirstFromIterator() throws Exception { + // Iterator, entry exists + Iterator iterator = iterableA.iterator(); + assertEquals(1, (int) IteratorUtils.first(iterator)); } // ----------------------------------------------------------------------- @@ -1064,20 +827,37 @@ public class IteratorUtilsTest { } @Test - public void testFind() { - Predicate testPredicate = equalPredicate((Number) 4); - Integer test = IteratorUtils.find(iterableA.iterator(), testPredicate); - assertTrue(test.equals(4)); - testPredicate = equalPredicate((Number) 45); - test = IteratorUtils.find(iterableA.iterator(), testPredicate); - assertTrue(test == null); - assertNull(IteratorUtils.find(null,testPredicate)); + public void testGetAtIndexFromIterator() throws Exception { + // Iterator, entry exists + Iterator iterator = iterableA.iterator(); + assertEquals(1, (int) IteratorUtils.get(iterator, 0)); + iterator = iterableA.iterator(); + assertEquals(2, (int) IteratorUtils.get(iterator, 1)); + + // Iterator, non-existent entry try { - assertNull(IteratorUtils.find(iterableA.iterator(), null)); - fail("expecting NullPointerException"); - } catch (final NullPointerException npe) { + IteratorUtils.get(iterator, 10); + fail("Expecting IndexOutOfBoundsException."); + } catch (final IndexOutOfBoundsException e) { // expected } + assertTrue(!iterator.hasNext()); + } + @Test + public void testGetIterator() { + final Object[] objArray = {"a", "b", "c"}; + final Map inMap = new HashMap<>(); + final Node[] nodes = createNodes(); + final NodeList nodeList = createNodeList(nodes); + + assertTrue("returns empty iterator when null passed", IteratorUtils.getIterator(null) instanceof EmptyIterator); + assertTrue("returns Iterator when Iterator directly ", IteratorUtils.getIterator(iterableA.iterator()) instanceof Iterator); + assertTrue("returns Iterator when iterable passed", IteratorUtils.getIterator(iterableA) instanceof Iterator); + assertTrue("returns ObjectArrayIterator when Object array passed", IteratorUtils.getIterator(objArray) instanceof ObjectArrayIterator); + assertTrue("returns Iterator when Map passed", IteratorUtils.getIterator(inMap) instanceof Iterator); + assertTrue("returns NodeListIterator when nodeList passed", IteratorUtils.getIterator(nodeList) instanceof NodeListIterator); + assertTrue("returns EnumerationIterator when Enumeration passed", IteratorUtils.getIterator(new Vector().elements()) instanceof EnumerationIterator); + } @Test @@ -1097,46 +877,102 @@ public class IteratorUtilsTest { } } + /** + * Tests method nodeListIterator(Node) + */ @Test - public void testGetAtIndexFromIterator() throws Exception { - // Iterator, entry exists - Iterator iterator = iterableA.iterator(); - assertEquals(1, (int) IteratorUtils.get(iterator, 0)); - iterator = iterableA.iterator(); - assertEquals(2, (int) IteratorUtils.get(iterator, 1)); + public void testNodeIterator() { + final Node[] nodes = createNodes(); + final NodeList nodeList = createNodeList(nodes); + final Node parentNode = createMock(Node.class); + expect(parentNode.getChildNodes()).andStubReturn(nodeList); + replay(parentNode); - // Iterator, non-existent entry - try { - IteratorUtils.get(iterator, 10); - fail("Expecting IndexOutOfBoundsException."); - } catch (final IndexOutOfBoundsException e) { - // expected + final Iterator iterator = IteratorUtils.nodeListIterator(parentNode); + int expectedNodeIndex = 0; + for (final Node actual : IteratorUtils.asIterable(iterator)) { + assertEquals(nodes[expectedNodeIndex], actual); + ++expectedNodeIndex; } - assertTrue(!iterator.hasNext()); + + // insure iteration occurred + assertTrue(expectedNodeIndex > 0); + + // single use iterator + assertFalse("should not be able to iterate twice", IteratorUtils.asIterable(iterator).iterator().hasNext()); } + /** + * Tests method nodeListIterator(NodeList) + */ @Test - public void testFirstFromIterator() throws Exception { - // Iterator, entry exists - Iterator iterator = iterableA.iterator(); - assertEquals(1, (int) IteratorUtils.first(iterator)); - } - - @Test - public void testGetIterator() { - final Object[] objArray = {"a", "b", "c"}; - final Map inMap = new HashMap<>(); + public void testNodeListIterator() { final Node[] nodes = createNodes(); final NodeList nodeList = createNodeList(nodes); - assertTrue("returns empty iterator when null passed", IteratorUtils.getIterator(null) instanceof EmptyIterator); - assertTrue("returns Iterator when Iterator directly ", IteratorUtils.getIterator(iterableA.iterator()) instanceof Iterator); - assertTrue("returns Iterator when iterable passed", IteratorUtils.getIterator(iterableA) instanceof Iterator); - assertTrue("returns ObjectArrayIterator when Object array passed", IteratorUtils.getIterator(objArray) instanceof ObjectArrayIterator); - assertTrue("returns Iterator when Map passed", IteratorUtils.getIterator(inMap) instanceof Iterator); - assertTrue("returns NodeListIterator when nodeList passed", IteratorUtils.getIterator(nodeList) instanceof NodeListIterator); - assertTrue("returns EnumerationIterator when Enumeration passed", IteratorUtils.getIterator(new Vector().elements()) instanceof EnumerationIterator); + final Iterator iterator = IteratorUtils.nodeListIterator(nodeList); + int expectedNodeIndex = 0; + for (final Node actual : IteratorUtils.asIterable(iterator)) { + assertEquals(nodes[expectedNodeIndex], actual); + ++expectedNodeIndex; + } + // insure iteration occurred + assertTrue(expectedNodeIndex > 0); + + // single use iterator + assertFalse("should not be able to iterate twice", IteratorUtils.asIterable(iterator).iterator().hasNext()); + } + + @Test + public void testToArray() { + final List list = new ArrayList<>(); + list.add(Integer.valueOf(1)); + list.add("Two"); + list.add(null); + final Object[] result = IteratorUtils.toArray(list.iterator()); + assertEquals(list, Arrays.asList(result)); + + try { + IteratorUtils.toArray(null); + fail("Expecting NullPointerException"); + } catch (final NullPointerException ex) { + // success + } + } + + @Test + public void testToArray2() { + final List list = new ArrayList<>(); + list.add("One"); + list.add("Two"); + list.add(null); + final String[] result = IteratorUtils.toArray(list.iterator(), String.class); + assertEquals(list, Arrays.asList(result)); + + try { + IteratorUtils.toArray(list.iterator(), null); + fail("Expecting NullPointerException"); + } catch (final NullPointerException ex) { + // success + } + + try { + IteratorUtils.toArray(null, String.class); + fail("Expecting NullPointerException"); + } catch (final NullPointerException ex) { + // success + } + } + + @Test + public void testToList() { + final List list = new ArrayList<>(); + list.add(Integer.valueOf(1)); + list.add("Two"); + list.add(null); + final List result = IteratorUtils.toList(list.iterator()); + assertEquals(list, result); } @Test @@ -1165,4 +1001,168 @@ public class IteratorUtilsTest { } } + /** + * Test remove() for an immutable Iterator. + */ + @Test + public void testUnmodifiableIteratorImmutability() { + final Iterator iterator = getImmutableIterator(); + + try { + iterator.remove(); + // We shouldn't get to here. + fail("remove() should throw an UnsupportedOperationException"); + } catch (final UnsupportedOperationException e) { + // This is correct; ignore the exception. + } + + iterator.next(); + + try { + iterator.remove(); + // We shouldn't get to here. + fail("remove() should throw an UnsupportedOperationException"); + } catch (final UnsupportedOperationException e) { + // This is correct; ignore the exception. + } + + } + + //----------------------------------------------------------------------- + /** + * Test next() and hasNext() for an immutable Iterator. + */ + @Test + public void testUnmodifiableIteratorIteration() { + final Iterator iterator = getImmutableIterator(); + + assertTrue(iterator.hasNext()); + + assertEquals("a", iterator.next()); + + assertTrue(iterator.hasNext()); + + assertEquals("b", iterator.next()); + + assertTrue(iterator.hasNext()); + + assertEquals("c", iterator.next()); + + assertTrue(iterator.hasNext()); + + assertEquals("d", iterator.next()); + + assertTrue(!iterator.hasNext()); + } + + /** + * Test remove() for an immutable ListIterator. + */ + @Test + public void testUnmodifiableListIteratorImmutability() { + final ListIterator listIterator = getImmutableListIterator(); + + try { + listIterator.remove(); + // We shouldn't get to here. + fail("remove() should throw an UnsupportedOperationException"); + } catch (final UnsupportedOperationException e) { + // This is correct; ignore the exception. + } + + try { + listIterator.set("a"); + // We shouldn't get to here. + fail("set(Object) should throw an UnsupportedOperationException"); + } catch (final UnsupportedOperationException e) { + // This is correct; ignore the exception. + } + + try { + listIterator.add("a"); + // We shouldn't get to here. + fail("add(Object) should throw an UnsupportedOperationException"); + } catch (final UnsupportedOperationException e) { + // This is correct; ignore the exception. + } + + listIterator.next(); + + try { + listIterator.remove(); + // We shouldn't get to here. + fail("remove() should throw an UnsupportedOperationException"); + } catch (final UnsupportedOperationException e) { + // This is correct; ignore the exception. + } + + try { + listIterator.set("a"); + // We shouldn't get to here. + fail("set(Object) should throw an UnsupportedOperationException"); + } catch (final UnsupportedOperationException e) { + // This is correct; ignore the exception. + } + + try { + listIterator.add("a"); + // We shouldn't get to here. + fail("add(Object) should throw an UnsupportedOperationException"); + } catch (final UnsupportedOperationException e) { + // This is correct; ignore the exception. + } + } + + /** + * Test next(), hasNext(), previous() and hasPrevious() for an immutable + * ListIterator. + */ + @Test + public void testUnmodifiableListIteratorIteration() { + final ListIterator listIterator = getImmutableListIterator(); + + assertTrue(!listIterator.hasPrevious()); + assertTrue(listIterator.hasNext()); + + assertEquals("a", listIterator.next()); + + assertTrue(listIterator.hasPrevious()); + assertTrue(listIterator.hasNext()); + + assertEquals("b", listIterator.next()); + + assertTrue(listIterator.hasPrevious()); + assertTrue(listIterator.hasNext()); + + assertEquals("c", listIterator.next()); + + assertTrue(listIterator.hasPrevious()); + assertTrue(listIterator.hasNext()); + + assertEquals("d", listIterator.next()); + + assertTrue(listIterator.hasPrevious()); + assertTrue(!listIterator.hasNext()); + + assertEquals("d", listIterator.previous()); + + assertTrue(listIterator.hasPrevious()); + assertTrue(listIterator.hasNext()); + + assertEquals("c", listIterator.previous()); + + assertTrue(listIterator.hasPrevious()); + assertTrue(listIterator.hasNext()); + + assertEquals("b", listIterator.previous()); + + assertTrue(listIterator.hasPrevious()); + assertTrue(listIterator.hasNext()); + + assertEquals("a", listIterator.previous()); + + assertTrue(!listIterator.hasPrevious()); + assertTrue(listIterator.hasNext()); + } + }