1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.NoSuchElementException ; 22 import java.util.Set ; 23 24 import org.apache.commons.collections.MapIterator; 25 26 39 public abstract class AbstractTestMapIterator extends AbstractTestIterator { 40 41 46 public AbstractTestMapIterator(String testName) { 47 super(testName); 48 } 49 50 56 public abstract MapIterator makeEmptyMapIterator(); 57 58 63 public abstract MapIterator makeFullMapIterator(); 64 65 71 public abstract Map getMap(); 72 73 79 public abstract Map getConfirmedMap(); 80 81 86 public final Iterator makeEmptyIterator() { 87 return makeEmptyMapIterator(); 88 } 89 90 95 public final Iterator makeFullIterator() { 96 return makeFullMapIterator(); 97 } 98 99 105 public boolean supportsSetValue() { 106 return true; 107 } 108 109 115 public boolean isGetStructuralModify() { 116 return false; 117 } 118 119 123 public Object [] addSetValues() { 124 return new Object [] {"A", "B"}; 125 } 126 127 131 public void testEmptyMapIterator() { 132 if (supportsEmptyIterator() == false) { 133 return; 134 } 135 136 MapIterator it = makeEmptyMapIterator(); 137 Map map = getMap(); 138 assertEquals(false, it.hasNext()); 139 140 try { 142 it.next(); 143 fail(); 144 } catch (NoSuchElementException ex) {} 145 146 try { 148 it.getKey(); 149 fail(); 150 } catch (IllegalStateException ex) {} 151 152 try { 154 it.getValue(); 155 fail(); 156 } catch (IllegalStateException ex) {} 157 158 if (supportsSetValue() == false) { 159 try { 161 it.setValue(addSetValues()[0]); 162 fail(); 163 } catch (UnsupportedOperationException ex) { 164 } catch (IllegalStateException ex) {} 165 } else { 166 try { 168 it.setValue(addSetValues()[0]); 169 fail(); 170 } catch (IllegalStateException ex) {} 171 } 172 } 173 174 178 public void testFullMapIterator() { 179 if (supportsFullIterator() == false) { 180 return; 181 } 182 183 MapIterator it = makeFullMapIterator(); 184 Map map = getMap(); 185 assertEquals(true, it.hasNext()); 186 187 assertEquals(true, it.hasNext()); 188 Set set = new HashSet (); 189 while (it.hasNext()) { 190 Object key = it.next(); 192 assertSame("it.next() should equals getKey()", key, it.getKey()); 193 assertTrue("Key must be in map", map.containsKey(key)); 194 assertTrue("Key must be unique", set.add(key)); 195 196 Object value = it.getValue(); 198 if (isGetStructuralModify() == false) { 199 assertSame("Value must be mapped to key", map.get(key), value); 200 } 201 assertTrue("Value must be in map", map.containsValue(value)); 202 203 verify(); 204 } 205 } 206 207 public void testMapIteratorSet() { 209 if (supportsFullIterator() == false) { 210 return; 211 } 212 213 Object newValue = addSetValues()[0]; 214 Object newValue2 = (addSetValues().length == 1 ? addSetValues()[0] : addSetValues()[1]); 215 MapIterator it = makeFullMapIterator(); 216 Map map = getMap(); 217 Map confirmed = getConfirmedMap(); 218 assertEquals(true, it.hasNext()); 219 Object key = it.next(); 220 Object value = it.getValue(); 221 222 if (supportsSetValue() == false) { 223 try { 224 it.setValue(newValue); 225 fail(); 226 } catch (UnsupportedOperationException ex) {} 227 return; 228 } 229 Object old = it.setValue(newValue); 230 confirmed.put(key, newValue); 231 assertSame("Key must not change after setValue", key, it.getKey()); 232 assertSame("Value must be changed after setValue", newValue, it.getValue()); 233 assertSame("setValue must return old value", value, old); 234 assertEquals("Map must contain key", true, map.containsKey(key)); 235 assertEquals("Map must not contain old value", 237 confirmed.containsValue(old), map.containsValue(old)); 238 assertEquals("Map must contain new value", true, map.containsValue(newValue)); 239 verify(); 240 241 it.setValue(newValue); confirmed.put(key, newValue); 243 assertSame("Key must not change after setValue", key, it.getKey()); 244 assertSame("Value must be changed after setValue", newValue, it.getValue()); 245 verify(); 246 247 it.setValue(newValue2); confirmed.put(key, newValue2); 249 assertSame("Key must not change after setValue", key, it.getKey()); 250 assertSame("Value must be changed after setValue", newValue2, it.getValue()); 251 verify(); 252 } 253 254 public void testRemove() { MapIterator it = makeFullMapIterator(); 257 Map map = getMap(); 258 Map confirmed = getConfirmedMap(); 259 assertEquals(true, it.hasNext()); 260 Object key = it.next(); 261 262 if (supportsRemove() == false) { 263 try { 264 it.remove(); 265 fail(); 266 } catch (UnsupportedOperationException ex) { 267 } 268 return; 269 } 270 271 it.remove(); 272 confirmed.remove(key); 273 assertEquals(false, map.containsKey(key)); 274 verify(); 275 276 try { 277 it.remove(); } catch (IllegalStateException ex) { 279 } 280 verify(); 281 } 282 283 public void testMapIteratorSetRemoveSet() { 285 if (supportsSetValue() == false || supportsRemove() == false) { 286 return; 287 } 288 Object newValue = addSetValues()[0]; 289 MapIterator it = makeFullMapIterator(); 290 Map map = getMap(); 291 Map confirmed = getConfirmedMap(); 292 293 assertEquals(true, it.hasNext()); 294 Object key = it.next(); 295 296 it.setValue(newValue); 297 it.remove(); 298 confirmed.remove(key); 299 verify(); 300 301 try { 302 it.setValue(newValue); 303 fail(); 304 } catch (IllegalStateException ex) {} 305 verify(); 306 } 307 308 public void testMapIteratorRemoveGetKey() { 310 if (supportsRemove() == false) { 311 return; 312 } 313 MapIterator it = makeFullMapIterator(); 314 Map map = getMap(); 315 Map confirmed = getConfirmedMap(); 316 317 assertEquals(true, it.hasNext()); 318 Object key = it.next(); 319 320 it.remove(); 321 confirmed.remove(key); 322 verify(); 323 324 try { 325 it.getKey(); 326 fail(); 327 } catch (IllegalStateException ex) {} 328 verify(); 329 } 330 331 public void testMapIteratorRemoveGetValue() { 333 if (supportsRemove() == false) { 334 return; 335 } 336 MapIterator it = makeFullMapIterator(); 337 Map map = getMap(); 338 Map confirmed = getConfirmedMap(); 339 340 assertEquals(true, it.hasNext()); 341 Object key = it.next(); 342 343 it.remove(); 344 confirmed.remove(key); 345 verify(); 346 347 try { 348 it.getValue(); 349 fail(); 350 } catch (IllegalStateException ex) {} 351 verify(); 352 } 353 354 } 355 | Popular Tags |