1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.iterators;
18
19 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.util.ListIterator;
25 import java.util.NoSuchElementException;
26
27 import org.junit.jupiter.api.Test;
28
29
30
31
32
33
34 public class ObjectArrayListIteratorTest<E> extends ObjectArrayIteratorTest<E> {
35
36 public ObjectArrayListIterator<E> makeArrayListIterator(final E[] array) {
37 return new ObjectArrayListIterator<>(array);
38 }
39
40 @Override
41 @SuppressWarnings("unchecked")
42 public ObjectArrayListIterator<E> makeEmptyIterator() {
43 return new ObjectArrayListIterator<>((E[]) new Object[0]);
44 }
45
46 @Override
47 @SuppressWarnings("unchecked")
48 public ObjectArrayListIterator<E> makeObject() {
49 return new ObjectArrayListIterator<>((E[]) testArray);
50 }
51
52
53
54
55
56 @Test
57 public void testListIterator() {
58 final ListIterator<E> iter = makeObject();
59
60
61
62 while (iter.hasNext()) {
63 iter.next();
64 }
65 for (int x = testArray.length - 1; x >= 0; x--) {
66 final Object testValue = testArray[x];
67 final Object iterValue = iter.previous();
68 assertEquals(testValue, iterValue, "Iteration value is correct");
69 }
70 assertFalse(iter.hasPrevious(), "Iterator should now be empty");
71 assertThrows(NoSuchElementException.class, iter::previous);
72 }
73
74
75
76
77 @Test
78 @SuppressWarnings("unchecked")
79 public void testListIteratorSet() {
80 final String[] testData = { "a", "b", "c" };
81
82 final String[] result = { "0", "1", "2" };
83
84 ListIterator<E> iter = makeArrayListIterator((E[]) testData);
85 int x = 0;
86
87 while (iter.hasNext()) {
88 iter.next();
89 iter.set((E) Integer.toString(x));
90 x++;
91 }
92
93 assertArrayEquals(testData, result, "The two arrays should have the same value, i.e. {0,1,2}");
94
95
96 iter = makeArrayListIterator((E[]) testArray);
97
98 final ListIterator<E> finalIter = iter;
99 assertThrows(IllegalStateException.class, () -> finalIter.set((E) "should fail"), "ListIterator#set should fail if next() or previous() have not yet been called.");
100 }
101
102 }