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.Arrays;
019: import java.util.Iterator;
020: import java.util.ListIterator;
021: import java.util.NoSuchElementException;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: /**
027: * Tests the ObjectArrayListIterator class.
028: *
029: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
030: *
031: * @author Neil O'Toole
032: */
033: public class TestObjectArrayListIterator extends
034: TestObjectArrayIterator {
035:
036: public TestObjectArrayListIterator(String testName) {
037: super (testName);
038: }
039:
040: public static Test suite() {
041: return new TestSuite(TestObjectArrayListIterator.class);
042: }
043:
044: public Iterator makeEmptyIterator() {
045: return new ObjectArrayListIterator(new Object[0]);
046: }
047:
048: public Iterator makeFullIterator() {
049: return new ObjectArrayListIterator(testArray);
050: }
051:
052: public ListIterator makeArrayListIterator(Object[] array) {
053: return new ObjectArrayListIterator(array);
054: }
055:
056: /**
057: * Test the basic ListIterator functionality - going backwards using
058: * <code>previous()</code>.
059: */
060: public void testListIterator() {
061: ListIterator iter = (ListIterator) makeFullIterator();
062:
063: // TestArrayIterator#testIterator() has already tested the iterator forward,
064: // now we need to test it in reverse
065:
066: // fast-forward the iterator to the end...
067: while (iter.hasNext()) {
068: iter.next();
069: }
070:
071: for (int x = testArray.length - 1; x >= 0; x--) {
072: Object testValue = testArray[x];
073: Object iterValue = iter.previous();
074:
075: assertEquals("Iteration value is correct", testValue,
076: iterValue);
077: }
078:
079: assertTrue("Iterator should now be empty", !iter.hasPrevious());
080:
081: try {
082: Object testValue = iter.previous();
083: } catch (Exception e) {
084: assertTrue("NoSuchElementException must be thrown", e
085: .getClass().equals(
086: (new NoSuchElementException()).getClass()));
087: }
088:
089: }
090:
091: /**
092: * Tests the {@link java.util.ListIterator#set} operation.
093: */
094: public void testListIteratorSet() {
095: String[] testData = new String[] { "a", "b", "c" };
096:
097: String[] result = new String[] { "0", "1", "2" };
098:
099: ListIterator iter = (ListIterator) makeArrayListIterator(testData);
100: int x = 0;
101:
102: while (iter.hasNext()) {
103: iter.next();
104: iter.set(Integer.toString(x));
105: x++;
106: }
107:
108: assertTrue(
109: "The two arrays should have the same value, i.e. {0,1,2}",
110: Arrays.equals(testData, result));
111:
112: // a call to set() before a call to next() or previous() should throw an IllegalStateException
113: iter = makeArrayListIterator(testArray);
114:
115: try {
116: iter.set("should fail");
117: fail("ListIterator#set should fail if next() or previous() have not yet been called.");
118: } catch (IllegalStateException e) {
119: // expected
120: } catch (Throwable t) { // should never happen
121: fail(t.toString());
122: }
123:
124: }
125:
126: }
|