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 TestSingletonIterator extends AbstractTestIterator { 35 36 private static final Object testValue = "foo"; 37 38 public static Test suite() { 39 return new TestSuite(TestSingletonIterator.class); 40 } 41 42 public TestSingletonIterator(String testName) { 43 super(testName); 44 } 45 46 50 public Iterator makeEmptyIterator() { 51 SingletonIterator iter = (SingletonIterator)makeFullIterator(); 52 iter.next(); 53 iter.remove(); 54 iter.reset(); 55 return iter; 56 } 57 58 public Iterator makeFullIterator() { 59 return new SingletonIterator( testValue ); 60 } 61 62 public boolean supportsRemove() { 63 return true; 64 } 65 66 public boolean supportsEmptyIterator() { 67 return true; 68 } 69 70 public void testIterator() { 71 Iterator iter = (Iterator ) makeObject(); 72 assertTrue("Iterator has a first item", iter.hasNext()); 73 74 Object iterValue = iter.next(); 75 assertEquals("Iteration value is correct", testValue, iterValue); 76 77 assertTrue("Iterator should now be empty", !iter.hasNext()); 78 79 try { 80 iter.next(); 81 } catch (Exception e) { 82 assertTrue( 83 "NoSuchElementException must be thrown", 84 e.getClass().equals((new NoSuchElementException ()).getClass())); 85 } 86 } 87 88 public void testSingletonIteratorRemove() { 89 ResettableIterator iter = new SingletonIterator("xyzzy"); 90 assertTrue(iter.hasNext()); 91 assertEquals("xyzzy",iter.next()); 92 iter.remove(); 93 iter.reset(); 94 assertTrue(! iter.hasNext()); 95 } 96 97 public void testReset() { 98 ResettableIterator it = (ResettableIterator) makeObject(); 99 100 assertEquals(true, it.hasNext()); 101 assertEquals(testValue, it.next()); 102 assertEquals(false, it.hasNext()); 103 104 it.reset(); 105 106 assertEquals(true, it.hasNext()); 107 assertEquals(testValue, it.next()); 108 assertEquals(false, it.hasNext()); 109 110 it.reset(); 111 it.reset(); 112 113 assertEquals(true, it.hasNext()); 114 } 115 116 } 117 | Popular Tags |