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