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