1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.Iterator ; 19 import java.util.NoSuchElementException ; 20 21 import junit.framework.Test; 22 import junit.framework.TestSuite; 23 24 import org.apache.commons.collections.ResettableIterator; 25 26 34 public class TestSingletonIterator2 extends AbstractTestIterator { 35 36 private static final Object testValue = "foo"; 37 38 public static Test suite() { 39 return new TestSuite(TestSingletonIterator2.class); 40 } 41 42 public TestSingletonIterator2(String testName) { 43 super(testName); 44 } 45 46 public Iterator makeEmptyIterator() { 48 SingletonIterator iter = new SingletonIterator(testValue); 49 iter.next(); 50 iter.remove(); 51 iter.reset(); 52 return iter; 53 } 54 55 public Iterator 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 public void testIterator() { 69 Iterator iter = (Iterator ) makeObject(); 70 assertTrue("Iterator has a first item", iter.hasNext()); 71 72 Object 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 e) { 80 assertTrue( 81 "NoSuchElementException must be thrown", 82 e.getClass().equals((new NoSuchElementException ()).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 |