KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.ListIterator JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22
23 /**
24  * Abstract class for testing the ListIterator interface.
25  * <p>
26  * This class provides a framework for testing an implementation of ListIterator.
27  * Concrete subclasses must provide the list iterator to be tested.
28  * They must also specify certain details of how the list 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 Rodney Waldhoff
35  * @author Stephen Colebourne
36  */

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

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

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

61     public abstract ListIterator JavaDoc makeFullListIterator();
62
63     /**
64      * Implements the abstract superclass method to return the list iterator.
65      *
66      * @return an empty iterator
67      */

68     public Iterator JavaDoc makeEmptyIterator() {
69         return makeEmptyListIterator();
70     }
71
72     /**
73      * Implements the abstract superclass method to return the list iterator.
74      *
75      * @return a full iterator
76      */

77     public Iterator JavaDoc makeFullIterator() {
78         return makeFullListIterator();
79     }
80
81     /**
82      * Whether or not we are testing an iterator that supports add().
83      * Default is true.
84      *
85      * @return true if Iterator supports add
86      */

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

97     public boolean supportsSet() {
98         return true;
99     }
100
101     /**
102      * The value to be used in the add and set tests.
103      * Default is null.
104      */

105     public Object JavaDoc addSetValue() {
106         return null;
107     }
108
109     //-----------------------------------------------------------------------
110
/**
111      * Test that the empty list iterator contract is correct.
112      */

113     public void testEmptyListIteratorIsIndeedEmpty() {
114         if (supportsEmptyIterator() == false) {
115             return;
116         }
117
118         ListIterator JavaDoc it = makeEmptyListIterator();
119         
120         assertEquals(false, it.hasNext());
121         assertEquals(0, it.nextIndex());
122         assertEquals(false, it.hasPrevious());
123         assertEquals(-1, it.previousIndex());
124         
125         // next() should throw a NoSuchElementException
126
try {
127             it.next();
128             fail("NoSuchElementException must be thrown from empty ListIterator");
129         } catch (NoSuchElementException JavaDoc e) {
130         }
131         
132         // previous() should throw a NoSuchElementException
133
try {
134             it.previous();
135             fail("NoSuchElementException must be thrown from empty ListIterator");
136         } catch (NoSuchElementException JavaDoc e) {
137         }
138     }
139     
140     /**
141      * Test navigation through the iterator.
142      */

143     public void testWalkForwardAndBack() {
144         ArrayList JavaDoc list = new ArrayList JavaDoc();
145         ListIterator JavaDoc it = makeFullListIterator();
146         while (it.hasNext()) {
147             list.add(it.next());
148         }
149         
150         // check state at end
151
assertEquals(false, it.hasNext());
152         assertEquals(true, it.hasPrevious());
153         try {
154             it.next();
155             fail("NoSuchElementException must be thrown from next at end of ListIterator");
156         } catch (NoSuchElementException JavaDoc e) {
157         }
158         
159         // loop back through comparing
160
for (int i = list.size() - 1; i >= 0; i--) {
161             assertEquals(i + 1, it.nextIndex());
162             assertEquals(i, it.previousIndex());
163         
164             Object JavaDoc obj = list.get(i);
165             assertEquals(obj, it.previous());
166         }
167         
168         // check state at start
169
assertEquals(true, it.hasNext());
170         assertEquals(false, it.hasPrevious());
171         try {
172             it.previous();
173             fail("NoSuchElementException must be thrown from previous at start of ListIterator");
174         } catch (NoSuchElementException JavaDoc e) {
175         }
176     }
177     
178     /**
179      * Test add behaviour.
180      */

181     public void testAdd() {
182         ListIterator JavaDoc it = makeFullListIterator();
183         
184         Object JavaDoc addValue = addSetValue();
185         if (supportsAdd() == false) {
186             // check for UnsupportedOperationException if not supported
187
try {
188                 it.add(addValue);
189             } catch (UnsupportedOperationException JavaDoc ex) {}
190             return;
191         }
192         
193         // add at start should be OK, added should be previous
194
it = makeFullListIterator();
195         it.add(addValue);
196         assertEquals(addValue, it.previous());
197
198         // add at start should be OK, added should not be next
199
it = makeFullListIterator();
200         it.add(addValue);
201         assertTrue(addValue != it.next());
202
203         // add in middle and at end should be OK
204
it = makeFullListIterator();
205         while (it.hasNext()) {
206             it.next();
207             it.add(addValue);
208             // check add OK
209
assertEquals(addValue, it.previous());
210             it.next();
211         }
212     }
213     
214     /**
215      * Test set behaviour.
216      */

217     public void testSet() {
218         ListIterator JavaDoc it = makeFullListIterator();
219         
220         if (supportsSet() == false) {
221             // check for UnsupportedOperationException if not supported
222
try {
223                 it.set(addSetValue());
224             } catch (UnsupportedOperationException JavaDoc ex) {}
225             return;
226         }
227         
228         // should throw IllegalStateException before next() called
229
try {
230             it.set(addSetValue());
231             fail();
232         } catch (IllegalStateException JavaDoc ex) {}
233         
234         // set after next should be fine
235
it.next();
236         it.set(addSetValue());
237         
238         // repeated set calls should be fine
239
it.set(addSetValue());
240
241     }
242     
243     public void testRemoveThenSet() {
244         ListIterator JavaDoc it = makeFullListIterator();
245         if (supportsRemove() && supportsSet()) {
246             it.next();
247             it.remove();
248             try {
249                 it.set(addSetValue());
250                 fail("IllegalStateException must be thrown from set after remove");
251             } catch (IllegalStateException JavaDoc e) {
252             }
253         }
254     }
255
256     public void testAddThenSet() {
257         ListIterator JavaDoc it = makeFullListIterator();
258         // add then set
259
if (supportsAdd() && supportsSet()) {
260             it.next();
261             it.add(addSetValue());
262             try {
263                 it.set(addSetValue());
264                 fail("IllegalStateException must be thrown from set after add");
265             } catch (IllegalStateException JavaDoc e) {
266             }
267         }
268     }
269     
270     /**
271      * Test remove after add behaviour.
272      */

273     public void testAddThenRemove() {
274         ListIterator JavaDoc it = makeFullListIterator();
275         
276         // add then remove
277
if (supportsAdd() && supportsRemove()) {
278             it.next();
279             it.add(addSetValue());
280             try {
281                 it.remove();
282                 fail("IllegalStateException must be thrown from remove after add");
283             } catch (IllegalStateException JavaDoc e) {
284             }
285         }
286     }
287     
288 }
289
Popular Tags