1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.ArrayList ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.NoSuchElementException ; 24 import java.util.Set ; 25 26 import org.apache.commons.collections.OrderedMapIterator; 27 28 41 public abstract class AbstractTestOrderedMapIterator extends AbstractTestMapIterator { 42 43 48 public AbstractTestOrderedMapIterator(String testName) { 49 super(testName); 50 } 51 52 public final OrderedMapIterator makeEmptyOrderedMapIterator() { 54 return (OrderedMapIterator) makeEmptyMapIterator(); 55 } 56 57 public final OrderedMapIterator makeFullOrderedMapIterator() { 58 return (OrderedMapIterator) makeFullMapIterator(); 59 } 60 61 65 public void testEmptyMapIterator() { 66 if (supportsEmptyIterator() == false) { 67 return; 68 } 69 70 super.testEmptyMapIterator(); 71 72 OrderedMapIterator it = makeEmptyOrderedMapIterator(); 73 Map map = getMap(); 74 assertEquals(false, it.hasPrevious()); 75 try { 76 it.previous(); 77 fail(); 78 } catch (NoSuchElementException ex) {} 79 } 80 81 85 public void testFullMapIterator() { 86 if (supportsFullIterator() == false) { 87 return; 88 } 89 90 super.testFullMapIterator(); 91 92 OrderedMapIterator it = makeFullOrderedMapIterator(); 93 Map map = getMap(); 94 95 assertEquals(true, it.hasNext()); 96 assertEquals(false, it.hasPrevious()); 97 Set set = new HashSet (); 98 while (it.hasNext()) { 99 Object key = it.next(); 101 assertSame("it.next() should equals getKey()", key, it.getKey()); 102 assertTrue("Key must be in map", map.containsKey(key)); 103 assertTrue("Key must be unique", set.add(key)); 104 105 Object value = it.getValue(); 107 if (isGetStructuralModify() == false) { 108 assertSame("Value must be mapped to key", map.get(key), value); 109 } 110 assertTrue("Value must be in map", map.containsValue(value)); 111 112 assertEquals(true, it.hasPrevious()); 113 114 verify(); 115 } 116 while (it.hasPrevious()) { 117 Object key = it.previous(); 119 assertSame("it.previous() should equals getKey()", key, it.getKey()); 120 assertTrue("Key must be in map", map.containsKey(key)); 121 assertTrue("Key must be unique", set.remove(key)); 122 123 Object value = it.getValue(); 125 if (isGetStructuralModify() == false) { 126 assertSame("Value must be mapped to key", map.get(key), value); 127 } 128 assertTrue("Value must be in map", map.containsValue(value)); 129 130 assertEquals(true, it.hasNext()); 131 132 verify(); 133 } 134 } 135 136 140 public void testMapIteratorOrder() { 141 if (supportsFullIterator() == false) { 142 return; 143 } 144 145 OrderedMapIterator it = makeFullOrderedMapIterator(); 146 Map map = getMap(); 147 148 assertEquals("keySet() not consistent", new ArrayList (map.keySet()), new ArrayList (map.keySet())); 149 150 Iterator it2 = map.keySet().iterator(); 151 assertEquals(true, it.hasNext()); 152 assertEquals(true, it2.hasNext()); 153 List list = new ArrayList (); 154 while (it.hasNext()) { 155 Object key = it.next(); 156 assertEquals(it2.next(), key); 157 list.add(key); 158 } 159 assertEquals(map.size(), list.size()); 160 while (it.hasPrevious()) { 161 Object key = it.previous(); 162 assertEquals(list.get(list.size() - 1), key); 163 list.remove(list.size() - 1); 164 } 165 assertEquals(0, list.size()); 166 } 167 168 } 169 | Popular Tags |