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.collections.set;
18 
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21 
22 import org.apache.commons.collections.collection.CompositeCollection;
23 
24 import java.util.Set;
25 import java.util.HashSet;
26 import java.util.Collection;
27 
28 /**
29  * Extension of {@link AbstractTestSet} for exercising the
30  * {@link CompositeSet} implementation.
31  *
32  * @since Commons Collections 3.0
33  * @version $Revision: 646780 $ $Date: 2008-04-10 14:48:07 +0200 (Thu, 10 Apr 2008) $
34  *
35  * @author Brian McCallister
36  * @author Phil Steitz
37  */
38 
39 public class TestCompositeSet extends AbstractTestSet {
TestCompositeSet(String name)40     public TestCompositeSet(String name) {
41         super(name);
42     }
43 
suite()44     public static Test suite() {
45         return new TestSuite(TestCompositeSet.class);
46     }
47 
makeEmptySet()48     public Set makeEmptySet() {
49         final HashSet contained = new HashSet();
50         CompositeSet set = new CompositeSet(contained);
51         set.setMutator(new CompositeSet.SetMutator() {
52             public void resolveCollision(CompositeSet comp, Set existing,
53                 Set added, Collection intersects) {
54                 throw new IllegalArgumentException();
55             }
56 
57             public boolean add(CompositeCollection composite,
58                 Collection[] collections, Object obj) {
59                 return contained.add(obj);
60             }
61 
62             public boolean addAll(CompositeCollection composite,
63                 Collection[] collections, Collection coll) {
64                 return contained.addAll(coll);
65             }
66 
67             public boolean remove(CompositeCollection composite,
68                 Collection[] collections, Object obj) {
69                 return contained.remove(obj);
70             }
71         });
72         return set;
73     }
74 
buildOne()75     public Set buildOne() {
76         HashSet set = new HashSet();
77         set.add("1");
78         set.add("2");
79         return set;
80     }
81 
buildTwo()82     public Set buildTwo() {
83         HashSet set = new HashSet();
84         set.add("3");
85         set.add("4");
86         return set;
87     }
88 
testContains()89     public void testContains() {
90         CompositeSet set = new CompositeSet(new Set[]{buildOne(), buildTwo()});
91         assertTrue(set.contains("1"));
92     }
93 
testRemoveUnderlying()94     public void testRemoveUnderlying() {
95         Set one = buildOne();
96         Set two = buildTwo();
97         CompositeSet set = new CompositeSet(new Set[]{one, two});
98         one.remove("1");
99         assertFalse(set.contains("1"));
100 
101         two.remove("3");
102         assertFalse(set.contains("3"));
103     }
104 
testRemoveComposited()105     public void testRemoveComposited() {
106         Set one = buildOne();
107         Set two = buildTwo();
108         CompositeSet set = new CompositeSet(new Set[]{one, two});
109         set.remove("1");
110         assertFalse(one.contains("1"));
111 
112         set.remove("3");
113         assertFalse(one.contains("3"));
114     }
115 
testFailedCollisionResolution()116     public void testFailedCollisionResolution() {
117         Set one = buildOne();
118         Set two = buildTwo();
119         CompositeSet set = new CompositeSet(new Set[]{one, two});
120         set.setMutator(new CompositeSet.SetMutator() {
121             public void resolveCollision(CompositeSet comp, Set existing,
122                 Set added, Collection intersects) {
123             }
124 
125             public boolean add(CompositeCollection composite,
126                 Collection[] collections, Object obj) {
127                 throw new UnsupportedOperationException();
128             }
129 
130             public boolean addAll(CompositeCollection composite,
131                 Collection[] collections, Collection coll) {
132                 throw new UnsupportedOperationException();
133             }
134 
135             public boolean remove(CompositeCollection composite,
136                 Collection[] collections, Object obj) {
137                 throw new UnsupportedOperationException();
138             }
139         });
140 
141         HashSet three = new HashSet();
142         three.add("1");
143         try {
144             set.addComposited(three);
145             fail("IllegalArgumentException should have been thrown");
146         }
147         catch (IllegalArgumentException e) {
148             // expected
149         }
150     }
151 
testAddComposited()152     public void testAddComposited() {
153         Set one = buildOne();
154         Set two = buildTwo();
155         CompositeSet set = new CompositeSet();
156         set.addComposited(one, two);
157         CompositeSet set2 = new CompositeSet(buildOne());
158         set2.addComposited(buildTwo());
159         assertTrue(set.equals(set2));
160         HashSet set3 = new HashSet();
161         set3.add("1");
162         set3.add("2");
163         set3.add("3");
164         HashSet set4 = new HashSet();
165         set4.add("4");
166         CompositeSet set5 = new CompositeSet(set3);
167         set5.addComposited(set4);
168         assertTrue(set.equals(set5));
169         try {
170             set.addComposited(set3);
171             fail("Expecting UnsupportedOperationException.");
172         } catch (UnsupportedOperationException ex) {
173             // expected
174         }
175     }
176 }
177