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.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.Collections;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.NoSuchElementException;
31  
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Tests the PeekingIterator.
37   *
38   * @param <E> the type of elements tested by this iterator.
39   */
40  public class PeekingIteratorTest<E> extends AbstractIteratorTest<E> {
41  
42      private final String[] testArray = { "a", "b", "c" };
43  
44      private List<E> testList;
45  
46      @Override
47      public Iterator<E> makeEmptyIterator() {
48          return PeekingIterator.peekingIterator(Collections.<E>emptyList().iterator());
49      }
50  
51      @Override
52      public PeekingIterator<E> makeObject() {
53          return PeekingIterator.peekingIterator(testList.iterator());
54      }
55  
56      @SuppressWarnings("unchecked")
57      @BeforeEach
58      protected void setUp() throws Exception {
59          testList = new ArrayList<>(Arrays.asList((E[]) testArray));
60      }
61  
62      @Override
63      public boolean supportsRemove() {
64          return true;
65      }
66  
67      @Test
68      public void testEmpty() {
69          final Iterator<E> it = makeEmptyIterator();
70          assertFalse(it.hasNext());
71      }
72  
73      @Test
74      public void testIllegalRemove() {
75          final PeekingIterator<E> it = makeObject();
76          it.next();
77          it.remove(); // supported
78  
79          assertTrue(it.hasNext());
80          assertEquals("b", it.peek());
81  
82          assertThrows(IllegalStateException.class, () -> it.remove());
83      }
84  
85      @Test
86      public void testIteratorExhausted() {
87          final PeekingIterator<E> it = makeObject();
88          it.next();
89          it.next();
90          it.next();
91          assertFalse(it.hasNext());
92          assertNull(it.peek());
93  
94          assertThrows(NoSuchElementException.class, () -> it.element());
95      }
96  
97      @Test
98      public void testMultiplePeek() {
99          final PeekingIterator<E> it = makeObject();
100         assertEquals("a", it.peek());
101         assertEquals("a", it.peek());
102         assertEquals("a", it.next());
103         assertTrue(it.hasNext());
104         assertEquals("b", it.peek());
105         assertEquals("b", it.peek());
106         assertEquals("b", it.next());
107         assertTrue(it.hasNext());
108         assertEquals("c", it.peek());
109         assertEquals("c", it.peek());
110         assertEquals("c", it.next());
111         assertFalse(it.hasNext());
112     }
113 
114     @Test
115     @SuppressWarnings("unchecked")
116     public void testSinglePeek() {
117         final PeekingIterator<E> it = makeObject();
118         assertEquals("a", it.peek());
119         assertEquals("a", it.element());
120         validate(it, (E[]) testArray);
121     }
122 
123     private void validate(final Iterator<E> iter, final E... items) {
124         for (final E x : items) {
125             assertTrue(iter.hasNext());
126             assertEquals(x, iter.next());
127         }
128         assertFalse(iter.hasNext());
129     }
130 
131 }