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 java.util.Iterator;
20  import java.util.NoSuchElementException;
21  
22  import org.apache.commons.collections4.ResettableIterator;
23  
24  /**
25   * Tests the SingletonIterator to ensure that the next() method will actually
26   * perform the iteration rather than the hasNext() method.
27   *
28   */
29  public class SingletonIteratorTest<E> extends AbstractIteratorTest<E> {
30  
31      private static final Object testValue = "foo";
32  
33      public SingletonIteratorTest(final String testName) {
34          super(testName);
35      }
36  
37      /**
38       * Returns a SingletonIterator from which
39       * the element has already been removed.
40       */
41      @Override
42      public SingletonIterator<E> makeEmptyIterator() {
43          final SingletonIterator<E> iter = makeObject();
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);
54      }
55  
56      @Override
57      public boolean supportsRemove() {
58          return true;
59      }
60  
61      @Override
62      public boolean supportsEmptyIterator() {
63          return true;
64      }
65  
66      public void testIterator() {
67          final Iterator<E> iter = makeObject();
68          assertTrue("Iterator has a first item", iter.hasNext());
69  
70          final E iterValue = iter.next();
71          assertEquals("Iteration value is correct", testValue, iterValue);
72  
73          assertTrue("Iterator should now be empty", !iter.hasNext());
74  
75          try {
76              iter.next();
77          } catch (final Exception e) {
78              assertTrue(
79                  "NoSuchElementException must be thrown",
80                  e.getClass().equals(new NoSuchElementException().getClass()));
81          }
82      }
83  
84      @SuppressWarnings("unchecked")
85      public void testSingletonIteratorRemove() {
86          final ResettableIterator<E> iter = new SingletonIterator<>((E) "xyzzy");
87          assertTrue(iter.hasNext());
88          assertEquals("xyzzy",iter.next());
89          iter.remove();
90          iter.reset();
91          assertTrue(! iter.hasNext());
92      }
93  
94      public void testReset() {
95          final ResettableIterator<E> it = makeObject();
96  
97          assertEquals(true, it.hasNext());
98          assertEquals(testValue, it.next());
99          assertEquals(false, it.hasNext());
100 
101         it.reset();
102 
103         assertEquals(true, it.hasNext());
104         assertEquals(testValue, it.next());
105         assertEquals(false, it.hasNext());
106 
107         it.reset();
108         it.reset();
109 
110         assertEquals(true, it.hasNext());
111     }
112 
113 }