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