View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.commons.collections4.iterators;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.NoSuchElementException;
27  
28  import org.junit.Test;
29  
30  /**
31   * Tests the PeekingIterator.
32   */
33  public class PeekingIteratorTest<E> extends AbstractIteratorTest<E> {
34  
35      private final String[] testArray = { "a", "b", "c" };
36  
37      private List<E> testList;
38  
39      public PeekingIteratorTest(final String testName) {
40          super(testName);
41      }
42  
43      /**
44       * {@inheritDoc}
45       */
46      @SuppressWarnings("unchecked")
47      @Override
48      protected void setUp() throws Exception {
49          super.setUp();
50          testList = new ArrayList<>(Arrays.asList((E[]) testArray));
51      }
52  
53      @Override
54      public Iterator<E> makeEmptyIterator() {
55          return PeekingIterator.peekingIterator(Collections.<E>emptyList().iterator());
56      }
57  
58      @Override
59      public PeekingIterator<E> makeObject() {
60          return PeekingIterator.peekingIterator(testList.iterator());
61      }
62  
63      @Override
64      public boolean supportsRemove() {
65          return true;
66      }
67  
68      //-----------------------------------------------------------------------
69  
70      @Test
71      public void testEmpty() {
72          final Iterator<E> it = makeEmptyIterator();
73          assertFalse(it.hasNext());
74      }
75  
76      @Test
77      @SuppressWarnings("unchecked")
78      public void testSinglePeek() {
79          final PeekingIterator<E> it = makeObject();
80          assertEquals("a", it.peek());
81          assertEquals("a", it.element());
82          validate(it, (E[]) testArray);
83      }
84  
85      @Test
86      public void testMultiplePeek() {
87          final PeekingIterator<E> it = makeObject();
88          assertEquals("a", it.peek());
89          assertEquals("a", it.peek());
90          assertEquals("a", it.next());
91          assertTrue(it.hasNext());
92          assertEquals("b", it.peek());
93          assertEquals("b", it.peek());
94          assertEquals("b", it.next());
95          assertTrue(it.hasNext());
96          assertEquals("c", it.peek());
97          assertEquals("c", it.peek());
98          assertEquals("c", it.next());
99          assertFalse(it.hasNext());
100     }
101 
102     @Test
103     public void testIteratorExhausted() {
104         final PeekingIterator<E> it = makeObject();
105         it.next();
106         it.next();
107         it.next();
108         assertFalse(it.hasNext());
109         assertNull(it.peek());
110 
111         try {
112             it.element();
113             fail();
114         } catch (final NoSuchElementException e) {
115             // expected
116         }
117     }
118 
119     @Test
120     public void testIllegalRemove() {
121         final PeekingIterator<E> it = makeObject();
122         it.next();
123         it.remove(); // supported
124 
125         assertTrue(it.hasNext());
126         assertEquals("b", it.peek());
127 
128         try {
129             it.remove();
130             fail();
131         } catch (final IllegalStateException e) {
132             // expected
133         }
134     }
135 
136     private void validate(final Iterator<E> iter, final E... items) {
137         for (final E x : items) {
138             assertTrue(iter.hasNext());
139             assertEquals(x, iter.next());
140         }
141         assertFalse(iter.hasNext());
142     }
143 
144 }