KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > iterators > AbstractTestMapIterator


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.iterators;
17
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.commons.collections.MapIterator;
25
26 /**
27  * Abstract class for testing the MapIterator interface.
28  * <p>
29  * This class provides a framework for testing an implementation of MapIterator.
30  * Concrete subclasses must provide the list iterator to be tested.
31  * They must also specify certain details of how the list iterator operates by
32  * overriding the supportsXxx() methods if necessary.
33  *
34  * @since Commons Collections 3.0
35  * @version $Revision: 1.12 $ $Date: 2004/04/09 14:38:13 $
36  *
37  * @author Stephen Colebourne
38  */

39 public abstract class AbstractTestMapIterator extends AbstractTestIterator {
40
41     /**
42      * JUnit constructor.
43      *
44      * @param testName the test class name
45      */

46     public AbstractTestMapIterator(String JavaDoc testName) {
47         super(testName);
48     }
49
50     //-----------------------------------------------------------------------
51
/**
52      * Implement this method to return a map iterator over an empty map.
53      *
54      * @return an empty iterator
55      */

56     public abstract MapIterator makeEmptyMapIterator();
57
58     /**
59      * Implement this method to return a map iterator over a map with elements.
60      *
61      * @return a full iterator
62      */

63     public abstract MapIterator makeFullMapIterator();
64
65     /**
66      * Implement this method to return the map which contains the same data as the
67      * iterator.
68      *
69      * @return a full map which can be updated
70      */

71     public abstract Map JavaDoc getMap();
72     
73     /**
74      * Implement this method to return the confirmed map which contains the same
75      * data as the iterator.
76      *
77      * @return a full map which can be updated
78      */

79     public abstract Map JavaDoc getConfirmedMap();
80     
81     /**
82      * Implements the abstract superclass method to return the list iterator.
83      *
84      * @return an empty iterator
85      */

86     public final Iterator JavaDoc makeEmptyIterator() {
87         return makeEmptyMapIterator();
88     }
89
90     /**
91      * Implements the abstract superclass method to return the list iterator.
92      *
93      * @return a full iterator
94      */

95     public final Iterator JavaDoc makeFullIterator() {
96         return makeFullMapIterator();
97     }
98
99     /**
100      * Whether or not we are testing an iterator that supports setValue().
101      * Default is true.
102      *
103      * @return true if Iterator supports set
104      */

105     public boolean supportsSetValue() {
106         return true;
107     }
108
109     /**
110      * Whether the get operation on the map structurally modifies the map,
111      * such as with LRUMap. Default is false.
112      *
113      * @return true if the get method structurally modifies the map
114      */

115     public boolean isGetStructuralModify() {
116         return false;
117     }
118     
119     /**
120      * The values to be used in the add and set tests.
121      * Default is two strings.
122      */

123     public Object JavaDoc[] addSetValues() {
124         return new Object JavaDoc[] {"A", "B"};
125     }
126
127     //-----------------------------------------------------------------------
128
/**
129      * Test that the empty list iterator contract is correct.
130      */

131     public void testEmptyMapIterator() {
132         if (supportsEmptyIterator() == false) {
133             return;
134         }
135
136         MapIterator it = makeEmptyMapIterator();
137         Map JavaDoc map = getMap();
138         assertEquals(false, it.hasNext());
139         
140         // next() should throw a NoSuchElementException
141
try {
142             it.next();
143             fail();
144         } catch (NoSuchElementException JavaDoc ex) {}
145         
146         // getKey() should throw an IllegalStateException
147
try {
148             it.getKey();
149             fail();
150         } catch (IllegalStateException JavaDoc ex) {}
151         
152         // getValue() should throw an IllegalStateException
153
try {
154             it.getValue();
155             fail();
156         } catch (IllegalStateException JavaDoc ex) {}
157         
158         if (supportsSetValue() == false) {
159             // setValue() should throw an UnsupportedOperationException/IllegalStateException
160
try {
161                 it.setValue(addSetValues()[0]);
162                 fail();
163             } catch (UnsupportedOperationException JavaDoc ex) {
164             } catch (IllegalStateException JavaDoc ex) {}
165         } else {
166             // setValue() should throw an IllegalStateException
167
try {
168                 it.setValue(addSetValues()[0]);
169                 fail();
170             } catch (IllegalStateException JavaDoc ex) {}
171         }
172     }
173
174     //-----------------------------------------------------------------------
175
/**
176      * Test that the full list iterator contract is correct.
177      */

178     public void testFullMapIterator() {
179         if (supportsFullIterator() == false) {
180             return;
181         }
182
183         MapIterator it = makeFullMapIterator();
184         Map JavaDoc map = getMap();
185         assertEquals(true, it.hasNext());
186         
187         assertEquals(true, it.hasNext());
188         Set JavaDoc set = new HashSet JavaDoc();
189         while (it.hasNext()) {
190             // getKey
191
Object JavaDoc key = it.next();
192             assertSame("it.next() should equals getKey()", key, it.getKey());
193             assertTrue("Key must be in map", map.containsKey(key));
194             assertTrue("Key must be unique", set.add(key));
195             
196             // getValue
197
Object JavaDoc value = it.getValue();
198             if (isGetStructuralModify() == false) {
199                 assertSame("Value must be mapped to key", map.get(key), value);
200             }
201             assertTrue("Value must be in map", map.containsValue(value));
202
203             verify();
204         }
205     }
206     
207     //-----------------------------------------------------------------------
208
public void testMapIteratorSet() {
209         if (supportsFullIterator() == false) {
210             return;
211         }
212
213         Object JavaDoc newValue = addSetValues()[0];
214         Object JavaDoc newValue2 = (addSetValues().length == 1 ? addSetValues()[0] : addSetValues()[1]);
215         MapIterator it = makeFullMapIterator();
216         Map JavaDoc map = getMap();
217         Map JavaDoc confirmed = getConfirmedMap();
218         assertEquals(true, it.hasNext());
219         Object JavaDoc key = it.next();
220         Object JavaDoc value = it.getValue();
221         
222         if (supportsSetValue() == false) {
223             try {
224                 it.setValue(newValue);
225                 fail();
226             } catch (UnsupportedOperationException JavaDoc ex) {}
227             return;
228         }
229         Object JavaDoc old = it.setValue(newValue);
230         confirmed.put(key, newValue);
231         assertSame("Key must not change after setValue", key, it.getKey());
232         assertSame("Value must be changed after setValue", newValue, it.getValue());
233         assertSame("setValue must return old value", value, old);
234         assertEquals("Map must contain key", true, map.containsKey(key));
235         // test against confirmed, as map may contain value twice
236
assertEquals("Map must not contain old value",
237             confirmed.containsValue(old), map.containsValue(old));
238         assertEquals("Map must contain new value", true, map.containsValue(newValue));
239         verify();
240         
241         it.setValue(newValue); // same value - should be OK
242
confirmed.put(key, newValue);
243         assertSame("Key must not change after setValue", key, it.getKey());
244         assertSame("Value must be changed after setValue", newValue, it.getValue());
245         verify();
246         
247         it.setValue(newValue2); // new value
248
confirmed.put(key, newValue2);
249         assertSame("Key must not change after setValue", key, it.getKey());
250         assertSame("Value must be changed after setValue", newValue2, it.getValue());
251         verify();
252     }
253
254     //-----------------------------------------------------------------------
255
public void testRemove() { // override
256
MapIterator it = makeFullMapIterator();
257         Map JavaDoc map = getMap();
258         Map JavaDoc confirmed = getConfirmedMap();
259         assertEquals(true, it.hasNext());
260         Object JavaDoc key = it.next();
261         
262         if (supportsRemove() == false) {
263             try {
264                 it.remove();
265                 fail();
266             } catch (UnsupportedOperationException JavaDoc ex) {
267             }
268             return;
269         }
270         
271         it.remove();
272         confirmed.remove(key);
273         assertEquals(false, map.containsKey(key));
274         verify();
275         
276         try {
277             it.remove(); // second remove fails
278
} catch (IllegalStateException JavaDoc ex) {
279         }
280         verify();
281     }
282
283     //-----------------------------------------------------------------------
284
public void testMapIteratorSetRemoveSet() {
285         if (supportsSetValue() == false || supportsRemove() == false) {
286             return;
287         }
288         Object JavaDoc newValue = addSetValues()[0];
289         MapIterator it = makeFullMapIterator();
290         Map JavaDoc map = getMap();
291         Map JavaDoc confirmed = getConfirmedMap();
292         
293         assertEquals(true, it.hasNext());
294         Object JavaDoc key = it.next();
295         
296         it.setValue(newValue);
297         it.remove();
298         confirmed.remove(key);
299         verify();
300         
301         try {
302             it.setValue(newValue);
303             fail();
304         } catch (IllegalStateException JavaDoc ex) {}
305         verify();
306     }
307
308     //-----------------------------------------------------------------------
309
public void testMapIteratorRemoveGetKey() {
310         if (supportsRemove() == false) {
311             return;
312         }
313         MapIterator it = makeFullMapIterator();
314         Map JavaDoc map = getMap();
315         Map JavaDoc confirmed = getConfirmedMap();
316         
317         assertEquals(true, it.hasNext());
318         Object JavaDoc key = it.next();
319         
320         it.remove();
321         confirmed.remove(key);
322         verify();
323         
324         try {
325             it.getKey();
326             fail();
327         } catch (IllegalStateException JavaDoc ex) {}
328         verify();
329     }
330
331     //-----------------------------------------------------------------------
332
public void testMapIteratorRemoveGetValue() {
333         if (supportsRemove() == false) {
334             return;
335         }
336         MapIterator it = makeFullMapIterator();
337         Map JavaDoc map = getMap();
338         Map JavaDoc confirmed = getConfirmedMap();
339         
340         assertEquals(true, it.hasNext());
341         Object JavaDoc key = it.next();
342         
343         it.remove();
344         confirmed.remove(key);
345         verify();
346         
347         try {
348             it.getValue();
349             fail();
350         } catch (IllegalStateException JavaDoc ex) {}
351         verify();
352     }
353
354 }
355
Popular Tags