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  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.NoSuchElementException;
26  
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests the UniqueFilterIterator class.
32   */
33  public class UniqueFilterIteratorTest<E> extends AbstractIteratorTest<E> {
34  
35      protected String[] testArray = {
36          "One", "Two", "Three", "Four", "Five", "Six"
37      };
38  
39      protected List<E> list1;
40  
41      public UniqueFilterIteratorTest() {
42          super(UniqueFilterIteratorTest.class.getSimpleName());
43      }
44  
45      @Override
46      public UniqueFilterIterator<E> makeEmptyIterator() {
47          final ArrayList<E> list = new ArrayList<>();
48          return new UniqueFilterIterator<>(list.iterator());
49      }
50  
51      @Override
52      public UniqueFilterIterator<E> makeObject() {
53          final Iterator<E> i = list1.iterator();
54          return new UniqueFilterIterator<>(i);
55      }
56  
57      @BeforeEach
58      @SuppressWarnings("unchecked")
59      public void setUp() {
60          list1 = new ArrayList<>();
61          list1.add((E) "One");
62          list1.add((E) "Two");
63          list1.add((E) "Three");
64          list1.add((E) "Two");
65          list1.add((E) "One");
66          list1.add((E) "Four");
67          list1.add((E) "Five");
68          list1.add((E) "Five");
69          list1.add((E) "Six");
70          list1.add((E) "Five");
71      }
72  
73      @Test
74      public void testIterator() {
75          final Iterator<E> iter = makeObject();
76          for (final String testValue : testArray) {
77              final E iterValue = iter.next();
78  
79              assertEquals(testValue, iterValue, "Iteration value is correct");
80          }
81  
82          assertFalse(iter.hasNext(), "Iterator should now be empty");
83  
84          try {
85              iter.next();
86          } catch (final Exception e) {
87              assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
88          }
89      }
90  
91  }
92