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