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