KickJava   Java API By Example, From Geeks To Geeks.

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


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.1 $ $Date: 2004/04/09 14:38:47 $
31  *
32  * @author James Strachan
33  */

34 public class TestSingletonIterator2 extends AbstractTestIterator {
35
36     private static final Object JavaDoc testValue = "foo";
37
38     public static Test suite() {
39         return new TestSuite(TestSingletonIterator2.class);
40     }
41
42     public TestSingletonIterator2(String JavaDoc testName) {
43         super(testName);
44     }
45
46     //-----------------------------------------------------------------------
47
public Iterator JavaDoc makeEmptyIterator() {
48         SingletonIterator iter = new SingletonIterator(testValue);
49         iter.next();
50         iter.remove();
51         iter.reset();
52         return iter;
53     }
54
55     public Iterator JavaDoc makeFullIterator() {
56         return new SingletonIterator(testValue, false);
57     }
58
59     public boolean supportsRemove() {
60         return false;
61     }
62
63     public boolean supportsEmptyIterator() {
64         return false;
65     }
66
67     //-----------------------------------------------------------------------
68
public void testIterator() {
69         Iterator JavaDoc iter = (Iterator JavaDoc) makeObject();
70         assertTrue("Iterator has a first item", iter.hasNext());
71
72         Object JavaDoc iterValue = iter.next();
73         assertEquals("Iteration value is correct", testValue, iterValue);
74
75         assertTrue("Iterator should now be empty", !iter.hasNext());
76
77         try {
78             iter.next();
79         } catch (Exception JavaDoc e) {
80             assertTrue(
81                 "NoSuchElementException must be thrown",
82                 e.getClass().equals((new NoSuchElementException JavaDoc()).getClass()));
83         }
84     }
85
86     public void testReset() {
87         ResettableIterator it = (ResettableIterator) makeObject();
88
89         assertEquals(true, it.hasNext());
90         assertEquals(testValue, it.next());
91         assertEquals(false, it.hasNext());
92
93         it.reset();
94
95         assertEquals(true, it.hasNext());
96         assertEquals(testValue, it.next());
97         assertEquals(false, it.hasNext());
98
99         it.reset();
100         it.reset();
101
102         assertEquals(true, it.hasNext());
103     }
104
105 }
106
Popular Tags