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.collection;
18  
19  import static org.junit.jupiter.api.Assertions.assertSame;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.List;
27  
28  import org.apache.commons.collections4.Unmodifiable;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Extension of {@link AbstractCollectionTest} for exercising the
33   * {@link UnmodifiableCollection} implementation.
34   */
35  public class UnmodifiableCollectionTest<E> extends AbstractCollectionTest<E> {
36  
37      public UnmodifiableCollectionTest() {
38          super(UnmodifiableCollectionTest.class.getSimpleName());
39      }
40  
41      @Override
42      public String getCompatibilityVersion() {
43          return "4";
44      }
45  
46      @Override
47      public boolean isAddSupported() {
48          return false;
49      }
50  
51      @Override
52      public boolean isRemoveSupported() {
53          return false;
54      }
55  
56      @Override
57      public Collection<E> makeConfirmedCollection() {
58          return new ArrayList<>();
59      }
60  
61      @Override
62      public Collection<E> makeConfirmedFullCollection() {
63          return new ArrayList<>(Arrays.asList(getFullElements()));
64      }
65  
66      @Override
67      public Collection<E> makeFullCollection() {
68          final List<E> list = new ArrayList<>(Arrays.asList(getFullElements()));
69          return UnmodifiableCollection.unmodifiableCollection(list);
70      }
71  
72      @Override
73      public Collection<E> makeObject() {
74          return UnmodifiableCollection.unmodifiableCollection(new ArrayList<>());
75      }
76  
77      @Test
78      public void testDecorateFactory() {
79          final Collection<E> coll = makeFullCollection();
80          assertSame(coll, UnmodifiableCollection.unmodifiableCollection(coll));
81  
82          assertThrows(NullPointerException.class, () -> UnmodifiableCollection.unmodifiableCollection(null));
83      }
84  
85      @Test
86      public void testUnmodifiable() {
87          assertTrue(makeObject() instanceof Unmodifiable);
88          assertTrue(makeFullCollection() instanceof Unmodifiable);
89      }
90  
91  //    public void testCreate() throws Exception {
92  //        resetEmpty();
93  //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableCollection.emptyCollection.version4.obj");
94  //        resetFull();
95  //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableCollection.fullCollection.version4.obj");
96  //    }
97  
98  }