View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.assertThrows;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.ListIterator;
26  import java.util.NoSuchElementException;
27  
28  import org.apache.commons.collections4.ResettableListIterator;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests the ListIteratorWrapper to ensure that it behaves as expected when wrapping a ListIterator.
34   *
35   * @param <E> the type of elements tested by this iterator.
36   */
37  public class ListIteratorWrapper2Test<E> extends AbstractIteratorTest<E> {
38  
39      protected String[] testArray = {
40          "One", "Two", "Three", "Four", "Five", "Six"
41      };
42  
43      protected List<E> list1;
44  
45      @Override
46      public ResettableListIterator<E> makeEmptyIterator() {
47          final ArrayList<E> list = new ArrayList<>();
48          return new ListIteratorWrapper<>(list.listIterator());
49      }
50  
51      @Override
52      public ResettableListIterator<E> makeObject() {
53          return new ListIteratorWrapper<>(list1.listIterator());
54      }
55  
56      @BeforeEach
57      @SuppressWarnings("unchecked")
58      public void setUp() {
59          list1 = new ArrayList<>();
60          list1.add((E) "One");
61          list1.add((E) "Two");
62          list1.add((E) "Three");
63          list1.add((E) "Four");
64          list1.add((E) "Five");
65          list1.add((E) "Six");
66      }
67  
68      @Test
69      public void testIterator() {
70          final ListIterator<E> iter = makeObject();
71          for (final String testValue : testArray) {
72              final Object iterValue = iter.next();
73              assertEquals(testValue, iterValue, "Iteration value is correct");
74          }
75          assertFalse(iter.hasNext(), "Iterator should now be empty");
76          assertThrows(NoSuchElementException.class, iter::next);
77          // now, read it backwards
78          for (int i = testArray.length - 1; i > -1; --i) {
79              final Object testValue = testArray[i];
80              final E iterValue = iter.previous();
81              assertEquals(testValue, iterValue, "Iteration value is correct");
82          }
83          assertThrows(NoSuchElementException.class, iter::previous);
84          // now, read it forwards again
85          for (final String testValue : testArray) {
86              final Object iterValue = iter.next();
87              assertEquals(testValue, iterValue, "Iteration value is correct");
88          }
89      }
90  
91      @Test
92      @Override
93      public void testRemove() {
94          final ListIterator<E> iter = makeObject();
95  
96          //initial state:
97          assertEquals(-1, iter.previousIndex());
98          assertEquals(0, iter.nextIndex());
99  
100         assertThrows(IllegalStateException.class, () -> iter.remove(), "ListIteratorWrapper#remove() should fail; must be initially positioned first");
101 
102         //no change from invalid op:
103         assertEquals(-1, iter.previousIndex());
104         assertEquals(0, iter.nextIndex());
105 
106         //establish size:
107         int sz = list1.size();
108 
109         //verify initial next() call:
110         assertEquals(list1.get(0), iter.next());
111         assertEquals(0, iter.previousIndex());
112         assertEquals(1, iter.nextIndex());
113 
114         //verify remove():
115         iter.remove();
116         assertEquals(--sz, list1.size());
117         //like we never started iterating:
118         assertEquals(-1, iter.previousIndex());
119         assertEquals(0, iter.nextIndex());
120 
121         assertThrows(IllegalStateException.class, () -> iter.remove(), "ListIteratorWrapper#remove() should fail; must be repositioned first");
122 
123         //no change from invalid op:
124         assertEquals(-1, iter.previousIndex());
125         assertEquals(0, iter.nextIndex());
126 
127         //two consecutive next() calls:
128         assertEquals(list1.get(0), iter.next());
129         assertEquals(0, iter.previousIndex());
130         assertEquals(1, iter.nextIndex());
131 
132         assertEquals(list1.get(1), iter.next());
133         assertEquals(1, iter.previousIndex());
134         assertEquals(2, iter.nextIndex());
135 
136         //call previous():
137         assertEquals(list1.get(1), iter.previous());
138         assertEquals(0, iter.previousIndex());
139         assertEquals(1, iter.nextIndex());
140 
141         //should support remove() after calling previous() once from tip because we haven't changed the underlying iterator's position:
142         iter.remove();
143         assertEquals(--sz, list1.size());
144         assertEquals(0, iter.previousIndex());
145         assertEquals(1, iter.nextIndex());
146 
147         //this would dig into cache on a plain Iterator, but forwards directly to wrapped ListIterator:
148         assertEquals(list1.get(0), iter.previous());
149         assertEquals(-1, iter.previousIndex());
150         assertEquals(0, iter.nextIndex());
151 
152         //here's the proof; remove() still works:
153         iter.remove();
154         assertEquals(--sz, list1.size());
155         assertEquals(-1, iter.previousIndex());
156         assertEquals(0, iter.nextIndex());
157 
158         //further testing would be fairly meaningless:
159     }
160 
161     @Test
162     public void testReset() {
163         final ResettableListIterator<E> iter = makeObject();
164         final E first = iter.next();
165         final E second = iter.next();
166 
167         iter.reset();
168 
169         // after reset, there shouldn't be any previous elements
170         assertFalse(iter.hasPrevious(), "No previous elements after reset()");
171 
172         // after reset, the results should be the same as before
173         assertEquals(first, iter.next(), "First element should be the same");
174         assertEquals(second, iter.next(), "Second element should be the same");
175 
176         // after passing the point, where we reset, continuation should work as expected
177         for (int i = 2; i < testArray.length; i++) {
178             final Object testValue = testArray[i];
179             final E iterValue = iter.next();
180 
181             assertEquals(testValue, iterValue, "Iteration value is correct");
182         }
183     }
184 
185 }