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 32 public class TestArrayIterator2 extends AbstractTestIterator { 33 34 protected int[] testArray = { 2, 4, 6, 8 }; 35 36 public static Test suite() { 37 return new TestSuite(TestArrayIterator2.class); 38 } 39 40 public TestArrayIterator2(String testName) { 41 super(testName); 42 } 43 44 public Iterator makeEmptyIterator() { 45 return new ArrayIterator(new int[0]); 46 } 47 48 public Iterator makeFullIterator() { 49 return new ArrayIterator(testArray); 50 } 51 52 59 public ArrayIterator makeArrayIterator() { 60 return (ArrayIterator) makeEmptyIterator(); 61 } 62 public ArrayIterator makeArrayIterator(Object array) { 63 return new ArrayIterator(array); 64 } 65 public ArrayIterator makeArrayIterator(Object array, int index) { 66 return new ArrayIterator(array, index); 67 } 68 public ArrayIterator makeArrayIterator(Object array, int start, int end) { 69 return new ArrayIterator(array, start, end); 70 } 71 72 public boolean supportsRemove() { 73 return false; 74 } 75 76 77 public void testIterator() { 78 Iterator iter = (Iterator ) makeFullIterator(); 79 for (int i = 0; i < testArray.length; i++) { 80 Integer testValue = new Integer (testArray[i]); 81 Number iterValue = (Number ) iter.next(); 82 83 assertEquals("Iteration value is correct", testValue, iterValue); 84 } 85 86 assertTrue("Iterator should now be empty", !iter.hasNext()); 87 88 try { 89 Object testValue = iter.next(); 90 } catch (Exception e) { 91 assertTrue( 92 "NoSuchElementException must be thrown", 93 e.getClass().equals((new NoSuchElementException ()).getClass())); 94 } 95 } 96 97 public void testSetArray() { 100 Iterator iter1 = makeArrayIterator(testArray); 101 int count1 = 0; 102 while (iter1.hasNext()) { 103 ++count1; 104 iter1.next(); 105 } 106 107 assertEquals("the count should be right using the constructor", count1, testArray.length); 108 109 ArrayIterator iter2 = makeArrayIterator(); 110 iter2.setArray(testArray); 111 int count2 = 0; 112 while (iter2.hasNext()) { 113 ++count2; 114 iter2.next(); 115 } 116 117 assertEquals("the count should be right using setArray(Object)", count2, testArray.length); 118 } 119 120 public void testIndexedArray() { 121 Iterator iter = makeArrayIterator(testArray, 2); 122 int count = 0; 123 while (iter.hasNext()) { 124 ++count; 125 iter.next(); 126 } 127 128 assertEquals("the count should be right using ArrayIterator(Object,2) ", count, testArray.length - 2); 129 130 iter = makeArrayIterator(testArray, 1, testArray.length - 1); 131 count = 0; 132 while (iter.hasNext()) { 133 ++count; 134 iter.next(); 135 } 136 137 assertEquals( 138 "the count should be right using ArrayIterator(Object,1," + (testArray.length - 1) + ") ", 139 count, 140 testArray.length - 2); 141 142 try { 143 iter = makeArrayIterator(testArray, -1); 144 fail("new ArrayIterator(Object,-1) should throw an ArrayIndexOutOfBoundsException"); 145 } catch (ArrayIndexOutOfBoundsException aioobe) { 146 } 148 149 try { 150 iter = makeArrayIterator(testArray, testArray.length + 1); 151 fail("new ArrayIterator(Object,length+1) should throw an ArrayIndexOutOfBoundsException"); 152 } catch (ArrayIndexOutOfBoundsException aioobe) { 153 } 155 156 try { 157 iter = makeArrayIterator(testArray, 0, -1); 158 fail("new ArrayIterator(Object,0,-1) should throw an ArrayIndexOutOfBoundsException"); 159 } catch (ArrayIndexOutOfBoundsException aioobe) { 160 } 162 163 try { 164 iter = makeArrayIterator(testArray, 0, testArray.length + 1); 165 fail("new ArrayIterator(Object,0,length+1) should throw an ArrayIndexOutOfBoundsException"); 166 } catch (ArrayIndexOutOfBoundsException aioobe) { 167 } 169 170 try { 171 iter = makeArrayIterator(testArray, 1, 1); 172 } catch (IllegalArgumentException iae) { 174 fail("new ArrayIterator(Object,1,1) should NOT throw an IllegalArgumentException"); 177 } 178 179 try { 180 iter = makeArrayIterator(testArray, testArray.length - 1, testArray.length - 2); 181 fail("new ArrayIterator(Object,length-2,length-1) should throw an IllegalArgumentException"); 182 } catch (IllegalArgumentException iae) { 183 } 185 } 186 } 187 | Popular Tags |