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.List;
28  import java.util.ListIterator;
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 UnmodifiableListIterator.
36   */
37  public class UnmodifiableListIteratorTest<E> extends AbstractListIteratorTest<E> {
38  
39      protected String[] testArray = {"One", "Two", "Three"};
40      protected List<E> testList;
41  
42      public UnmodifiableListIteratorTest() {
43          super(UnmodifiableListIteratorTest.class.getSimpleName());
44      }
45  
46      @Override
47      public ListIterator<E> makeEmptyIterator() {
48          return UnmodifiableListIterator.unmodifiableListIterator(Collections.<E>emptyList().listIterator());
49      }
50  
51      @Override
52      public ListIterator<E> makeObject() {
53          return UnmodifiableListIterator.unmodifiableListIterator(testList.listIterator());
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      @SuppressWarnings("unchecked")
60      @BeforeEach
61      protected void setUp() throws Exception {
62          testList = new ArrayList<>(Arrays.asList((E[]) testArray));
63      }
64  
65      @Override
66      public boolean supportsAdd() {
67          return false;
68      }
69  
70      @Override
71      public boolean supportsRemove() {
72          return false;
73      }
74  
75      @Override
76      public boolean supportsSet() {
77          return false;
78      }
79  
80      @Test
81      public void testDecorateFactory() {
82          ListIterator<E> it = makeObject();
83          assertSame(it, UnmodifiableListIterator.unmodifiableListIterator(it));
84  
85          it = testList.listIterator();
86          assertNotSame(it, UnmodifiableListIterator.unmodifiableListIterator(it));
87  
88          assertThrows(NullPointerException.class, () -> UnmodifiableListIterator.unmodifiableListIterator(null));
89      }
90  
91      @Test
92      public void testListIterator() {
93          assertTrue(makeEmptyIterator() instanceof Unmodifiable);
94      }
95  
96  }