1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 package org.apache.commons.collections.iterators;
18 
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.NoSuchElementException;
25 import java.util.Set;
26 
27 import org.apache.commons.collections.OrderedMapIterator;
28 
29 /**
30  * Abstract class for testing the OrderedMapIterator interface.
31  * <p>
32  * This class provides a framework for testing an implementation of MapIterator.
33  * Concrete subclasses must provide the list iterator to be tested.
34  * They must also specify certain details of how the list iterator operates by
35  * overriding the supportsXxx() methods if necessary.
36  *
37  * @since Commons Collections 3.0
38  * @version $Revision: 646780 $ $Date: 2008-04-10 14:48:07 +0200 (Thu, 10 Apr 2008) $
39  *
40  * @author Stephen Colebourne
41  */
42 public abstract class AbstractTestOrderedMapIterator extends AbstractTestMapIterator {
43 
44     /**
45      * JUnit constructor.
46      *
47      * @param testName  the test class name
48      */
AbstractTestOrderedMapIterator(String testName)49     public AbstractTestOrderedMapIterator(String testName) {
50         super(testName);
51     }
52 
53     //-----------------------------------------------------------------------
makeEmptyOrderedMapIterator()54     public final OrderedMapIterator makeEmptyOrderedMapIterator() {
55         return (OrderedMapIterator) makeEmptyMapIterator();
56     }
57 
makeFullOrderedMapIterator()58     public final OrderedMapIterator makeFullOrderedMapIterator() {
59         return (OrderedMapIterator) makeFullMapIterator();
60     }
61 
62     //-----------------------------------------------------------------------
63     /**
64      * Test that the empty list iterator contract is correct.
65      */
testEmptyMapIterator()66     public void testEmptyMapIterator() {
67         if (supportsEmptyIterator() == false) {
68             return;
69         }
70 
71         super.testEmptyMapIterator();
72 
73         OrderedMapIterator it = makeEmptyOrderedMapIterator();
74         Map map = getMap();
75         assertEquals(false, it.hasPrevious());
76         try {
77             it.previous();
78             fail();
79         } catch (NoSuchElementException ex) {}
80     }
81 
82     //-----------------------------------------------------------------------
83     /**
84      * Test that the full list iterator contract is correct.
85      */
testFullMapIterator()86     public void testFullMapIterator() {
87         if (supportsFullIterator() == false) {
88             return;
89         }
90 
91         super.testFullMapIterator();
92 
93         OrderedMapIterator it = makeFullOrderedMapIterator();
94         Map map = getMap();
95 
96         assertEquals(true, it.hasNext());
97         assertEquals(false, it.hasPrevious());
98         Set set = new HashSet();
99         while (it.hasNext()) {
100             // getKey
101             Object key = it.next();
102             assertSame("it.next() should equals getKey()", key, it.getKey());
103             assertTrue("Key must be in map",  map.containsKey(key));
104             assertTrue("Key must be unique", set.add(key));
105 
106             // getValue
107             Object value = it.getValue();
108             if (isGetStructuralModify() == false) {
109                 assertSame("Value must be mapped to key", map.get(key), value);
110             }
111             assertTrue("Value must be in map",  map.containsValue(value));
112 
113             assertEquals(true, it.hasPrevious());
114 
115             verify();
116         }
117         while (it.hasPrevious()) {
118             // getKey
119             Object key = it.previous();
120             assertSame("it.previous() should equals getKey()", key, it.getKey());
121             assertTrue("Key must be in map",  map.containsKey(key));
122             assertTrue("Key must be unique", set.remove(key));
123 
124             // getValue
125             Object value = it.getValue();
126             if (isGetStructuralModify() == false) {
127                 assertSame("Value must be mapped to key", map.get(key), value);
128             }
129             assertTrue("Value must be in map",  map.containsValue(value));
130 
131             assertEquals(true, it.hasNext());
132 
133             verify();
134         }
135     }
136 
137     //-----------------------------------------------------------------------
138     /**
139      * Test that the iterator order matches the keySet order.
140      */
testMapIteratorOrder()141     public void testMapIteratorOrder() {
142         if (supportsFullIterator() == false) {
143             return;
144         }
145 
146         OrderedMapIterator it = makeFullOrderedMapIterator();
147         Map map = getMap();
148 
149         assertEquals("keySet() not consistent", new ArrayList(map.keySet()), new ArrayList(map.keySet()));
150 
151         Iterator it2 = map.keySet().iterator();
152         assertEquals(true, it.hasNext());
153         assertEquals(true, it2.hasNext());
154         List list = new ArrayList();
155         while (it.hasNext()) {
156             Object key = it.next();
157             assertEquals(it2.next(), key);
158             list.add(key);
159         }
160         assertEquals(map.size(), list.size());
161         while (it.hasPrevious()) {
162             Object key = it.previous();
163             assertEquals(list.get(list.size() - 1), key);
164             list.remove(list.size() - 1);
165         }
166         assertEquals(0, list.size());
167     }
168 
169 }
170