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