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 public class ArrayListIteratorTest<E> extends ArrayIteratorTest<E> {
33
34 public ArrayListIteratorTest() {
35 }
36
37 public ArrayListIterator<E> makeArrayListIterator(final Object array) {
38 return new ArrayListIterator<>(array);
39 }
40
41 @Override
42 public ArrayListIterator<E> makeEmptyIterator() {
43 return new ArrayListIterator<>(new Object[0]);
44 }
45
46 @Override
47 public ArrayListIterator<E> makeObject() {
48 return new ArrayListIterator<>(testArray);
49 }
50
51 @Override
52 public boolean supportsRemove() {
53 return false;
54 }
55
56
57
58
59
60 @Test
61 public void testListIterator() {
62 final ListIterator<E> iter = makeObject();
63
64
65
66
67
68 while (iter.hasNext()) {
69 iter.next();
70 }
71
72 for (int x = testArray.length - 1; x >= 0; x--) {
73 final Object testValue = testArray[x];
74 final Object iterValue = iter.previous();
75
76 assertEquals(testValue, iterValue, "Iteration value is correct");
77 }
78
79 assertFalse(iter.hasPrevious(), "Iterator should now be empty");
80
81 try {
82 iter.previous();
83 } catch (final Exception e) {
84 assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
85 }
86
87 }
88
89
90
91
92 @Test
93 @SuppressWarnings("unchecked")
94 public void testListIteratorSet() {
95 final String[] testData = { "a", "b", "c" };
96
97 final String[] result = { "0", "1", "2" };
98
99 ListIterator<E> iter = makeArrayListIterator(testData);
100 int x = 0;
101
102 while (iter.hasNext()) {
103 iter.next();
104 iter.set((E) Integer.toString(x));
105 x++;
106 }
107
108 assertArrayEquals(testData, result, "The two arrays should have the same value, i.e. {0,1,2}");
109
110
111 iter = makeArrayListIterator(testArray);
112
113 final ListIterator<E> finalIter = iter;
114 assertThrows(IllegalStateException.class, () -> finalIter.set((E) "should fail"), "ListIterator#set should fail if next() or previous() have not yet been called.");
115 }
116
117 }