001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.iterators;
017:
018: import java.util.Iterator;
019: import java.util.NoSuchElementException;
020:
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023:
024: /**
025: * Tests the ArrayIterator with primitive type arrays.
026: *
027: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
028: *
029: * @author Morgan Delagrange
030: * @author James Strachan
031: */
032: public class TestArrayIterator2 extends AbstractTestIterator {
033:
034: protected int[] testArray = { 2, 4, 6, 8 };
035:
036: public static Test suite() {
037: return new TestSuite(TestArrayIterator2.class);
038: }
039:
040: public TestArrayIterator2(String testName) {
041: super (testName);
042: }
043:
044: public Iterator makeEmptyIterator() {
045: return new ArrayIterator(new int[0]);
046: }
047:
048: public Iterator makeFullIterator() {
049: return new ArrayIterator(testArray);
050: }
051:
052: /*
053: * We use these <code>makeArrayIterator</code> factory methods instead of
054: * directly calling the constructor so as to allow subclasses
055: * (e.g. TestArrayListIterator2) to use the existing test code.
056: *
057: * @return ArrayIterator
058: */
059: public ArrayIterator makeArrayIterator() {
060: return (ArrayIterator) makeEmptyIterator();
061: }
062:
063: public ArrayIterator makeArrayIterator(Object array) {
064: return new ArrayIterator(array);
065: }
066:
067: public ArrayIterator makeArrayIterator(Object array, int index) {
068: return new ArrayIterator(array, index);
069: }
070:
071: public ArrayIterator makeArrayIterator(Object array, int start,
072: int end) {
073: return new ArrayIterator(array, start, end);
074: }
075:
076: public boolean supportsRemove() {
077: return false;
078: }
079:
080: public void testIterator() {
081: Iterator iter = (Iterator) makeFullIterator();
082: for (int i = 0; i < testArray.length; i++) {
083: Integer testValue = new Integer(testArray[i]);
084: Number iterValue = (Number) iter.next();
085:
086: assertEquals("Iteration value is correct", testValue,
087: iterValue);
088: }
089:
090: assertTrue("Iterator should now be empty", !iter.hasNext());
091:
092: try {
093: Object testValue = iter.next();
094: } catch (Exception e) {
095: assertTrue("NoSuchElementException must be thrown", e
096: .getClass().equals(
097: (new NoSuchElementException()).getClass()));
098: }
099: }
100:
101: // proves that an ArrayIterator set with the constructor has the same number of elements
102: // as an ArrayIterator set with setArray(Object)
103: public void testSetArray() {
104: Iterator iter1 = makeArrayIterator(testArray);
105: int count1 = 0;
106: while (iter1.hasNext()) {
107: ++count1;
108: iter1.next();
109: }
110:
111: assertEquals("the count should be right using the constructor",
112: count1, testArray.length);
113:
114: ArrayIterator iter2 = makeArrayIterator();
115: iter2.setArray(testArray);
116: int count2 = 0;
117: while (iter2.hasNext()) {
118: ++count2;
119: iter2.next();
120: }
121:
122: assertEquals(
123: "the count should be right using setArray(Object)",
124: count2, testArray.length);
125: }
126:
127: public void testIndexedArray() {
128: Iterator iter = makeArrayIterator(testArray, 2);
129: int count = 0;
130: while (iter.hasNext()) {
131: ++count;
132: iter.next();
133: }
134:
135: assertEquals(
136: "the count should be right using ArrayIterator(Object,2) ",
137: count, testArray.length - 2);
138:
139: iter = makeArrayIterator(testArray, 1, testArray.length - 1);
140: count = 0;
141: while (iter.hasNext()) {
142: ++count;
143: iter.next();
144: }
145:
146: assertEquals(
147: "the count should be right using ArrayIterator(Object,1,"
148: + (testArray.length - 1) + ") ", count,
149: testArray.length - 2);
150:
151: try {
152: iter = makeArrayIterator(testArray, -1);
153: fail("new ArrayIterator(Object,-1) should throw an ArrayIndexOutOfBoundsException");
154: } catch (ArrayIndexOutOfBoundsException aioobe) {
155: // expected
156: }
157:
158: try {
159: iter = makeArrayIterator(testArray, testArray.length + 1);
160: fail("new ArrayIterator(Object,length+1) should throw an ArrayIndexOutOfBoundsException");
161: } catch (ArrayIndexOutOfBoundsException aioobe) {
162: // expected
163: }
164:
165: try {
166: iter = makeArrayIterator(testArray, 0, -1);
167: fail("new ArrayIterator(Object,0,-1) should throw an ArrayIndexOutOfBoundsException");
168: } catch (ArrayIndexOutOfBoundsException aioobe) {
169: // expected
170: }
171:
172: try {
173: iter = makeArrayIterator(testArray, 0, testArray.length + 1);
174: fail("new ArrayIterator(Object,0,length+1) should throw an ArrayIndexOutOfBoundsException");
175: } catch (ArrayIndexOutOfBoundsException aioobe) {
176: // expected
177: }
178:
179: try {
180: iter = makeArrayIterator(testArray, 1, 1);
181: // expected not to fail
182: } catch (IllegalArgumentException iae) {
183: // MODIFIED: an iterator over a zero-length section of array
184: // should be perfectly legal behavior
185: fail("new ArrayIterator(Object,1,1) should NOT throw an IllegalArgumentException");
186: }
187:
188: try {
189: iter = makeArrayIterator(testArray, testArray.length - 1,
190: testArray.length - 2);
191: fail("new ArrayIterator(Object,length-2,length-1) should throw an IllegalArgumentException");
192: } catch (IllegalArgumentException iae) {
193: // expected
194: }
195: }
196: }
|