1 16 package org.apache.commons.collections.map; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import junit.framework.Test; 24 import junit.textui.TestRunner; 25 26 import org.apache.commons.collections.BulkTest; 27 import org.apache.commons.collections.MapIterator; 28 import org.apache.commons.collections.OrderedMap; 29 import org.apache.commons.collections.ResettableIterator; 30 import org.apache.commons.collections.list.AbstractTestList; 31 32 39 public class TestLinkedMap extends AbstractTestOrderedMap { 40 41 public TestLinkedMap(String testName) { 42 super(testName); 43 } 44 45 public static void main(String [] args) { 46 TestRunner.run(suite()); 47 } 48 49 public static Test suite() { 50 return BulkTest.makeSuite(TestLinkedMap.class); 51 } 52 53 public Map makeEmptyMap() { 54 return new LinkedMap(); 55 } 56 57 public String getCompatibilityVersion() { 58 return "3"; 59 } 60 61 public void testReset() { 63 resetEmpty(); 64 OrderedMap ordered = (OrderedMap) map; 65 ((ResettableIterator) ordered.mapIterator()).reset(); 66 67 resetFull(); 68 ordered = (OrderedMap) map; 69 List list = new ArrayList (ordered.keySet()); 70 ResettableIterator it = (ResettableIterator) ordered.mapIterator(); 71 assertSame(list.get(0), it.next()); 72 assertSame(list.get(1), it.next()); 73 it.reset(); 74 assertSame(list.get(0), it.next()); 75 } 76 77 public void testInsertionOrder() { 79 if (isPutAddSupported() == false || isPutChangeSupported() == false) return; 80 Object [] keys = getSampleKeys(); 81 Object [] values = getSampleValues(); 82 Iterator it = null; 83 84 resetEmpty(); 85 map.put(keys[0], values[0]); 86 map.put(keys[1], values[1]); 87 it = map.keySet().iterator(); 88 assertSame(keys[0], it.next()); 89 assertSame(keys[1], it.next()); 90 it = map.values().iterator(); 91 assertSame(values[0], it.next()); 92 assertSame(values[1], it.next()); 93 94 map.put(keys[1], values[1]); 96 it = map.keySet().iterator(); 97 assertSame(keys[0], it.next()); 98 assertSame(keys[1], it.next()); 99 it = map.values().iterator(); 100 assertSame(values[0], it.next()); 101 assertSame(values[1], it.next()); 102 103 map.put(keys[1], values[2]); 105 it = map.keySet().iterator(); 106 assertSame(keys[0], it.next()); 107 assertSame(keys[1], it.next()); 108 it = map.values().iterator(); 109 assertSame(values[0], it.next()); 110 assertSame(values[2], it.next()); 111 112 map.put(keys[0], values[3]); 114 it = map.keySet().iterator(); 115 assertSame(keys[0], it.next()); 116 assertSame(keys[1], it.next()); 117 it = map.values().iterator(); 118 assertSame(values[3], it.next()); 119 assertSame(values[2], it.next()); 120 } 121 122 public void testGetByIndex() { 124 resetEmpty(); 125 LinkedMap lm = (LinkedMap) map; 126 try { 127 lm.get(0); 128 } catch (IndexOutOfBoundsException ex) {} 129 try { 130 lm.get(-1); 131 } catch (IndexOutOfBoundsException ex) {} 132 133 resetFull(); 134 lm = (LinkedMap) map; 135 try { 136 lm.get(-1); 137 } catch (IndexOutOfBoundsException ex) {} 138 try { 139 lm.get(lm.size()); 140 } catch (IndexOutOfBoundsException ex) {} 141 142 int i = 0; 143 for (MapIterator it = lm.mapIterator(); it.hasNext(); i++) { 144 assertSame(it.next(), lm.get(i)); 145 } 146 } 147 148 public void testGetValueByIndex() { 149 resetEmpty(); 150 LinkedMap lm = (LinkedMap) map; 151 try { 152 lm.getValue(0); 153 } catch (IndexOutOfBoundsException ex) {} 154 try { 155 lm.getValue(-1); 156 } catch (IndexOutOfBoundsException ex) {} 157 158 resetFull(); 159 lm = (LinkedMap) map; 160 try { 161 lm.getValue(-1); 162 } catch (IndexOutOfBoundsException ex) {} 163 try { 164 lm.getValue(lm.size()); 165 } catch (IndexOutOfBoundsException ex) {} 166 167 int i = 0; 168 for (MapIterator it = lm.mapIterator(); it.hasNext(); i++) { 169 it.next(); 170 assertSame(it.getValue(), lm.getValue(i)); 171 } 172 } 173 174 public void testIndexOf() { 175 resetEmpty(); 176 LinkedMap lm = (LinkedMap) map; 177 assertEquals(-1, lm.indexOf(getOtherKeys())); 178 179 resetFull(); 180 lm = (LinkedMap) map; 181 List list = new ArrayList (); 182 for (MapIterator it = lm.mapIterator(); it.hasNext();) { 183 list.add(it.next()); 184 } 185 for (int i = 0; i < list.size(); i++) { 186 assertEquals(i, lm.indexOf(list.get(i))); 187 } 188 } 189 190 public void testRemoveByIndex() { 191 resetEmpty(); 192 LinkedMap lm = (LinkedMap) map; 193 try { 194 lm.remove(0); 195 } catch (IndexOutOfBoundsException ex) {} 196 try { 197 lm.remove(-1); 198 } catch (IndexOutOfBoundsException ex) {} 199 200 resetFull(); 201 lm = (LinkedMap) map; 202 try { 203 lm.remove(-1); 204 } catch (IndexOutOfBoundsException ex) {} 205 try { 206 lm.remove(lm.size()); 207 } catch (IndexOutOfBoundsException ex) {} 208 209 List list = new ArrayList (); 210 for (MapIterator it = lm.mapIterator(); it.hasNext();) { 211 list.add(it.next()); 212 } 213 for (int i = 0; i < list.size(); i++) { 214 Object key = list.get(i); 215 Object value = lm.get(key); 216 assertEquals(value, lm.remove(i)); 217 list.remove(i); 218 assertEquals(false, lm.containsKey(key)); 219 } 220 } 221 222 public BulkTest bulkTestListView() { 223 return new TestListView(); 224 } 225 226 public class TestListView extends AbstractTestList { 227 228 TestListView() { 229 super("TestListView"); 230 } 231 232 public List makeEmptyList() { 233 return ((LinkedMap) TestLinkedMap.this.makeEmptyMap()).asList(); 234 } 235 236 public List makeFullList() { 237 return ((LinkedMap) TestLinkedMap.this.makeFullMap()).asList(); 238 } 239 240 public Object [] getFullElements() { 241 return TestLinkedMap.this.getSampleKeys(); 242 } 243 public boolean isAddSupported() { 244 return false; 245 } 246 public boolean isRemoveSupported() { 247 return false; 248 } 249 public boolean isSetSupported() { 250 return false; 251 } 252 public boolean isNullSupported() { 253 return TestLinkedMap.this.isAllowNullKey(); 254 } 255 public boolean isTestSerialization() { 256 return false; 257 } 258 } 259 260 public void testClone() { 261 LinkedMap map = new LinkedMap(10); 262 map.put("1", "1"); 263 Map cloned = (Map ) map.clone(); 264 assertEquals(map.size(), cloned.size()); 265 assertSame(map.get("1"), cloned.get("1")); 266 } 267 268 } 275 | Popular Tags |