1 16 package org.apache.commons.collections.map; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.SortedMap ; 24 import java.util.TreeMap ; 25 26 import org.apache.commons.collections.BulkTest; 27 28 35 public abstract class AbstractTestSortedMap extends AbstractTestMap { 36 37 42 public AbstractTestSortedMap(String testName) { 43 super(testName); 44 } 45 46 52 public boolean isAllowNullKey() { 53 return false; 54 } 55 56 61 public Map makeConfirmedMap() { 62 return new TreeMap (); 63 } 64 65 public void testComparator() { 67 SortedMap sm = (SortedMap ) makeFullMap(); 68 } 70 71 public void testFirstKey() { 72 SortedMap sm = (SortedMap ) makeFullMap(); 73 assertSame(sm.keySet().iterator().next(), sm.firstKey()); 74 } 75 76 public void testLastKey() { 77 SortedMap sm = (SortedMap ) makeFullMap(); 78 Object obj = null; 79 for (Iterator it = sm.keySet().iterator(); it.hasNext();) { 80 obj = (Object ) it.next(); 81 } 82 assertSame(obj, sm.lastKey()); 83 } 84 85 public BulkTest bulkTestHeadMap() { 87 return new TestHeadMap(this); 88 } 89 90 public BulkTest bulkTestTailMap() { 91 return new TestTailMap(this); 92 } 93 94 public BulkTest bulkTestSubMap() { 95 return new TestSubMap(this); 96 } 97 98 public static abstract class TestViewMap extends AbstractTestSortedMap { 99 protected final AbstractTestMap main; 100 protected final List subSortedKeys = new ArrayList (); 101 protected final List subSortedValues = new ArrayList (); 102 protected final List subSortedNewValues = new ArrayList (); 103 104 public TestViewMap(String name, AbstractTestMap main) { 105 super(name); 106 this.main = main; 107 } 108 public void resetEmpty() { 109 main.resetEmpty(); 111 super.resetEmpty(); 112 } 113 public void resetFull() { 114 main.resetFull(); 116 super.resetFull(); 117 } 118 public void verify() { 119 super.verify(); 121 main.verify(); 122 } 123 public BulkTest bulkTestHeadMap() { 124 return null; } 126 public BulkTest bulkTestTailMap() { 127 return null; } 129 public BulkTest bulkTestSubMap() { 130 return null; } 132 133 public Object [] getSampleKeys() { 134 return subSortedKeys.toArray(); 135 } 136 public Object [] getSampleValues() { 137 return subSortedValues.toArray(); 138 } 139 public Object [] getNewSampleValues() { 140 return subSortedNewValues.toArray(); 141 } 142 143 public boolean isAllowNullKey() { 144 return main.isAllowNullKey(); 145 } 146 public boolean isAllowNullValue() { 147 return main.isAllowNullValue(); 148 } 149 public boolean isPutAddSupported() { 150 return main.isPutAddSupported(); 151 } 152 public boolean isPutChangeSupported() { 153 return main.isPutChangeSupported(); 154 } 155 public boolean isRemoveSupported() { 156 return main.isRemoveSupported(); 157 } 158 public boolean isTestSerialization() { 159 return false; 160 } 161 } 178 179 public static class TestHeadMap extends TestViewMap { 180 static final int SUBSIZE = 6; 181 final Object toKey; 182 183 public TestHeadMap(AbstractTestMap main) { 184 super("SortedMap.HeadMap", main); 185 SortedMap sm = (SortedMap ) main.makeFullMap(); 186 for (Iterator it = sm.entrySet().iterator(); it.hasNext();) { 187 Map.Entry entry = (Map.Entry ) it.next(); 188 this.subSortedKeys.add(entry.getKey()); 189 this.subSortedValues.add(entry.getValue()); 190 } 191 this.toKey = this.subSortedKeys.get(SUBSIZE); 192 this.subSortedKeys.subList(SUBSIZE, this.subSortedKeys.size()).clear(); 193 this.subSortedValues.subList(SUBSIZE, this.subSortedValues.size()).clear(); 194 this.subSortedNewValues.addAll(Arrays.asList(main.getNewSampleValues()).subList(0, SUBSIZE)); 195 } 196 public Map makeEmptyMap() { 197 return ((SortedMap ) main.makeEmptyMap()).headMap(toKey); 199 } 200 public Map makeFullMap() { 201 return ((SortedMap ) main.makeFullMap()).headMap(toKey); 202 } 203 public void testHeadMapOutOfRange() { 204 if (isPutAddSupported() == false) return; 205 resetEmpty(); 206 try { 207 ((SortedMap ) map).put(toKey, subSortedValues.get(0)); 208 fail(); 209 } catch (IllegalArgumentException ex) {} 210 verify(); 211 } 212 public String getCompatibilityVersion() { 213 return main.getCompatibilityVersion() + ".HeadMapView"; 214 } 215 216 } 227 228 public static class TestTailMap extends TestViewMap { 229 static final int SUBSIZE = 6; 230 final Object fromKey; 231 final Object invalidKey; 232 233 public TestTailMap(AbstractTestMap main) { 234 super("SortedMap.TailMap", main); 235 SortedMap sm = (SortedMap ) main.makeFullMap(); 236 for (Iterator it = sm.entrySet().iterator(); it.hasNext();) { 237 Map.Entry entry = (Map.Entry ) it.next(); 238 this.subSortedKeys.add(entry.getKey()); 239 this.subSortedValues.add(entry.getValue()); 240 } 241 this.fromKey = this.subSortedKeys.get(this.subSortedKeys.size() - SUBSIZE); 242 this.invalidKey = this.subSortedKeys.get(this.subSortedKeys.size() - SUBSIZE - 1); 243 this.subSortedKeys.subList(0, this.subSortedKeys.size() - SUBSIZE).clear(); 244 this.subSortedValues.subList(0, this.subSortedValues.size() - SUBSIZE).clear(); 245 this.subSortedNewValues.addAll(Arrays.asList(main.getNewSampleValues()).subList(0, SUBSIZE)); 246 } 247 public Map makeEmptyMap() { 248 return ((SortedMap ) main.makeEmptyMap()).tailMap(fromKey); 250 } 251 public Map makeFullMap() { 252 return ((SortedMap ) main.makeFullMap()).tailMap(fromKey); 253 } 254 public void testTailMapOutOfRange() { 255 if (isPutAddSupported() == false) return; 256 resetEmpty(); 257 try { 258 ((SortedMap ) map).put(invalidKey, subSortedValues.get(0)); 259 fail(); 260 } catch (IllegalArgumentException ex) {} 261 verify(); 262 } 263 public String getCompatibilityVersion() { 264 return main.getCompatibilityVersion() + ".TailMapView"; 265 } 266 267 } 278 279 public static class TestSubMap extends TestViewMap { 280 static final int SUBSIZE = 3; 281 final Object fromKey; 282 final Object toKey; 283 284 public TestSubMap(AbstractTestMap main) { 285 super("SortedMap.SubMap", main); 286 SortedMap sm = (SortedMap ) main.makeFullMap(); 287 for (Iterator it = sm.entrySet().iterator(); it.hasNext();) { 288 Map.Entry entry = (Map.Entry ) it.next(); 289 this.subSortedKeys.add(entry.getKey()); 290 this.subSortedValues.add(entry.getValue()); 291 } 292 this.fromKey = this.subSortedKeys.get(SUBSIZE); 293 this.toKey = this.subSortedKeys.get(this.subSortedKeys.size() - SUBSIZE); 294 295 this.subSortedKeys.subList(0, SUBSIZE).clear(); 296 this.subSortedKeys.subList(this.subSortedKeys.size() - SUBSIZE, this.subSortedKeys.size()).clear(); 297 298 this.subSortedValues.subList(0, SUBSIZE).clear(); 299 this.subSortedValues.subList(this.subSortedValues.size() - SUBSIZE, this.subSortedValues.size()).clear(); 300 301 this.subSortedNewValues.addAll(Arrays.asList(main.getNewSampleValues()).subList( 302 SUBSIZE, this.main.getNewSampleValues().length - SUBSIZE)); 303 } 304 305 public Map makeEmptyMap() { 306 return ((SortedMap ) main.makeEmptyMap()).subMap(fromKey, toKey); 308 } 309 public Map makeFullMap() { 310 return ((SortedMap ) main.makeFullMap()).subMap(fromKey, toKey); 311 } 312 public void testSubMapOutOfRange() { 313 if (isPutAddSupported() == false) return; 314 resetEmpty(); 315 try { 316 ((SortedMap ) map).put(toKey, subSortedValues.get(0)); 317 fail(); 318 } catch (IllegalArgumentException ex) {} 319 verify(); 320 } 321 public String getCompatibilityVersion() { 322 return main.getCompatibilityVersion() + ".SubMapView"; 323 } 324 325 } 336 337 } 338 | Popular Tags |