001: /*
002: * Copyright 1999-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;
017:
018: import java.util.Iterator;
019: import java.util.NoSuchElementException;
020:
021: /**
022: * Base class for tetsing Iterator interface
023: *
024: * @author Morgan Delagrange
025: */
026: public abstract class TestIterator extends TestObject {
027:
028: public abstract Iterator makeEmptyIterator();
029:
030: public abstract Iterator makeFullIterator();
031:
032: /**
033: * Whether or not we are testing an iterator that can be
034: * empty. Default is true.
035: *
036: * @return true if Iterators can be empty
037: */
038: public boolean supportsEmptyIterator() {
039: return true;
040: }
041:
042: /**
043: * Whether or not we are testing an iterator that can contain
044: * elements. Default is true.
045: *
046: * @return true if Iterators can be empty
047: */
048: public boolean supportsFullIterator() {
049: return true;
050: }
051:
052: /**
053: * Whether or not we are testing an iterator that supports remove.
054: * Default is true.
055: *
056: * @return true if Iterators can remove elements
057: */
058: public boolean supportsRemove() {
059: return true;
060: }
061:
062: /**
063: * Should throw a NoSuchElementException.
064: */
065: public void testEmptyIterator() {
066: if (supportsEmptyIterator() == false) {
067: return;
068: }
069:
070: Iterator iter = makeEmptyIterator();
071: assertTrue("hasNext() should return false for empty iterators",
072: iter.hasNext() == false);
073: try {
074: iter.next();
075: fail("NoSuchElementException must be thrown when Iterator is exhausted");
076: } catch (NoSuchElementException e) {
077: }
078: }
079:
080: /**
081: * NoSuchElementException (or any other exception)
082: * should not be thrown for the first element.
083: * NoSuchElementException must be thrown when
084: * hasNext() returns false
085: */
086: public void testFullIterator() {
087: if (supportsFullIterator() == false) {
088: return;
089: }
090:
091: Iterator iter = makeFullIterator();
092:
093: assertTrue(
094: "hasNext() should return true for at least one element",
095: iter.hasNext());
096:
097: try {
098: iter.next();
099: } catch (NoSuchElementException e) {
100: fail("Full iterators must have at least one element");
101: }
102:
103: while (iter.hasNext()) {
104: iter.next();
105: }
106:
107: try {
108: iter.next();
109: fail("NoSuchElementException must be thrown when Iterator is exhausted");
110: } catch (NoSuchElementException e) {
111: }
112: }
113:
114: }
|