001: /*
002: * Copyright 2001-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.Iterator;
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 org.apache.commons.collections.ResettableListIterator;
027:
028: /**
029: * Tests the ListIteratorWrapper to insure that it simulates
030: * a ListIterator correctly.
031: *
032: * @version $Revision: 400314 $ $Date: 2006-05-06 14:59:30 +0100 (Sat, 06 May 2006) $
033: *
034: * @author Morgan Delagrange
035: */
036: public class TestListIteratorWrapper extends AbstractTestIterator {
037:
038: protected String[] testArray = { "One", "Two", "Three", "Four",
039: "Five", "Six" };
040:
041: protected List list1 = null;
042:
043: public static Test suite() {
044: return new TestSuite(TestListIteratorWrapper.class);
045: }
046:
047: public TestListIteratorWrapper(String testName) {
048: super (testName);
049: }
050:
051: public void setUp() {
052: list1 = new ArrayList();
053: list1.add("One");
054: list1.add("Two");
055: list1.add("Three");
056: list1.add("Four");
057: list1.add("Five");
058: list1.add("Six");
059: }
060:
061: public Iterator makeEmptyIterator() {
062: ArrayList list = new ArrayList();
063: return new ListIteratorWrapper(list.iterator());
064: }
065:
066: public Iterator makeFullIterator() {
067: Iterator i = list1.iterator();
068:
069: return new ListIteratorWrapper(i);
070: }
071:
072: public void testIterator() {
073: ListIterator iter = (ListIterator) makeFullIterator();
074: for (int i = 0; i < testArray.length; i++) {
075: Object testValue = testArray[i];
076: Object iterValue = iter.next();
077:
078: assertEquals("Iteration value is correct", testValue,
079: iterValue);
080: }
081:
082: assertTrue("Iterator should now be empty", !iter.hasNext());
083:
084: try {
085: iter.next();
086: } catch (Exception e) {
087: assertTrue("NoSuchElementException must be thrown", e
088: .getClass().equals(
089: (new NoSuchElementException()).getClass()));
090: }
091:
092: // now, read it backwards
093: for (int i = testArray.length - 1; i > -1; --i) {
094: Object testValue = testArray[i];
095: Object iterValue = iter.previous();
096:
097: assertEquals("Iteration value is correct", testValue,
098: iterValue);
099: }
100:
101: try {
102: iter.previous();
103: } catch (Exception e) {
104: assertTrue("NoSuchElementException must be thrown", e
105: .getClass().equals(
106: (new NoSuchElementException()).getClass()));
107: }
108:
109: // now, read it forwards again
110: for (int i = 0; i < testArray.length; i++) {
111: Object testValue = testArray[i];
112: Object iterValue = iter.next();
113:
114: assertEquals("Iteration value is correct", testValue,
115: iterValue);
116: }
117:
118: }
119:
120: public void testRemove() {
121: Iterator iter = (Iterator) makeFullIterator();
122:
123: try {
124: iter.remove();
125: fail("FilterIterator does not support the remove() method");
126: } catch (UnsupportedOperationException e) {
127:
128: }
129:
130: }
131:
132: public void testReset() {
133: ResettableListIterator iter = (ResettableListIterator) makeFullIterator();
134: Object first = iter.next();
135: Object second = iter.next();
136:
137: iter.reset();
138:
139: // after reset, there shouldn't be any previous elements
140: assertFalse("No previous elements after reset()", iter
141: .hasPrevious());
142:
143: // after reset, the results should be the same as before
144: assertEquals("First element should be the same", first, iter
145: .next());
146: assertEquals("Second elment should be the same", second, iter
147: .next());
148:
149: // after passing the point, where we resetted, continuation should work as expected
150: for (int i = 2; i < testArray.length; i++) {
151: Object testValue = testArray[i];
152: Object iterValue = iter.next();
153:
154: assertEquals("Iteration value is correct", testValue,
155: iterValue);
156: }
157: }
158:
159: }
|