KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > set > TestCompositeSet


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.set;
17
18 import junit.framework.Test;
19 import junit.framework.TestSuite;
20
21 import org.apache.commons.collections.collection.CompositeCollection;
22
23 import java.util.Set JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Collection JavaDoc;
26
27 /**
28  * Extension of {@link AbstractTestSet} for exercising the
29  * {@link CompositeSet} implementation.
30  *
31  * @since Commons Collections 3.0
32  * @version $Revision: 1.3 $ $Date: 2004/02/18 01:20:39 $
33  *
34  * @author Brian McCallister
35  * @author Phil Steitz
36  */

37
38 public class TestCompositeSet extends AbstractTestSet {
39     public TestCompositeSet(String JavaDoc name) {
40         super(name);
41     }
42     
43     public static Test suite() {
44         return new TestSuite(TestCompositeSet.class);
45     }
46     
47     public Set JavaDoc makeEmptySet() {
48         final HashSet JavaDoc contained = new HashSet JavaDoc();
49         CompositeSet set = new CompositeSet(contained);
50         set.setMutator(new CompositeSet.SetMutator() {
51             public void resolveCollision(CompositeSet comp, Set JavaDoc existing,
52                 Set JavaDoc added, Collection intersects) {
53                 throw new IllegalArgumentException JavaDoc();
54             }
55             
56             public boolean add(CompositeCollection composite,
57                 Collection[] collections, Object JavaDoc obj) {
58                 return contained.add(obj);
59             }
60             
61             public boolean addAll(CompositeCollection composite,
62                 Collection[] collections, Collection coll) {
63                 return contained.addAll(coll);
64             }
65             
66             public boolean remove(CompositeCollection composite,
67                 Collection[] collections, Object JavaDoc obj) {
68                 return contained.remove(obj);
69             }
70         });
71         return set;
72     }
73     
74     public Set JavaDoc buildOne() {
75         HashSet JavaDoc set = new HashSet JavaDoc();
76         set.add("1");
77         set.add("2");
78         return set;
79     }
80     
81     public Set JavaDoc buildTwo() {
82         HashSet JavaDoc set = new HashSet JavaDoc();
83         set.add("3");
84         set.add("4");
85         return set;
86     }
87     
88     public void testContains() {
89         CompositeSet set = new CompositeSet(new Set JavaDoc[]{buildOne(), buildTwo()});
90         assertTrue(set.contains("1"));
91     }
92     
93     public void testRemoveUnderlying() {
94         Set JavaDoc one = buildOne();
95         Set JavaDoc two = buildTwo();
96         CompositeSet set = new CompositeSet(new Set JavaDoc[]{one, two});
97         one.remove("1");
98         assertFalse(set.contains("1"));
99         
100         two.remove("3");
101         assertFalse(set.contains("3"));
102     }
103     
104     public void testRemoveComposited() {
105         Set JavaDoc one = buildOne();
106         Set JavaDoc two = buildTwo();
107         CompositeSet set = new CompositeSet(new Set JavaDoc[]{one, two});
108         set.remove("1");
109         assertFalse(one.contains("1"));
110         
111         set.remove("3");
112         assertFalse(one.contains("3"));
113     }
114     
115     public void testFailedCollisionResolution() {
116         Set JavaDoc one = buildOne();
117         Set JavaDoc two = buildTwo();
118         CompositeSet set = new CompositeSet(new Set JavaDoc[]{one, two});
119         set.setMutator(new CompositeSet.SetMutator() {
120             public void resolveCollision(CompositeSet comp, Set JavaDoc existing,
121                 Set JavaDoc added, Collection intersects) {
122             }
123             
124             public boolean add(CompositeCollection composite,
125                 Collection[] collections, Object JavaDoc obj) {
126                 throw new UnsupportedOperationException JavaDoc();
127             }
128             
129             public boolean addAll(CompositeCollection composite,
130                 Collection[] collections, Collection coll) {
131                 throw new UnsupportedOperationException JavaDoc();
132             }
133             
134             public boolean remove(CompositeCollection composite,
135                 Collection[] collections, Object JavaDoc obj) {
136                 throw new UnsupportedOperationException JavaDoc();
137             }
138         });
139         
140         HashSet JavaDoc three = new HashSet JavaDoc();
141         three.add("1");
142         try {
143             set.addComposited(three);
144             fail("IllegalArgumentException should have been thrown");
145         }
146         catch (IllegalArgumentException JavaDoc e) {
147             // expected
148
}
149     }
150     
151     public void testAddComposited() {
152         Set JavaDoc one = buildOne();
153         Set JavaDoc two = buildTwo();
154         CompositeSet set = new CompositeSet();
155         set.addComposited(one, two);
156         CompositeSet set2 = new CompositeSet(buildOne());
157         set2.addComposited(buildTwo());
158         assertTrue(set.equals(set2));
159         HashSet JavaDoc set3 = new HashSet JavaDoc();
160         set3.add("1");
161         set3.add("2");
162         set3.add("3");
163         HashSet JavaDoc set4 = new HashSet JavaDoc();
164         set4.add("4");
165         CompositeSet set5 = new CompositeSet(set3);
166         set5.addComposited(set4);
167         assertTrue(set.equals(set5));
168         try {
169             set.addComposited(set3);
170             fail("Expecting UnsupportedOperationException.");
171         } catch (UnsupportedOperationException JavaDoc ex) {
172             // expected
173
}
174     }
175 }
176
Popular Tags