1 16 package org.apache.commons.collections.map; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.NoSuchElementException ; 25 import java.util.TreeMap ; 26 27 import org.apache.commons.collections.BulkTest; 28 import org.apache.commons.collections.MapIterator; 29 import org.apache.commons.collections.OrderedMap; 30 import org.apache.commons.collections.comparators.NullComparator; 31 import org.apache.commons.collections.iterators.AbstractTestOrderedMapIterator; 32 33 40 public abstract class AbstractTestOrderedMap extends AbstractTestIterableMap { 41 42 47 public AbstractTestOrderedMap(String testName) { 48 super(testName); 49 } 50 51 57 public Map makeConfirmedMap() { 58 return new TreeMap (new NullComparator()); 59 } 60 61 65 public Object [] getSampleKeys() { 66 List list = new ArrayList (Arrays.asList(super.getSampleKeys())); 67 Collections.sort(list, new NullComparator()); 68 return list.toArray(); 69 } 70 71 public void testFirstKey() { 73 resetEmpty(); 74 OrderedMap ordered = (OrderedMap) map; 75 try { 76 ordered.firstKey(); 77 fail(); 78 } catch (NoSuchElementException ex) {} 79 80 resetFull(); 81 ordered = (OrderedMap) map; 82 Object confirmedFirst = confirmed.keySet().iterator().next(); 83 assertEquals(confirmedFirst, ordered.firstKey()); 84 } 85 86 public void testLastKey() { 87 resetEmpty(); 88 OrderedMap ordered = (OrderedMap) map; 89 try { 90 ordered.lastKey(); 91 fail(); 92 } catch (NoSuchElementException ex) {} 93 94 resetFull(); 95 ordered = (OrderedMap) map; 96 Object confirmedLast = null; 97 for (Iterator it = confirmed.keySet().iterator(); it.hasNext();) { 98 confirmedLast = it.next(); 99 } 100 assertEquals(confirmedLast, ordered.lastKey()); 101 } 102 103 public void testNextKey() { 105 resetEmpty(); 106 OrderedMap ordered = (OrderedMap) map; 107 assertEquals(null, ordered.nextKey(getOtherKeys()[0])); 108 if (isAllowNullKey() == false) { 109 try { 110 assertEquals(null, ordered.nextKey(null)); } catch (NullPointerException ex) {} 112 } else { 113 assertEquals(null, ordered.nextKey(null)); 114 } 115 116 resetFull(); 117 ordered = (OrderedMap) map; 118 Iterator it = confirmed.keySet().iterator(); 119 Object confirmedLast = it.next(); 120 while (it.hasNext()) { 121 Object confirmedObject = it.next(); 122 assertEquals(confirmedObject, ordered.nextKey(confirmedLast)); 123 confirmedLast = confirmedObject; 124 } 125 assertEquals(null, ordered.nextKey(confirmedLast)); 126 127 if (isAllowNullKey() == false) { 128 try { 129 ordered.nextKey(null); 130 fail(); 131 } catch (NullPointerException ex) {} 132 } else { 133 assertEquals(null, ordered.nextKey(null)); 134 } 135 } 136 137 public void testPreviousKey() { 138 resetEmpty(); 139 OrderedMap ordered = (OrderedMap) map; 140 assertEquals(null, ordered.previousKey(getOtherKeys()[0])); 141 if (isAllowNullKey() == false) { 142 try { 143 assertEquals(null, ordered.previousKey(null)); } catch (NullPointerException ex) {} 145 } else { 146 assertEquals(null, ordered.previousKey(null)); 147 } 148 149 resetFull(); 150 ordered = (OrderedMap) map; 151 List list = new ArrayList (confirmed.keySet()); 152 Collections.reverse(list); 153 Iterator it = list.iterator(); 154 Object confirmedLast = it.next(); 155 while (it.hasNext()) { 156 Object confirmedObject = it.next(); 157 assertEquals(confirmedObject, ordered.previousKey(confirmedLast)); 158 confirmedLast = confirmedObject; 159 } 160 assertEquals(null, ordered.previousKey(confirmedLast)); 161 162 if (isAllowNullKey() == false) { 163 try { 164 ordered.previousKey(null); 165 fail(); 166 } catch (NullPointerException ex) {} 167 } else { 168 if (isAllowNullKey() == false) { 169 assertEquals(null, ordered.previousKey(null)); 170 } 171 } 172 } 173 174 public BulkTest bulkTestOrderedMapIterator() { 176 return new InnerTestOrderedMapIterator(); 177 } 178 179 public class InnerTestOrderedMapIterator extends AbstractTestOrderedMapIterator { 180 public InnerTestOrderedMapIterator() { 181 super("InnerTestOrderedMapIterator"); 182 } 183 184 public boolean supportsRemove() { 185 return AbstractTestOrderedMap.this.isRemoveSupported(); 186 } 187 188 public boolean isGetStructuralModify() { 189 return AbstractTestOrderedMap.this.isGetStructuralModify(); 190 } 191 192 public boolean supportsSetValue() { 193 return AbstractTestOrderedMap.this.isSetValueSupported(); 194 } 195 196 public MapIterator makeEmptyMapIterator() { 197 resetEmpty(); 198 return ((OrderedMap) AbstractTestOrderedMap.this.map).orderedMapIterator(); 199 } 200 201 public MapIterator makeFullMapIterator() { 202 resetFull(); 203 return ((OrderedMap) AbstractTestOrderedMap.this.map).orderedMapIterator(); 204 } 205 206 public Map getMap() { 207 return AbstractTestOrderedMap.this.map; 209 } 210 211 public Map getConfirmedMap() { 212 return AbstractTestOrderedMap.this.confirmed; 214 } 215 216 public void verify() { 217 super.verify(); 218 AbstractTestOrderedMap.this.verify(); 219 } 220 } 221 222 } 223 | Popular Tags |