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