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