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.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertNotSame;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.util.ArrayList;
26 import java.util.ListIterator;
27 import java.util.NoSuchElementException;
28
29 import org.junit.jupiter.api.Test;
30
31
32
33
34
35
36
37
38
39
40
41
42 public abstract class AbstractListIteratorTest<E> extends AbstractIteratorTest<E> {
43
44
45
46
47
48 public E addSetValue() {
49 return null;
50 }
51
52
53
54
55
56
57 @Override
58 public abstract ListIterator<E> makeEmptyIterator();
59
60
61
62
63
64
65 @Override
66 public abstract ListIterator<E> makeObject();
67
68
69
70
71
72
73
74 public boolean supportsAdd() {
75 return true;
76 }
77
78
79
80
81
82
83
84 public boolean supportsSet() {
85 return true;
86 }
87
88
89
90
91 @Test
92 public void testAdd() {
93 ListIterator<E> it = makeObject();
94
95 final E addValue = addSetValue();
96 if (!supportsAdd()) {
97
98 final ListIterator<E> finalIt0 = it;
99 assertThrows(UnsupportedOperationException.class, () -> finalIt0.add(addValue),
100 "UnsupportedOperationException must be thrown from add of " + it.getClass().getSimpleName());
101 return;
102 }
103
104
105 it = makeObject();
106 it.add(addValue);
107 assertEquals(addValue, it.previous());
108
109
110 it = makeObject();
111 it.add(addValue);
112 assertNotSame(addValue, it.next());
113
114
115 it = makeObject();
116 while (it.hasNext()) {
117 it.next();
118 it.add(addValue);
119
120 assertEquals(addValue, it.previous());
121 it.next();
122 }
123 }
124
125
126
127
128 @Test
129 public void testAddThenRemove() {
130 final ListIterator<E> it = makeObject();
131
132
133 if (supportsAdd() && supportsRemove()) {
134 it.next();
135 it.add(addSetValue());
136 assertThrows(IllegalStateException.class, () -> it.remove(),
137 "IllegalStateException must be thrown from remove after add");
138 }
139 }
140
141 @Test
142 public void testAddThenSet() {
143 final ListIterator<E> it = makeObject();
144
145 if (supportsAdd() && supportsSet()) {
146 it.next();
147 it.add(addSetValue());
148 assertThrows(IllegalStateException.class, () -> it.set(addSetValue()),
149 "IllegalStateException must be thrown from set after add");
150 }
151 }
152
153
154
155
156 @Test
157 public void testEmptyListIteratorIsIndeedEmpty() {
158 if (!supportsEmptyIterator()) {
159 return;
160 }
161
162 final ListIterator<E> it = makeEmptyIterator();
163
164 assertFalse(it.hasNext());
165 assertEquals(0, it.nextIndex());
166 assertFalse(it.hasPrevious());
167 assertEquals(-1, it.previousIndex());
168
169
170 assertThrows(NoSuchElementException.class, () -> it.next(),
171 "NoSuchElementException must be thrown from empty ListIterator");
172
173
174 assertThrows(NoSuchElementException.class, () -> it.previous(),
175 "NoSuchElementException must be thrown from empty ListIterator");
176 }
177
178 @Test
179 public void testRemoveThenSet() {
180 final ListIterator<E> it = makeObject();
181 if (supportsRemove() && supportsSet()) {
182 it.next();
183 it.remove();
184 assertThrows(IllegalStateException.class, () -> it.set(addSetValue()),
185 "IllegalStateException must be thrown from set after remove");
186 }
187 }
188
189
190
191
192 @Test
193 public void testSet() {
194 final ListIterator<E> it = makeObject();
195
196 if (!supportsSet()) {
197
198 assertThrows(UnsupportedOperationException.class, () -> it.set(addSetValue()),
199 "UnsupportedOperationException must be thrown from set in " + it.getClass().getSimpleName());
200 return;
201 }
202
203
204 assertThrows(IllegalStateException.class, () -> it.set(addSetValue()));
205
206
207 it.next();
208 it.set(addSetValue());
209
210
211 it.set(addSetValue());
212
213 }
214
215
216
217
218 @Test
219 public void testWalkForwardAndBack() {
220 final ArrayList<E> list = new ArrayList<>();
221 final ListIterator<E> it = makeObject();
222 while (it.hasNext()) {
223 list.add(it.next());
224 }
225
226
227 assertFalse(it.hasNext());
228 assertTrue(it.hasPrevious());
229 assertThrows(NoSuchElementException.class, () -> it.next(),
230 "NoSuchElementException must be thrown from next at end of ListIterator");
231
232
233 for (int i = list.size() - 1; i >= 0; i--) {
234 assertEquals(i + 1, it.nextIndex());
235 assertEquals(i, it.previousIndex());
236
237 final Object obj = list.get(i);
238 assertEquals(obj, it.previous());
239 }
240
241
242 assertTrue(it.hasNext());
243 assertFalse(it.hasPrevious());
244 assertThrows(NoSuchElementException.class, () -> it.previous(),
245 "NoSuchElementException must be thrown from previous at start of ListIterator");
246 }
247
248 }