KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.Test;
22 import junit.framework.TestSuite;
23
24 /**
25  * Tests the ObjectArrayIterator.
26  *
27  * @version $Revision: 1.5 $ $Date: 2004/02/18 01:20:33 $
28  *
29  * @author James Strachan
30  * @author Mauricio S. Moura
31  * @author Morgan Delagrange
32  * @author Stephen Colebourne
33  */

34 public class TestObjectArrayIterator extends AbstractTestIterator {
35
36     protected String JavaDoc[] testArray = { "One", "Two", "Three" };
37
38     public static Test suite() {
39         return new TestSuite(TestObjectArrayIterator.class);
40     }
41
42     public TestObjectArrayIterator(String JavaDoc testName) {
43         super(testName);
44     }
45
46     public Iterator JavaDoc makeEmptyIterator() {
47         return new ObjectArrayIterator(new Object JavaDoc[0]);
48     }
49
50     public Iterator JavaDoc makeFullIterator() {
51         return new ObjectArrayIterator(testArray);
52     }
53
54     public ObjectArrayIterator makeArrayIterator() {
55         return new ObjectArrayIterator();
56     }
57
58     public ObjectArrayIterator makeArrayIterator(Object JavaDoc[] array) {
59         return new ObjectArrayIterator(array);
60     }
61
62     public ObjectArrayIterator makeArrayIterator(Object JavaDoc[] array, int index) {
63         return new ObjectArrayIterator(array, index);
64     }
65
66     public ObjectArrayIterator makeArrayIterator(Object JavaDoc[] array, int start, int end) {
67         return new ObjectArrayIterator(array, start, end);
68     }
69
70     public boolean supportsRemove() {
71         return false;
72     }
73
74     public void testIterator() {
75         Iterator JavaDoc iter = (Iterator JavaDoc) makeFullIterator();
76         for (int i = 0; i < testArray.length; i++) {
77             Object JavaDoc testValue = testArray[i];
78             Object JavaDoc iterValue = iter.next();
79
80             assertEquals("Iteration value is correct", testValue, iterValue);
81         }
82
83         assertTrue("Iterator should now be empty", !iter.hasNext());
84
85         try {
86             Object JavaDoc testValue = iter.next();
87         } catch (Exception JavaDoc e) {
88             assertTrue(
89                 "NoSuchElementException must be thrown",
90                 e.getClass().equals((new NoSuchElementException JavaDoc()).getClass()));
91         }
92     }
93
94     public void testNullArray() {
95         try {
96             Iterator JavaDoc iter = makeArrayIterator(null);
97
98             fail("Constructor should throw a NullPointerException when constructed with a null array");
99         } catch (NullPointerException JavaDoc e) {
100             // expected
101
}
102
103         ObjectArrayIterator iter = makeArrayIterator();
104         try {
105             iter.setArray(null);
106
107             fail("setArray(null) should throw a NullPointerException");
108         } catch (NullPointerException JavaDoc e) {
109             // expected
110
}
111     }
112
113     public void testDoubleSet() {
114         ObjectArrayIterator it = makeArrayIterator();
115         it.setArray(new String JavaDoc[0]);
116         try {
117             it.setArray(new String JavaDoc[0]);
118             fail();
119         } catch (IllegalStateException JavaDoc ex) {
120         }
121     }
122
123     public void testReset() {
124         ObjectArrayIterator it = makeArrayIterator(testArray);
125         it.next();
126         it.reset();
127         assertEquals("One", it.next());
128     }
129
130 }
131
Popular Tags