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 to ensure that the next() method will actually
026: * perform the iteration rather than the hasNext() method.
027: * The code of this test was supplied by Mauricio S. Moura.
028: *
029: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
030: *
031: * @author James Strachan
032: * @author Mauricio S. Moura
033: * @author Morgan Delagrange
034: * @author Stephen Colebourne
035: */
036: public class TestArrayIterator extends AbstractTestIterator {
037:
038: protected String[] testArray = { "One", "Two", "Three" };
039:
040: public static Test suite() {
041: return new TestSuite(TestArrayIterator.class);
042: }
043:
044: public TestArrayIterator(String testName) {
045: super (testName);
046: }
047:
048: public Iterator makeEmptyIterator() {
049: return new ArrayIterator(new Object[0]);
050: }
051:
052: public Iterator makeFullIterator() {
053: return new ArrayIterator(testArray);
054: }
055:
056: public boolean supportsRemove() {
057: return false;
058: }
059:
060: public void testIterator() {
061: Iterator iter = (Iterator) makeFullIterator();
062: for (int i = 0; i < testArray.length; i++) {
063: Object testValue = testArray[i];
064: Object iterValue = iter.next();
065:
066: assertEquals("Iteration value is correct", testValue,
067: iterValue);
068: }
069:
070: assertTrue("Iterator should now be empty", !iter.hasNext());
071:
072: try {
073: Object testValue = iter.next();
074: } catch (Exception e) {
075: assertTrue("NoSuchElementException must be thrown", e
076: .getClass().equals(
077: (new NoSuchElementException()).getClass()));
078: }
079: }
080:
081: public void testNullArray() {
082: try {
083: Iterator iter = new ArrayIterator(null);
084:
085: fail("Constructor should throw a NullPointerException when constructed with a null array");
086: } catch (NullPointerException e) {
087: // expected
088: }
089:
090: ArrayIterator iter = new ArrayIterator();
091: try {
092: iter.setArray(null);
093:
094: fail("setArray(null) should throw a NullPointerException");
095: } catch (NullPointerException e) {
096: // expected
097: }
098: }
099:
100: public void testReset() {
101: ArrayIterator it = (ArrayIterator) makeFullIterator();
102: it.next();
103: it.reset();
104: assertEquals("One", it.next());
105: }
106:
107: }
|