KickJava   Java API By Example, From Geeks To Geeks.

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


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 ArrayIterator with primitive type arrays.
26  *
27  * @version $Revision: 1.7 $ $Date: 2004/02/18 01:20:33 $
28  *
29  * @author Morgan Delagrange
30  * @author James Strachan
31  */

32 public class TestArrayIterator2 extends AbstractTestIterator {
33
34     protected int[] testArray = { 2, 4, 6, 8 };
35
36     public static Test suite() {
37         return new TestSuite(TestArrayIterator2.class);
38     }
39
40     public TestArrayIterator2(String JavaDoc testName) {
41         super(testName);
42     }
43
44     public Iterator JavaDoc makeEmptyIterator() {
45         return new ArrayIterator(new int[0]);
46     }
47
48     public Iterator JavaDoc makeFullIterator() {
49         return new ArrayIterator(testArray);
50     }
51
52     /*
53      * We use these <code>makeArrayIterator</code> factory methods instead of
54      * directly calling the constructor so as to allow subclasses
55      * (e.g. TestArrayListIterator2) to use the existing test code.
56      *
57      * @return ArrayIterator
58      */

59     public ArrayIterator makeArrayIterator() {
60         return (ArrayIterator) makeEmptyIterator();
61     }
62     public ArrayIterator makeArrayIterator(Object JavaDoc array) {
63         return new ArrayIterator(array);
64     }
65     public ArrayIterator makeArrayIterator(Object JavaDoc array, int index) {
66         return new ArrayIterator(array, index);
67     }
68     public ArrayIterator makeArrayIterator(Object JavaDoc array, int start, int end) {
69         return new ArrayIterator(array, start, end);
70     }
71
72     public boolean supportsRemove() {
73         return false;
74     }
75
76
77     public void testIterator() {
78         Iterator JavaDoc iter = (Iterator JavaDoc) makeFullIterator();
79         for (int i = 0; i < testArray.length; i++) {
80             Integer JavaDoc testValue = new Integer JavaDoc(testArray[i]);
81             Number JavaDoc iterValue = (Number JavaDoc) iter.next();
82
83             assertEquals("Iteration value is correct", testValue, iterValue);
84         }
85
86         assertTrue("Iterator should now be empty", !iter.hasNext());
87
88         try {
89             Object JavaDoc testValue = iter.next();
90         } catch (Exception JavaDoc e) {
91             assertTrue(
92                 "NoSuchElementException must be thrown",
93                 e.getClass().equals((new NoSuchElementException JavaDoc()).getClass()));
94         }
95     }
96
97     // proves that an ArrayIterator set with the constructor has the same number of elements
98
// as an ArrayIterator set with setArray(Object)
99
public void testSetArray() {
100         Iterator JavaDoc iter1 = makeArrayIterator(testArray);
101         int count1 = 0;
102         while (iter1.hasNext()) {
103             ++count1;
104             iter1.next();
105         }
106
107         assertEquals("the count should be right using the constructor", count1, testArray.length);
108
109         ArrayIterator iter2 = makeArrayIterator();
110         iter2.setArray(testArray);
111         int count2 = 0;
112         while (iter2.hasNext()) {
113             ++count2;
114             iter2.next();
115         }
116
117         assertEquals("the count should be right using setArray(Object)", count2, testArray.length);
118     }
119
120     public void testIndexedArray() {
121         Iterator JavaDoc iter = makeArrayIterator(testArray, 2);
122         int count = 0;
123         while (iter.hasNext()) {
124             ++count;
125             iter.next();
126         }
127
128         assertEquals("the count should be right using ArrayIterator(Object,2) ", count, testArray.length - 2);
129
130         iter = makeArrayIterator(testArray, 1, testArray.length - 1);
131         count = 0;
132         while (iter.hasNext()) {
133             ++count;
134             iter.next();
135         }
136
137         assertEquals(
138             "the count should be right using ArrayIterator(Object,1," + (testArray.length - 1) + ") ",
139             count,
140             testArray.length - 2);
141
142         try {
143             iter = makeArrayIterator(testArray, -1);
144             fail("new ArrayIterator(Object,-1) should throw an ArrayIndexOutOfBoundsException");
145         } catch (ArrayIndexOutOfBoundsException JavaDoc aioobe) {
146             // expected
147
}
148
149         try {
150             iter = makeArrayIterator(testArray, testArray.length + 1);
151             fail("new ArrayIterator(Object,length+1) should throw an ArrayIndexOutOfBoundsException");
152         } catch (ArrayIndexOutOfBoundsException JavaDoc aioobe) {
153             // expected
154
}
155
156         try {
157             iter = makeArrayIterator(testArray, 0, -1);
158             fail("new ArrayIterator(Object,0,-1) should throw an ArrayIndexOutOfBoundsException");
159         } catch (ArrayIndexOutOfBoundsException JavaDoc aioobe) {
160             // expected
161
}
162
163         try {
164             iter = makeArrayIterator(testArray, 0, testArray.length + 1);
165             fail("new ArrayIterator(Object,0,length+1) should throw an ArrayIndexOutOfBoundsException");
166         } catch (ArrayIndexOutOfBoundsException JavaDoc aioobe) {
167             // expected
168
}
169
170         try {
171             iter = makeArrayIterator(testArray, 1, 1);
172             // expected not to fail
173
} catch (IllegalArgumentException JavaDoc iae) {
174             // MODIFIED: an iterator over a zero-length section of array
175
// should be perfectly legal behavior
176
fail("new ArrayIterator(Object,1,1) should NOT throw an IllegalArgumentException");
177         }
178
179         try {
180             iter = makeArrayIterator(testArray, testArray.length - 1, testArray.length - 2);
181             fail("new ArrayIterator(Object,length-2,length-1) should throw an IllegalArgumentException");
182         } catch (IllegalArgumentException JavaDoc iae) {
183             // expected
184
}
185     }
186 }
187
Popular Tags