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.Iterator;
25  import java.util.NoSuchElementException;
26  
27  import org.apache.commons.collections4.ResettableIterator;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests the SingletonIterator to ensure that the next() method will actually
32   * perform the iteration rather than the hasNext() method.
33   *
34   * @param <E> the type of elements tested by this iterator.
35   */
36  public class SingletonIteratorTest<E> extends AbstractIteratorTest<E> {
37  
38      private static final Object testValue = "foo";
39  
40      /**
41       * Returns a SingletonIterator from which
42       * the element has already been removed.
43       */
44      @Override
45      public SingletonIterator<E> makeEmptyIterator() {
46          final SingletonIterator<E> iter = makeObject();
47          iter.next();
48          iter.remove();
49          iter.reset();
50          return iter;
51      }
52  
53      @Override
54      @SuppressWarnings("unchecked")
55      public SingletonIterator<E> makeObject() {
56          return new SingletonIterator<>((E) testValue);
57      }
58  
59      @Override
60      public boolean supportsEmptyIterator() {
61          return true;
62      }
63  
64      @Override
65      public boolean supportsRemove() {
66          return true;
67      }
68  
69      @Test
70      public void testIterator() {
71          final Iterator<E> iter = makeObject();
72          assertTrue(iter.hasNext(), "Iterator has a first item");
73          final E iterValue = iter.next();
74          assertEquals(testValue, iterValue, "Iteration value is correct");
75          assertFalse(iter.hasNext(), "Iterator should now be empty");
76          assertThrows(NoSuchElementException.class, iter::next);
77      }
78  
79      @Test
80      public void testReset() {
81          final ResettableIterator<E> it = makeObject();
82  
83          assertTrue(it.hasNext());
84          assertEquals(testValue, it.next());
85          assertFalse(it.hasNext());
86  
87          it.reset();
88  
89          assertTrue(it.hasNext());
90          assertEquals(testValue, it.next());
91          assertFalse(it.hasNext());
92  
93          it.reset();
94          it.reset();
95  
96          assertTrue(it.hasNext());
97      }
98  
99      @Test
100     @SuppressWarnings("unchecked")
101     public void testSingletonIteratorRemove() {
102         final ResettableIterator<E> iter = new SingletonIterator<>((E) "xyzzy");
103         assertTrue(iter.hasNext());
104         assertEquals("xyzzy", iter.next());
105         iter.remove();
106         iter.reset();
107         assertFalse(iter.hasNext());
108     }
109 
110 }