KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > collection > TestCompositeCollection


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.collection;
17
18 import java.util.Arrays JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 /**
27  * Extension of {@link AbstractTestCollection} for exercising the
28  * {@link CompositeCollection} implementation.
29  *
30  * @since Commons Collections 3.0
31  * @version $Revision: 1.6 $ $Date: 2004/02/18 01:20:40 $
32  *
33  * @author Brian McCallister
34  * @author Phil Steitz
35  */

36 public class TestCompositeCollection extends AbstractTestCollection {
37     
38     public TestCompositeCollection(String JavaDoc name) {
39         super(name);
40     }
41     
42     public static Test suite() {
43         return new TestSuite(TestCompositeCollection.class);
44     }
45
46     public static void main(String JavaDoc args[]) {
47         String JavaDoc[] testCaseName = { TestCompositeCollection.class.getName()};
48         junit.textui.TestRunner.main(testCaseName);
49     }
50  
51  //-----------------------------------------------------------------------------
52
/**
53      * Run stock collection tests without Mutator, so turn off add, remove
54      */

55     public boolean isAddSupported() {
56         return false;
57     }
58     
59     public boolean isRemoveSupported() {
60         return false;
61     }
62     
63     /**
64      * Empty collection is empty composite
65      */

66     public Collection JavaDoc makeCollection() {
67         return new CompositeCollection();
68     }
69     
70     public Collection JavaDoc makeConfirmedCollection() {
71         return new HashSet JavaDoc();
72     }
73     
74     public Object JavaDoc[] getFullElements() {
75         return new Object JavaDoc[] {"1", "2", "3", "4"};
76     }
77     
78     /**
79      * Full collection consists of 4 collections, each with one element
80      */

81     public Collection JavaDoc makeFullCollection() {
82         CompositeCollection compositeCollection = new CompositeCollection();
83         Object JavaDoc[] elements = getFullElements();
84         for (int i = 0; i < elements.length; i++) {
85             Collection JavaDoc summand = new HashSet JavaDoc();
86             summand.add(elements[i]);
87             compositeCollection.addComposited(summand);
88         }
89         return compositeCollection;
90     }
91     
92     /**
93      * Full collection should look like a collection with 4 elements
94      */

95     public Collection JavaDoc makeConfirmedFullCollection() {
96         Collection JavaDoc collection = new HashSet JavaDoc();
97         collection.addAll(Arrays.asList(getFullElements()));
98         return collection;
99     }
100     
101     /**
102      * Override testUnsupportedRemove, since the default impl expects removeAll,
103      * retainAll and iterator().remove to throw
104      */

105     public void testUnsupportedRemove() {
106         resetFull();
107         try {
108             collection.remove(null);
109             fail("remove should raise UnsupportedOperationException");
110         } catch (UnsupportedOperationException JavaDoc e) {
111             // expected
112
}
113         verify();
114     }
115     
116     //--------------------------------------------------------------------------
117

118     protected CompositeCollection c;
119     protected Collection JavaDoc one;
120     protected Collection JavaDoc two;
121     
122     protected void setUpTest() {
123         c = new CompositeCollection();
124         one = new HashSet JavaDoc();
125         two = new HashSet JavaDoc();
126     }
127     
128     protected void setUpMutatorTest() {
129         setUpTest();
130         c.setMutator(new CompositeCollection.CollectionMutator() {
131             public boolean add(CompositeCollection composite,
132             Collection JavaDoc[] collections, Object JavaDoc obj) {
133                 for (int i = 0; i < collections.length; i++) {
134                     collections[i].add(obj);
135                 }
136                 return true;
137             }
138             
139             public boolean addAll(CompositeCollection composite,
140             Collection JavaDoc[] collections, Collection JavaDoc coll) {
141                 for (int i = 0; i < collections.length; i++) {
142                     collections[i].addAll(coll);
143                 }
144                 return true;
145             }
146             
147             public boolean remove(CompositeCollection composite,
148             Collection JavaDoc[] collections, Object JavaDoc obj) {
149                 for (int i = 0; i < collections.length; i++) {
150                     collections[i].remove(obj);
151                 }
152                 return true;
153             }
154         });
155     }
156             
157     public void testSize() {
158         setUpTest();
159         HashSet JavaDoc set = new HashSet JavaDoc();
160         set.add("a");
161         set.add("b");
162         c.addComposited(set);
163         assertEquals(set.size(), c.size());
164     }
165     
166     public void testMultipleCollectionsSize() {
167         setUpTest();
168         HashSet JavaDoc set = new HashSet JavaDoc();
169         set.add("a");
170         set.add("b");
171         c.addComposited(set);
172         HashSet JavaDoc other = new HashSet JavaDoc();
173         other.add("c");
174         c.addComposited(other);
175         assertEquals(set.size() + other.size(), c.size());
176     }
177     
178     public void testIsEmpty() {
179         setUpTest();
180         assertTrue(c.isEmpty());
181         HashSet JavaDoc empty = new HashSet JavaDoc();
182         c.addComposited(empty);
183         assertTrue(c.isEmpty());
184         empty.add("a");
185         assertTrue(!c.isEmpty());
186     }
187     
188     
189     public void testIterator() {
190         setUpTest();
191         one.add("1");
192         two.add("2");
193         c.addComposited(one);
194         c.addComposited(two);
195         Iterator JavaDoc i = c.iterator();
196         Object JavaDoc next = i.next();
197         assertTrue(c.contains(next));
198         assertTrue(one.contains(next));
199         next = i.next();
200         i.remove();
201         assertTrue(!c.contains(next));
202         assertTrue(!two.contains(next));
203     }
204     
205     public void testClear() {
206         setUpTest();
207         one.add("1");
208         two.add("2");
209         c.addComposited(one, two);
210         c.clear();
211         assertTrue(one.isEmpty());
212         assertTrue(two.isEmpty());
213         assertTrue(c.isEmpty());
214     }
215     
216     public void testContainsAll() {
217         setUpTest();
218         one.add("1");
219         two.add("1");
220         c.addComposited(one);
221         assertTrue(c.containsAll(two));
222     }
223     
224     public void testRetainAll() {
225         setUpTest();
226         one.add("1");
227         one.add("2");
228         two.add("1");
229         c.addComposited(one);
230         c.retainAll(two);
231         assertTrue(!c.contains("2"));
232         assertTrue(!one.contains("2"));
233         assertTrue(c.contains("1"));
234         assertTrue(one.contains("1"));
235     }
236     
237     public void testAddAllMutator() {
238         setUpTest();
239         c.setMutator(new CompositeCollection.CollectionMutator() {
240             public boolean add(CompositeCollection composite,
241             Collection JavaDoc[] collections, Object JavaDoc obj) {
242                 for (int i = 0; i < collections.length; i++) {
243                     collections[i].add(obj);
244                 }
245                 return true;
246             }
247             
248             public boolean addAll(CompositeCollection composite,
249             Collection JavaDoc[] collections, Collection JavaDoc coll) {
250                 for (int i = 0; i < collections.length; i++) {
251                     collections[i].addAll(coll);
252                 }
253                 return true;
254             }
255             
256             public boolean remove(CompositeCollection composite,
257             Collection JavaDoc[] collections, Object JavaDoc obj) {
258                 return false;
259             }
260         });
261         
262         c.addComposited(one);
263         two.add("foo");
264         c.addAll(two);
265         assertTrue(c.contains("foo"));
266         assertTrue(one.contains("foo"));
267     }
268     
269     public void testAddMutator() {
270         setUpTest();
271         c.setMutator(new CompositeCollection.CollectionMutator() {
272             public boolean add(CompositeCollection composite,
273             Collection JavaDoc[] collections, Object JavaDoc obj) {
274                 for (int i = 0; i < collections.length; i++) {
275                     collections[i].add(obj);
276                 }
277                 return true;
278             }
279             
280             public boolean addAll(CompositeCollection composite,
281             Collection JavaDoc[] collections, Collection JavaDoc coll) {
282                 for (int i = 0; i < collections.length; i++) {
283                     collections[i].addAll(coll);
284                 }
285                 return true;
286             }
287             
288             public boolean remove(CompositeCollection composite,
289             Collection JavaDoc[] collections, Object JavaDoc obj) {
290                 return false;
291             }
292         });
293         
294         c.addComposited(one);
295         c.add("foo");
296         assertTrue(c.contains("foo"));
297         assertTrue(one.contains("foo"));
298     }
299     
300     public void testToCollection() {
301         setUpTest();
302         one.add("1");
303         two.add("2");
304         c.addComposited(one, two);
305         Collection JavaDoc foo = c.toCollection();
306         assertTrue(foo.containsAll(c));
307         assertEquals(c.size(), foo.size());
308         one.add("3");
309         assertTrue(!foo.containsAll(c));
310     }
311     
312     public void testAddAllToCollection() {
313         setUpTest();
314         one.add("1");
315         two.add("2");
316         c.addComposited(one, two);
317         Collection JavaDoc toCollection = new HashSet JavaDoc();
318         toCollection.addAll(c);
319         assertTrue(toCollection.containsAll(c));
320         assertEquals(c.size(), toCollection.size());
321     }
322     
323     public void testRemove() {
324         setUpMutatorTest();
325         one.add("1");
326         two.add("2");
327         two.add("1");
328         c.addComposited(one, two);
329         c.remove("1");
330         assertTrue(!c.contains("1"));
331         assertTrue(!one.contains("1"));
332         assertTrue(!two.contains("1"));
333     }
334     
335     public void testRemoveAll() {
336         setUpMutatorTest();
337         one.add("1");
338         two.add("2");
339         two.add("1");
340         c.addComposited(one, two);
341         c.removeAll(one);
342         assertTrue(!c.contains("1"));
343         assertTrue(!one.contains("1"));
344         assertTrue(!two.contains("1"));
345     }
346     
347     public void testRemoveComposited() {
348         setUpMutatorTest();
349         one.add("1");
350         two.add("2");
351         two.add("1");
352         c.addComposited(one, two);
353         c.removeComposited(one);
354         assertTrue(c.contains("1"));
355         assertEquals(c.size(), 2);
356     }
357 }
358
Popular Tags