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.map;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.NoSuchElementException;
025: import java.util.TreeMap;
026:
027: import org.apache.commons.collections.BulkTest;
028: import org.apache.commons.collections.MapIterator;
029: import org.apache.commons.collections.OrderedMap;
030: import org.apache.commons.collections.comparators.NullComparator;
031: import org.apache.commons.collections.iterators.AbstractTestOrderedMapIterator;
032:
033: /**
034: * Abstract test class for {@link OrderedMap} methods and contracts.
035: *
036: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
037: *
038: * @author Stephen Colebourne
039: */
040: public abstract class AbstractTestOrderedMap extends
041: AbstractTestIterableMap {
042:
043: /**
044: * JUnit constructor.
045: *
046: * @param testName the test name
047: */
048: public AbstractTestOrderedMap(String testName) {
049: super (testName);
050: }
051:
052: //-----------------------------------------------------------------------
053: /**
054: * OrderedMap uses TreeMap as its known comparison.
055: *
056: * @return a map that is known to be valid
057: */
058: public Map makeConfirmedMap() {
059: return new TreeMap(new NullComparator());
060: }
061:
062: /**
063: * The only confirmed collection we have that is ordered is the sorted one.
064: * Thus, sort the keys.
065: */
066: public Object[] getSampleKeys() {
067: List list = new ArrayList(Arrays.asList(super .getSampleKeys()));
068: Collections.sort(list, new NullComparator());
069: return list.toArray();
070: }
071:
072: //-----------------------------------------------------------------------
073: public void testFirstKey() {
074: resetEmpty();
075: OrderedMap ordered = (OrderedMap) map;
076: try {
077: ordered.firstKey();
078: fail();
079: } catch (NoSuchElementException ex) {
080: }
081:
082: resetFull();
083: ordered = (OrderedMap) map;
084: Object confirmedFirst = confirmed.keySet().iterator().next();
085: assertEquals(confirmedFirst, ordered.firstKey());
086: }
087:
088: public void testLastKey() {
089: resetEmpty();
090: OrderedMap ordered = (OrderedMap) map;
091: try {
092: ordered.lastKey();
093: fail();
094: } catch (NoSuchElementException ex) {
095: }
096:
097: resetFull();
098: ordered = (OrderedMap) map;
099: Object confirmedLast = null;
100: for (Iterator it = confirmed.keySet().iterator(); it.hasNext();) {
101: confirmedLast = it.next();
102: }
103: assertEquals(confirmedLast, ordered.lastKey());
104: }
105:
106: //-----------------------------------------------------------------------
107: public void testNextKey() {
108: resetEmpty();
109: OrderedMap ordered = (OrderedMap) map;
110: assertEquals(null, ordered.nextKey(getOtherKeys()[0]));
111: if (isAllowNullKey() == false) {
112: try {
113: assertEquals(null, ordered.nextKey(null)); // this is allowed too
114: } catch (NullPointerException ex) {
115: }
116: } else {
117: assertEquals(null, ordered.nextKey(null));
118: }
119:
120: resetFull();
121: ordered = (OrderedMap) map;
122: Iterator it = confirmed.keySet().iterator();
123: Object confirmedLast = it.next();
124: while (it.hasNext()) {
125: Object confirmedObject = it.next();
126: assertEquals(confirmedObject, ordered
127: .nextKey(confirmedLast));
128: confirmedLast = confirmedObject;
129: }
130: assertEquals(null, ordered.nextKey(confirmedLast));
131:
132: if (isAllowNullKey() == false) {
133: try {
134: ordered.nextKey(null);
135: fail();
136: } catch (NullPointerException ex) {
137: }
138: } else {
139: assertEquals(null, ordered.nextKey(null));
140: }
141: }
142:
143: public void testPreviousKey() {
144: resetEmpty();
145: OrderedMap ordered = (OrderedMap) map;
146: assertEquals(null, ordered.previousKey(getOtherKeys()[0]));
147: if (isAllowNullKey() == false) {
148: try {
149: assertEquals(null, ordered.previousKey(null)); // this is allowed too
150: } catch (NullPointerException ex) {
151: }
152: } else {
153: assertEquals(null, ordered.previousKey(null));
154: }
155:
156: resetFull();
157: ordered = (OrderedMap) map;
158: List list = new ArrayList(confirmed.keySet());
159: Collections.reverse(list);
160: Iterator it = list.iterator();
161: Object confirmedLast = it.next();
162: while (it.hasNext()) {
163: Object confirmedObject = it.next();
164: assertEquals(confirmedObject, ordered
165: .previousKey(confirmedLast));
166: confirmedLast = confirmedObject;
167: }
168: assertEquals(null, ordered.previousKey(confirmedLast));
169:
170: if (isAllowNullKey() == false) {
171: try {
172: ordered.previousKey(null);
173: fail();
174: } catch (NullPointerException ex) {
175: }
176: } else {
177: if (isAllowNullKey() == false) {
178: assertEquals(null, ordered.previousKey(null));
179: }
180: }
181: }
182:
183: //-----------------------------------------------------------------------
184: public BulkTest bulkTestOrderedMapIterator() {
185: return new InnerTestOrderedMapIterator();
186: }
187:
188: public class InnerTestOrderedMapIterator extends
189: AbstractTestOrderedMapIterator {
190: public InnerTestOrderedMapIterator() {
191: super ("InnerTestOrderedMapIterator");
192: }
193:
194: public boolean supportsRemove() {
195: return AbstractTestOrderedMap.this .isRemoveSupported();
196: }
197:
198: public boolean isGetStructuralModify() {
199: return AbstractTestOrderedMap.this .isGetStructuralModify();
200: }
201:
202: public boolean supportsSetValue() {
203: return AbstractTestOrderedMap.this .isSetValueSupported();
204: }
205:
206: public MapIterator makeEmptyMapIterator() {
207: resetEmpty();
208: return ((OrderedMap) AbstractTestOrderedMap.this .map)
209: .orderedMapIterator();
210: }
211:
212: public MapIterator makeFullMapIterator() {
213: resetFull();
214: return ((OrderedMap) AbstractTestOrderedMap.this .map)
215: .orderedMapIterator();
216: }
217:
218: public Map getMap() {
219: // assumes makeFullMapIterator() called first
220: return AbstractTestOrderedMap.this .map;
221: }
222:
223: public Map getConfirmedMap() {
224: // assumes makeFullMapIterator() called first
225: return AbstractTestOrderedMap.this .confirmed;
226: }
227:
228: public void verify() {
229: super.verify();
230: AbstractTestOrderedMap.this.verify();
231: }
232: }
233:
234: }
|