KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.ListIterator JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 /**
27  * Test the ArrayListIterator class.
28  *
29  * @version $Revision: 1.5 $ $Date: 2004/02/18 01:20:33 $
30  * @author Neil O'Toole
31  */

32 public class TestArrayListIterator extends TestArrayIterator {
33
34     public TestArrayListIterator(String JavaDoc testName) {
35         super(testName);
36     }
37
38     public static Test suite() {
39         return new TestSuite(TestArrayListIterator.class);
40     }
41
42     public Iterator JavaDoc makeEmptyIterator() {
43         return new ArrayListIterator(new Object JavaDoc[0]);
44     }
45
46     public Iterator JavaDoc makeFullIterator() {
47         return new ArrayListIterator(testArray);
48     }
49
50     public ListIterator JavaDoc makeArrayListIterator(Object JavaDoc array) {
51         return new ArrayListIterator(array);
52     }
53
54     public boolean supportsRemove() {
55         return false;
56     }
57
58     /**
59      * Test the basic ListIterator functionality - going backwards using
60      * <code>previous()</code>.
61      */

62     public void testListIterator() {
63         ListIterator JavaDoc iter = (ListIterator JavaDoc) makeFullIterator();
64
65         // TestArrayIterator#testIterator() has already tested the iterator forward,
66
// now we need to test it in reverse
67

68         // fast-forward the iterator to the end...
69
while (iter.hasNext()) {
70             iter.next();
71         }
72
73         for (int x = testArray.length - 1; x >= 0; x--) {
74             Object JavaDoc testValue = testArray[x];
75             Object JavaDoc iterValue = iter.previous();
76
77             assertEquals("Iteration value is correct", testValue, iterValue);
78         }
79
80         assertTrue("Iterator should now be empty", !iter.hasPrevious());
81
82         try {
83             Object JavaDoc testValue = iter.previous();
84         } catch (Exception JavaDoc e) {
85             assertTrue(
86                 "NoSuchElementException must be thrown",
87                 e.getClass().equals((new NoSuchElementException JavaDoc()).getClass()));
88         }
89
90     }
91
92     /**
93      * Tests the {@link java.util.ListIterator#set} operation.
94      */

95     public void testListIteratorSet() {
96         String JavaDoc[] testData = new String JavaDoc[] { "a", "b", "c" };
97
98         String JavaDoc[] result = new String JavaDoc[] { "0", "1", "2" };
99
100         ListIterator JavaDoc iter = (ListIterator JavaDoc) makeArrayListIterator(testData);
101         int x = 0;
102
103         while (iter.hasNext()) {
104             iter.next();
105             iter.set(Integer.toString(x));
106             x++;
107         }
108
109         assertTrue("The two arrays should have the same value, i.e. {0,1,2}", Arrays.equals(testData, result));
110
111         // a call to set() before a call to next() or previous() should throw an IllegalStateException
112
iter = makeArrayListIterator(testArray);
113
114         try {
115             iter.set("should fail");
116             fail("ListIterator#set should fail if next() or previous() have not yet been called.");
117         } catch (IllegalStateException JavaDoc e) {
118             // expected
119
} catch (Throwable JavaDoc t) { // should never happen
120
fail(t.toString());
121         }
122
123     }
124
125 }
126
Popular Tags