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