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      @Override
38      public String getCompatibilityVersion() {
39          return "4";
40      }
41  
42      @Override
43      public boolean isAddSupported() {
44          return false;
45      }
46  
47      @Override
48      public boolean isRemoveSupported() {
49          return false;
50      }
51  
52      @Override
53      public Collection<E> makeConfirmedCollection() {
54          return new ArrayList<>();
55      }
56  
57      @Override
58      public Collection<E> makeConfirmedFullCollection() {
59          return new ArrayList<>(Arrays.asList(getFullElements()));
60      }
61  
62      @Override
63      public Collection<E> makeFullCollection() {
64          final List<E> list = new ArrayList<>(Arrays.asList(getFullElements()));
65          return UnmodifiableCollection.unmodifiableCollection(list);
66      }
67  
68      @Override
69      public Collection<E> makeObject() {
70          return UnmodifiableCollection.unmodifiableCollection(new ArrayList<>());
71      }
72  
73      @Test
74      public void testDecorateFactory() {
75          final Collection<E> coll = makeFullCollection();
76          assertSame(coll, UnmodifiableCollection.unmodifiableCollection(coll));
77  
78          assertThrows(NullPointerException.class, () -> UnmodifiableCollection.unmodifiableCollection(null));
79      }
80  
81      @Test
82      public void testUnmodifiable() {
83          assertTrue(makeObject() instanceof Unmodifiable);
84          assertTrue(makeFullCollection() instanceof Unmodifiable);
85      }
86  
87  //    public void testCreate() throws Exception {
88  //        resetEmpty();
89  //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableCollection.emptyCollection.version4.obj");
90  //        resetFull();
91  //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableCollection.fullCollection.version4.obj");
92  //    }
93  
94  }