KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestBagUtils


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;
17
18 import junit.framework.Test;
19
20 import org.apache.commons.collections.bag.HashBag;
21 import org.apache.commons.collections.bag.PredicatedBag;
22 import org.apache.commons.collections.bag.PredicatedSortedBag;
23 import org.apache.commons.collections.bag.SynchronizedBag;
24 import org.apache.commons.collections.bag.SynchronizedSortedBag;
25 import org.apache.commons.collections.bag.TransformedBag;
26 import org.apache.commons.collections.bag.TransformedSortedBag;
27 import org.apache.commons.collections.bag.TreeBag;
28 import org.apache.commons.collections.bag.UnmodifiableBag;
29 import org.apache.commons.collections.bag.UnmodifiableSortedBag;
30
31 /**
32  * Tests for BagUtils factory methods.
33  *
34  * @version $Revision: 1.7 $ $Date: 2004/02/18 01:20:35 $
35  *
36  * @author Phil Steitz
37  */

38 public class TestBagUtils extends BulkTest {
39
40     public TestBagUtils(String JavaDoc name) {
41         super(name);
42     }
43
44
45     public static Test suite() {
46         return BulkTest.makeSuite(TestBagUtils.class);
47     }
48     
49     //----------------------------------------------------------------------
50

51     protected Class JavaDoc stringClass = this.getName().getClass();
52     protected Predicate truePredicate = PredicateUtils.truePredicate();
53     protected Transformer nopTransformer = TransformerUtils.nopTransformer();
54     
55     //----------------------------------------------------------------------
56

57     public void testSynchronizedBag() {
58         Bag bag = BagUtils.synchronizedBag(new HashBag());
59         assertTrue("Returned object should be a SynchronizedBag.",
60             bag instanceof SynchronizedBag);
61         try {
62             bag = BagUtils.synchronizedBag(null);
63             fail("Expecting IllegalArgumentException for null bag.");
64         } catch (IllegalArgumentException JavaDoc ex) {
65             // expected
66
}
67     }
68     
69     public void testUnmodifiableBag() {
70         Bag bag = BagUtils.unmodifiableBag(new HashBag());
71         assertTrue("Returned object should be an UnmodifiableBag.",
72             bag instanceof UnmodifiableBag);
73         try {
74             bag = BagUtils.unmodifiableBag(null);
75             fail("Expecting IllegalArgumentException for null bag.");
76         } catch (IllegalArgumentException JavaDoc ex) {
77             // expected
78
}
79     }
80     
81     public void testPredicatedBag() {
82         Bag bag = BagUtils.predicatedBag(new HashBag(), truePredicate);
83         assertTrue("Returned object should be a PredicatedBag.",
84             bag instanceof PredicatedBag);
85         try {
86             bag = BagUtils.predicatedBag(null,truePredicate);
87             fail("Expecting IllegalArgumentException for null bag.");
88         } catch (IllegalArgumentException JavaDoc ex) {
89             // expected
90
}
91         try {
92             bag = BagUtils.predicatedBag(new HashBag(), null);
93             fail("Expecting IllegalArgumentException for null predicate.");
94         } catch (IllegalArgumentException JavaDoc ex) {
95             // expected
96
}
97     }
98     
99     public void testTypedBag() {
100         Bag bag = BagUtils.typedBag(new HashBag(), stringClass);
101         assertTrue("Returned object should be a TypedBag.",
102             bag instanceof PredicatedBag);
103         try {
104             bag = BagUtils.typedBag(null, stringClass);
105             fail("Expecting IllegalArgumentException for null bag.");
106         } catch (IllegalArgumentException JavaDoc ex) {
107             // expected
108
}
109         try {
110             bag = BagUtils.typedBag(new HashBag(), null);
111             fail("Expecting IllegalArgumentException for null type.");
112         } catch (IllegalArgumentException JavaDoc ex) {
113             // expected
114
}
115     }
116     
117      public void testTransformedBag() {
118         Bag bag = BagUtils.transformedBag(new HashBag(), nopTransformer);
119         assertTrue("Returned object should be an TransformedBag.",
120             bag instanceof TransformedBag);
121         try {
122             bag = BagUtils.transformedBag(null, nopTransformer);
123             fail("Expecting IllegalArgumentException for null bag.");
124         } catch (IllegalArgumentException JavaDoc ex) {
125             // expected
126
}
127         try {
128             bag = BagUtils.transformedBag(new HashBag(), null);
129             fail("Expecting IllegalArgumentException for null transformer.");
130         } catch (IllegalArgumentException JavaDoc ex) {
131             // expected
132
}
133     }
134      
135     public void testSynchronizedSortedBag() {
136         Bag bag = BagUtils.synchronizedSortedBag(new TreeBag());
137         assertTrue("Returned object should be a SynchronizedSortedBag.",
138             bag instanceof SynchronizedSortedBag);
139         try {
140             bag = BagUtils.synchronizedSortedBag(null);
141             fail("Expecting IllegalArgumentException for null bag.");
142         } catch (IllegalArgumentException JavaDoc ex) {
143             // expected
144
}
145     }
146     
147     public void testUnmodifiableSortedBag() {
148         Bag bag = BagUtils.unmodifiableSortedBag(new TreeBag());
149         assertTrue("Returned object should be an UnmodifiableSortedBag.",
150             bag instanceof UnmodifiableSortedBag);
151         try {
152             bag = BagUtils.unmodifiableSortedBag(null);
153             fail("Expecting IllegalArgumentException for null bag.");
154         } catch (IllegalArgumentException JavaDoc ex) {
155             // expected
156
}
157     }
158     
159     public void testPredicatedSortedBag() {
160         Bag bag = BagUtils.predicatedSortedBag(new TreeBag(), truePredicate);
161         assertTrue("Returned object should be a PredicatedSortedBag.",
162             bag instanceof PredicatedSortedBag);
163         try {
164             bag = BagUtils.predicatedSortedBag(null, truePredicate);
165             fail("Expecting IllegalArgumentException for null bag.");
166         } catch (IllegalArgumentException JavaDoc ex) {
167             // expected
168
}
169         try {
170             bag = BagUtils.predicatedSortedBag(new TreeBag(), null);
171             fail("Expecting IllegalArgumentException for null predicate.");
172         } catch (IllegalArgumentException JavaDoc ex) {
173             // expected
174
}
175     }
176     
177     public void testTypedSortedBag() {
178         Bag bag = BagUtils.typedSortedBag(new TreeBag(), stringClass);
179         assertTrue("Returned object should be a TypedSortedBag.",
180             bag instanceof PredicatedSortedBag);
181         try {
182             bag = BagUtils.typedSortedBag(null, stringClass);
183             fail("Expecting IllegalArgumentException for null bag.");
184         } catch (IllegalArgumentException JavaDoc ex) {
185             // expected
186
}
187         try {
188             bag = BagUtils.typedSortedBag(new TreeBag(), null);
189             fail("Expecting IllegalArgumentException for null type.");
190         } catch (IllegalArgumentException JavaDoc ex) {
191             // expected
192
}
193     }
194     
195     public void testTransformedSortedBag() {
196         Bag bag = BagUtils.transformedSortedBag(new TreeBag(), nopTransformer);
197         assertTrue("Returned object should be an TransformedSortedBag",
198             bag instanceof TransformedSortedBag);
199         try {
200             bag = BagUtils.transformedSortedBag(null, nopTransformer);
201             fail("Expecting IllegalArgumentException for null bag.");
202         } catch (IllegalArgumentException JavaDoc ex) {
203             // expected
204
}
205         try {
206             bag = BagUtils.transformedSortedBag(new TreeBag(), null);
207             fail("Expecting IllegalArgumentException for null transformer.");
208         } catch (IllegalArgumentException JavaDoc ex) {
209             // expected
210
}
211     }
212 }
213
214
215
Popular Tags