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.functor.core.collection;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import java.math.BigInteger;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Collections;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.NoSuchElementException;
32  
33  import org.apache.commons.functor.BaseFunctorTest;
34  import org.apache.commons.functor.UnaryPredicate;
35  import org.apache.commons.functor.core.Constant;
36  import org.apache.commons.functor.core.IsEqual;
37  import org.junit.After;
38  import org.junit.Before;
39  import org.junit.Test;
40  
41  /**
42   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
43   */
44  @SuppressWarnings("unchecked")
45  public class TestFilteredIterable extends BaseFunctorTest {
46  
47      // Attributes
48      // ------------------------------------------------------------------------
49      private List<Integer> list = null;
50      private List<Integer> evens = null;
51  
52      private UnaryPredicate<Integer> isEven = new UnaryPredicate<Integer>() {
53          public boolean test(Integer obj) {
54              return obj != null && obj % 2 == 0;
55          }
56      };
57  
58      public Object makeFunctor() {
59          List<String> list = new ArrayList<String>();
60          list.add("xyzzy");
61          return FilteredIterable.of(list);
62      }
63  
64      // Lifecycle
65      // ------------------------------------------------------------------------
66  
67      @Before
68      public void setUp() throws Exception {
69          list = new ArrayList<Integer>();
70          evens = new ArrayList<Integer>();
71          for (int i = 0; i < 10; i++) {
72              list.add(i);
73              if (i % 2 == 0) {
74                  evens.add(i);
75              }
76          }
77      }
78  
79      @After
80      public void tearDown() throws Exception {
81          list = null;
82          evens = null;
83      }
84  
85      // Tests
86      // ------------------------------------------------------------------------
87  
88      @Test
89      public void testSomePass() {
90          Iterator<Integer> expected = evens.iterator();
91  
92          for (Integer i : FilteredIterable.of(list).retain(isEven)) {
93              assertTrue(expected.hasNext());
94              assertEquals(expected.next(), i);
95          }
96          assertFalse(expected.hasNext());
97      }
98  
99      @Test
100     public void testAllPass() {
101         Iterator<Integer> expected = evens.iterator();
102 
103         for (Integer i : FilteredIterable.of(evens)) {
104             assertTrue(expected.hasNext());
105             assertEquals(expected.next(), i);
106         }
107         assertFalse(expected.hasNext());
108     }
109 
110     @Test
111     public void testAllPass2() {
112         Iterator<Integer> expected = list.iterator();
113         for (Integer i : FilteredIterable.of(list)) {
114             assertTrue(expected.hasNext());
115             assertEquals(expected.next(), i);
116         }
117         assertFalse(expected.hasNext());
118     }
119 
120     @Test
121     public void testEmptyFilteredIterable() {
122         assertFalse(FilteredIterable.empty().iterator().hasNext());
123     }
124 
125     @Test
126     public void testEmptyList() {
127         assertFalse(FilteredIterable.of(Collections.EMPTY_LIST).iterator().hasNext());
128     }
129 
130     @Test
131     public void testNonePass() {
132         assertFalse(FilteredIterable.of(list).retain(Constant.falsePredicate()).iterator().hasNext());
133     }
134 
135     @Test
136     public void testNextWithoutHasNext() {
137         Iterator<Integer> testing = FilteredIterable.of(list).retain(isEven).iterator();
138         Iterator<Integer> expected = evens.iterator();
139         while (expected.hasNext()) {
140             assertEquals(expected.next(), testing.next());
141         }
142         assertFalse(testing.hasNext());
143     }
144 
145     @Test
146     public void testNextAfterEndOfList() {
147         Iterator<Integer> testing = FilteredIterable.of(list).retain(isEven).iterator();
148         Iterator<Integer> expected = evens.iterator();
149         while (expected.hasNext()) {
150             assertEquals(expected.next(), testing.next());
151         }
152         try {
153             testing.next();
154             fail("Expected NoSuchElementException");
155         } catch (NoSuchElementException e) {
156             // expected
157         }
158     }
159 
160     @Test
161     public void testNextOnEmptyList() {
162         try {
163             FilteredIterable.empty().iterator().next();
164             fail("Expected NoSuchElementException");
165         } catch (NoSuchElementException e) {
166             // expected
167         }
168     }
169 
170     @Test
171     public void testRemoveBeforeNext() {
172         Iterator<Integer> testing = FilteredIterable.of(list).retain(isEven).iterator();
173         try {
174             testing.remove();
175             fail("Expected IllegalStateException");
176         } catch (IllegalStateException e) {
177             // expected
178         }
179     }
180 
181     @Test
182     public void testRemoveAfterNext() {
183         Iterator<Integer> testing = FilteredIterable.of(list).retain(isEven).iterator();
184         testing.next();
185         testing.remove();
186         try {
187             testing.remove();
188             fail("Expected IllegalStateException");
189         } catch (IllegalStateException e) {
190             // expected
191         }
192     }
193 
194     @Test
195     public void testRemoveSome() {
196         Iterator<Integer> testing = FilteredIterable.of(list).retain(isEven).iterator();
197         while (testing.hasNext()) {
198             testing.next();
199             testing.remove();
200         }
201         assertTrue(Collections.disjoint(list, evens));
202     }
203 
204     @Test
205     public void testRemoveAll() {
206         Iterator<Integer> testing = FilteredIterable.of(list).iterator();
207         while (testing.hasNext()) {
208             testing.next();
209             testing.remove();
210         }
211         assertTrue(list.isEmpty());
212     }
213 
214     @Test
215     public void testRemoveWithoutHasNext() {
216         Iterator<Integer> testing = FilteredIterable.of(list).iterator();
217         for (int i = 0, m = list.size(); i < m; i++) {
218             testing.next();
219             testing.remove();
220         }
221         assertTrue(list.isEmpty());
222     }
223 
224     @Test
225     public void testFilterWithNullIteratorReturnsNull() {
226         assertNull(FilteredIterable.of(null));
227     }
228 
229     @Test
230     public void testRetainOneType() {
231         Iterable<Object> objects = Arrays.asList((Object) "foo", "bar", "baz", 2L, BigInteger.ZERO);
232         Iterable<String> strings = FilteredIterable.of(objects).retain(String.class);
233         for (String s : strings) {
234             assertTrue(s instanceof String);
235         }
236     }
237 
238     @Test
239     public void testRetainOneType2() {
240         Iterable<Object> objects = Arrays.asList((Object) "foo", "bar", "baz", 2L, BigInteger.ZERO);
241         Iterator<Number> iterator = FilteredIterable.of(objects).retain(Number.class).iterator();
242         assertEquals(2L, iterator.next());
243         assertEquals(BigInteger.ZERO, iterator.next());
244         assertFalse(iterator.hasNext());
245     }
246 
247     @Test
248     public void testRetainMultipleTypes() {
249         Iterable<Object> objects = Arrays.asList((Object) "foo", "bar", "baz", 2L, BigInteger.ZERO);
250         Iterator<Object> iterator = FilteredIterable.of(objects).retain(Long.class, BigInteger.class).iterator();
251         assertEquals(2L, iterator.next());
252         assertEquals(BigInteger.ZERO, iterator.next());
253         assertFalse(iterator.hasNext());
254     }
255 
256     @Test
257     public void testMultipleLevels() {
258         Iterable<Object> objects = Arrays.asList((Object) "foo", "bar", "baz", 2L, BigInteger.ZERO);
259         Iterator<String> iterator = FilteredIterable.of(objects).retain(String.class).retain(IsEqual.to("foo"))
260                 .iterator();
261         assertEquals("foo", iterator.next());
262         assertFalse(iterator.hasNext());
263     }
264 
265     @Test
266     public void testMultipleLevels2() {
267         Iterable<Object> objects = Arrays.asList((Object) "foo", "bar", "baz", 2L, BigInteger.ZERO);
268         Iterator<Long> iterator = FilteredIterable.of(objects).retain(Number.class).retain(Long.class).iterator();
269         assertEquals(2L, iterator.next().longValue());
270         assertFalse(iterator.hasNext());
271     }
272 
273     @Test
274     public void testRetainNullType() {
275         try {
276             FilteredIterable.of(Collections.singleton("foo")).retain((Class<?>) null);
277             fail("Expected NullPointerException");
278         } catch (NullPointerException e) {
279             // okay
280         }
281     }
282 
283     @Test
284     public void testRetainNullTypes() {
285         try {
286             FilteredIterable.of(Collections.singleton("foo")).retain((Class<?>[]) null);
287             fail("Expected NullPointerException");
288         } catch (NullPointerException e) {
289             // okay
290         }
291     }
292 
293     @Test
294     public void testRetainNullPredicate() {
295         try {
296             FilteredIterable.of(Collections.singleton("foo")).retain((UnaryPredicate<String>) null);
297             fail("Expected NullPointerException");
298         } catch (NullPointerException e) {
299             // okay
300         }
301     }
302 }