KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > bag > AbstractTestBag


1 /*
2  * Copyright 2001-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.bag;
17
18 import java.io.IOException JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.ConcurrentModificationException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25
26 import org.apache.commons.collections.AbstractTestObject;
27 import org.apache.commons.collections.Bag;
28
29 /**
30  * Abstract test class for {@link org.apache.commons.collections.Bag Bag} methods and contracts.
31  * <p>
32  * To use, simply extend this class, and implement
33  * the {@link #makeBag} method.
34  * <p>
35  * If your bag fails one of these tests by design,
36  * you may still use this base set of cases. Simply override the
37  * test case (method) your bag fails.
38  *
39  * @version $Revision: 1.9 $ $Date: 2004/05/31 22:39:20 $
40  *
41  * @author Chuck Burdick
42  * @author Stephen Colebourne
43  */

44 public abstract class AbstractTestBag extends AbstractTestObject {
45 // TODO: this class should really extend from TestCollection, but the bag
46
// implementations currently do not conform to the Collection interface. Once
47
// those are fixed or at least a strategy is made for resolving the issue, this
48
// can be changed back to extend TestCollection instead.
49

50     /**
51      * JUnit constructor.
52      *
53      * @param testName the test class name
54      */

55     public AbstractTestBag(String JavaDoc testName) {
56         super(testName);
57     }
58
59     //-----------------------------------------------------------------------
60
/**
61      * Return a new, empty bag to used for testing.
62      *
63      * @return the bag to be tested
64      */

65     public abstract Bag makeBag();
66
67     /**
68      * Implements the superclass method to return the Bag.
69      *
70      * @return the bag to be tested
71      */

72     public Object JavaDoc makeObject() {
73         return makeBag();
74     }
75
76     //-----------------------------------------------------------------------
77
public void testBagAdd() {
78         Bag bag = makeBag();
79         bag.add("A");
80         assertTrue("Should contain 'A'", bag.contains("A"));
81         assertEquals("Should have count of 1", 1, bag.getCount("A"));
82         bag.add("A");
83         assertTrue("Should contain 'A'", bag.contains("A"));
84         assertEquals("Should have count of 2", 2, bag.getCount("A"));
85         bag.add("B");
86         assertTrue(bag.contains("A"));
87         assertTrue(bag.contains("B"));
88     }
89
90     public void testBagEqualsSelf() {
91         Bag bag = makeBag();
92         assertTrue(bag.equals(bag));
93         bag.add("elt");
94         assertTrue(bag.equals(bag));
95         bag.add("elt"); // again
96
assertTrue(bag.equals(bag));
97         bag.add("elt2");
98         assertTrue(bag.equals(bag));
99     }
100
101     public void testRemove() {
102         Bag bag = makeBag();
103         bag.add("A");
104         assertEquals("Should have count of 1", 1, bag.getCount("A"));
105         bag.remove("A");
106         assertEquals("Should have count of 0", 0, bag.getCount("A"));
107         bag.add("A");
108         bag.add("A");
109         bag.add("A");
110         bag.add("A");
111         assertEquals("Should have count of 4", 4, bag.getCount("A"));
112         bag.remove("A", 0);
113         assertEquals("Should have count of 4", 4, bag.getCount("A"));
114         bag.remove("A", 2);
115         assertEquals("Should have count of 2", 2, bag.getCount("A"));
116         bag.remove("A");
117         assertEquals("Should have count of 0", 0, bag.getCount("A"));
118     }
119
120     public void testRemoveAll() {
121         Bag bag = makeBag();
122         bag.add("A", 2);
123         assertEquals("Should have count of 2", 2, bag.getCount("A"));
124         bag.add("B");
125         bag.add("C");
126         assertEquals("Should have count of 4", 4, bag.size());
127         List JavaDoc delete = new ArrayList JavaDoc();
128         delete.add("A");
129         delete.add("B");
130         bag.removeAll(delete);
131         assertEquals("Should have count of 1", 1, bag.getCount("A"));
132         assertEquals("Should have count of 0", 0, bag.getCount("B"));
133         assertEquals("Should have count of 1", 1, bag.getCount("C"));
134         assertEquals("Should have count of 2", 2, bag.size());
135     }
136     
137     public void testContains() {
138         Bag bag = makeBag();
139         
140         assertEquals("Bag does not have at least 1 'A'", false, bag.contains("A"));
141         assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));
142         
143         bag.add("A"); // bag 1A
144
assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
145         assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));
146         
147         bag.add("A"); // bag 2A
148
assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
149         assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));
150         
151         bag.add("B"); // bag 2A,1B
152
assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
153         assertEquals("Bag has at least 1 'B'", true, bag.contains("B"));
154     }
155
156     public void testContainsAll() {
157         Bag bag = makeBag();
158         List JavaDoc known = new ArrayList JavaDoc();
159         List JavaDoc known1A = new ArrayList JavaDoc();
160         known1A.add("A");
161         List JavaDoc known2A = new ArrayList JavaDoc();
162         known2A.add("A");
163         known2A.add("A");
164         List JavaDoc known1B = new ArrayList JavaDoc();
165         known1B.add("B");
166         List JavaDoc known1A1B = new ArrayList JavaDoc();
167         known1A1B.add("A");
168         known1A1B.add("B");
169         
170         assertEquals("Bag containsAll of empty", true, bag.containsAll(known));
171         assertEquals("Bag does not containsAll of 1 'A'", false, bag.containsAll(known1A));
172         assertEquals("Bag does not containsAll of 2 'A'", false, bag.containsAll(known2A));
173         assertEquals("Bag does not containsAll of 1 'B'", false, bag.containsAll(known1B));
174         assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, bag.containsAll(known1A1B));
175         
176         bag.add("A"); // bag 1A
177
assertEquals("Bag containsAll of empty", true, bag.containsAll(known));
178         assertEquals("Bag containsAll of 1 'A'", true, bag.containsAll(known1A));
179         assertEquals("Bag does not containsAll of 2 'A'", false, bag.containsAll(known2A));
180         assertEquals("Bag does not containsAll of 1 'B'", false, bag.containsAll(known1B));
181         assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, bag.containsAll(known1A1B));
182         
183         bag.add("A"); // bag 2A
184
assertEquals("Bag containsAll of empty", true, bag.containsAll(known));
185         assertEquals("Bag containsAll of 1 'A'", true, bag.containsAll(known1A));
186         assertEquals("Bag containsAll of 2 'A'", true, bag.containsAll(known2A));
187         assertEquals("Bag does not containsAll of 1 'B'", false, bag.containsAll(known1B));
188         assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, bag.containsAll(known1A1B));
189         
190         bag.add("A"); // bag 3A
191
assertEquals("Bag containsAll of empty", true, bag.containsAll(known));
192         assertEquals("Bag containsAll of 1 'A'", true, bag.containsAll(known1A));
193         assertEquals("Bag containsAll of 2 'A'", true, bag.containsAll(known2A));
194         assertEquals("Bag does not containsAll of 1 'B'", false, bag.containsAll(known1B));
195         assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, bag.containsAll(known1A1B));
196         
197         bag.add("B"); // bag 3A1B
198
assertEquals("Bag containsAll of empty", true, bag.containsAll(known));
199         assertEquals("Bag containsAll of 1 'A'", true, bag.containsAll(known1A));
200         assertEquals("Bag containsAll of 2 'A'", true, bag.containsAll(known2A));
201         assertEquals("Bag containsAll of 1 'B'", true, bag.containsAll(known1B));
202         assertEquals("Bag containsAll of 1 'A' 1 'B'", true, bag.containsAll(known1A1B));
203     }
204
205     public void testSize() {
206         Bag bag = makeBag();
207         assertEquals("Should have 0 total items", 0, bag.size());
208         bag.add("A");
209         assertEquals("Should have 1 total items", 1, bag.size());
210         bag.add("A");
211         assertEquals("Should have 2 total items", 2, bag.size());
212         bag.add("A");
213         assertEquals("Should have 3 total items", 3, bag.size());
214         bag.add("B");
215         assertEquals("Should have 4 total items", 4, bag.size());
216         bag.add("B");
217         assertEquals("Should have 5 total items", 5, bag.size());
218         bag.remove("A", 2);
219         assertEquals("Should have 1 'A'", 1, bag.getCount("A"));
220         assertEquals("Should have 3 total items", 3, bag.size());
221         bag.remove("B");
222         assertEquals("Should have 1 total item", 1, bag.size());
223     }
224     
225     public void testRetainAll() {
226         Bag bag = makeBag();
227         bag.add("A");
228         bag.add("A");
229         bag.add("A");
230         bag.add("B");
231         bag.add("B");
232         bag.add("C");
233         List JavaDoc retains = new ArrayList JavaDoc();
234         retains.add("B");
235         retains.add("C");
236         bag.retainAll(retains);
237         assertEquals("Should have 2 total items", 2, bag.size());
238     }
239
240     public void testIterator() {
241         Bag bag = makeBag();
242         bag.add("A");
243         bag.add("A");
244         bag.add("B");
245         assertEquals("Bag should have 3 items", 3, bag.size());
246         Iterator JavaDoc i = bag.iterator();
247     
248         boolean foundA = false;
249         while (i.hasNext()) {
250             String JavaDoc element = (String JavaDoc) i.next();
251             // ignore the first A, remove the second via Iterator.remove()
252
if (element.equals("A")) {
253                 if (foundA == false) {
254                     foundA = true;
255                 } else {
256                     i.remove();
257                 }
258             }
259         }
260     
261         assertTrue("Bag should still contain 'A'", bag.contains("A"));
262         assertEquals("Bag should have 2 items", 2, bag.size());
263         assertEquals("Bag should have 1 'A'", 1, bag.getCount("A"));
264     }
265
266     public void testIteratorFail() {
267         Bag bag = makeBag();
268         bag.add("A");
269         bag.add("A");
270         bag.add("B");
271         Iterator JavaDoc it = bag.iterator();
272         it.next();
273         bag.remove("A");
274         try {
275             it.next();
276             fail("Should throw ConcurrentModificationException");
277         } catch (ConcurrentModificationException JavaDoc e) {
278             // expected
279
}
280     }
281     
282     public void testIteratorFailNoMore() {
283         Bag bag = makeBag();
284         bag.add("A");
285         bag.add("A");
286         bag.add("B");
287         Iterator JavaDoc it = bag.iterator();
288         it.next();
289         it.next();
290         it.next();
291         try {
292             it.next();
293             fail("Should throw NoSuchElementException");
294         } catch (NoSuchElementException JavaDoc ex) {
295             // expected
296
}
297     }
298     
299     public void testIteratorFailDoubleRemove() {
300         Bag bag = makeBag();
301         bag.add("A");
302         bag.add("A");
303         bag.add("B");
304         Iterator JavaDoc it = bag.iterator();
305         it.next();
306         it.next();
307         assertEquals(3, bag.size());
308         it.remove();
309         assertEquals(2, bag.size());
310         try {
311             it.remove();
312             fail("Should throw IllegalStateException");
313         } catch (IllegalStateException JavaDoc ex) {
314             // expected
315
}
316         assertEquals(2, bag.size());
317         it.next();
318         it.remove();
319         assertEquals(1, bag.size());
320     }
321     
322     public void testToArray() {
323         Bag bag = makeBag();
324         bag.add("A");
325         bag.add("A");
326         bag.add("B");
327         bag.add("B");
328         bag.add("C");
329         Object JavaDoc[] array = bag.toArray();
330         int a = 0, b = 0, c = 0;
331         for (int i = 0; i < array.length; i++) {
332             a += (array[i].equals("A") ? 1 : 0);
333             b += (array[i].equals("B") ? 1 : 0);
334             c += (array[i].equals("C") ? 1 : 0);
335         }
336         assertEquals(2, a);
337         assertEquals(2, b);
338         assertEquals(1, c);
339     }
340
341     public void testToArrayPopulate() {
342         Bag bag = makeBag();
343         bag.add("A");
344         bag.add("A");
345         bag.add("B");
346         bag.add("B");
347         bag.add("C");
348         String JavaDoc[] array = (String JavaDoc[]) bag.toArray(new String JavaDoc[0]);
349         int a = 0, b = 0, c = 0;
350         for (int i = 0; i < array.length; i++) {
351             a += (array[i].equals("A") ? 1 : 0);
352             b += (array[i].equals("B") ? 1 : 0);
353             c += (array[i].equals("C") ? 1 : 0);
354         }
355         assertEquals(2, a);
356         assertEquals(2, b);
357         assertEquals(1, c);
358     }
359
360     //-----------------------------------------------------------------------
361
public void testEquals() {
362         Bag bag = makeBag();
363         Bag bag2 = makeBag();
364         assertEquals(true, bag.equals(bag2));
365         bag.add("A");
366         assertEquals(false, bag.equals(bag2));
367         bag2.add("A");
368         assertEquals(true, bag.equals(bag2));
369         bag.add("A");
370         bag.add("B");
371         bag.add("B");
372         bag.add("C");
373         bag2.add("A");
374         bag2.add("B");
375         bag2.add("B");
376         bag2.add("C");
377         assertEquals(true, bag.equals(bag2));
378     }
379
380     public void testEqualsHashBag() {
381         Bag bag = makeBag();
382         Bag bag2 = new HashBag();
383         assertEquals(true, bag.equals(bag2));
384         bag.add("A");
385         assertEquals(false, bag.equals(bag2));
386         bag2.add("A");
387         assertEquals(true, bag.equals(bag2));
388         bag.add("A");
389         bag.add("B");
390         bag.add("B");
391         bag.add("C");
392         bag2.add("A");
393         bag2.add("B");
394         bag2.add("B");
395         bag2.add("C");
396         assertEquals(true, bag.equals(bag2));
397     }
398
399     public void testHashCode() {
400         Bag bag = makeBag();
401         Bag bag2 = makeBag();
402         assertEquals(0, bag.hashCode());
403         assertEquals(0, bag2.hashCode());
404         assertEquals(bag.hashCode(), bag2.hashCode());
405         bag.add("A");
406         bag.add("A");
407         bag.add("B");
408         bag.add("B");
409         bag.add("C");
410         bag2.add("A");
411         bag2.add("A");
412         bag2.add("B");
413         bag2.add("B");
414         bag2.add("C");
415         assertEquals(bag.hashCode(), bag2.hashCode());
416         
417         int total = 0;
418         total += ("A".hashCode() ^ 2);
419         total += ("B".hashCode() ^ 2);
420         total += ("C".hashCode() ^ 1);
421         assertEquals(total, bag.hashCode());
422         assertEquals(total, bag2.hashCode());
423     }
424
425     //-----------------------------------------------------------------------
426
public void testEmptyBagSerialization() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
427         Bag bag = makeBag();
428         if (!(bag instanceof Serializable JavaDoc && isTestSerialization())) return;
429         
430         byte[] objekt = writeExternalFormToBytes((Serializable JavaDoc) bag);
431         Bag bag2 = (Bag) readExternalFormFromBytes(objekt);
432
433         assertEquals("Bag should be empty",0, bag.size());
434         assertEquals("Bag should be empty",0, bag2.size());
435     }
436
437     public void testFullBagSerialization() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
438         Bag bag = makeBag();
439         bag.add("A");
440         bag.add("A");
441         bag.add("B");
442         bag.add("B");
443         bag.add("C");
444         int size = bag.size();
445         if (!(bag instanceof Serializable JavaDoc && isTestSerialization())) return;
446         
447         byte[] objekt = writeExternalFormToBytes((Serializable JavaDoc) bag);
448         Bag bag2 = (Bag) readExternalFormFromBytes(objekt);
449
450         assertEquals("Bag should be same size", size, bag.size());
451         assertEquals("Bag should be same size", size, bag2.size());
452     }
453
454     /**
455      * Compare the current serialized form of the Bag
456      * against the canonical version in CVS.
457      */

458     public void testEmptyBagCompatibility() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
459         // test to make sure the canonical form has been preserved
460
Bag bag = makeBag();
461         if(bag instanceof Serializable JavaDoc && !skipSerializedCanonicalTests() && isTestSerialization()) {
462             Bag bag2 = (Bag) readExternalFormFromDisk(getCanonicalEmptyCollectionName(bag));
463             assertTrue("Bag is empty",bag2.size() == 0);
464             assertEquals(bag, bag2);
465         }
466     }
467
468     /**
469      * Compare the current serialized form of the Bag
470      * against the canonical version in CVS.
471      */

472     public void testFullBagCompatibility() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
473         // test to make sure the canonical form has been preserved
474
Bag bag = makeBag();
475         bag.add("A");
476         bag.add("A");
477         bag.add("B");
478         bag.add("B");
479         bag.add("C");
480         if(bag instanceof Serializable JavaDoc && !skipSerializedCanonicalTests() && isTestSerialization()) {
481             Bag bag2 = (Bag) readExternalFormFromDisk(getCanonicalFullCollectionName(bag));
482             assertEquals("Bag is the right size",bag.size(), bag2.size());
483             assertEquals(bag, bag2);
484         }
485     }
486 }
487
Popular Tags