1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4;
18
19 import static org.junit.jupiter.api.Assertions.assertAll;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertNotEquals;
23 import static org.junit.jupiter.api.Assertions.assertNull;
24 import static org.junit.jupiter.api.Assertions.assertSame;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.HashSet;
31 import java.util.Set;
32
33 import org.apache.commons.collections4.SetUtils.SetView;
34 import org.apache.commons.collections4.set.PredicatedSet;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37
38
39
40
41 public class SetUtilsTest {
42
43 private Set<Integer> setA;
44 private Set<Integer> setB;
45
46 @BeforeEach
47 public void setUp() {
48 setA = new HashSet<>();
49 setA.add(1);
50 setA.add(2);
51 setA.add(3);
52 setA.add(4);
53 setA.add(5);
54
55 setB = new HashSet<>();
56 setB.add(3);
57 setB.add(4);
58 setB.add(5);
59 setB.add(6);
60 setB.add(7);
61 }
62
63 @Test
64 public void testDifference() {
65 final SetView<Integer> set = SetUtils.difference(setA, setB);
66 assertEquals(2, set.size());
67 assertTrue(set.contains(1));
68 assertTrue(set.contains(2));
69 for (final Integer i : setB) {
70 assertFalse(set.contains(i));
71 }
72
73 final Set<Integer> set2 = SetUtils.difference(setA, SetUtils.<Integer>emptySet());
74 assertEquals(setA, set2);
75 assertAll(
76 () -> assertThrows(NullPointerException.class, () -> SetUtils.difference(setA, null),
77 "Expecting NullPointerException"),
78 () -> assertThrows(NullPointerException.class, () -> SetUtils.difference(null, setA),
79 "Expecting NullPointerException")
80 );
81 }
82
83 @Test
84 public void testDisjunction() {
85 final SetView<Integer> set = SetUtils.disjunction(setA, setB);
86 assertEquals(4, set.size());
87 assertTrue(set.contains(1));
88 assertTrue(set.contains(2));
89 assertTrue(set.contains(6));
90 assertTrue(set.contains(7));
91 assertFalse(set.contains(3));
92 assertFalse(set.contains(4));
93 assertFalse(set.contains(5));
94
95 final Set<Integer> set2 = SetUtils.disjunction(setA, SetUtils.<Integer>emptySet());
96 assertEquals(setA, set2);
97 assertAll(
98 () -> assertThrows(NullPointerException.class, () -> SetUtils.disjunction(setA, null),
99 "Expecting NullPointerException"),
100 () -> assertThrows(NullPointerException.class, () -> SetUtils.disjunction(null, setA),
101 "Expecting NullPointerException")
102 );
103 }
104
105 @Test
106 public void testEmptyIfNull() {
107 assertTrue(SetUtils.emptyIfNull(null).isEmpty());
108
109 final Set<Long> set = new HashSet<>();
110 assertSame(set, SetUtils.emptyIfNull(set));
111 }
112
113 @Test
114 public void testEquals() {
115 final Collection<String> data = Arrays.asList("a", "b", "c");
116
117 final Set<String> a = new HashSet<>(data);
118 final Set<String> b = new HashSet<>(data);
119
120 assertEquals(a, b);
121 assertTrue(SetUtils.isEqualSet(a, b));
122 a.clear();
123 assertFalse(SetUtils.isEqualSet(a, b));
124 assertFalse(SetUtils.isEqualSet(a, null));
125 assertFalse(SetUtils.isEqualSet(null, b));
126 assertTrue(SetUtils.isEqualSet(null, null));
127 }
128
129 @Test
130 public void testHashCode() {
131 final Collection<String> data = Arrays.asList("a", "b", "c");
132
133 final Set<String> a = new HashSet<>(data);
134 final Set<String> b = new HashSet<>(data);
135
136 assertEquals(a.hashCode(), b.hashCode());
137 assertEquals(a.hashCode(), SetUtils.hashCodeForSet(a));
138 assertEquals(b.hashCode(), SetUtils.hashCodeForSet(b));
139 assertEquals(SetUtils.hashCodeForSet(a), SetUtils.hashCodeForSet(b));
140 a.clear();
141 assertNotEquals(SetUtils.hashCodeForSet(a), SetUtils.hashCodeForSet(b));
142 assertEquals(0, SetUtils.hashCodeForSet(null));
143 }
144
145 @Test
146 public void testHashSet() {
147 final Set<?> set1 = SetUtils.unmodifiableSet();
148 assertTrue(set1.isEmpty(), "set is empty");
149
150 final Set<Integer> set2 = SetUtils.hashSet(1, 2, 2, 3);
151 assertEquals(3, set2.size(), "set has 3 elements");
152 assertTrue(set2.contains(1), "set contains 1");
153 assertTrue(set2.contains(2), "set contains 2");
154 assertTrue(set2.contains(3), "set contains 3");
155
156 final Set<String> set3 = SetUtils.hashSet("1", "2", "2", "3");
157 assertEquals(3, set3.size(), "set has 3 elements");
158 assertTrue(set3.contains("1"), "set contains 1");
159 assertTrue(set3.contains("2"), "set contains 2");
160 assertTrue(set3.contains("3"), "set contains 3");
161
162 final Set<?> set4 = SetUtils.hashSet(null, null);
163 assertEquals(1, set4.size(), "set has 1 element");
164 assertTrue(set4.contains(null), "set contains null");
165
166 final Set<?> set5 = SetUtils.hashSet((Object[]) null);
167 assertNull(set5, "set is null");
168 }
169
170 @Test
171 public void testIntersection() {
172 final SetView<Integer> set = SetUtils.intersection(setA, setB);
173 assertEquals(3, set.size());
174 assertTrue(set.contains(3));
175 assertTrue(set.contains(4));
176 assertTrue(set.contains(5));
177 assertFalse(set.contains(1));
178 assertFalse(set.contains(2));
179 assertFalse(set.contains(6));
180 assertFalse(set.contains(7));
181
182 final Set<Integer> set2 = SetUtils.intersection(setA, SetUtils.<Integer>emptySet());
183 assertEquals(SetUtils.<Integer>emptySet(), set2);
184 assertAll(
185 () -> assertThrows(NullPointerException.class, () -> SetUtils.intersection(setA, null),
186 "Expecting NullPointerException"),
187 () -> assertThrows(NullPointerException.class, () -> SetUtils.intersection(null, setA),
188 "Expecting NullPointerException")
189 );
190 }
191
192 @Test
193 public void testNewIdentityHashSet() {
194 final Set<String> set = SetUtils.newIdentityHashSet();
195 final String a = new String("a");
196 set.add(a);
197 set.add(new String("b"));
198 set.add(a);
199
200 assertEquals(2, set.size());
201
202 set.add(new String("a"));
203 assertEquals(3, set.size());
204
205 set.remove(a);
206 assertEquals(2, set.size());
207 }
208
209 @Test
210 public void testpredicatedSet() {
211 final Predicate<Object> predicate = String.class::isInstance;
212 final Set<Object> set = SetUtils.predicatedSet(new HashSet<>(), predicate);
213 assertTrue(set instanceof PredicatedSet, "returned object should be a PredicatedSet");
214 assertAll(
215 () -> assertThrows(NullPointerException.class, () -> SetUtils.predicatedSet(new HashSet<>(), null),
216 "Expecting NullPointerException for null predicate."),
217 () -> assertThrows(NullPointerException.class, () -> SetUtils.predicatedSet(null, predicate),
218 "Expecting NullPointerException for null set.")
219 );
220 }
221
222 @Test
223 public void testUnion() {
224 final SetView<Integer> set = SetUtils.union(setA, setB);
225 assertEquals(7, set.size());
226 assertTrue(set.containsAll(setA));
227 assertTrue(set.containsAll(setB));
228
229 final Set<Integer> set2 = SetUtils.union(setA, SetUtils.<Integer>emptySet());
230 assertEquals(setA, set2);
231 assertAll(
232 () -> assertThrows(NullPointerException.class, () -> SetUtils.union(setA, null),
233 "Expecting NullPointerException"),
234 () -> assertThrows(NullPointerException.class, () -> SetUtils.union(null, setA),
235 "Expecting NullPointerException")
236 );
237 }
238
239 @Test
240 public void testUnmodifiableSet() {
241 final Set<?> set1 = SetUtils.unmodifiableSet();
242 assertTrue(set1.isEmpty(), "set is empty");
243
244 final Set<Integer> set2 = SetUtils.unmodifiableSet(1, 2, 2, 3);
245 assertEquals(3, set2.size(), "set has 3 elements");
246 assertTrue(set2.contains(1), "set contains 1");
247 assertTrue(set2.contains(2), "set contains 2");
248 assertTrue(set2.contains(3), "set contains 3");
249
250 final Set<String> set3 = SetUtils.unmodifiableSet("1", "2", "2", "3");
251 assertEquals(3, set3.size(), "set has 3 elements");
252 assertTrue(set3.contains("1"), "set contains 1");
253 assertTrue(set3.contains("2"), "set contains 2");
254 assertTrue(set3.contains("3"), "set contains 3");
255
256 final Set<?> set4 = SetUtils.unmodifiableSet(null, null);
257 assertEquals(1, set4.size(), "set has 1 element");
258 assertTrue(set4.contains(null), "set contains null");
259
260 final Set<?> set5 = SetUtils.unmodifiableSet((Object[]) null);
261 assertNull(set5, "set is null");
262 }
263
264 @Test
265 public void testUnmodifiableSetWrap() {
266 final Set<Integer> set1 = SetUtils.unmodifiableSet(1, 2, 2, 3);
267 final Set<Integer> set2 = SetUtils.unmodifiableSet(set1);
268 assertSame(set1, set2);
269 }
270
271 }