KickJava   Java API By Example, From Geeks To Geeks.

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


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.bag;
17
18 import java.util.Set JavaDoc;
19
20 import junit.framework.Test;
21 import junit.framework.TestSuite;
22
23 import org.apache.commons.collections.Bag;
24
25 /**
26  * Extension of {@link TestBag} for exercising the {@link TypedBag}
27  * implementation.
28  *
29  * @since Commons Collections 3.0
30  * @version $Revision: 1.7 $ $Date: 2004/06/02 22:05:03 $
31  *
32  * @author Phil Steitz
33  */

34 public class TestTypedBag extends AbstractTestBag {
35     
36     public TestTypedBag(String JavaDoc testName) {
37         super(testName);
38     }
39
40     public static Test suite() {
41         return new TestSuite(TestTypedBag.class);
42     }
43
44     public static void main(String JavaDoc args[]) {
45         String JavaDoc[] testCaseName = { TestTypedBag.class.getName()};
46         junit.textui.TestRunner.main(testCaseName);
47     }
48
49     //--------------------------------------------------------------------------
50

51     protected Class JavaDoc stringClass = this.getName().getClass();
52     private Object JavaDoc obj = new Object JavaDoc();
53     protected Class JavaDoc objectClass = obj.getClass();
54     
55     protected Bag decorateBag(HashBag bag, Class JavaDoc claz) {
56         return TypedBag.decorate(bag, claz);
57     }
58
59     public Bag makeBag() {
60         return decorateBag(new HashBag(), objectClass);
61     }
62     
63     protected Bag makeTestBag() {
64         return decorateBag(new HashBag(), stringClass);
65     }
66     
67     //--------------------------------------------------------------------------
68

69     public void testlegalAddRemove() {
70         Bag bag = makeTestBag();
71         assertEquals(0, bag.size());
72         Object JavaDoc[] els = new Object JavaDoc[] {"1", "3", "5", "7", "2", "4", "1"};
73         for (int i = 0; i < els.length; i++) {
74             bag.add(els[i]);
75             assertEquals(i + 1, bag.size());
76             assertEquals(true, bag.contains(els[i]));
77         }
78         Set JavaDoc set = ((PredicatedBag) bag).uniqueSet();
79         assertTrue("Unique set contains the first element",set.contains(els[0]));
80         assertEquals(true, bag.remove(els[0]));
81         set = ((PredicatedBag) bag).uniqueSet();
82         assertTrue("Unique set now does not contain the first element",
83             !set.contains(els[0]));
84     }
85  
86     public void testIllegalAdd() {
87         Bag bag = makeTestBag();
88         Integer JavaDoc i = new Integer JavaDoc(3);
89         try {
90             bag.add(i);
91             fail("Integer should fail type check.");
92         } catch (IllegalArgumentException JavaDoc e) {
93             // expected
94
}
95         assertTrue("Collection shouldn't contain illegal element",
96          !bag.contains(i));
97     }
98
99     public void testIllegalDecorate() {
100         HashBag elements = new HashBag();
101         elements.add("one");
102         elements.add("two");
103         elements.add(new Integer JavaDoc(3));
104         elements.add("four");
105         try {
106             Bag bag = decorateBag(elements, stringClass);
107             fail("Bag contains an element that should fail the type test.");
108         } catch (IllegalArgumentException JavaDoc e) {
109             // expected
110
}
111         try {
112             Bag bag = decorateBag(new HashBag(), null);
113             fail("Expectiing IllegalArgumentException for null predicate.");
114         } catch (IllegalArgumentException JavaDoc e) {
115             // expected
116
}
117     }
118
119     protected boolean skipSerializedCanonicalTests() {
120         return true;
121     }
122
123 }
124
Popular Tags