KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.commons.collections.Predicate;
25 import org.apache.commons.collections.PredicateUtils;
26
27 /**
28  * Extension of {@link TestBag} for exercising the {@link PredicatedBag}
29  * implementation.
30  *
31  * @since Commons Collections 3.0
32  * @version $Revision: 1.7 $ $Date: 2004/06/02 22:05:03 $
33  *
34  * @author Phil Steitz
35  */

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

53     protected Predicate stringPredicate() {
54         return new Predicate() {
55             public boolean evaluate(Object JavaDoc o) {
56                 return o instanceof String JavaDoc;
57             }
58         };
59     }
60     
61     protected Predicate truePredicate = PredicateUtils.truePredicate();
62     
63     protected Bag decorateBag(HashBag bag, Predicate predicate) {
64         return PredicatedBag.decorate(bag, predicate);
65     }
66
67     public Bag makeBag() {
68         return decorateBag(new HashBag(), truePredicate);
69     }
70     
71     protected Bag makeTestBag() {
72         return decorateBag(new HashBag(), stringPredicate());
73     }
74     
75     //--------------------------------------------------------------------------
76

77     public void testlegalAddRemove() {
78         Bag bag = makeTestBag();
79         assertEquals(0, bag.size());
80         Object JavaDoc[] els = new Object JavaDoc[] {"1", "3", "5", "7", "2", "4", "1"};
81         for (int i = 0; i < els.length; i++) {
82             bag.add(els[i]);
83             assertEquals(i + 1, bag.size());
84             assertEquals(true, bag.contains(els[i]));
85         }
86         Set JavaDoc set = ((PredicatedBag) bag).uniqueSet();
87         assertTrue("Unique set contains the first element",set.contains(els[0]));
88         assertEquals(true, bag.remove(els[0]));
89         set = ((PredicatedBag) bag).uniqueSet();
90         assertTrue("Unique set now does not contain the first element",
91             !set.contains(els[0]));
92     }
93  
94     public void testIllegalAdd() {
95         Bag bag = makeTestBag();
96         Integer JavaDoc i = new Integer JavaDoc(3);
97         try {
98             bag.add(i);
99             fail("Integer should fail string predicate.");
100         } catch (IllegalArgumentException JavaDoc e) {
101             // expected
102
}
103         assertTrue("Collection shouldn't contain illegal element",
104          !bag.contains(i));
105     }
106
107     public void testIllegalDecorate() {
108         HashBag elements = new HashBag();
109         elements.add("one");
110         elements.add("two");
111         elements.add(new Integer JavaDoc(3));
112         elements.add("four");
113         try {
114             Bag bag = decorateBag(elements, stringPredicate());
115             fail("Bag contains an element that should fail the predicate.");
116         } catch (IllegalArgumentException JavaDoc e) {
117             // expected
118
}
119         try {
120             Bag bag = decorateBag(new HashBag(), null);
121             fail("Expectiing IllegalArgumentException for null predicate.");
122         } catch (IllegalArgumentException JavaDoc e) {
123             // expected
124
}
125     }
126
127     public String JavaDoc getCompatibilityVersion() {
128         return "3.1";
129     }
130
131 // public void testCreate() throws Exception {
132
// Bag bag = makeBag();
133
// writeExternalFormToDisk((java.io.Serializable) bag, "D:/dev/collections/data/test/PredicatedBag.emptyCollection.version3.1.obj");
134
// bag = makeBag();
135
// bag.add("A");
136
// bag.add("A");
137
// bag.add("B");
138
// bag.add("B");
139
// bag.add("C");
140
// writeExternalFormToDisk((java.io.Serializable) bag, "D:/dev/collections/data/test/PredicatedBag.fullCollection.version3.1.obj");
141
// }
142

143 }
144
Popular Tags