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.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28
29 import org.apache.commons.collections4.set.CompositeSet.SetMutator;
30 import org.junit.jupiter.api.Test;
31
32
33
34
35
36 public class CompositeSetTest<E> extends AbstractSetTest<E> {
37
38 @SuppressWarnings("unchecked")
39 public Set<E> buildOne() {
40 final HashSet<E> set = new HashSet<>();
41 set.add((E) "1");
42 set.add((E) "2");
43 return set;
44 }
45
46 @SuppressWarnings("unchecked")
47 public Set<E> buildTwo() {
48 final HashSet<E> set = new HashSet<>();
49 set.add((E) "3");
50 set.add((E) "4");
51 return set;
52 }
53
54 @Override
55 public String getCompatibilityVersion() {
56 return "4";
57 }
58
59 @Override
60 protected int getIterationBehaviour() {
61 return UNORDERED;
62 }
63
64 @Override
65 public CompositeSet<E> makeObject() {
66 final HashSet<E> contained = new HashSet<>();
67 final CompositeSet<E> set = new CompositeSet<>(contained);
68 set.setMutator(new EmptySetMutator<>(contained));
69 return set;
70 }
71
72 @Test
73 @SuppressWarnings("unchecked")
74 public void testAddComposited() {
75 final Set<E> one = buildOne();
76 final Set<E> two = buildTwo();
77 final CompositeSet<E> set = new CompositeSet<>();
78 set.addComposited(one, two);
79 set.addComposited((Set<E>) null);
80 set.addComposited((Set<E>[]) null);
81 set.addComposited(null, null);
82 set.addComposited(null, null, null);
83 final CompositeSet<E> set2 = new CompositeSet<>(buildOne());
84 set2.addComposited(buildTwo());
85 assertEquals(set, set2);
86 final HashSet<E> set3 = new HashSet<>();
87 set3.add((E) "1");
88 set3.add((E) "2");
89 set3.add((E) "3");
90 final HashSet<E> set4 = new HashSet<>();
91 set4.add((E) "4");
92 final CompositeSet<E> set5 = new CompositeSet<>(set3);
93 set5.addComposited(set4);
94 assertEquals(set, set5);
95 assertThrows(UnsupportedOperationException.class, () -> set.addComposited(set3),
96 "Expecting UnsupportedOperationException.");
97 }
98
99 @Test
100 @SuppressWarnings("unchecked")
101 public void testAddCompositedCollision() {
102 final HashSet<E> set1 = new HashSet<>();
103 set1.add((E) "1");
104 set1.add((E) "2");
105 set1.add((E) "3");
106 final HashSet<E> set2 = new HashSet<>();
107 set2.add((E) "4");
108 final CompositeSet<E> set3 = new CompositeSet<>(set1);
109 assertThrows(UnsupportedOperationException.class, () -> set3.addComposited(set1, buildOne()),
110 "Expecting UnsupportedOperationException.");
111 assertThrows(UnsupportedOperationException.class, () -> set3.addComposited(set1, buildOne(), buildTwo()),
112 "Expecting UnsupportedOperationException.");
113 }
114
115 @Test
116 @SuppressWarnings("unchecked")
117 public void testContains() {
118 final CompositeSet<E> set = new CompositeSet<>(buildOne(), buildTwo());
119 assertTrue(set.contains("1"));
120 }
121
122 @Test
123 @SuppressWarnings("unchecked")
124 public void testContainsAll() {
125 final CompositeSet<E> set = new CompositeSet<>(buildOne(), buildTwo());
126 assertFalse(set.containsAll(null));
127 }
128
129 @Test
130 @SuppressWarnings("unchecked")
131 public void testFailedCollisionResolution() {
132 final Set<E> one = buildOne();
133 final Set<E> two = buildTwo();
134 final CompositeSet<E> set = new CompositeSet<>(one, two);
135 set.setMutator(new SetMutator<E>() {
136 private static final long serialVersionUID = 1L;
137
138 @Override
139 public boolean add(final CompositeSet<E> composite,
140 final List<Set<E>> collections, final E obj) {
141 throw new UnsupportedOperationException();
142 }
143
144 @Override
145 public boolean addAll(final CompositeSet<E> composite,
146 final List<Set<E>> collections, final Collection<? extends E> coll) {
147 throw new UnsupportedOperationException();
148 }
149
150 @Override
151 public void resolveCollision(final CompositeSet<E> comp, final Set<E> existing,
152 final Set<E> added, final Collection<E> intersects) {
153
154 }
155 });
156
157 final HashSet<E> three = new HashSet<>();
158 three.add((E) "1");
159 assertThrows(IllegalArgumentException.class, () -> set.addComposited(three),
160 "IllegalArgumentException should have been thrown");
161 }
162
163 @Test
164 @SuppressWarnings("unchecked")
165 public void testRemoveAll() {
166 final CompositeSet<E> set = new CompositeSet<>(buildOne(), buildTwo());
167 assertFalse(set.removeAll(null));
168 }
169
170 @Test
171 @SuppressWarnings("unchecked")
172 public void testRemoveComposited() {
173 final Set<E> one = buildOne();
174 final Set<E> two = buildTwo();
175 final CompositeSet<E> set = new CompositeSet<>(one, two);
176 set.remove("1");
177 assertFalse(one.contains("1"));
178
179 set.remove("3");
180 assertFalse(one.contains("3"));
181 }
182
183 @Test
184 @SuppressWarnings("unchecked")
185 public void testRemoveUnderlying() {
186 final Set<E> one = buildOne();
187 final Set<E> two = buildTwo();
188 final CompositeSet<E> set = new CompositeSet<>(one, two);
189 one.remove("1");
190 assertFalse(set.contains("1"));
191
192 two.remove("3");
193 assertFalse(set.contains("3"));
194 }
195
196
197
198
199
200
201
202
203 }