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 java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  /**
25   * Extension of {@link AbstractListTest} for exercising the
26   * {@link UnmodifiableList} implementation.
27   *
28   * @since 3.0
29   */
30  public class UnmodifiableListTest<E> extends AbstractListTest<E> {
31  
32      public UnmodifiableListTest(final String testName) {
33          super(testName);
34      }
35  
36      //-----------------------------------------------------------------------
37      @Override
38      public UnmodifiableList<E> makeObject() {
39          return new UnmodifiableList<>(new ArrayList<E>());
40      }
41  
42      @Override
43      public UnmodifiableList<E> makeFullCollection() {
44          final ArrayList<E> list = new ArrayList<>();
45          list.addAll(Arrays.asList(getFullElements()));
46          return new UnmodifiableList<>(list);
47      }
48  
49      @Override
50      public boolean isSetSupported() {
51          return false;
52      }
53  
54      @Override
55      public boolean isAddSupported() {
56          return false;
57      }
58  
59      @Override
60      public boolean isRemoveSupported() {
61          return false;
62      }
63  
64      //-----------------------------------------------------------------------
65      protected UnmodifiableList<E> list;
66      protected ArrayList<E> array;
67  
68      @SuppressWarnings("unchecked")
69      protected void setupList() {
70          list = makeFullCollection();
71          array = new ArrayList<>();
72          array.add((E) Integer.valueOf(1));
73      }
74  
75      /**
76       * Verify that base list and sublists are not modifiable
77       */
78      public void testUnmodifiable() {
79          setupList();
80          verifyUnmodifiable(list);
81          verifyUnmodifiable(list.subList(0, 2));
82      }
83  
84      public void testDecorateFactory() {
85          final List<E> list = makeObject();
86          assertSame(list, UnmodifiableList.unmodifiableList(list));
87  
88          try {
89              UnmodifiableList.unmodifiableList(null);
90              fail();
91          } catch (final NullPointerException ex) {}
92      }
93  
94      @SuppressWarnings("unchecked")
95      protected void verifyUnmodifiable(final List<E> list) {
96          try {
97              list.add(0, (E) Integer.valueOf(0));
98              fail("Expecting UnsupportedOperationException.");
99          } catch (final UnsupportedOperationException e) {
100             // expected
101         }
102         try {
103             list.add((E) Integer.valueOf(0));
104              fail("Expecting UnsupportedOperationException.");
105         } catch (final UnsupportedOperationException e) {
106             // expected
107         }
108         try {
109             list.addAll(0, array);
110              fail("Expecting UnsupportedOperationException.");
111         } catch (final UnsupportedOperationException e) {
112             // expected
113         }
114         try {
115             list.addAll(array);
116              fail("Expecting UnsupportedOperationException.");
117         } catch (final UnsupportedOperationException e) {
118             // expected
119         }
120         try {
121             list.clear();
122              fail("Expecting UnsupportedOperationException.");
123         } catch (final UnsupportedOperationException e) {
124             // expected
125         }
126         try {
127             list.remove(0);
128              fail("Expecting UnsupportedOperationException.");
129         } catch (final UnsupportedOperationException e) {
130             // expected
131         }
132         try {
133             list.remove(Integer.valueOf(0));
134              fail("Expecting UnsupportedOperationException.");
135         } catch (final UnsupportedOperationException e) {
136             // expected
137         }
138         try {
139             list.removeAll(array);
140              fail("Expecting UnsupportedOperationException.");
141         } catch (final UnsupportedOperationException e) {
142             // expected
143         }
144         try {
145             list.retainAll(array);
146              fail("Expecting UnsupportedOperationException.");
147         } catch (final UnsupportedOperationException e) {
148             // expected
149         }
150         try {
151             list.set(0, (E) Integer.valueOf(0));
152              fail("Expecting UnsupportedOperationException.");
153         } catch (final UnsupportedOperationException e) {
154             // expected
155         }
156     }
157 
158     /**
159      * Verify that iterator is not modifiable
160      */
161     public void testUnmodifiableIterator() {
162         setupList();
163         final Iterator<E> iterator = list.iterator();
164         try {
165             iterator.next();
166             iterator.remove();
167             fail("Expecting UnsupportedOperationException.");
168         } catch (final UnsupportedOperationException e) {
169             // expected
170         }
171     }
172 
173     //-----------------------------------------------------------------------
174 
175     @Override
176     public String getCompatibilityVersion() {
177         return "4";
178     }
179 
180 //    public void testCreate() throws Exception {
181 //        resetEmpty();
182 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableList.emptyCollection.version4.obj");
183 //        resetFull();
184 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableList.fullCollection.version4.obj");
185 //    }
186 
187 }