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 public UnmodifiableNavigableSetTest() {
41 super(UnmodifiableNavigableSetTest.class.getSimpleName());
42 }
43
44 @Override
45 public String getCompatibilityVersion() {
46 return "4.1";
47 }
48
49 @Override
50 public boolean isAddSupported() {
51 return false;
52 }
53
54 @Override
55 public boolean isRemoveSupported() {
56 return false;
57 }
58
59 @Override
60 public UnmodifiableNavigableSet<E> makeFullCollection() {
61 final TreeSet<E> set = new TreeSet<>(Arrays.asList(getFullElements()));
62 return (UnmodifiableNavigableSet<E>) UnmodifiableNavigableSet.unmodifiableNavigableSet(set);
63 }
64
65 @Override
66 public NavigableSet<E> makeObject() {
67 return UnmodifiableNavigableSet.unmodifiableNavigableSet(new TreeSet<>());
68 }
69
70 @SuppressWarnings("unchecked")
71 protected void setupSet() {
72 set = makeFullCollection();
73 array = new ArrayList<>();
74 array.add((E) Integer.valueOf(1));
75 }
76
77 @Test
78 public void testComparator() {
79 setupSet();
80 final Comparator<? super E> c = set.comparator();
81 assertNull(c, "natural order, so comparator should be null");
82 }
83
84 @Test
85 public void testDecorateFactory() {
86 final NavigableSet<E> set = makeFullCollection();
87 assertSame(set, UnmodifiableNavigableSet.unmodifiableNavigableSet(set));
88 assertThrows(NullPointerException.class, () -> UnmodifiableNavigableSet.unmodifiableNavigableSet(null));
89 }
90
91
92
93
94 @Test
95 @SuppressWarnings("unchecked")
96 public void testUnmodifiable() {
97 setupSet();
98 verifyUnmodifiable(set);
99 verifyUnmodifiable(set.descendingSet());
100 verifyUnmodifiable(set.headSet((E) Integer.valueOf(1)));
101 verifyUnmodifiable(set.headSet((E) Integer.valueOf(1), true));
102 verifyUnmodifiable(set.tailSet((E) Integer.valueOf(1)));
103 verifyUnmodifiable(set.tailSet((E) Integer.valueOf(1), false));
104 verifyUnmodifiable(set.subSet((E) Integer.valueOf(1), (E) Integer.valueOf(3)));
105 verifyUnmodifiable(set.subSet((E) Integer.valueOf(1), false, (E) Integer.valueOf(3), false));
106 verifyUnmodifiable(set.subSet((E) Integer.valueOf(1), true, (E) Integer.valueOf(3), true));
107 }
108
109
110
111
112 @SuppressWarnings("unchecked")
113 public void verifyUnmodifiable(final Set<E> set) {
114 assertThrows(UnsupportedOperationException.class, () -> set.add((E) "value"));
115 assertThrows(UnsupportedOperationException.class, () -> set.addAll(new TreeSet<>()));
116 assertThrows(UnsupportedOperationException.class, () -> set.clear());
117 assertThrows(UnsupportedOperationException.class, () -> set.iterator().remove());
118 assertThrows(UnsupportedOperationException.class, () -> set.remove("x"));
119 assertThrows(UnsupportedOperationException.class, () -> set.removeAll(array));
120 assertThrows(UnsupportedOperationException.class, () -> set.removeIf(element -> true));
121 assertThrows(UnsupportedOperationException.class, () -> set.retainAll(array));
122
123 if (set instanceof NavigableSet) {
124 final NavigableSet<E> navigableSet = (NavigableSet<E>) set;
125 assertThrows(UnsupportedOperationException.class, () -> navigableSet.pollFirst());
126 assertThrows(UnsupportedOperationException.class, () -> navigableSet.pollLast());
127 }
128 }
129
130
131
132
133
134
135
136
137 }