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.list;
18  
19  import static org.junit.jupiter.api.Assertions.assertSame;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Extension of {@link AbstractListTest} for exercising the
31   * {@link UnmodifiableList} implementation.
32   */
33  public class UnmodifiableListTest<E> extends AbstractListTest<E> {
34  
35      protected UnmodifiableList<E> list;
36  
37      protected ArrayList<E> array;
38  
39      @Override
40      public String getCompatibilityVersion() {
41          return "4";
42      }
43  
44      @Override
45      public boolean isAddSupported() {
46          return false;
47      }
48  
49      @Override
50      public boolean isRemoveSupported() {
51          return false;
52      }
53  
54      @Override
55      public boolean isSetSupported() {
56          return false;
57      }
58      @Override
59      public UnmodifiableList<E> makeFullCollection() {
60          final ArrayList<E> list = new ArrayList<>(Arrays.asList(getFullElements()));
61          return new UnmodifiableList<>(list);
62      }
63  
64      @Override
65      public UnmodifiableList<E> makeObject() {
66          return new UnmodifiableList<>(new ArrayList<>());
67      }
68  
69      @SuppressWarnings("unchecked")
70      protected void setupList() {
71          list = makeFullCollection();
72          array = new ArrayList<>();
73          array.add((E) Integer.valueOf(1));
74      }
75  
76      @Test
77      public void testDecorateFactory() {
78          final List<E> list = makeObject();
79          assertSame(list, UnmodifiableList.unmodifiableList(list));
80  
81          assertThrows(NullPointerException.class, () -> UnmodifiableList.unmodifiableList(null));
82      }
83  
84      /**
85       * Verify that base list and sublists are not modifiable
86       */
87      @Test
88      public void testUnmodifiable() {
89          setupList();
90          verifyUnmodifiable(list);
91          verifyUnmodifiable(list.subList(0, 2));
92      }
93  
94      /**
95       * Verify that iterator is not modifiable
96       */
97      @Test
98      public void testUnmodifiableIterator() {
99          setupList();
100         final Iterator<E> iterator = list.iterator();
101         iterator.next();
102 
103         assertThrows(UnsupportedOperationException.class, () -> iterator.remove(),
104                 "Expecting UnsupportedOperationException.");
105     }
106 
107     @SuppressWarnings("unchecked")
108     protected void verifyUnmodifiable(final List<E> list) {
109         assertThrows(UnsupportedOperationException.class, () -> list.add(0, (E) Integer.valueOf(0)));
110         assertThrows(UnsupportedOperationException.class, () -> list.add((E) Integer.valueOf(0)));
111         assertThrows(UnsupportedOperationException.class, () -> list.addAll(0, array));
112         assertThrows(UnsupportedOperationException.class, () -> list.addAll(array));
113         assertThrows(UnsupportedOperationException.class, () -> list.clear());
114         assertThrows(UnsupportedOperationException.class, () -> list.remove(0));
115         assertThrows(UnsupportedOperationException.class, () -> list.remove(Integer.valueOf(0)));
116         assertThrows(UnsupportedOperationException.class, () -> list.removeAll(array));
117         assertThrows(UnsupportedOperationException.class, () -> list.retainAll(array));
118         assertThrows(UnsupportedOperationException.class, () -> list.set(0, (E) Integer.valueOf(0)));
119     }
120 
121 //    public void testCreate() throws Exception {
122 //        resetEmpty();
123 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableList.emptyCollection.version4.obj");
124 //        resetFull();
125 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableList.fullCollection.version4.obj");
126 //    }
127 
128 }