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.assertNull;
20  import static org.junit.jupiter.api.Assertions.assertSame;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Comparator;
26  import java.util.Set;
27  import java.util.SortedSet;
28  import java.util.TreeSet;
29  
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Extension of {@link AbstractSortedSetTest} for exercising the
34   * {@link UnmodifiableSortedSet} implementation.
35   */
36  public class UnmodifiableSortedSetTest<E> extends AbstractSortedSetTest<E> {
37      protected UnmodifiableSortedSet<E> set;
38      protected ArrayList<E> array;
39  
40      @Override
41      public String getCompatibilityVersion() {
42          return "4";
43      }
44  
45      @Override
46      public boolean isAddSupported() {
47          return false;
48      }
49  
50      @Override
51      public boolean isRemoveSupported() {
52          return false;
53      }
54  
55      @Override
56      public UnmodifiableSortedSet<E> makeFullCollection() {
57          final TreeSet<E> set = new TreeSet<>(Arrays.asList(getFullElements()));
58          return (UnmodifiableSortedSet<E>) UnmodifiableSortedSet.unmodifiableSortedSet(set);
59      }
60  
61      @Override
62      public SortedSet<E> makeObject() {
63          return UnmodifiableSortedSet.unmodifiableSortedSet(new TreeSet<>());
64      }
65  
66      @SuppressWarnings("unchecked")
67      protected void setupSet() {
68          set = makeFullCollection();
69          array = new ArrayList<>();
70          array.add((E) Integer.valueOf(1));
71      }
72  
73      @Test
74      public void testComparator() {
75          setupSet();
76          final Comparator<? super E> c = set.comparator();
77          assertNull(c, "natural order, so comparator should be null");
78      }
79  
80      @Test
81      public void testDecorateFactory() {
82          final SortedSet<E> set = makeFullCollection();
83          assertSame(set, UnmodifiableSortedSet.unmodifiableSortedSet(set));
84  
85          assertThrows(NullPointerException.class, () -> UnmodifiableSortedSet.unmodifiableSortedSet(null));
86      }
87  
88      /**
89       * Verify that base set and subsets are not modifiable
90       */
91      @Test
92      @SuppressWarnings("unchecked")
93      public void testUnmodifiable() {
94          setupSet();
95          verifyUnmodifiable(set);
96          verifyUnmodifiable(set.headSet((E) Integer.valueOf(1)));
97          verifyUnmodifiable(set.tailSet((E) Integer.valueOf(1)));
98          verifyUnmodifiable(set.subSet((E) Integer.valueOf(1), (E) Integer.valueOf(3)));
99      }
100 
101     /**
102      * Verifies that a set is not modifiable
103      */
104     @SuppressWarnings("unchecked")
105     public void verifyUnmodifiable(final Set<E> set) {
106         assertThrows(UnsupportedOperationException.class, () -> set.add((E) "value"));
107         assertThrows(UnsupportedOperationException.class, () -> set.addAll(new TreeSet<>()));
108         assertThrows(UnsupportedOperationException.class, () -> set.clear());
109         assertThrows(UnsupportedOperationException.class, () -> set.remove("x"));
110         assertThrows(UnsupportedOperationException.class, () -> set.removeAll(array));
111         assertThrows(UnsupportedOperationException.class, () -> set.retainAll(array));
112     }
113 
114 //    public void testCreate() throws Exception {
115 //        resetEmpty();
116 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableSortedSet.emptyCollection.version4.obj");
117 //        resetFull();
118 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableSortedSet.fullCollection.version4.obj");
119 //    }
120 
121 }