KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Tests the ObjectArrayListIterator class.
28  *
29  * @version $Revision: 1.5 $ $Date: 2004/02/18 01:20:33 $
30  *
31  * @author Neil O'Toole
32  */

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

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

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

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