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