1 16 package org.apache.commons.collections.map; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import junit.framework.Test; 24 25 import org.apache.commons.collections.BulkTest; 26 import org.apache.commons.collections.MapIterator; 27 import org.apache.commons.collections.list.AbstractTestList; 28 29 39 public class TestListOrderedMap extends AbstractTestOrderedMap { 40 41 public TestListOrderedMap(String testName) { 42 super(testName); 43 } 44 45 public static Test suite() { 46 return BulkTest.makeSuite(TestListOrderedMap.class); 47 } 48 49 public static void main(String args[]) { 50 String [] testCaseName = { TestListOrderedMap.class.getName()}; 51 junit.textui.TestRunner.main(testCaseName); 52 } 53 54 public Map makeEmptyMap() { 55 return ListOrderedMap.decorate(new HashMap ()); 56 } 57 58 public void testGetByIndex() { 60 resetEmpty(); 61 ListOrderedMap lom = (ListOrderedMap) map; 62 try { 63 lom.get(0); 64 } catch (IndexOutOfBoundsException ex) {} 65 try { 66 lom.get(-1); 67 } catch (IndexOutOfBoundsException ex) {} 68 69 resetFull(); 70 lom = (ListOrderedMap) map; 71 try { 72 lom.get(-1); 73 } catch (IndexOutOfBoundsException ex) {} 74 try { 75 lom.get(lom.size()); 76 } catch (IndexOutOfBoundsException ex) {} 77 78 int i = 0; 79 for (MapIterator it = lom.mapIterator(); it.hasNext(); i++) { 80 assertSame(it.next(), lom.get(i)); 81 } 82 } 83 84 public void testGetValueByIndex() { 85 resetEmpty(); 86 ListOrderedMap lom = (ListOrderedMap) map; 87 try { 88 lom.getValue(0); 89 } catch (IndexOutOfBoundsException ex) {} 90 try { 91 lom.getValue(-1); 92 } catch (IndexOutOfBoundsException ex) {} 93 94 resetFull(); 95 lom = (ListOrderedMap) map; 96 try { 97 lom.getValue(-1); 98 } catch (IndexOutOfBoundsException ex) {} 99 try { 100 lom.getValue(lom.size()); 101 } catch (IndexOutOfBoundsException ex) {} 102 103 int i = 0; 104 for (MapIterator it = lom.mapIterator(); it.hasNext(); i++) { 105 it.next(); 106 assertSame(it.getValue(), lom.getValue(i)); 107 } 108 } 109 110 public void testIndexOf() { 111 resetEmpty(); 112 ListOrderedMap lom = (ListOrderedMap) map; 113 assertEquals(-1, lom.indexOf(getOtherKeys())); 114 115 resetFull(); 116 lom = (ListOrderedMap) map; 117 List list = new ArrayList (); 118 for (MapIterator it = lom.mapIterator(); it.hasNext();) { 119 list.add(it.next()); 120 } 121 for (int i = 0; i < list.size(); i++) { 122 assertEquals(i, lom.indexOf(list.get(i))); 123 } 124 } 125 126 public void testRemoveByIndex() { 127 resetEmpty(); 128 ListOrderedMap lom = (ListOrderedMap) map; 129 try { 130 lom.remove(0); 131 } catch (IndexOutOfBoundsException ex) {} 132 try { 133 lom.remove(-1); 134 } catch (IndexOutOfBoundsException ex) {} 135 136 resetFull(); 137 lom = (ListOrderedMap) map; 138 try { 139 lom.remove(-1); 140 } catch (IndexOutOfBoundsException ex) {} 141 try { 142 lom.remove(lom.size()); 143 } catch (IndexOutOfBoundsException ex) {} 144 145 List list = new ArrayList (); 146 for (MapIterator it = lom.mapIterator(); it.hasNext();) { 147 list.add(it.next()); 148 } 149 for (int i = 0; i < list.size(); i++) { 150 Object key = list.get(i); 151 Object value = lom.get(key); 152 assertEquals(value, lom.remove(i)); 153 list.remove(i); 154 assertEquals(false, lom.containsKey(key)); 155 } 156 } 157 158 public BulkTest bulkTestListView() { 159 return new TestListView(); 160 } 161 162 public class TestListView extends AbstractTestList { 163 164 TestListView() { 165 super("TestListView"); 166 } 167 168 public List makeEmptyList() { 169 return ((ListOrderedMap) TestListOrderedMap.this.makeEmptyMap()).asList(); 170 } 171 172 public List makeFullList() { 173 return ((ListOrderedMap) TestListOrderedMap.this.makeFullMap()).asList(); 174 } 175 176 public Object [] getFullElements() { 177 return TestListOrderedMap.this.getSampleKeys(); 178 } 179 public boolean isAddSupported() { 180 return false; 181 } 182 public boolean isRemoveSupported() { 183 return false; 184 } 185 public boolean isSetSupported() { 186 return false; 187 } 188 public boolean isNullSupported() { 189 return TestListOrderedMap.this.isAllowNullKey(); 190 } 191 public boolean isTestSerialization() { 192 return false; 193 } 194 } 195 196 public String getCompatibilityVersion() { 197 return "3.1"; 198 } 199 200 } 211 | Popular Tags |