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.assertAll;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
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.Iterator;
27  import java.util.List;
28  import java.util.NoSuchElementException;
29  
30  import org.apache.commons.collections4.IteratorUtils;
31  import org.apache.commons.collections4.Predicate;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Tests the IteratorChain class.
37   */
38  public class IteratorChainTest extends AbstractIteratorTest<String> {
39  
40      protected String[] testArray = {
41          "One", "Two", "Three", "Four", "Five", "Six"
42      };
43  
44      protected List<String> list1;
45      protected List<String> list2;
46      protected List<String> list3;
47  
48      public IteratorChainTest() {
49          super(IteratorChainTest.class.getSimpleName());
50      }
51  
52      @Override
53      public IteratorChain<String> makeEmptyIterator() {
54          final ArrayList<String> list = new ArrayList<>();
55          return new IteratorChain<>(list.iterator());
56      }
57  
58      @Override
59      public IteratorChain<String> makeObject() {
60          final IteratorChain<String> chain = new IteratorChain<>();
61  
62          chain.addIterator(list1.iterator());
63          chain.addIterator(list2.iterator());
64          chain.addIterator(list3.iterator());
65          return chain;
66      }
67  
68      @BeforeEach
69      public void setUp() {
70          list1 = new ArrayList<>();
71          list1.add("One");
72          list1.add("Two");
73          list1.add("Three");
74          list2 = new ArrayList<>();
75          list2.add("Four");
76          list3 = new ArrayList<>();
77          list3.add("Five");
78          list3.add("Six");
79      }
80  
81      @Test
82      public void testEmptyChain() {
83          final IteratorChain<Object> chain = new IteratorChain<>();
84          assertFalse(chain.hasNext());
85          assertAll(
86                  () -> assertThrows(NoSuchElementException.class, () -> chain.next()),
87                  () -> assertThrows(IllegalStateException.class, () -> chain.remove())
88          );
89      }
90  
91      @Test
92      public void testFirstIteratorIsEmptyBug() {
93          final List<String> empty = new ArrayList<>();
94          final List<String> notEmpty = new ArrayList<>();
95          notEmpty.add("A");
96          notEmpty.add("B");
97          notEmpty.add("C");
98          final IteratorChain<String> chain = new IteratorChain<>();
99          chain.addIterator(empty.iterator());
100         chain.addIterator(notEmpty.iterator());
101         assertTrue(chain.hasNext(), "should have next");
102         assertEquals("A", chain.next());
103         assertTrue(chain.hasNext(), "should have next");
104         assertEquals("B", chain.next());
105         assertTrue(chain.hasNext(), "should have next");
106         assertEquals("C", chain.next());
107         assertFalse(chain.hasNext(), "should not have next");
108     }
109 
110     @Test
111     public void testIterator() {
112         final Iterator<String> iter = makeObject();
113         for (final String testValue : testArray) {
114             final Object iterValue = iter.next();
115 
116             assertEquals(testValue, iterValue, "Iteration value is correct");
117         }
118 
119         assertFalse(iter.hasNext(), "Iterator should now be empty");
120 
121         try {
122             iter.next();
123         } catch (final Exception e) {
124             assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
125         }
126     }
127 
128     @Test
129     @Override
130     public void testRemove() {
131         final Iterator<String> iter = makeObject();
132 
133         assertThrows(IllegalStateException.class, () -> iter.remove(),
134                 "Calling remove before the first call to next() should throw an exception");
135 
136         for (final String testValue : testArray) {
137             final String iterValue = iter.next();
138 
139             assertEquals(testValue, iterValue, "Iteration value is correct");
140 
141             if (!iterValue.equals("Four")) {
142                 iter.remove();
143             }
144         }
145 
146         assertTrue(list1.isEmpty(), "List is empty");
147         assertEquals(1, list2.size(), "List is empty");
148         assertTrue(list3.isEmpty(), "List is empty");
149     }
150 
151     @Test
152     public void testRemoveFromFilteredIterator() {
153 
154         final Predicate<Integer> myPredicate = i -> i.compareTo(Integer.valueOf(4)) < 0;
155 
156         final List<Integer> list1 = new ArrayList<>();
157         final List<Integer> list2 = new ArrayList<>();
158 
159         list1.add(Integer.valueOf(1));
160         list1.add(Integer.valueOf(2));
161         list2.add(Integer.valueOf(3));
162         list2.add(Integer.valueOf(4)); // will be ignored by the predicate
163 
164         final Iterator<Integer> it1 = IteratorUtils.filteredIterator(list1.iterator(), myPredicate);
165         final Iterator<Integer> it2 = IteratorUtils.filteredIterator(list2.iterator(), myPredicate);
166 
167         final Iterator<Integer> it = IteratorUtils.chainedIterator(it1, it2);
168         while (it.hasNext()) {
169             it.next();
170             it.remove();
171         }
172         assertEquals(0, list1.size());
173         assertEquals(1, list2.size());
174     }
175 
176 }