KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > iterators > TestSingletonIterator


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.iterators;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.NoSuchElementException JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23
24 import org.apache.commons.collections.ResettableIterator;
25
26 /**
27  * Tests the SingletonIterator to ensure that the next() method will actually
28  * perform the iteration rather than the hasNext() method.
29  *
30  * @version $Revision: 1.11 $ $Date: 2004/02/18 01:20:33 $
31  *
32  * @author James Strachan
33  */

34 public class TestSingletonIterator extends AbstractTestIterator {
35
36     private static final Object JavaDoc testValue = "foo";
37     
38     public static Test suite() {
39         return new TestSuite(TestSingletonIterator.class);
40     }
41     
42     public TestSingletonIterator(String JavaDoc testName) {
43         super(testName);
44     }
45     
46     /**
47      * Returns a SingletonIterator from which
48      * the element has already been removed.
49      */

50     public Iterator JavaDoc makeEmptyIterator() {
51         SingletonIterator iter = (SingletonIterator)makeFullIterator();
52         iter.next();
53         iter.remove();
54         iter.reset();
55         return iter;
56     }
57
58     public Iterator JavaDoc makeFullIterator() {
59         return new SingletonIterator( testValue );
60     }
61
62     public boolean supportsRemove() {
63         return true;
64     }
65
66     public boolean supportsEmptyIterator() {
67         return true;
68     }
69
70     public void testIterator() {
71         Iterator JavaDoc iter = (Iterator JavaDoc) makeObject();
72         assertTrue("Iterator has a first item", iter.hasNext());
73
74         Object JavaDoc iterValue = iter.next();
75         assertEquals("Iteration value is correct", testValue, iterValue);
76
77         assertTrue("Iterator should now be empty", !iter.hasNext());
78
79         try {
80             iter.next();
81         } catch (Exception JavaDoc e) {
82             assertTrue(
83                 "NoSuchElementException must be thrown",
84                 e.getClass().equals((new NoSuchElementException JavaDoc()).getClass()));
85         }
86     }
87     
88     public void testSingletonIteratorRemove() {
89         ResettableIterator iter = new SingletonIterator("xyzzy");
90         assertTrue(iter.hasNext());
91         assertEquals("xyzzy",iter.next());
92         iter.remove();
93         iter.reset();
94         assertTrue(! iter.hasNext());
95     }
96     
97     public void testReset() {
98         ResettableIterator it = (ResettableIterator) makeObject();
99         
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 }
117
Popular Tags