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.set;
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.Arrays;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import org.apache.commons.collections4.Unmodifiable;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Extension of {@link AbstractSetTest} for exercising the
32   * {@link UnmodifiableSet} implementation.
33   */
34  public class UnmodifiableSetTest<E> extends AbstractSetTest<E> {
35  
36      @Override
37      public String getCompatibilityVersion() {
38          return "4";
39      }
40  
41      @Override
42      protected int getIterationBehaviour() {
43          return UNORDERED;
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 Set<E> makeFullCollection() {
58          final HashSet<E> set = new HashSet<>(Arrays.asList(getFullElements()));
59          return UnmodifiableSet.unmodifiableSet(set);
60      }
61  
62      @Override
63      public Set<E> makeObject() {
64          return UnmodifiableSet.unmodifiableSet(new HashSet<>());
65      }
66  
67      @Test
68      public void testDecorateFactory() {
69          final Set<E> set = makeFullCollection();
70          assertSame(set, UnmodifiableSet.unmodifiableSet(set));
71  
72          assertThrows(NullPointerException.class, () -> UnmodifiableSet.unmodifiableSet(null));
73      }
74  
75      @Test
76      public void testUnmodifiable() {
77          assertTrue(makeObject() instanceof Unmodifiable);
78          assertTrue(makeFullCollection() instanceof Unmodifiable);
79      }
80  
81  //    public void testCreate() throws Exception {
82  //        resetEmpty();
83  //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableSet.emptyCollection.version4.obj");
84  //        resetFull();
85  //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableSet.fullCollection.version4.obj");
86  //    }
87  
88  }