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;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
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   * Tests for SetUtils.
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  
76          assertThrows(NullPointerException.class, () -> SetUtils.difference(setA, null));
77          assertThrows(NullPointerException.class, () -> SetUtils.difference(null, setA));
78      }
79  
80      @Test
81      public void testDisjunction() {
82          final SetView<Integer> set = SetUtils.disjunction(setA, setB);
83          assertEquals(4, set.size());
84          assertTrue(set.contains(1));
85          assertTrue(set.contains(2));
86          assertTrue(set.contains(6));
87          assertTrue(set.contains(7));
88          assertFalse(set.contains(3));
89          assertFalse(set.contains(4));
90          assertFalse(set.contains(5));
91  
92          final Set<Integer> set2 = SetUtils.disjunction(setA, SetUtils.<Integer>emptySet());
93          assertEquals(setA, set2);
94  
95          assertThrows(NullPointerException.class, () -> SetUtils.disjunction(setA, null));
96          assertThrows(NullPointerException.class, () -> SetUtils.disjunction(null, setA));
97      }
98  
99      @Test
100     public void testEmptyIfNull() {
101         assertTrue(SetUtils.emptyIfNull(null).isEmpty());
102 
103         final Set<Long> set = new HashSet<>();
104         assertSame(set, SetUtils.emptyIfNull(set));
105     }
106 
107     @Test
108     public void testEquals() {
109         final Collection<String> data = Arrays.asList("a", "b", "c");
110 
111         final Set<String> a = new HashSet<>(data);
112         final Set<String> b = new HashSet<>(data);
113 
114         assertEquals(a, b);
115         assertTrue(SetUtils.isEqualSet(a, b));
116         a.clear();
117         assertFalse(SetUtils.isEqualSet(a, b));
118         assertFalse(SetUtils.isEqualSet(a, null));
119         assertFalse(SetUtils.isEqualSet(null, b));
120         assertTrue(SetUtils.isEqualSet(null, null));
121     }
122 
123     @Test
124     public void testHashCode() {
125         final Collection<String> data = Arrays.asList("a", "b", "c");
126 
127         final Set<String> a = new HashSet<>(data);
128         final Set<String> b = new HashSet<>(data);
129 
130         assertEquals(a.hashCode(), b.hashCode());
131         assertEquals(a.hashCode(), SetUtils.hashCodeForSet(a));
132         assertEquals(b.hashCode(), SetUtils.hashCodeForSet(b));
133         assertEquals(SetUtils.hashCodeForSet(a), SetUtils.hashCodeForSet(b));
134         a.clear();
135         assertNotEquals(SetUtils.hashCodeForSet(a), SetUtils.hashCodeForSet(b));
136         assertEquals(0, SetUtils.hashCodeForSet(null));
137     }
138 
139     @Test
140     public void testHashSet() {
141         final Set<?> set1 = SetUtils.unmodifiableSet();
142         assertTrue(set1.isEmpty(), "set is empty");
143 
144         final Set<Integer> set2 = SetUtils.hashSet(1, 2, 2, 3);
145         assertEquals(3, set2.size(), "set has 3 elements");
146         assertTrue(set2.contains(1), "set contains 1");
147         assertTrue(set2.contains(2), "set contains 2");
148         assertTrue(set2.contains(3), "set contains 3");
149 
150         final Set<String> set3 = SetUtils.hashSet("1", "2", "2", "3");
151         assertEquals(3, set3.size(), "set has 3 elements");
152         assertTrue(set3.contains("1"), "set contains 1");
153         assertTrue(set3.contains("2"), "set contains 2");
154         assertTrue(set3.contains("3"), "set contains 3");
155 
156         final Set<?> set4 = SetUtils.hashSet(null, null);
157         assertEquals(1, set4.size(), "set has 1 element");
158         assertTrue(set4.contains(null), "set contains null");
159 
160         final Set<?> set5 = SetUtils.hashSet((Object[]) null);
161         assertNull(set5, "set is null");
162     }
163 
164     @Test
165     public void testIntersection() {
166         final SetView<Integer> set = SetUtils.intersection(setA, setB);
167         assertEquals(3, set.size());
168         assertTrue(set.contains(3));
169         assertTrue(set.contains(4));
170         assertTrue(set.contains(5));
171         assertFalse(set.contains(1));
172         assertFalse(set.contains(2));
173         assertFalse(set.contains(6));
174         assertFalse(set.contains(7));
175 
176         final Set<Integer> set2 = SetUtils.intersection(setA, SetUtils.<Integer>emptySet());
177         assertEquals(SetUtils.<Integer>emptySet(), set2);
178 
179         assertThrows(NullPointerException.class, () -> SetUtils.intersection(setA, null));
180         assertThrows(NullPointerException.class, () -> SetUtils.intersection(null, setA));
181     }
182 
183     @Test
184     public void testNewIdentityHashSet() {
185         final Set<String> set = SetUtils.newIdentityHashSet();
186         final String a = new String("a");
187         set.add(a);
188         set.add(new String("b"));
189         set.add(a);
190 
191         assertEquals(2, set.size());
192 
193         set.add(new String("a"));
194         assertEquals(3, set.size());
195 
196         set.remove(a);
197         assertEquals(2, set.size());
198     }
199 
200     @Test
201     public void testpredicatedSet() {
202         final Predicate<Object> predicate = String.class::isInstance;
203         final Set<Object> set = SetUtils.predicatedSet(new HashSet<>(), predicate);
204         assertInstanceOf(PredicatedSet.class, set, "returned object should be a PredicatedSet");
205         assertThrows(NullPointerException.class, () -> SetUtils.predicatedSet(new HashSet<>(), null), "Expecting NullPointerException for null predicate.");
206         assertThrows(NullPointerException.class, () -> SetUtils.predicatedSet(null, predicate), "Expecting NullPointerException for null set.");
207     }
208 
209     @Test
210     public void testUnion() {
211         final SetView<Integer> set = SetUtils.union(setA, setB);
212         assertEquals(7, set.size());
213         assertTrue(set.containsAll(setA));
214         assertTrue(set.containsAll(setB));
215         final Set<Integer> set2 = SetUtils.union(setA, SetUtils.<Integer>emptySet());
216         assertEquals(setA, set2);
217         assertThrows(NullPointerException.class, () -> SetUtils.union(setA, null));
218         assertThrows(NullPointerException.class, () -> SetUtils.union(null, setA));
219     }
220 
221     @Test
222     public void testUnmodifiableSet() {
223         final Set<?> set1 = SetUtils.unmodifiableSet();
224         assertTrue(set1.isEmpty(), "set is empty");
225 
226         final Set<Integer> set2 = SetUtils.unmodifiableSet(1, 2, 2, 3);
227         assertEquals(3, set2.size(), "set has 3 elements");
228         assertTrue(set2.contains(1), "set contains 1");
229         assertTrue(set2.contains(2), "set contains 2");
230         assertTrue(set2.contains(3), "set contains 3");
231 
232         final Set<String> set3 = SetUtils.unmodifiableSet("1", "2", "2", "3");
233         assertEquals(3, set3.size(), "set has 3 elements");
234         assertTrue(set3.contains("1"), "set contains 1");
235         assertTrue(set3.contains("2"), "set contains 2");
236         assertTrue(set3.contains("3"), "set contains 3");
237 
238         final Set<?> set4 = SetUtils.unmodifiableSet(null, null);
239         assertEquals(1, set4.size(), "set has 1 element");
240         assertTrue(set4.contains(null), "set contains null");
241 
242         final Set<?> set5 = SetUtils.unmodifiableSet((Object[]) null);
243         assertNull(set5, "set is null");
244     }
245 
246     @Test
247     public void testUnmodifiableSetWrap() {
248         final Set<Integer> set1 = SetUtils.unmodifiableSet(1, 2, 2, 3);
249         final Set<Integer> set2 = SetUtils.unmodifiableSet(set1);
250         assertSame(set1, set2);
251     }
252 
253 }