1 19 20 package org.netbeans.modules.java.source.util; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.NoSuchElementException ; 26 import junit.framework.*; 27 import org.netbeans.modules.java.source.TestUtil; 28 29 33 public class IteratorTest extends TestCase { 34 35 public IteratorTest(String testName) { 36 super(testName); 37 } 38 39 protected void setUp() throws Exception { 40 } 41 42 protected void tearDown() throws Exception { 43 } 44 45 public static Test suite() { 46 TestSuite suite = new TestSuite(IteratorTest.class); 47 return suite; 48 } 49 50 52 protected Iterable <IteratorDescription> createDescriptions() { 53 54 List <IteratorDescription> descs = new ArrayList <IteratorDescription>(); 55 56 List <Integer > 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 69 protected static final class IteratorDescription { 70 71 private String name; 72 private Iterator iterator; 73 private Iterator values; 74 private int expectedSize; 75 private boolean isModifiable; 76 77 public IteratorDescription( String name, Iterator iterator, 78 Iterator 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 getName() { 88 return name; 89 } 90 91 public Iterator getIterator() { 92 if ( iterator == null ) { 93 throw new IllegalStateException ( "IteratorDescription can only be used once."); 94 } 95 Iterator i = iterator; 96 iterator = null; 97 return i; 98 } 99 100 public Iterator getValues() { 101 if ( values == null ) { 102 throw new IllegalStateException ( "IteratorDescription can only be used once."); 103 } 104 Iterator 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 121 public void testValues() { 122 123 for( IteratorDescription id : createDescriptions() ) { 124 String 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 { 150 List <String > keys = new ArrayList <String >(1000); 151 for (int i=0; i<keys.size(); i++) { 152 keys.add(String.valueOf(i)); 153 } 154 Iterable <Integer > result = Iterators.translating(keys, new Factory<Integer ,String > () { 155 public Integer create(String parameter) { 156 return Integer.parseInt(parameter); 157 } 158 }); 159 Iterator <Integer > it = result.iterator(); 160 for (int i=0; i<keys.size(); i++) { 161 assertTrue (it.hasNext()); 162 assertEquals(i,it.next().intValue()); 163 } 164 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 174 public static <E> void assertIteratorSize( String name, Iterator <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 e ) { 184 return; } 186 187 fail( "NoSuchElementException should have been thrown from iterator: " + name + " but was not." ); 188 } 189 190 public static <E> void assertIteratorUnmodifiable( String name, Iterator <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 e ) { 200 } 202 catch( UnsupportedOperationException e ) { 203 } 205 } 206 207 E o = it.next(); 208 first = false; 209 210 try { 211 it.remove(); 212 } 213 catch( UnsupportedOperationException e ) { 214 continue; } 216 fail( "UnsupportedOperationException should have been thrown from iterator: " + name + " but was not." ); 217 } 218 } 219 220 public static <E> void assertIteratorModifiable( String name, Iterator <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 e ) { 230 } 232 } 233 234 E o = it.next(); 235 first = false; 236 237 try { 238 it.remove(); 239 } 240 catch( UnsupportedOperationException e ) { 241 fail( "Iterator: " + name + " should be modifiable but UnsupportedOperationException should has been thrown." ); 242 } 243 244 } 245 } 246 247 public static List <Integer > createSequentialList( int size ) { 248 List <Integer > result = new ArrayList <Integer >( size ); 249 for( int i = 0; i < size; i++ ) { 250 result.add( new Integer (i) ); 251 } 252 return result; 253 } 254 255 } 256 | Popular Tags |