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 TestSingletonIterator2 extends AbstractTestIterator {
035:
036: private static final Object testValue = "foo";
037:
038: public static Test suite() {
039: return new TestSuite(TestSingletonIterator2.class);
040: }
041:
042: public TestSingletonIterator2(String testName) {
043: super (testName);
044: }
045:
046: //-----------------------------------------------------------------------
047: public Iterator makeEmptyIterator() {
048: SingletonIterator iter = new SingletonIterator(testValue);
049: iter.next();
050: iter.remove();
051: iter.reset();
052: return iter;
053: }
054:
055: public Iterator makeFullIterator() {
056: return new SingletonIterator(testValue, false);
057: }
058:
059: public boolean supportsRemove() {
060: return false;
061: }
062:
063: public boolean supportsEmptyIterator() {
064: return false;
065: }
066:
067: //-----------------------------------------------------------------------
068: public void testIterator() {
069: Iterator iter = (Iterator) makeObject();
070: assertTrue("Iterator has a first item", iter.hasNext());
071:
072: Object iterValue = iter.next();
073: assertEquals("Iteration value is correct", testValue, iterValue);
074:
075: assertTrue("Iterator should now be empty", !iter.hasNext());
076:
077: try {
078: iter.next();
079: } catch (Exception e) {
080: assertTrue("NoSuchElementException must be thrown", e
081: .getClass().equals(
082: (new NoSuchElementException()).getClass()));
083: }
084: }
085:
086: public void testReset() {
087: ResettableIterator it = (ResettableIterator) makeObject();
088:
089: assertEquals(true, it.hasNext());
090: assertEquals(testValue, it.next());
091: assertEquals(false, it.hasNext());
092:
093: it.reset();
094:
095: assertEquals(true, it.hasNext());
096: assertEquals(testValue, it.next());
097: assertEquals(false, it.hasNext());
098:
099: it.reset();
100: it.reset();
101:
102: assertEquals(true, it.hasNext());
103: }
104:
105: }
|