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.ListIterator;
019: import java.util.NoSuchElementException;
020:
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023:
024: import org.apache.commons.collections.ResettableListIterator;
025:
026: /**
027: * Tests the SingletonListIterator.
028: *
029: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
030: *
031: * @author Stephen Colebourne
032: */
033: public class TestSingletonListIterator extends AbstractTestListIterator {
034:
035: private static final Object testValue = "foo";
036:
037: public static Test suite() {
038: return new TestSuite(TestSingletonListIterator.class);
039: }
040:
041: public TestSingletonListIterator(String testName) {
042: super (testName);
043: }
044:
045: /**
046: * Returns a SingletonListIterator from which
047: * the element has already been removed.
048: */
049: public ListIterator makeEmptyListIterator() {
050: SingletonListIterator iter = (SingletonListIterator) makeFullIterator();
051: iter.next();
052: iter.remove();
053: iter.reset();
054: return iter;
055: }
056:
057: public ListIterator makeFullListIterator() {
058: return new SingletonListIterator(testValue);
059: }
060:
061: public boolean supportsAdd() {
062: return false;
063: }
064:
065: public boolean supportsRemove() {
066: return true;
067: }
068:
069: public boolean supportsEmptyIterator() {
070: return true;
071: }
072:
073: public void testIterator() {
074: ListIterator iter = (ListIterator) makeObject();
075: assertTrue("Iterator should have next item", iter.hasNext());
076: assertTrue("Iterator should have no previous item", !iter
077: .hasPrevious());
078: assertEquals("Iteration next index", 0, iter.nextIndex());
079: assertEquals("Iteration previous index", -1, iter
080: .previousIndex());
081:
082: Object iterValue = iter.next();
083: assertEquals("Iteration value is correct", testValue, iterValue);
084:
085: assertTrue("Iterator should have no next item", !iter.hasNext());
086: assertTrue("Iterator should have previous item", iter
087: .hasPrevious());
088: assertEquals("Iteration next index", 1, iter.nextIndex());
089: assertEquals("Iteration previous index", 0, iter
090: .previousIndex());
091:
092: iterValue = iter.previous();
093: assertEquals("Iteration value is correct", testValue, iterValue);
094:
095: assertTrue("Iterator should have next item", iter.hasNext());
096: assertTrue("Iterator should have no previous item", !iter
097: .hasPrevious());
098: assertEquals("Iteration next index", 0, iter.nextIndex());
099: assertEquals("Iteration previous index", -1, iter
100: .previousIndex());
101:
102: iterValue = iter.next();
103: assertEquals("Iteration value is correct", testValue, iterValue);
104:
105: assertTrue("Iterator should have no next item", !iter.hasNext());
106: assertTrue("Iterator should have previous item", iter
107: .hasPrevious());
108: assertEquals("Iteration next index", 1, iter.nextIndex());
109: assertEquals("Iteration previous index", 0, iter
110: .previousIndex());
111:
112: try {
113: iter.next();
114: } catch (Exception e) {
115: assertTrue("NoSuchElementException must be thrown", e
116: .getClass().equals(
117: (new NoSuchElementException()).getClass()));
118: }
119: iter.previous();
120: try {
121: iter.previous();
122: } catch (Exception e) {
123: assertTrue("NoSuchElementException must be thrown", e
124: .getClass().equals(
125: (new NoSuchElementException()).getClass()));
126: }
127: }
128:
129: public void testReset() {
130: ResettableListIterator it = (ResettableListIterator) makeObject();
131:
132: assertEquals(true, it.hasNext());
133: assertEquals(false, it.hasPrevious());
134: assertEquals(testValue, it.next());
135: assertEquals(false, it.hasNext());
136: assertEquals(true, it.hasPrevious());
137:
138: it.reset();
139:
140: assertEquals(true, it.hasNext());
141: assertEquals(false, it.hasPrevious());
142: assertEquals(testValue, it.next());
143: assertEquals(false, it.hasNext());
144: assertEquals(true, it.hasPrevious());
145:
146: it.reset();
147: it.reset();
148:
149: assertEquals(true, it.hasNext());
150: }
151:
152: }
|