1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
35
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
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
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
128
129
130
131
132
133
134 }