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.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.NavigableSet;
27 import java.util.Set;
28 import java.util.TreeSet;
29
30 import org.junit.jupiter.api.Test;
31
32
33
34
35
36 public class UnmodifiableNavigableSetTest<E> extends AbstractNavigableSetTest<E> {
37 protected UnmodifiableNavigableSet<E> set;
38 protected ArrayList<E> array;
39
40 @Override
41 public String getCompatibilityVersion() {
42 return "4.1";
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 UnmodifiableNavigableSet<E> makeFullCollection() {
57 final TreeSet<E> set = new TreeSet<>(Arrays.asList(getFullElements()));
58 return (UnmodifiableNavigableSet<E>) UnmodifiableNavigableSet.unmodifiableNavigableSet(set);
59 }
60
61 @Override
62 public NavigableSet<E> makeObject() {
63 return UnmodifiableNavigableSet.unmodifiableNavigableSet(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 NavigableSet<E> set = makeFullCollection();
83 assertSame(set, UnmodifiableNavigableSet.unmodifiableNavigableSet(set));
84 assertThrows(NullPointerException.class, () -> UnmodifiableNavigableSet.unmodifiableNavigableSet(null));
85 }
86
87
88
89
90 @Test
91 @SuppressWarnings("unchecked")
92 public void testUnmodifiable() {
93 setupSet();
94 verifyUnmodifiable(set);
95 verifyUnmodifiable(set.descendingSet());
96 verifyUnmodifiable(set.headSet((E) Integer.valueOf(1)));
97 verifyUnmodifiable(set.headSet((E) Integer.valueOf(1), true));
98 verifyUnmodifiable(set.tailSet((E) Integer.valueOf(1)));
99 verifyUnmodifiable(set.tailSet((E) Integer.valueOf(1), false));
100 verifyUnmodifiable(set.subSet((E) Integer.valueOf(1), (E) Integer.valueOf(3)));
101 verifyUnmodifiable(set.subSet((E) Integer.valueOf(1), false, (E) Integer.valueOf(3), false));
102 verifyUnmodifiable(set.subSet((E) Integer.valueOf(1), true, (E) Integer.valueOf(3), true));
103 }
104
105
106
107
108 @SuppressWarnings("unchecked")
109 public void verifyUnmodifiable(final Set<E> set) {
110 assertThrows(UnsupportedOperationException.class, () -> set.add((E) "value"));
111 assertThrows(UnsupportedOperationException.class, () -> set.addAll(new TreeSet<>()));
112 assertThrows(UnsupportedOperationException.class, () -> set.clear());
113 assertThrows(UnsupportedOperationException.class, () -> set.iterator().remove());
114 assertThrows(UnsupportedOperationException.class, () -> set.remove("x"));
115 assertThrows(UnsupportedOperationException.class, () -> set.removeAll(array));
116 assertThrows(UnsupportedOperationException.class, () -> set.removeIf(element -> true));
117 assertThrows(UnsupportedOperationException.class, () -> set.retainAll(array));
118
119 if (set instanceof NavigableSet) {
120 final NavigableSet<E> navigableSet = (NavigableSet<E>) set;
121 assertThrows(UnsupportedOperationException.class, () -> navigableSet.pollFirst());
122 assertThrows(UnsupportedOperationException.class, () -> navigableSet.pollLast());
123 }
124 }
125
126
127
128
129
130
131
132
133 }