KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > impl > ContextBaseTestCase


1 /*
2  * Copyright 1999-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.chain.impl;
17
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29 import org.apache.commons.chain.Context;
30
31
32
33 /**
34  * <p>Test case for the <code>ContextBase</code> class.</p>
35  *
36  * @author Craig R. McClanahan
37  * @version $Revision: 1.6 $ $Date: 2004/02/25 00:01:05 $
38  */

39
40 public class ContextBaseTestCase extends TestCase {
41
42
43     // ---------------------------------------------------- Instance Variables
44

45
46     /**
47      * The {@link Context} instance under test.
48      */

49     protected Context context = null;
50
51
52
53     // ---------------------------------------------------------- Constructors
54

55     /**
56      * Construct a new instance of this test case.
57      *
58      * @param name Name of the test case
59      */

60     public ContextBaseTestCase(String JavaDoc name) {
61         super(name);
62     }
63
64
65     // -------------------------------------------------- Overall Test Methods
66

67
68     /**
69      * Set up instance variables required by this test case.
70      */

71     public void setUp() {
72         context = createContext();
73     }
74
75
76     /**
77      * Return the tests included in this test suite.
78      */

79     public static Test suite() {
80         return (new TestSuite(ContextBaseTestCase.class));
81     }
82
83     /**
84      * Tear down instance variables required by this test case.
85      */

86     public void tearDown() {
87         context = null;
88     }
89
90
91     // ------------------------------------------------ Individual Test Methods
92

93
94     // Test ability to get, put, and remove attributes
95
public void testAttributes() {
96
97         Object JavaDoc value = null;
98         checkAttributeCount(0);
99
100         context.put("foo", "This is foo");
101         checkAttributeCount(1);
102         value = context.get("foo");
103         assertNotNull("Returned foo", value);
104         assertTrue("Returned foo type", value instanceof String JavaDoc);
105         assertEquals("Returned foo value", "This is foo",
106                      (String JavaDoc) value);
107
108         context.put("bar", "This is bar");
109         checkAttributeCount(2);
110         value = context.get("bar");
111         assertNotNull("Returned bar", value);
112         assertTrue("Returned bar type", value instanceof String JavaDoc);
113         assertEquals("Returned bar value", "This is bar",
114                      (String JavaDoc) value);
115
116         context.put("baz", "This is baz");
117         checkAttributeCount(3);
118         value = context.get("baz");
119         assertNotNull("Returned baz", value);
120         assertTrue("Returned baz type", value instanceof String JavaDoc);
121         assertEquals("Returned baz value", "This is baz",
122                      (String JavaDoc) value);
123
124         context.put("baz", "This is new baz");
125         checkAttributeCount(3); // Replaced, not added
126
value = context.get("baz");
127         assertNotNull("Returned baz", value);
128         assertTrue("Returned baz type", value instanceof String JavaDoc);
129         assertEquals("Returned baz value", "This is new baz",
130                      (String JavaDoc) value);
131
132         context.remove("bar");
133         checkAttributeCount(2);
134         assertNull("Did not return bar",
135                    context.get("bar"));
136         assertNotNull("Still returned foo",
137                       context.get("foo"));
138         assertNotNull("Still returned baz",
139                       context.get("baz"));
140
141         context.clear();
142         checkAttributeCount(0);
143         assertNull("Did not return foo",
144                    context.get("foo"));
145         assertNull("Did not return bar",
146                    context.get("bar"));
147         assertNull("Did not return baz",
148                    context.get("baz"));
149
150     }
151
152
153     // Test containsKey() and containsValue()
154
public void testContains() {
155
156         assertTrue(!context.containsKey("bop"));
157         assertTrue(!context.containsValue("bop value"));
158         context.put("bop", "bop value");
159         assertTrue(context.containsKey("bop"));
160         assertTrue(context.containsValue("bop value"));
161         context.remove("bop");
162         assertTrue(!context.containsKey("bop"));
163         assertTrue(!context.containsValue("bop value"));
164
165     }
166
167
168     // Test equals() and hashCode()
169
public void testEquals() {
170
171         // Compare to self
172
assertTrue(context.equals(context));
173         assertTrue(context.hashCode() == context.hashCode());
174
175         // Compare to equivalent instance
176
Context other = createContext();
177         assertTrue(context.equals(other));
178         assertTrue(context.hashCode() == other.hashCode());
179
180         // Compare to non-equivalent instance - other modified
181
other.put("bop", "bop value");
182         assertTrue(!context.equals(other));
183         assertTrue(context.hashCode() != other.hashCode());
184
185         // Compare to non-equivalent instance - self modified
186
other = createContext(); // reset to equivalence
187
context.put("bop", "bop value");
188         assertTrue(!context.equals(other));
189         assertTrue(context.hashCode() != other.hashCode());
190
191     }
192
193
194     // Test keySet()
195
public void testKeySet() {
196
197         Set JavaDoc keySet = null;
198         Collection JavaDoc all = new ArrayList JavaDoc();
199
200         // Unsupported operations
201
keySet = context.keySet();
202         try {
203             keySet.add("bop");
204             fail("Should have thrown UnsupportedOperationException");
205         } catch (UnsupportedOperationException JavaDoc e) {
206             ; // Expected result
207
}
208         try {
209             Collection JavaDoc adds = new ArrayList JavaDoc();
210             adds.add("bop");
211             keySet.addAll(adds);
212             fail("Should have thrown UnsupportedOperationException");
213         } catch (UnsupportedOperationException JavaDoc e) {
214             ; // Expected result
215
}
216
217         // Before-modification checks
218
keySet = context.keySet();
219         assertEquals(createContext().size(), keySet.size());
220         assertTrue(!keySet.contains("foo"));
221         assertTrue(!keySet.contains("bar"));
222         assertTrue(!keySet.contains("baz"));
223         assertTrue(!keySet.contains("bop"));
224
225         // Add the new elements
226
context.put("foo", "foo value");
227         context.put("bar", "bar value");
228         context.put("baz", "baz value");
229         all.add("foo");
230         all.add("bar");
231         all.add("baz");
232
233         // After-modification checks
234
keySet = context.keySet();
235         assertEquals(expectedAttributeCount() + 3, keySet.size());
236         assertTrue(keySet.contains("foo"));
237         assertTrue(keySet.contains("bar"));
238         assertTrue(keySet.contains("baz"));
239         assertTrue(!keySet.contains("bop"));
240         assertTrue(keySet.containsAll(all));
241
242         // Remove a single element via remove()
243
context.remove("bar");
244         all.remove("bar");
245         keySet = context.keySet();
246         assertEquals(expectedAttributeCount() + 2, keySet.size());
247         assertTrue(keySet.contains("foo"));
248         assertTrue(!keySet.contains("bar"));
249         assertTrue(keySet.contains("baz"));
250         assertTrue(!keySet.contains("bop"));
251         assertTrue(keySet.containsAll(all));
252
253         // Remove a single element via keySet.remove()
254
keySet.remove("baz");
255         all.remove("baz");
256         keySet = context.keySet();
257         assertEquals(expectedAttributeCount() + 1, keySet.size());
258         assertTrue(keySet.contains("foo"));
259         assertTrue(!keySet.contains("bar"));
260         assertTrue(!keySet.contains("baz"));
261         assertTrue(!keySet.contains("bop"));
262         assertTrue(keySet.containsAll(all));
263
264         // Remove all elements via keySet.clear()
265
keySet.clear();
266         all.clear();
267         assertEquals(expectedAttributeCount(), keySet.size());
268         assertTrue(!keySet.contains("foo"));
269         assertTrue(!keySet.contains("bar"));
270         assertTrue(!keySet.contains("baz"));
271         assertTrue(!keySet.contains("bop"));
272         assertTrue(keySet.containsAll(all));
273
274         // Add the new elements #2
275
context.put("foo", "foo value");
276         context.put("bar", "bar value");
277         context.put("baz", "baz value");
278         all.add("foo");
279         all.add("bar");
280         all.add("baz");
281
282         // After-modification checks #2
283
keySet = context.keySet();
284         assertEquals(expectedAttributeCount() + 3, keySet.size());
285         assertTrue(keySet.contains("foo"));
286         assertTrue(keySet.contains("bar"));
287         assertTrue(keySet.contains("baz"));
288         assertTrue(!keySet.contains("bop"));
289         assertTrue(keySet.containsAll(all));
290
291     }
292
293
294     // Test state of newly created instance
295
public void testPristine() {
296
297         checkAttributeCount(0);
298         assertNull("No 'foo' attribute",
299                    context.get("foo"));
300
301     }
302
303
304     // Test putAll()
305
public void testPutAll() {
306
307         // Check preconditions
308
checkAttributeCount(0);
309         assertNull(context.get("foo"));
310         assertNull(context.get("bar"));
311         assertNull(context.get("baz"));
312         assertTrue(!context.containsKey("foo"));
313         assertTrue(!context.containsKey("bar"));
314         assertTrue(!context.containsKey("baz"));
315         assertTrue(!context.containsValue("foo value"));
316         assertTrue(!context.containsValue("bar value"));
317         assertTrue(!context.containsValue("baz value"));
318
319         // Call putAll()
320
Map JavaDoc adds = new HashMap JavaDoc();
321         adds.put("foo", "foo value");
322         adds.put("bar", "bar value");
323         adds.put("baz", "baz value");
324         context.putAll(adds);
325
326         // Check postconditions
327
checkAttributeCount(3);
328         assertEquals("foo value", (String JavaDoc) context.get("foo"));
329         assertEquals("bar value", (String JavaDoc) context.get("bar"));
330         assertEquals("baz value", (String JavaDoc) context.get("baz"));
331         assertTrue(context.containsKey("foo"));
332         assertTrue(context.containsKey("bar"));
333         assertTrue(context.containsKey("baz"));
334         assertTrue(context.containsValue("foo value"));
335         assertTrue(context.containsValue("bar value"));
336         assertTrue(context.containsValue("baz value"));
337
338     }
339
340
341     // -------------------------------------------------------- Support Methods
342

343
344     // Verify the number of defined attributes
345
protected void checkAttributeCount(int expected) {
346         int actual = 0;
347         Iterator JavaDoc keys = context.keySet().iterator();
348         while (keys.hasNext()) {
349             Object JavaDoc key = (Object JavaDoc) keys.next();
350             actual++;
351         }
352         assertEquals("Correct attribute count",
353                      expectedAttributeCount() + expected, actual);
354         if (expected == 0) {
355             assertTrue("Context should be empty", context.isEmpty());
356         } else {
357             assertTrue("Context should not be empty", !context.isEmpty());
358         }
359     }
360
361
362     // Create a new instance of the appropriate Context type for this test case
363
protected Context createContext() {
364         return (new ContextBase());
365     }
366
367
368     // Return the expected size() for a Context for this test case
369
protected int expectedAttributeCount() {
370         return (createContext().size());
371     }
372
373
374 }
375
Popular Tags