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.assertTrue;
22  
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests the PushbackIterator.
34   *
35   * @param <E> the type of elements tested by this iterator.
36   */
37  public class PushbackIteratorTest<E> extends AbstractIteratorTest<E> {
38  
39      private final String[] testArray = { "a", "b", "c" };
40  
41      private List<E> testList;
42  
43      @Override
44      public Iterator<E> makeEmptyIterator() {
45          return PushbackIterator.pushbackIterator(Collections.<E>emptyList().iterator());
46      }
47  
48      @Override
49      public PushbackIterator<E> makeObject() {
50          return PushbackIterator.pushbackIterator(testList.iterator());
51      }
52  
53      @SuppressWarnings("unchecked")
54      @BeforeEach
55      protected void setUp() throws Exception {
56          testList = new ArrayList<>(Arrays.asList((E[]) testArray));
57      }
58  
59      @Override
60      public boolean supportsRemove() {
61          return false;
62      }
63  
64      @Test
65      @SuppressWarnings("unchecked")
66      public void testDelayedPushback() {
67          final PushbackIterator<E> iter = makeObject();
68          assertEquals("a", iter.next());
69          iter.pushback((E) "x");
70          assertEquals("x", iter.next());
71          assertEquals("b", iter.next());
72          validate(iter, "c");
73      }
74  
75      @Test
76      @SuppressWarnings("unchecked")
77      public void testImmediatePushback() {
78          final PushbackIterator<E> iter = makeObject();
79          iter.pushback((E) "x");
80          assertEquals("x", iter.next());
81          assertEquals("a", iter.next());
82          validate(iter, "b", "c");
83      }
84  
85      @Test
86      @SuppressWarnings("unchecked")
87      public void testMultiplePushback() {
88          final PushbackIterator<E> iter = makeObject();
89          assertEquals("a", iter.next());
90          iter.pushback((E) "x");
91          iter.pushback((E) "y");
92          assertEquals("y", iter.next());
93          assertEquals("x", iter.next());
94          assertEquals("b", iter.next());
95          validate(iter, "c");
96      }
97  
98      @Test
99      public void testNormalIteration() {
100         final PushbackIterator<E> iter = makeObject();
101         assertEquals("a", iter.next());
102         assertEquals("b", iter.next());
103         assertEquals("c", iter.next());
104         assertFalse(iter.hasNext());
105     }
106 
107     private void validate(final Iterator<E> iter, final Object... items) {
108         for (final Object x : items) {
109             assertTrue(iter.hasNext());
110             assertEquals(x, iter.next());
111         }
112         assertFalse(iter.hasNext());
113     }
114 
115 }