1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.Iterator ; 19 import java.util.NoSuchElementException ; 20 21 import org.apache.commons.collections.AbstractTestObject; 22 23 37 public abstract class AbstractTestIterator extends AbstractTestObject { 38 39 44 public AbstractTestIterator(String testName) { 45 super(testName); 46 } 47 48 54 public abstract Iterator makeEmptyIterator(); 55 56 61 public abstract Iterator makeFullIterator(); 62 63 68 public Object makeObject() { 69 return makeFullIterator(); 70 } 71 72 78 public boolean supportsEmptyIterator() { 79 return true; 80 } 81 82 88 public boolean supportsFullIterator() { 89 return true; 90 } 91 92 98 public boolean supportsRemove() { 99 return true; 100 } 101 102 105 public void verify() { 106 } 108 109 113 public void testEmptyIterator() { 114 if (supportsEmptyIterator() == false) { 115 return; 116 } 117 118 Iterator it = makeEmptyIterator(); 119 120 assertEquals("hasNext() should return false for empty iterators", false, it.hasNext()); 122 123 try { 125 it.next(); 126 fail("NoSuchElementException must be thrown when Iterator is exhausted"); 127 } catch (NoSuchElementException e) { 128 } 129 verify(); 130 131 assertNotNull(it.toString()); 132 } 133 134 137 public void testFullIterator() { 138 if (supportsFullIterator() == false) { 139 return; 140 } 141 142 Iterator it = makeFullIterator(); 143 144 assertEquals("hasNext() should return true for at least one element", true, it.hasNext()); 146 147 try { 149 it.next(); 150 } catch (NoSuchElementException e) { 151 fail("Full iterators must have at least one element"); 152 } 153 154 while (it.hasNext()) { 156 it.next(); 157 verify(); 158 } 159 160 try { 162 it.next(); 163 fail("NoSuchElementException must be thrown when Iterator is exhausted"); 164 } catch (NoSuchElementException e) { 165 } 166 167 assertNotNull(it.toString()); 168 } 169 170 173 public void testRemove() { 174 Iterator it = makeFullIterator(); 175 176 if (supportsRemove() == false) { 177 try { 179 it.remove(); 180 } catch (UnsupportedOperationException ex) {} 181 return; 182 } 183 184 try { 186 it.remove(); 187 fail(); 188 } catch (IllegalStateException ex) {} 189 verify(); 190 191 it.next(); 193 it.remove(); 194 195 try { 197 it.remove(); 198 fail(); 199 } catch (IllegalStateException ex) {} 200 } 201 202 } 203 | Popular Tags |