001: /*
002: * Copyright 2006 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.ArrayList;
019: import java.util.Arrays;
020: import java.util.List;
021: import java.util.ListIterator;
022: import java.util.NoSuchElementException;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026: import junit.textui.TestRunner;
027:
028: import org.apache.commons.collections.ResettableListIterator;
029:
030: /**
031: * Tests the ReverseListIterator.
032: *
033: * @version $Revision: $ $Date: 2006-05-06 17:10:31 +0100 (Sat, 06 May 2006) $
034: */
035: public class TestReverseListIterator extends AbstractTestListIterator {
036:
037: protected String[] testArray = { "One", "Two", "Three", "Four" };
038:
039: public static void main(String args[]) {
040: String[] testCaseName = { TestReverseListIterator.class
041: .getName() };
042: TestRunner.main(testCaseName);
043: }
044:
045: public static Test suite() {
046: return new TestSuite(TestReverseListIterator.class);
047: }
048:
049: public TestReverseListIterator(String testName) {
050: super (testName);
051: }
052:
053: public ListIterator makeEmptyListIterator() {
054: List list = new ArrayList();
055: return new ReverseListIterator(list);
056: }
057:
058: public ListIterator makeFullListIterator() {
059: List list = new ArrayList(Arrays.asList(testArray));
060: return new ReverseListIterator(list);
061: }
062:
063: // overrides
064: //-----------------------------------------------------------------------
065: public void testEmptyListIteratorIsIndeedEmpty() {
066: ListIterator it = makeEmptyListIterator();
067:
068: assertEquals(false, it.hasNext());
069: assertEquals(-1, it.nextIndex()); // reversed index
070: assertEquals(false, it.hasPrevious());
071: assertEquals(0, it.previousIndex()); // reversed index
072:
073: // next() should throw a NoSuchElementException
074: try {
075: it.next();
076: fail("NoSuchElementException must be thrown from empty ListIterator");
077: } catch (NoSuchElementException e) {
078: }
079:
080: // previous() should throw a NoSuchElementException
081: try {
082: it.previous();
083: fail("NoSuchElementException must be thrown from empty ListIterator");
084: } catch (NoSuchElementException e) {
085: }
086: }
087:
088: public void testWalkForwardAndBack() {
089: ArrayList list = new ArrayList();
090: ListIterator it = makeFullListIterator();
091: while (it.hasNext()) {
092: list.add(it.next());
093: }
094:
095: // check state at end
096: assertEquals(false, it.hasNext());
097: assertEquals(true, it.hasPrevious());
098:
099: // this had to be commented out, as there is a bug in the JDK before JDK1.5
100: // where calling previous at the start of an iterator would push the cursor
101: // back to an invalid negative value
102: // try {
103: // it.next();
104: // fail("NoSuchElementException must be thrown from next at end of ListIterator");
105: // } catch (NoSuchElementException e) {
106: // }
107:
108: // loop back through comparing
109: for (int i = list.size() - 1; i >= 0; i--) {
110: assertEquals("" + i, list.size() - i - 2, it.nextIndex()); // reversed index
111: assertEquals(list.size() - i - 1, it.previousIndex()); // reversed index
112:
113: Object obj = list.get(i);
114: assertEquals(obj, it.previous());
115: }
116:
117: // check state at start
118: assertEquals(true, it.hasNext());
119: assertEquals(false, it.hasPrevious());
120: try {
121: it.previous();
122: fail("NoSuchElementException must be thrown from previous at start of ListIterator");
123: } catch (NoSuchElementException e) {
124: }
125: }
126:
127: //-----------------------------------------------------------------------
128: public void testReverse() {
129: ListIterator it = makeFullListIterator();
130: assertEquals(true, it.hasNext());
131: assertEquals(3, it.nextIndex());
132: assertEquals(false, it.hasPrevious());
133: assertEquals(4, it.previousIndex());
134: assertEquals("Four", it.next());
135: assertEquals(2, it.nextIndex());
136: assertEquals(true, it.hasNext());
137: assertEquals(3, it.previousIndex());
138: assertEquals(true, it.hasPrevious());
139: assertEquals("Three", it.next());
140: assertEquals(true, it.hasNext());
141: assertEquals(1, it.nextIndex());
142: assertEquals(true, it.hasPrevious());
143: assertEquals(2, it.previousIndex());
144: assertEquals("Two", it.next());
145: assertEquals(true, it.hasNext());
146: assertEquals(0, it.nextIndex());
147: assertEquals(true, it.hasPrevious());
148: assertEquals(1, it.previousIndex());
149: assertEquals("One", it.next());
150: assertEquals(false, it.hasNext());
151: assertEquals(-1, it.nextIndex());
152: assertEquals(true, it.hasPrevious());
153: assertEquals(0, it.previousIndex());
154: assertEquals("One", it.previous());
155: assertEquals("Two", it.previous());
156: assertEquals("Three", it.previous());
157: assertEquals("Four", it.previous());
158: }
159:
160: public void testReset() {
161: ResettableListIterator it = (ResettableListIterator) makeFullListIterator();
162: assertEquals("Four", it.next());
163: it.reset();
164: assertEquals("Four", it.next());
165: it.next();
166: it.next();
167: it.reset();
168: assertEquals("Four", it.next());
169: }
170:
171: }
|