KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.ListIterator JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 /**
28  * Tests the ListIteratorWrapper to insure that it simulates
29  * a ListIterator correctly.
30  *
31  * @version $Revision: 1.6 $ $Date: 2004/02/18 01:20:33 $
32  *
33  * @author Morgan Delagrange
34  */

35 public class TestListIteratorWrapper extends AbstractTestIterator {
36
37     protected String JavaDoc[] testArray = {
38         "One", "Two", "Three", "Four", "Five", "Six"
39     };
40
41     protected List JavaDoc list1 = null;
42
43     public static Test suite() {
44         return new TestSuite(TestListIteratorWrapper.class);
45     }
46
47     public TestListIteratorWrapper(String JavaDoc testName) {
48         super(testName);
49     }
50
51     public void setUp() {
52         list1 = new ArrayList JavaDoc();
53         list1.add("One");
54         list1.add("Two");
55         list1.add("Three");
56         list1.add("Four");
57         list1.add("Five");
58         list1.add("Six");
59     }
60
61     public Iterator JavaDoc makeEmptyIterator() {
62         ArrayList JavaDoc list = new ArrayList JavaDoc();
63         return new ListIteratorWrapper(list.iterator());
64     }
65
66     public Iterator JavaDoc makeFullIterator() {
67         Iterator JavaDoc i = list1.iterator();
68
69         return new ListIteratorWrapper(i);
70     }
71
72     public void testIterator() {
73         ListIterator JavaDoc iter = (ListIterator JavaDoc) makeFullIterator();
74         for ( int i = 0; i < testArray.length; i++ ) {
75             Object JavaDoc testValue = testArray[i];
76             Object JavaDoc iterValue = iter.next();
77
78             assertEquals( "Iteration value is correct", testValue, iterValue );
79         }
80
81         assertTrue("Iterator should now be empty", ! iter.hasNext() );
82
83         try {
84             Object JavaDoc testValue = iter.next();
85         } catch (Exception JavaDoc e) {
86             assertTrue("NoSuchElementException must be thrown",
87                        e.getClass().equals((new NoSuchElementException JavaDoc()).getClass()));
88         }
89
90         // now, read it backwards
91
for (int i = testArray.length - 1; i > -1; --i) {
92             Object JavaDoc testValue = testArray[i];
93             Object JavaDoc iterValue = iter.previous();
94
95             assertEquals( "Iteration value is correct", testValue, iterValue );
96         }
97
98         try {
99             Object JavaDoc testValue = iter.previous();
100         } catch (Exception JavaDoc e) {
101             assertTrue("NoSuchElementException must be thrown",
102                        e.getClass().equals((new NoSuchElementException JavaDoc()).getClass()));
103         }
104
105         // now, read it forwards again
106
for ( int i = 0; i < testArray.length; i++ ) {
107             Object JavaDoc testValue = testArray[i];
108             Object JavaDoc iterValue = iter.next();
109
110             assertEquals( "Iteration value is correct", testValue, iterValue );
111         }
112
113     }
114
115     public void testRemove() {
116         Iterator JavaDoc iter = (Iterator JavaDoc) makeFullIterator();
117
118         try {
119             iter.remove();
120             fail("FilterIterator does not support the remove() method");
121         } catch (UnsupportedOperationException JavaDoc e) {
122
123         }
124
125     }
126
127 }
128
129
Popular Tags