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