KickJava   Java API By Example, From Geeks To Geeks.

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


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

41 public abstract class AbstractTestOrderedMapIterator extends AbstractTestMapIterator {
42
43     /**
44      * JUnit constructor.
45      *
46      * @param testName the test class name
47      */

48     public AbstractTestOrderedMapIterator(String JavaDoc testName) {
49         super(testName);
50     }
51
52     //-----------------------------------------------------------------------
53
public final OrderedMapIterator makeEmptyOrderedMapIterator() {
54         return (OrderedMapIterator) makeEmptyMapIterator();
55     }
56
57     public final OrderedMapIterator makeFullOrderedMapIterator() {
58         return (OrderedMapIterator) makeFullMapIterator();
59     }
60     
61     //-----------------------------------------------------------------------
62
/**
63      * Test that the empty list iterator contract is correct.
64      */

65     public void testEmptyMapIterator() {
66         if (supportsEmptyIterator() == false) {
67             return;
68         }
69
70         super.testEmptyMapIterator();
71         
72         OrderedMapIterator it = makeEmptyOrderedMapIterator();
73         Map JavaDoc map = getMap();
74         assertEquals(false, it.hasPrevious());
75         try {
76             it.previous();
77             fail();
78         } catch (NoSuchElementException JavaDoc ex) {}
79     }
80
81     //-----------------------------------------------------------------------
82
/**
83      * Test that the full list iterator contract is correct.
84      */

85     public void testFullMapIterator() {
86         if (supportsFullIterator() == false) {
87             return;
88         }
89
90         super.testFullMapIterator();
91         
92         OrderedMapIterator it = makeFullOrderedMapIterator();
93         Map JavaDoc map = getMap();
94         
95         assertEquals(true, it.hasNext());
96         assertEquals(false, it.hasPrevious());
97         Set JavaDoc set = new HashSet JavaDoc();
98         while (it.hasNext()) {
99             // getKey
100
Object JavaDoc key = it.next();
101             assertSame("it.next() should equals getKey()", key, it.getKey());
102             assertTrue("Key must be in map", map.containsKey(key));
103             assertTrue("Key must be unique", set.add(key));
104             
105             // getValue
106
Object JavaDoc value = it.getValue();
107             if (isGetStructuralModify() == false) {
108                 assertSame("Value must be mapped to key", map.get(key), value);
109             }
110             assertTrue("Value must be in map", map.containsValue(value));
111
112             assertEquals(true, it.hasPrevious());
113             
114             verify();
115         }
116         while (it.hasPrevious()) {
117             // getKey
118
Object JavaDoc key = it.previous();
119             assertSame("it.previous() should equals getKey()", key, it.getKey());
120             assertTrue("Key must be in map", map.containsKey(key));
121             assertTrue("Key must be unique", set.remove(key));
122             
123             // getValue
124
Object JavaDoc value = it.getValue();
125             if (isGetStructuralModify() == false) {
126                 assertSame("Value must be mapped to key", map.get(key), value);
127             }
128             assertTrue("Value must be in map", map.containsValue(value));
129
130             assertEquals(true, it.hasNext());
131             
132             verify();
133         }
134     }
135     
136     //-----------------------------------------------------------------------
137
/**
138      * Test that the iterator order matches the keySet order.
139      */

140     public void testMapIteratorOrder() {
141         if (supportsFullIterator() == false) {
142             return;
143         }
144
145         OrderedMapIterator it = makeFullOrderedMapIterator();
146         Map JavaDoc map = getMap();
147         
148         assertEquals("keySet() not consistent", new ArrayList JavaDoc(map.keySet()), new ArrayList JavaDoc(map.keySet()));
149         
150         Iterator JavaDoc it2 = map.keySet().iterator();
151         assertEquals(true, it.hasNext());
152         assertEquals(true, it2.hasNext());
153         List JavaDoc list = new ArrayList JavaDoc();
154         while (it.hasNext()) {
155             Object JavaDoc key = it.next();
156             assertEquals(it2.next(), key);
157             list.add(key);
158         }
159         assertEquals(map.size(), list.size());
160         while (it.hasPrevious()) {
161             Object JavaDoc key = it.previous();
162             assertEquals(list.get(list.size() - 1), key);
163             list.remove(list.size() - 1);
164         }
165         assertEquals(0, list.size());
166     }
167     
168 }
169
Popular Tags