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.assertAll;
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  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.List;
28  import java.util.ListIterator;
29  import java.util.NoSuchElementException;
30  
31  import org.apache.commons.collections4.ResettableListIterator;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests the ReverseListIterator.
36   */
37  public class ReverseListIteratorTest<E> extends AbstractListIteratorTest<E> {
38  
39      protected String[] testArray = { "One", "Two", "Three", "Four" };
40  
41      public ReverseListIteratorTest() {
42          super(ReverseListIteratorTest.class.getSimpleName());
43      }
44  
45      @Override
46      public ListIterator<E> makeEmptyIterator() {
47          return new ReverseListIterator<>(new ArrayList<>());
48      }
49  
50      @Override
51      @SuppressWarnings("unchecked")
52      public ReverseListIterator<E> makeObject() {
53          final List<E> list = new ArrayList<>(Arrays.asList((E[]) testArray));
54          return new ReverseListIterator<>(list);
55      }
56  
57      // overrides
58      @Test
59      @Override
60      public void testEmptyListIteratorIsIndeedEmpty() {
61          final ListIterator<E> it = makeEmptyIterator();
62  
63          assertFalse(it.hasNext());
64          assertEquals(-1, it.nextIndex());  // reversed index
65          assertFalse(it.hasPrevious());
66          assertEquals(0, it.previousIndex());  // reversed index
67  
68          assertAll(
69                  // next() should throw a NoSuchElementException
70                  () -> assertThrows(NoSuchElementException.class, () -> it.next(),
71                          "NoSuchElementException must be thrown from empty ListIterator"),
72                  // previous() should throw a NoSuchElementException
73                  () -> assertThrows(NoSuchElementException.class, () -> it.previous(),
74                          "NoSuchElementException must be thrown from empty ListIterator")
75          );
76      }
77  
78      @Test
79      public void testReset() {
80          final ResettableListIterator<E> it = makeObject();
81          assertEquals("Four", it.next());
82          it.reset();
83          assertEquals("Four", it.next());
84          it.next();
85          it.next();
86          it.reset();
87          assertEquals("Four", it.next());
88      }
89  
90      @Test
91      public void testReverse() {
92          final ListIterator<E> it = makeObject();
93          assertTrue(it.hasNext());
94          assertEquals(3, it.nextIndex());
95          assertFalse(it.hasPrevious());
96          assertEquals(4, it.previousIndex());
97          assertEquals("Four", it.next());
98          assertEquals(2, it.nextIndex());
99          assertTrue(it.hasNext());
100         assertEquals(3, it.previousIndex());
101         assertTrue(it.hasPrevious());
102         assertEquals("Three", it.next());
103         assertTrue(it.hasNext());
104         assertEquals(1, it.nextIndex());
105         assertTrue(it.hasPrevious());
106         assertEquals(2, it.previousIndex());
107         assertEquals("Two", it.next());
108         assertTrue(it.hasNext());
109         assertEquals(0, it.nextIndex());
110         assertTrue(it.hasPrevious());
111         assertEquals(1, it.previousIndex());
112         assertEquals("One", it.next());
113         assertFalse(it.hasNext());
114         assertEquals(-1, it.nextIndex());
115         assertTrue(it.hasPrevious());
116         assertEquals(0, it.previousIndex());
117         assertEquals("One", it.previous());
118         assertEquals("Two", it.previous());
119         assertEquals("Three", it.previous());
120         assertEquals("Four", it.previous());
121     }
122 
123     @Test
124     @Override
125     public void testWalkForwardAndBack() {
126         final ArrayList<E> list = new ArrayList<>();
127         final ListIterator<E> it = makeObject();
128         while (it.hasNext()) {
129             list.add(it.next());
130         }
131 
132         // check state at end
133         assertFalse(it.hasNext());
134         assertTrue(it.hasPrevious());
135 
136         // this had to be commented out, as there is a bug in the JDK before JDK1.5
137         // where calling previous at the start of an iterator would push the cursor
138         // back to an invalid negative value
139 //        try {
140 //            it.next();
141 //            fail("NoSuchElementException must be thrown from next at end of ListIterator");
142 //        } catch (NoSuchElementException e) {
143 //        }
144 
145         // loop back through comparing
146         for (int i = list.size() - 1; i >= 0; i--) {
147             assertEquals(list.size() - i - 2, it.nextIndex(), "" + i);  // reversed index
148             assertEquals(list.size() - i - 1, it.previousIndex());  // reversed index
149 
150             final Object obj = list.get(i);
151             assertEquals(obj, it.previous());
152         }
153 
154         // check state at start
155         assertTrue(it.hasNext());
156         assertFalse(it.hasPrevious());
157 
158         assertThrows(NoSuchElementException.class, () -> it.previous(),
159                 "NoSuchElementException must be thrown from previous at start of ListIterator");
160     }
161 
162 }