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.Iterator;
019: import java.util.NoSuchElementException;
020:
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023:
024: import org.apache.commons.collections.ResettableIterator;
025:
026: /**
027: * Tests the SingletonIterator to ensure that the next() method will actually
028: * perform the iteration rather than the hasNext() method.
029: *
030: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
031: *
032: * @author James Strachan
033: */
034: public class TestSingletonIterator extends AbstractTestIterator {
035:
036: private static final Object testValue = "foo";
037:
038: public static Test suite() {
039: return new TestSuite(TestSingletonIterator.class);
040: }
041:
042: public TestSingletonIterator(String testName) {
043: super (testName);
044: }
045:
046: /**
047: * Returns a SingletonIterator from which
048: * the element has already been removed.
049: */
050: public Iterator makeEmptyIterator() {
051: SingletonIterator iter = (SingletonIterator) makeFullIterator();
052: iter.next();
053: iter.remove();
054: iter.reset();
055: return iter;
056: }
057:
058: public Iterator makeFullIterator() {
059: return new SingletonIterator(testValue);
060: }
061:
062: public boolean supportsRemove() {
063: return true;
064: }
065:
066: public boolean supportsEmptyIterator() {
067: return true;
068: }
069:
070: public void testIterator() {
071: Iterator iter = (Iterator) makeObject();
072: assertTrue("Iterator has a first item", iter.hasNext());
073:
074: Object iterValue = iter.next();
075: assertEquals("Iteration value is correct", testValue, iterValue);
076:
077: assertTrue("Iterator should now be empty", !iter.hasNext());
078:
079: try {
080: iter.next();
081: } catch (Exception e) {
082: assertTrue("NoSuchElementException must be thrown", e
083: .getClass().equals(
084: (new NoSuchElementException()).getClass()));
085: }
086: }
087:
088: public void testSingletonIteratorRemove() {
089: ResettableIterator iter = new SingletonIterator("xyzzy");
090: assertTrue(iter.hasNext());
091: assertEquals("xyzzy", iter.next());
092: iter.remove();
093: iter.reset();
094: assertTrue(!iter.hasNext());
095: }
096:
097: public void testReset() {
098: ResettableIterator it = (ResettableIterator) makeObject();
099:
100: assertEquals(true, it.hasNext());
101: assertEquals(testValue, it.next());
102: assertEquals(false, it.hasNext());
103:
104: it.reset();
105:
106: assertEquals(true, it.hasNext());
107: assertEquals(testValue, it.next());
108: assertEquals(false, it.hasNext());
109:
110: it.reset();
111: it.reset();
112:
113: assertEquals(true, it.hasNext());
114: }
115:
116: }
|