KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > util > IteratorTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.source.util;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26 import junit.framework.*;
27 import org.netbeans.modules.java.source.TestUtil;
28
29 /** Basic class for testing iterators.
30  *
31  * @author Petr Hrebejk
32  */

33 public class IteratorTest extends TestCase {
34
35     public IteratorTest(String JavaDoc testName) {
36         super(testName);
37     }
38
39     protected void setUp() throws Exception JavaDoc {
40     }
41
42     protected void tearDown() throws Exception JavaDoc {
43     }
44
45     public static Test suite() {
46         TestSuite suite = new TestSuite(IteratorTest.class);
47         return suite;
48     }
49     
50     // Protected methods -------------------------------------------------------
51

52     protected Iterable JavaDoc<IteratorDescription> createDescriptions() {
53     
54         List JavaDoc<IteratorDescription> descs = new ArrayList JavaDoc<IteratorDescription>();
55         
56         List JavaDoc<Integer JavaDoc> gl = IteratorTest.createSequentialList( 100 );
57         
58         descs.add( new IteratorDescription( "PlainListIterator",
59                                             gl.iterator(),
60                                             gl.iterator(),
61                                             gl.size(),
62                                             true ) );
63         
64         return descs;
65     }
66     
67     // Protected innerclasses --------------------------------------------------
68

69     protected static final class IteratorDescription {
70         
71         private String JavaDoc name;
72         private Iterator JavaDoc iterator;
73         private Iterator JavaDoc values;
74         private int expectedSize;
75         private boolean isModifiable;
76
77         public IteratorDescription( String JavaDoc name, Iterator JavaDoc iterator,
78                                     Iterator JavaDoc values, int expectedSize,
79                                     boolean isModifiable ) {
80             this.name = name;
81             this.iterator = iterator;
82             this.values = values;
83             this.expectedSize = expectedSize;
84             this.isModifiable = isModifiable;
85         }
86         
87         public String JavaDoc getName() {
88             return name;
89         }
90
91         public Iterator JavaDoc getIterator() {
92             if ( iterator == null ) {
93                 throw new IllegalStateException JavaDoc( "IteratorDescription can only be used once.");
94             }
95             Iterator JavaDoc i = iterator;
96             iterator = null;
97             return i;
98         }
99
100         public Iterator JavaDoc getValues() {
101             if ( values == null ) {
102                 throw new IllegalStateException JavaDoc( "IteratorDescription can only be used once.");
103             }
104             Iterator JavaDoc i = values;
105             values = null;
106             return i;
107         }
108
109         public int getExpectedSize() {
110             return expectedSize;
111         }
112
113         public boolean isModifiable() {
114             return isModifiable;
115         }
116         
117     }
118            
119     // Test methods ------------------------------------------------------------
120

121     public void testValues() {
122         
123         for( IteratorDescription id : createDescriptions() ) {
124             String JavaDoc diff = TestUtil.collectionDiff( id.getValues(), id.getIterator());
125             assertEquals( "Diff of iterator: " + id.getName() + " should be empty", "", diff );
126         }
127     }
128     
129     public void testIteratorSize() {
130         
131         for( IteratorDescription id : createDescriptions() ) {
132             assertIteratorSize( id.getName(), id.getIterator(), id.getExpectedSize());
133         }
134     }
135         
136     public void testModifications() {
137         
138         for( IteratorDescription id : createDescriptions() ) {
139             if ( id.isModifiable() ) {
140                 assertIteratorModifiable( id.getName(), id.getIterator());
141             }
142             else {
143                 assertIteratorUnmodifiable( id.getName(), id.getIterator());
144             }
145         }
146     }
147     
148     
149     public void testTranslatingIterable () throws Exception JavaDoc {
150         List JavaDoc<String JavaDoc> keys = new ArrayList JavaDoc<String JavaDoc>(1000);
151         for (int i=0; i<keys.size(); i++) {
152             keys.add(String.valueOf(i));
153         }
154         Iterable JavaDoc<Integer JavaDoc> result = Iterators.translating(keys, new Factory<Integer JavaDoc,String JavaDoc> () {
155             public Integer JavaDoc create(String JavaDoc parameter) {
156                 return Integer.parseInt(parameter);
157             }
158         });
159         Iterator JavaDoc<Integer JavaDoc> it = result.iterator();
160         for (int i=0; i<keys.size(); i++) {
161             assertTrue (it.hasNext());
162             assertEquals(i,it.next().intValue());
163         }
164         //Iterable.iterator() should be idempotent
165
it = result.iterator();
166         for (int i=0; i<keys.size(); i++) {
167             assertTrue (it.hasNext());
168             assertEquals(i,it.next().intValue());
169         }
170     }
171         
172     // Reusable static methods -------------------------------------------------
173

174     public static <E> void assertIteratorSize( String JavaDoc name, Iterator JavaDoc<E> it, int expectedSize ) {
175         for( int i = 0; i < expectedSize; i++ ) {
176             assertTrue( "Iterator: " + name + " should have next element.", it.hasNext() );
177             E o = it.next();
178         }
179         
180         try {
181             it.next();
182         }
183         catch ( NoSuchElementException JavaDoc e ) {
184             return; // That's fine
185
}
186         
187         fail( "NoSuchElementException should have been thrown from iterator: " + name + " but was not." );
188     }
189     
190     public static <E> void assertIteratorUnmodifiable( String JavaDoc name, Iterator JavaDoc<E> it ) {
191         boolean first = true;
192         while( it.hasNext() ) {
193             
194             if ( first ) {
195                 try {
196                     it.remove();
197                     fail( "IllegalStateException or UnsupportedOperationException should have been thrown from iterator: " + name + " but was not." );
198                 }
199                 catch( IllegalStateException JavaDoc e ) {
200                     // That's fine
201
}
202                 catch( UnsupportedOperationException JavaDoc e ) {
203                     // That's fine too
204
}
205             }
206             
207             E o = it.next();
208             first = false;
209             
210             try {
211                 it.remove();
212             }
213             catch( UnsupportedOperationException JavaDoc e ) {
214                 continue; // That's fine
215
}
216             fail( "UnsupportedOperationException should have been thrown from iterator: " + name + " but was not." );
217         }
218     }
219     
220     public static <E> void assertIteratorModifiable( String JavaDoc name, Iterator JavaDoc<E> it ) {
221         boolean first = true;
222         while( it.hasNext() ) {
223             
224             if ( first ) {
225                 try {
226                     it.remove();
227                     fail( "IllegalStateException should have been thrown from iterator: " + name + " but was not." );
228                 }
229                 catch( IllegalStateException JavaDoc e ) {
230                     // That's fine
231
}
232             }
233             
234             E o = it.next();
235             first = false;
236             
237             try {
238                 it.remove();
239             }
240             catch( UnsupportedOperationException JavaDoc e ) {
241                 fail( "Iterator: " + name + " should be modifiable but UnsupportedOperationException should has been thrown." );
242             }
243             
244         }
245     }
246     
247     public static List JavaDoc<Integer JavaDoc> createSequentialList( int size ) {
248         List JavaDoc<Integer JavaDoc> result = new ArrayList JavaDoc<Integer JavaDoc>( size );
249         for( int i = 0; i < size; i++ ) {
250             result.add( new Integer JavaDoc(i) );
251         }
252         return result;
253     }
254         
255 }
256
Popular Tags