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 ObjectArrayIterator.
026: *
027: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
028: *
029: * @author James Strachan
030: * @author Mauricio S. Moura
031: * @author Morgan Delagrange
032: * @author Stephen Colebourne
033: */
034: public class TestObjectArrayIterator extends AbstractTestIterator {
035:
036: protected String[] testArray = { "One", "Two", "Three" };
037:
038: public static Test suite() {
039: return new TestSuite(TestObjectArrayIterator.class);
040: }
041:
042: public TestObjectArrayIterator(String testName) {
043: super (testName);
044: }
045:
046: public Iterator makeEmptyIterator() {
047: return new ObjectArrayIterator(new Object[0]);
048: }
049:
050: public Iterator makeFullIterator() {
051: return new ObjectArrayIterator(testArray);
052: }
053:
054: public ObjectArrayIterator makeArrayIterator() {
055: return new ObjectArrayIterator();
056: }
057:
058: public ObjectArrayIterator makeArrayIterator(Object[] array) {
059: return new ObjectArrayIterator(array);
060: }
061:
062: public ObjectArrayIterator makeArrayIterator(Object[] array,
063: int index) {
064: return new ObjectArrayIterator(array, index);
065: }
066:
067: public ObjectArrayIterator makeArrayIterator(Object[] array,
068: int start, int end) {
069: return new ObjectArrayIterator(array, start, end);
070: }
071:
072: public boolean supportsRemove() {
073: return false;
074: }
075:
076: public void testIterator() {
077: Iterator iter = (Iterator) makeFullIterator();
078: for (int i = 0; i < testArray.length; i++) {
079: Object testValue = testArray[i];
080: Object iterValue = iter.next();
081:
082: assertEquals("Iteration value is correct", testValue,
083: iterValue);
084: }
085:
086: assertTrue("Iterator should now be empty", !iter.hasNext());
087:
088: try {
089: Object testValue = iter.next();
090: } catch (Exception e) {
091: assertTrue("NoSuchElementException must be thrown", e
092: .getClass().equals(
093: (new NoSuchElementException()).getClass()));
094: }
095: }
096:
097: public void testNullArray() {
098: try {
099: Iterator iter = makeArrayIterator(null);
100:
101: fail("Constructor should throw a NullPointerException when constructed with a null array");
102: } catch (NullPointerException e) {
103: // expected
104: }
105:
106: ObjectArrayIterator iter = makeArrayIterator();
107: try {
108: iter.setArray(null);
109:
110: fail("setArray(null) should throw a NullPointerException");
111: } catch (NullPointerException e) {
112: // expected
113: }
114: }
115:
116: public void testDoubleSet() {
117: ObjectArrayIterator it = makeArrayIterator();
118: it.setArray(new String[0]);
119: try {
120: it.setArray(new String[0]);
121: fail();
122: } catch (IllegalStateException ex) {
123: }
124: }
125:
126: public void testReset() {
127: ObjectArrayIterator it = makeArrayIterator(testArray);
128: it.next();
129: it.reset();
130: assertEquals("One", it.next());
131: }
132:
133: }
|