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.assertNotSame;
20  import static org.junit.jupiter.api.Assertions.assertSame;
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.Arrays;
26  import java.util.Collections;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  import org.apache.commons.collections4.Unmodifiable;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests the UnmodifiableIterator.
36   *
37   * @param <E> the type of elements tested by this iterator.
38   */
39  public class UnmodifiableIteratorTest<E> extends AbstractIteratorTest<E> {
40  
41      protected String[] testArray = { "One", "Two", "Three" };
42      protected List<E> testList;
43  
44      @Override
45      public Iterator<E> makeEmptyIterator() {
46          return UnmodifiableIterator.unmodifiableIterator(Collections.<E>emptyList().iterator());
47      }
48  
49      @Override
50      public Iterator<E> makeObject() {
51          return UnmodifiableIterator.unmodifiableIterator(testList.iterator());
52      }
53  
54      @SuppressWarnings("unchecked")
55      @BeforeEach
56      protected void setUp() throws Exception {
57          testList = new ArrayList<>(Arrays.asList((E[]) testArray));
58      }
59  
60      @Override
61      public boolean supportsRemove() {
62          return false;
63      }
64  
65      @Test
66      public void testDecorateFactory() {
67          Iterator<E> it = makeObject();
68          assertSame(it, UnmodifiableIterator.unmodifiableIterator(it));
69  
70          it = testList.iterator();
71          assertNotSame(it, UnmodifiableIterator.unmodifiableIterator(it));
72  
73          assertThrows(NullPointerException.class, () -> UnmodifiableIterator.unmodifiableIterator(null));
74      }
75  
76      @Test
77      public void testIterator() {
78          assertTrue(makeEmptyIterator() instanceof Unmodifiable);
79      }
80  
81  }