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 SingletonIterator2Test<E> extends AbstractIteratorTest<E> {
37  
38      private static final Object testValue = "foo";
39  
40      @Override
41      @SuppressWarnings("unchecked")
42      public SingletonIterator<E> makeEmptyIterator() {
43          final SingletonIterator<E> iter = new SingletonIterator<>((E) testValue);
44          iter.next();
45          iter.remove();
46          iter.reset();
47          return iter;
48      }
49  
50      @Override
51      @SuppressWarnings("unchecked")
52      public SingletonIterator<E> makeObject() {
53          return new SingletonIterator<>((E) testValue, false);
54      }
55  
56      @Override
57      public boolean supportsEmptyIterator() {
58          return false;
59      }
60  
61      @Override
62      public boolean supportsRemove() {
63          return false;
64      }
65  
66      @Test
67      public void testIterator() {
68          final Iterator<E> iter = makeObject();
69          assertTrue(iter.hasNext(), "Iterator has a first item");
70          final E iterValue = iter.next();
71          assertEquals(testValue, iterValue, "Iteration value is correct");
72          assertFalse(iter.hasNext(), "Iterator should now be empty");
73          assertThrows(NoSuchElementException.class, iter::next);
74      }
75  
76      @Test
77      public void testReset() {
78          final ResettableIterator<E> it = makeObject();
79  
80          assertTrue(it.hasNext());
81          assertEquals(testValue, it.next());
82          assertFalse(it.hasNext());
83  
84          it.reset();
85  
86          assertTrue(it.hasNext());
87          assertEquals(testValue, it.next());
88          assertFalse(it.hasNext());
89  
90          it.reset();
91          it.reset();
92  
93          assertTrue(it.hasNext());
94      }
95  
96  }