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 36 public class TestArrayIterator extends AbstractTestIterator { 37 38 protected String [] testArray = { "One", "Two", "Three" }; 39 40 public static Test suite() { 41 return new TestSuite(TestArrayIterator.class); 42 } 43 44 public TestArrayIterator(String testName) { 45 super(testName); 46 } 47 48 public Iterator makeEmptyIterator() { 49 return new ArrayIterator(new Object [0]); 50 } 51 52 public Iterator makeFullIterator() { 53 return new ArrayIterator(testArray); 54 } 55 56 public boolean supportsRemove() { 57 return false; 58 } 59 60 61 public void testIterator() { 62 Iterator iter = (Iterator ) makeFullIterator(); 63 for (int i = 0; i < testArray.length; i++) { 64 Object testValue = testArray[i]; 65 Object iterValue = iter.next(); 66 67 assertEquals("Iteration value is correct", testValue, iterValue); 68 } 69 70 assertTrue("Iterator should now be empty", !iter.hasNext()); 71 72 try { 73 Object testValue = iter.next(); 74 } catch (Exception e) { 75 assertTrue( 76 "NoSuchElementException must be thrown", 77 e.getClass().equals((new NoSuchElementException ()).getClass())); 78 } 79 } 80 81 public void testNullArray() { 82 try { 83 Iterator iter = new ArrayIterator(null); 84 85 fail("Constructor should throw a NullPointerException when constructed with a null array"); 86 } catch (NullPointerException e) { 87 } 89 90 ArrayIterator iter = new ArrayIterator(); 91 try { 92 iter.setArray(null); 93 94 fail("setArray(null) should throw a NullPointerException"); 95 } catch (NullPointerException e) { 96 } 98 } 99 100 public void testReset() { 101 ArrayIterator it = (ArrayIterator) makeFullIterator(); 102 it.next(); 103 it.reset(); 104 assertEquals("One", it.next()); 105 } 106 107 } 108 | Popular Tags |