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.ListIterator;
25  import java.util.NoSuchElementException;
26  
27  import org.apache.commons.collections4.ResettableListIterator;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests the SingletonListIterator.
32   *
33   * @param <E> the type of elements tested by this iterator.
34   */
35  public class SingletonListIteratorTest<E> extends AbstractListIteratorTest<E> {
36  
37      private static final Object testValue = "foo";
38  
39      /**
40       * Returns a SingletonListIterator from which
41       * the element has already been removed.
42       */
43      @Override
44      public SingletonListIterator<E> makeEmptyIterator() {
45          final SingletonListIterator<E> iter = makeObject();
46          iter.next();
47          iter.remove();
48          iter.reset();
49          return iter;
50      }
51  
52      @Override
53      @SuppressWarnings("unchecked")
54      public SingletonListIterator<E> makeObject() {
55          return new SingletonListIterator<>((E) testValue);
56      }
57  
58      @Override
59      public boolean supportsAdd() {
60          return false;
61      }
62  
63      @Override
64      public boolean supportsEmptyIterator() {
65          return true;
66      }
67  
68      @Override
69      public boolean supportsRemove() {
70          return true;
71      }
72  
73      @Test
74      public void testIterator() {
75          final ListIterator<E> iter = makeObject();
76          assertTrue(iter.hasNext(), "Iterator should have next item");
77          assertFalse(iter.hasPrevious(), "Iterator should have no previous item");
78          assertEquals(0, iter.nextIndex(), "Iteration next index");
79          assertEquals(-1, iter.previousIndex(), "Iteration previous index");
80  
81          Object iterValue = iter.next();
82          assertEquals(testValue, iterValue, "Iteration value is correct");
83  
84          assertFalse(iter.hasNext(), "Iterator should have no next item");
85          assertTrue(iter.hasPrevious(), "Iterator should have previous item");
86          assertEquals(1, iter.nextIndex(), "Iteration next index");
87          assertEquals(0, iter.previousIndex(), "Iteration previous index");
88  
89          iterValue = iter.previous();
90          assertEquals(testValue, iterValue, "Iteration value is correct");
91  
92          assertTrue(iter.hasNext(), "Iterator should have next item");
93          assertFalse(iter.hasPrevious(), "Iterator should have no previous item");
94          assertEquals(0, iter.nextIndex(), "Iteration next index");
95          assertEquals(-1, iter.previousIndex(), "Iteration previous index");
96  
97          iterValue = iter.next();
98          assertEquals(testValue, iterValue, "Iteration value is correct");
99  
100         assertFalse(iter.hasNext(), "Iterator should have no next item");
101         assertTrue(iter.hasPrevious(), "Iterator should have previous item");
102         assertEquals(1, iter.nextIndex(), "Iteration next index");
103         assertEquals(0, iter.previousIndex(), "Iteration previous index");
104 
105         assertThrows(NoSuchElementException.class, iter::next);
106         iter.previous();
107         assertThrows(NoSuchElementException.class, iter::previous);
108     }
109 
110     @Test
111     public void testReset() {
112         final ResettableListIterator<E> it = makeObject();
113 
114         assertTrue(it.hasNext());
115         assertFalse(it.hasPrevious());
116         assertEquals(testValue, it.next());
117         assertFalse(it.hasNext());
118         assertTrue(it.hasPrevious());
119 
120         it.reset();
121 
122         assertTrue(it.hasNext());
123         assertFalse(it.hasPrevious());
124         assertEquals(testValue, it.next());
125         assertFalse(it.hasNext());
126         assertTrue(it.hasPrevious());
127 
128         it.reset();
129         it.reset();
130 
131         assertTrue(it.hasNext());
132     }
133 
134 }
135