KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > iterators > AbstractTestIterator


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.iterators;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.NoSuchElementException JavaDoc;
20
21 import org.apache.commons.collections.AbstractTestObject;
22
23 /**
24  * Abstract class for testing the Iterator interface.
25  * <p>
26  * This class provides a framework for testing an implementation of Iterator.
27  * Concrete subclasses must provide the iterator to be tested.
28  * They must also specify certain details of how the iterator operates by
29  * overriding the supportsXxx() methods if necessary.
30  *
31  * @since Commons Collections 3.0
32  * @version $Revision: 1.8 $ $Date: 2004/02/18 01:20:33 $
33  *
34  * @author Morgan Delagrange
35  * @author Stephen Colebourne
36  */

37 public abstract class AbstractTestIterator extends AbstractTestObject {
38
39     /**
40      * JUnit constructor.
41      *
42      * @param testName the test class name
43      */

44     public AbstractTestIterator(String JavaDoc testName) {
45         super(testName);
46     }
47
48     //-----------------------------------------------------------------------
49
/**
50      * Implement this method to return an iterator over an empty collection.
51      *
52      * @return an empty iterator
53      */

54     public abstract Iterator JavaDoc makeEmptyIterator();
55
56     /**
57      * Implement this method to return an iterator over a collection with elements.
58      *
59      * @return a full iterator
60      */

61     public abstract Iterator JavaDoc makeFullIterator();
62
63     /**
64      * Implements the abstract superclass method to return the full iterator.
65      *
66      * @return a full iterator
67      */

68     public Object JavaDoc makeObject() {
69         return makeFullIterator();
70     }
71
72     /**
73      * Whether or not we are testing an iterator that can be empty.
74      * Default is true.
75      *
76      * @return true if Iterator can be empty
77      */

78     public boolean supportsEmptyIterator() {
79         return true;
80     }
81
82     /**
83      * Whether or not we are testing an iterator that can contain elements.
84      * Default is true.
85      *
86      * @return true if Iterator can be full
87      */

88     public boolean supportsFullIterator() {
89         return true;
90     }
91
92     /**
93      * Whether or not we are testing an iterator that supports remove().
94      * Default is true.
95      *
96      * @return true if Iterator supports remove
97      */

98     public boolean supportsRemove() {
99         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 JavaDoc it = makeEmptyIterator();
119         
120         // hasNext() should return false
121
assertEquals("hasNext() should return false for empty iterators", false, it.hasNext());
122         
123         // next() should throw a NoSuchElementException
124
try {
125             it.next();
126             fail("NoSuchElementException must be thrown when Iterator is exhausted");
127         } catch (NoSuchElementException JavaDoc e) {
128         }
129         verify();
130         
131         assertNotNull(it.toString());
132     }
133
134     /**
135      * Test normal iteration behaviour.
136      */

137     public void testFullIterator() {
138         if (supportsFullIterator() == false) {
139             return;
140         }
141
142         Iterator JavaDoc it = makeFullIterator();
143
144         // hasNext() must be true (ensure makeFullIterator is correct!)
145
assertEquals("hasNext() should return true for at least one element", true, it.hasNext());
146
147         // next() must not throw exception (ensure makeFullIterator is correct!)
148
try {
149             it.next();
150         } catch (NoSuchElementException JavaDoc e) {
151             fail("Full iterators must have at least one element");
152         }
153
154         // iterate through
155
while (it.hasNext()) {
156             it.next();
157             verify();
158         }
159
160         // next() must throw NoSuchElementException now
161
try {
162             it.next();
163             fail("NoSuchElementException must be thrown when Iterator is exhausted");
164         } catch (NoSuchElementException JavaDoc e) {
165         }
166         
167         assertNotNull(it.toString());
168     }
169
170     /**
171      * Test remove behaviour.
172      */

173     public void testRemove() {
174         Iterator JavaDoc it = makeFullIterator();
175         
176         if (supportsRemove() == false) {
177             // check for UnsupportedOperationException if not supported
178
try {
179                 it.remove();
180             } catch (UnsupportedOperationException JavaDoc ex) {}
181             return;
182         }
183         
184         // should throw IllegalStateException before next() called
185
try {
186             it.remove();
187             fail();
188         } catch (IllegalStateException JavaDoc ex) {}
189         verify();
190         
191         // remove after next should be fine
192
it.next();
193         it.remove();
194         
195         // should throw IllegalStateException for second remove()
196
try {
197             it.remove();
198             fail();
199         } catch (IllegalStateException JavaDoc ex) {}
200     }
201     
202 }
203
Popular Tags