KickJava   Java API By Example, From Geeks To Geeks.

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


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.ListIterator 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.ResettableListIterator;
25
26 /**
27  * Tests the SingletonListIterator.
28  *
29  * @version $Revision: 1.11 $ $Date: 2004/02/18 01:20:33 $
30  *
31  * @author Stephen Colebourne
32  */

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

49     public ListIterator JavaDoc makeEmptyListIterator() {
50         SingletonListIterator iter = (SingletonListIterator)makeFullIterator();
51         iter.next();
52         iter.remove();
53         iter.reset();
54         return iter;
55     }
56
57     public ListIterator JavaDoc makeFullListIterator() {
58         return new SingletonListIterator( testValue );
59     }
60
61     public boolean supportsAdd() {
62         return false;
63     }
64
65     public boolean supportsRemove() {
66         return true;
67     }
68
69     public boolean supportsEmptyIterator() {
70         return true;
71     }
72
73     public void testIterator() {
74         ListIterator JavaDoc iter = (ListIterator JavaDoc) makeObject();
75         assertTrue( "Iterator should have next item", iter.hasNext() );
76         assertTrue( "Iterator should have no previous item", !iter.hasPrevious() );
77         assertEquals( "Iteration next index", 0, iter.nextIndex() );
78         assertEquals( "Iteration previous index", -1, iter.previousIndex() );
79         
80         Object JavaDoc iterValue = iter.next();
81         assertEquals( "Iteration value is correct", testValue, iterValue );
82         
83         assertTrue( "Iterator should have no next item", !iter.hasNext() );
84         assertTrue( "Iterator should have previous item", iter.hasPrevious() );
85         assertEquals( "Iteration next index", 1, iter.nextIndex() );
86         assertEquals( "Iteration previous index", 0, iter.previousIndex() );
87
88         iterValue = iter.previous();
89         assertEquals( "Iteration value is correct", testValue, iterValue );
90         
91         assertTrue( "Iterator should have next item", iter.hasNext() );
92         assertTrue( "Iterator should have no previous item", !iter.hasPrevious() );
93         assertEquals( "Iteration next index", 0, iter.nextIndex() );
94         assertEquals( "Iteration previous index", -1, iter.previousIndex() );
95
96         iterValue = iter.next();
97         assertEquals( "Iteration value is correct", testValue, iterValue );
98         
99         assertTrue( "Iterator should have no next item", !iter.hasNext() );
100         assertTrue( "Iterator should have previous item", iter.hasPrevious() );
101         assertEquals( "Iteration next index", 1, iter.nextIndex() );
102         assertEquals( "Iteration previous index", 0, iter.previousIndex() );
103
104         try {
105             iter.next();
106         } catch (Exception JavaDoc e) {
107           assertTrue("NoSuchElementException must be thrown",
108              e.getClass().equals((new NoSuchElementException JavaDoc()).getClass()));
109         }
110         iter.previous();
111         try {
112             iter.previous();
113         } catch (Exception JavaDoc e) {
114           assertTrue("NoSuchElementException must be thrown",
115              e.getClass().equals((new NoSuchElementException JavaDoc()).getClass()));
116         }
117     }
118     
119     public void testReset() {
120         ResettableListIterator it = (ResettableListIterator) makeObject();
121         
122         assertEquals(true, it.hasNext());
123         assertEquals(false, it.hasPrevious());
124         assertEquals(testValue, it.next());
125         assertEquals(false, it.hasNext());
126         assertEquals(true, it.hasPrevious());
127
128         it.reset();
129         
130         assertEquals(true, it.hasNext());
131         assertEquals(false, it.hasPrevious());
132         assertEquals(testValue, it.next());
133         assertEquals(false, it.hasNext());
134         assertEquals(true, it.hasPrevious());
135         
136         it.reset();
137         it.reset();
138         
139         assertEquals(true, it.hasNext());
140     }
141     
142 }
143
144
Popular Tags