KickJava   Java API By Example, From Geeks To Geeks.

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


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 to ensure that the next() method will actually
26  * perform the iteration rather than the hasNext() method.
27  * The code of this test was supplied by Mauricio S. Moura.
28  *
29  * @version $Revision: 1.7 $ $Date: 2004/02/18 01:20:33 $
30  *
31  * @author James Strachan
32  * @author Mauricio S. Moura
33  * @author Morgan Delagrange
34  * @author Stephen Colebourne
35  */

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