001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.iterators;
017:
018: import java.util.ArrayList;
019: import java.util.HashSet;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.NoSuchElementException;
024: import java.util.Set;
025:
026: import org.apache.commons.collections.OrderedMapIterator;
027:
028: /**
029: * Abstract class for testing the OrderedMapIterator interface.
030: * <p>
031: * This class provides a framework for testing an implementation of MapIterator.
032: * Concrete subclasses must provide the list iterator to be tested.
033: * They must also specify certain details of how the list iterator operates by
034: * overriding the supportsXxx() methods if necessary.
035: *
036: * @since Commons Collections 3.0
037: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
038: *
039: * @author Stephen Colebourne
040: */
041: public abstract class AbstractTestOrderedMapIterator extends
042: AbstractTestMapIterator {
043:
044: /**
045: * JUnit constructor.
046: *
047: * @param testName the test class name
048: */
049: public AbstractTestOrderedMapIterator(String testName) {
050: super (testName);
051: }
052:
053: //-----------------------------------------------------------------------
054: public final OrderedMapIterator makeEmptyOrderedMapIterator() {
055: return (OrderedMapIterator) makeEmptyMapIterator();
056: }
057:
058: public final OrderedMapIterator makeFullOrderedMapIterator() {
059: return (OrderedMapIterator) makeFullMapIterator();
060: }
061:
062: //-----------------------------------------------------------------------
063: /**
064: * Test that the empty list iterator contract is correct.
065: */
066: public void testEmptyMapIterator() {
067: if (supportsEmptyIterator() == false) {
068: return;
069: }
070:
071: super .testEmptyMapIterator();
072:
073: OrderedMapIterator it = makeEmptyOrderedMapIterator();
074: Map map = getMap();
075: assertEquals(false, it.hasPrevious());
076: try {
077: it.previous();
078: fail();
079: } catch (NoSuchElementException ex) {
080: }
081: }
082:
083: //-----------------------------------------------------------------------
084: /**
085: * Test that the full list iterator contract is correct.
086: */
087: public void testFullMapIterator() {
088: if (supportsFullIterator() == false) {
089: return;
090: }
091:
092: super .testFullMapIterator();
093:
094: OrderedMapIterator it = makeFullOrderedMapIterator();
095: Map map = getMap();
096:
097: assertEquals(true, it.hasNext());
098: assertEquals(false, it.hasPrevious());
099: Set set = new HashSet();
100: while (it.hasNext()) {
101: // getKey
102: Object key = it.next();
103: assertSame("it.next() should equals getKey()", key, it
104: .getKey());
105: assertTrue("Key must be in map", map.containsKey(key));
106: assertTrue("Key must be unique", set.add(key));
107:
108: // getValue
109: Object value = it.getValue();
110: if (isGetStructuralModify() == false) {
111: assertSame("Value must be mapped to key", map.get(key),
112: value);
113: }
114: assertTrue("Value must be in map", map.containsValue(value));
115:
116: assertEquals(true, it.hasPrevious());
117:
118: verify();
119: }
120: while (it.hasPrevious()) {
121: // getKey
122: Object key = it.previous();
123: assertSame("it.previous() should equals getKey()", key, it
124: .getKey());
125: assertTrue("Key must be in map", map.containsKey(key));
126: assertTrue("Key must be unique", set.remove(key));
127:
128: // getValue
129: Object value = it.getValue();
130: if (isGetStructuralModify() == false) {
131: assertSame("Value must be mapped to key", map.get(key),
132: value);
133: }
134: assertTrue("Value must be in map", map.containsValue(value));
135:
136: assertEquals(true, it.hasNext());
137:
138: verify();
139: }
140: }
141:
142: //-----------------------------------------------------------------------
143: /**
144: * Test that the iterator order matches the keySet order.
145: */
146: public void testMapIteratorOrder() {
147: if (supportsFullIterator() == false) {
148: return;
149: }
150:
151: OrderedMapIterator it = makeFullOrderedMapIterator();
152: Map map = getMap();
153:
154: assertEquals("keySet() not consistent", new ArrayList(map
155: .keySet()), new ArrayList(map.keySet()));
156:
157: Iterator it2 = map.keySet().iterator();
158: assertEquals(true, it.hasNext());
159: assertEquals(true, it2.hasNext());
160: List list = new ArrayList();
161: while (it.hasNext()) {
162: Object key = it.next();
163: assertEquals(it2.next(), key);
164: list.add(key);
165: }
166: assertEquals(map.size(), list.size());
167: while (it.hasPrevious()) {
168: Object key = it.previous();
169: assertEquals(list.get(list.size() - 1), key);
170: list.remove(list.size() - 1);
171: }
172: assertEquals(0, list.size());
173: }
174:
175: }
|