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.Iterator;
019: import java.util.NoSuchElementException;
020:
021: import org.apache.commons.collections.AbstractTestObject;
022:
023: /**
024: * Abstract class for testing the Iterator interface.
025: * <p>
026: * This class provides a framework for testing an implementation of Iterator.
027: * Concrete subclasses must provide the iterator to be tested.
028: * They must also specify certain details of how the iterator operates by
029: * overriding the supportsXxx() methods if necessary.
030: *
031: * @since Commons Collections 3.0
032: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
033: *
034: * @author Morgan Delagrange
035: * @author Stephen Colebourne
036: */
037: public abstract class AbstractTestIterator extends AbstractTestObject {
038:
039: /**
040: * JUnit constructor.
041: *
042: * @param testName the test class name
043: */
044: public AbstractTestIterator(String testName) {
045: super (testName);
046: }
047:
048: //-----------------------------------------------------------------------
049: /**
050: * Implement this method to return an iterator over an empty collection.
051: *
052: * @return an empty iterator
053: */
054: public abstract Iterator makeEmptyIterator();
055:
056: /**
057: * Implement this method to return an iterator over a collection with elements.
058: *
059: * @return a full iterator
060: */
061: public abstract Iterator makeFullIterator();
062:
063: /**
064: * Implements the abstract superclass method to return the full iterator.
065: *
066: * @return a full iterator
067: */
068: public Object makeObject() {
069: return makeFullIterator();
070: }
071:
072: /**
073: * Whether or not we are testing an iterator that can be empty.
074: * Default is true.
075: *
076: * @return true if Iterator can be empty
077: */
078: public boolean supportsEmptyIterator() {
079: return true;
080: }
081:
082: /**
083: * Whether or not we are testing an iterator that can contain elements.
084: * Default is true.
085: *
086: * @return true if Iterator can be full
087: */
088: public boolean supportsFullIterator() {
089: return true;
090: }
091:
092: /**
093: * Whether or not we are testing an iterator that supports remove().
094: * Default is true.
095: *
096: * @return true if Iterator supports remove
097: */
098: public boolean supportsRemove() {
099: return true;
100: }
101:
102: /**
103: * Allows subclasses to add complex cross verification
104: */
105: public void verify() {
106: // do nothing
107: }
108:
109: //-----------------------------------------------------------------------
110: /**
111: * Test the empty iterator.
112: */
113: public void testEmptyIterator() {
114: if (supportsEmptyIterator() == false) {
115: return;
116: }
117:
118: Iterator it = makeEmptyIterator();
119:
120: // hasNext() should return false
121: assertEquals(
122: "hasNext() should return false for empty iterators",
123: false, it.hasNext());
124:
125: // next() should throw a NoSuchElementException
126: try {
127: it.next();
128: fail("NoSuchElementException must be thrown when Iterator is exhausted");
129: } catch (NoSuchElementException e) {
130: }
131: verify();
132:
133: assertNotNull(it.toString());
134: }
135:
136: /**
137: * Test normal iteration behaviour.
138: */
139: public void testFullIterator() {
140: if (supportsFullIterator() == false) {
141: return;
142: }
143:
144: Iterator it = makeFullIterator();
145:
146: // hasNext() must be true (ensure makeFullIterator is correct!)
147: assertEquals(
148: "hasNext() should return true for at least one element",
149: true, it.hasNext());
150:
151: // next() must not throw exception (ensure makeFullIterator is correct!)
152: try {
153: it.next();
154: } catch (NoSuchElementException e) {
155: fail("Full iterators must have at least one element");
156: }
157:
158: // iterate through
159: while (it.hasNext()) {
160: it.next();
161: verify();
162: }
163:
164: // next() must throw NoSuchElementException now
165: try {
166: it.next();
167: fail("NoSuchElementException must be thrown when Iterator is exhausted");
168: } catch (NoSuchElementException e) {
169: }
170:
171: assertNotNull(it.toString());
172: }
173:
174: /**
175: * Test remove behaviour.
176: */
177: public void testRemove() {
178: Iterator it = makeFullIterator();
179:
180: if (supportsRemove() == false) {
181: // check for UnsupportedOperationException if not supported
182: try {
183: it.remove();
184: } catch (UnsupportedOperationException ex) {
185: }
186: return;
187: }
188:
189: // should throw IllegalStateException before next() called
190: try {
191: it.remove();
192: fail();
193: } catch (IllegalStateException ex) {
194: }
195: verify();
196:
197: // remove after next should be fine
198: it.next();
199: it.remove();
200:
201: // should throw IllegalStateException for second remove()
202: try {
203: it.remove();
204: fail();
205: } catch (IllegalStateException ex) {
206: }
207: }
208:
209: }
|