Sort members.

This commit is contained in:
Gary Gregory 2018-02-22 13:30:44 -07:00
parent 8accf274ba
commit 864fda4370
1 changed files with 463 additions and 463 deletions

View File

@ -73,9 +73,65 @@ public class IteratorUtilsTest {
*/ */
private List<Integer> collectionOdd = null; private List<Integer> collectionOdd = null;
private final Collection<Integer> emptyCollection = new ArrayList<>(1);
private Iterable<Integer> iterableA = null; private Iterable<Integer> iterableA = null;
private final Collection<Integer> 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<String> getImmutableIterator() {
final List<String> 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<String> getImmutableListIterator() {
final List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
return IteratorUtils.unmodifiableListIterator(list.listIterator());
}
@Before @Before
public void setUp() { public void setUp() {
@ -97,125 +153,6 @@ public class IteratorUtilsTest {
collectionOdd = Arrays.asList(1, 3, 5, 7, 9, 11); collectionOdd = Arrays.asList(1, 3, 5, 7, 9, 11);
} }
@Test
public void testAsIterable() {
final List<Integer> list = new ArrayList<>();
list.add(Integer.valueOf(0));
list.add(Integer.valueOf(1));
list.add(Integer.valueOf(2));
final Iterator<Integer> iterator = list.iterator();
final Iterable<Integer> 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<Integer> list = new ArrayList<>();
list.add(Integer.valueOf(0));
list.add(Integer.valueOf(1));
list.add(Integer.valueOf(2));
final Iterator<Integer> iterator = list.iterator();
final Iterable<Integer> 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<Object> list = new ArrayList<>();
list.add(Integer.valueOf(1));
list.add("Two");
list.add(null);
final List<Object> result = IteratorUtils.toList(list.iterator());
assertEquals(list, result);
}
@Test
public void testToArray() {
final List<Object> 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<String> 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 @Test
public void testArrayIterator() { public void testArrayIterator() {
final Object[] objArray = {"a", "b", "c"}; final Object[] objArray = {"a", "b", "c"};
@ -500,29 +437,124 @@ public class IteratorUtilsTest {
} }
} }
@Test
public void testAsIterable() {
final List<Integer> list = new ArrayList<>();
list.add(Integer.valueOf(0));
list.add(Integer.valueOf(1));
list.add(Integer.valueOf(2));
final Iterator<Integer> iterator = list.iterator();
/** final Iterable<Integer> iterable = IteratorUtils.asIterable(iterator);
* Gets an immutable Iterator operating on the elements ["a", "b", "c", "d"]. int expected = 0;
*/ for(final Integer actual : iterable) {
private Iterator<String> getImmutableIterator() { assertEquals(expected, actual.intValue());
final List<String> list = new ArrayList<>(); ++expected;
list.add("a"); }
list.add("b"); // insure iteration occurred
list.add("c"); assertTrue(expected > 0);
list.add("d");
return IteratorUtils.unmodifiableIterator(list.iterator()); // 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<Integer> list = new ArrayList<>();
list.add(Integer.valueOf(0));
list.add(Integer.valueOf(1));
list.add(Integer.valueOf(2));
final Iterator<Integer> iterator = list.iterator();
final Iterable<Integer> 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<String> getImmutableListIterator() { @Test
final List<String> list = new ArrayList<>(); public void testCollatedIterator() {
list.add("a"); try {
list.add("b"); IteratorUtils.collatedIterator(null, collectionOdd.iterator(), null);
list.add("c"); fail("expecting NullPointerException");
list.add("d"); } catch (final NullPointerException npe) {
return IteratorUtils.unmodifiableListIterator(list.listIterator()); // expected
}
try {
IteratorUtils.collatedIterator(null, null, collectionEven.iterator());
fail("expecting NullPointerException");
} catch (final NullPointerException npe) {
// expected
}
// natural ordering
Iterator<Integer> it =
IteratorUtils.collatedIterator(null, collectionOdd.iterator(), collectionEven.iterator());
List<Integer> result = IteratorUtils.toList(it);
assertEquals(12, result.size());
final List<Integer> 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<Integer> reverseComparator =
ComparatorUtils.reversedComparator(ComparatorUtils.<Integer>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) {} } catch (final IllegalStateException ex) {}
} }
//-----------------------------------------------------------------------
/**
* Test next() and hasNext() for an immutable Iterator.
*/
@Test @Test
public void testUnmodifiableIteratorIteration() { public void testFind() {
final Iterator<String> iterator = getImmutableIterator(); Predicate<Number> testPredicate = equalPredicate((Number) 4);
Integer test = IteratorUtils.find(iterableA.iterator(), testPredicate);
assertTrue(iterator.hasNext()); assertTrue(test.equals(4));
testPredicate = equalPredicate((Number) 45);
assertEquals("a", iterator.next()); test = IteratorUtils.find(iterableA.iterator(), testPredicate);
assertTrue(test == null);
assertTrue(iterator.hasNext()); assertNull(IteratorUtils.find(null,testPredicate));
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<String> 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<String> iterator = getImmutableIterator();
try { try {
iterator.remove(); assertNull(IteratorUtils.find(iterableA.iterator(), null));
// 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<String> 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<Node> 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<Node> 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);
fail("expecting NullPointerException"); fail("expecting NullPointerException");
} catch (final NullPointerException npe) { } catch (final NullPointerException npe) {
// expected // expected
} }
}
try { @Test
IteratorUtils.collatedIterator(null, null, collectionEven.iterator()); public void testFirstFromIterator() throws Exception {
fail("expecting NullPointerException"); // Iterator, entry exists
} catch (final NullPointerException npe) { Iterator<Integer> iterator = iterableA.iterator();
// expected assertEquals(1, (int) IteratorUtils.first(iterator));
}
// natural ordering
Iterator<Integer> it =
IteratorUtils.collatedIterator(null, collectionOdd.iterator(), collectionEven.iterator());
List<Integer> result = IteratorUtils.toList(it);
assertEquals(12, result.size());
final List<Integer> 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<Integer> reverseComparator =
ComparatorUtils.reversedComparator(ComparatorUtils.<Integer>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);
} }
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@ -1064,20 +827,37 @@ public class IteratorUtilsTest {
} }
@Test @Test
public void testFind() { public void testGetAtIndexFromIterator() throws Exception {
Predicate<Number> testPredicate = equalPredicate((Number) 4); // Iterator, entry exists
Integer test = IteratorUtils.find(iterableA.iterator(), testPredicate); Iterator<Integer> iterator = iterableA.iterator();
assertTrue(test.equals(4)); assertEquals(1, (int) IteratorUtils.get(iterator, 0));
testPredicate = equalPredicate((Number) 45); iterator = iterableA.iterator();
test = IteratorUtils.find(iterableA.iterator(), testPredicate); assertEquals(2, (int) IteratorUtils.get(iterator, 1));
assertTrue(test == null);
assertNull(IteratorUtils.find(null,testPredicate)); // Iterator, non-existent entry
try { try {
assertNull(IteratorUtils.find(iterableA.iterator(), null)); IteratorUtils.get(iterator, 10);
fail("expecting NullPointerException"); fail("Expecting IndexOutOfBoundsException.");
} catch (final NullPointerException npe) { } catch (final IndexOutOfBoundsException e) {
// expected // expected
} }
assertTrue(!iterator.hasNext());
}
@Test
public void testGetIterator() {
final Object[] objArray = {"a", "b", "c"};
final Map<String, String> 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 @Test
@ -1097,46 +877,102 @@ public class IteratorUtilsTest {
} }
} }
/**
* Tests method nodeListIterator(Node)
*/
@Test @Test
public void testGetAtIndexFromIterator() throws Exception { public void testNodeIterator() {
// Iterator, entry exists final Node[] nodes = createNodes();
Iterator<Integer> iterator = iterableA.iterator(); final NodeList nodeList = createNodeList(nodes);
assertEquals(1, (int) IteratorUtils.get(iterator, 0)); final Node parentNode = createMock(Node.class);
iterator = iterableA.iterator(); expect(parentNode.getChildNodes()).andStubReturn(nodeList);
assertEquals(2, (int) IteratorUtils.get(iterator, 1)); replay(parentNode);
// Iterator, non-existent entry final Iterator<Node> iterator = IteratorUtils.nodeListIterator(parentNode);
try { int expectedNodeIndex = 0;
IteratorUtils.get(iterator, 10); for (final Node actual : IteratorUtils.asIterable(iterator)) {
fail("Expecting IndexOutOfBoundsException."); assertEquals(nodes[expectedNodeIndex], actual);
} catch (final IndexOutOfBoundsException e) { ++expectedNodeIndex;
// expected
} }
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 @Test
public void testFirstFromIterator() throws Exception { public void testNodeListIterator() {
// Iterator, entry exists
Iterator<Integer> iterator = iterableA.iterator();
assertEquals(1, (int) IteratorUtils.first(iterator));
}
@Test
public void testGetIterator() {
final Object[] objArray = {"a", "b", "c"};
final Map<String, String> inMap = new HashMap<>();
final Node[] nodes = createNodes(); final Node[] nodes = createNodes();
final NodeList nodeList = createNodeList(nodes); final NodeList nodeList = createNodeList(nodes);
assertTrue("returns empty iterator when null passed", IteratorUtils.getIterator(null) instanceof EmptyIterator); final Iterator<Node> iterator = IteratorUtils.nodeListIterator(nodeList);
assertTrue("returns Iterator when Iterator directly ", IteratorUtils.getIterator(iterableA.iterator()) instanceof Iterator); int expectedNodeIndex = 0;
assertTrue("returns Iterator when iterable passed", IteratorUtils.getIterator(iterableA) instanceof Iterator); for (final Node actual : IteratorUtils.asIterable(iterator)) {
assertTrue("returns ObjectArrayIterator when Object array passed", IteratorUtils.getIterator(objArray) instanceof ObjectArrayIterator); assertEquals(nodes[expectedNodeIndex], actual);
assertTrue("returns Iterator when Map passed", IteratorUtils.getIterator(inMap) instanceof Iterator); ++expectedNodeIndex;
assertTrue("returns NodeListIterator when nodeList passed", IteratorUtils.getIterator(nodeList) instanceof NodeListIterator); }
assertTrue("returns EnumerationIterator when Enumeration passed", IteratorUtils.getIterator(new Vector().elements()) instanceof EnumerationIterator);
// 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<Object> 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<String> 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<Object> list = new ArrayList<>();
list.add(Integer.valueOf(1));
list.add("Two");
list.add(null);
final List<Object> result = IteratorUtils.toList(list.iterator());
assertEquals(list, result);
} }
@Test @Test
@ -1165,4 +1001,168 @@ public class IteratorUtilsTest {
} }
} }
/**
* Test remove() for an immutable Iterator.
*/
@Test
public void testUnmodifiableIteratorImmutability() {
final Iterator<String> 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<String> 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<String> 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<String> 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());
}
} }