1 19 20 package org.netbeans.lib.editor.util; 21 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import org.netbeans.junit.NbTestCase; 25 26 31 public class CompactMapTest extends NbTestCase { 32 33 public CompactMapTest(java.lang.String testName) { 34 super(testName); 35 } 36 37 @SuppressWarnings ("unchecked") 38 public void test() { 39 CompactMap testMap = new CompactMap(); 40 Object key1 = new NamedObject("key1"); 41 Object key2 = new NamedObject("key2"); 42 Object val1 = new NamedObject("val1"); 43 Object val2 = new NamedObject("val2"); 44 45 assertEquals(0, testMap.size()); 46 assertEquals(null, testMap.get(key1)); 47 testMap.put(key1, val1); 48 assertEquals(1, testMap.size()); 49 assertEquals(val1, testMap.get(key1)); 50 testMap.put(key2, val2); 51 assertEquals(2, testMap.size()); 52 assertEquals(val2, testMap.get(key2)); 53 assertEquals(val1, testMap.get(key1)); 54 55 Iterator it = testMap.entrySet().iterator(); 56 assertTrue(it.hasNext()); 57 Map.Entry entry = (Map.Entry )it.next(); 58 boolean wasKey2; 59 if (entry.getKey() == key2) { 60 assertEquals(key2, entry.getKey()); 61 assertEquals(val2, entry.getValue()); 62 wasKey2 = true; 63 } else { assertEquals(key1, entry.getKey()); 65 assertEquals(val1, entry.getValue()); 66 wasKey2 = false; 67 } 68 assertTrue(it.hasNext()); 69 entry = (Map.Entry )it.next(); 70 if (wasKey2) { 71 assertEquals(key1, entry.getKey()); 72 assertEquals(val1, entry.getValue()); 73 } else { 74 assertEquals(key2, entry.getKey()); 75 assertEquals(val2, entry.getValue()); 76 } 77 78 assertEquals(val1, testMap.put(key1, val2)); 79 assertEquals(val2, testMap.get(key2)); 80 assertEquals(val2, testMap.remove(key1)); 81 assertEquals(val2, testMap.get(key2)); 82 83 it = testMap.entrySet().iterator(); 84 assertTrue(it.hasNext()); 85 entry = (Map.Entry )it.next(); 86 assertEquals(key2, entry.getKey()); 87 assertEquals(val2, entry.getValue()); 88 89 Entry e1 = new Entry(key1); 90 e1.setValue(val1); 91 assertEquals(null, testMap.putEntry(e1)); 92 assertEquals(val1, testMap.get(key1)); 93 94 Entry e2 = new Entry(key2); 95 e2.setValue(val1); 96 CompactMap.MapEntry e = testMap.putEntry(e2); 97 assertEquals(key2, e.getKey()); 98 assertEquals(val2, e.getValue()); 99 100 CompactMap.MapEntry mapEntry = testMap.getFirstEntry(0); 101 assertEquals(mapEntry, e1); 102 103 Entry ehe = new Entry(mapEntry.getKey()); 105 ehe.setValue(mapEntry.getValue()); 106 assertEquals(mapEntry.hashCode(), ehe.hashCode()); 107 assertEquals(mapEntry, ehe); 108 109 testMap.clear(); 111 assertEquals(0, testMap.size()); 112 113 Object key3 = new NamedObject("key3"); 115 Object key4 = new NamedObject("key4"); 116 Object key5 = new NamedObject("key5"); 117 Object val3 = new NamedObject("val3"); 118 Object val4 = new NamedObject("val4"); 119 Object val5 = new NamedObject("val5"); 120 testMap.put(key1, val1); 121 testMap.put(key2, val2); 122 testMap.put(key3, val3); 123 testMap.put(key4, val4); 124 testMap.put(key5, val5); 125 assertEquals(5, testMap.size()); 126 assertEquals(val2, testMap.get(key2)); 127 assertEquals(val1, testMap.get(key1)); 128 assertEquals(val3, testMap.get(key3)); 129 assertEquals(val4, testMap.get(key4)); 130 assertEquals(val5, testMap.get(key5)); 131 132 } 133 134 private static final class Entry extends CompactMap.MapEntry { 135 136 private final Object key; 137 138 private Object value; 139 140 public Entry(Object key) { 141 this.key = key; 142 } 143 144 public Object setValue(Object value) { 145 Object oldValue = this.value; 146 this.value = value; 147 return oldValue; 148 } 149 150 public Object getValue() { 151 return value; 152 } 153 154 public Object getKey() { 155 return key; 156 } 157 158 protected int valueHashCode() { 159 return (value != null) ? value.hashCode() : 0; 160 } 161 162 protected boolean valueEquals(Object value2) { 163 return (value == value2 || (value != null && value.equals(value2))); 164 } 165 166 } 167 168 private static final class NamedObject extends Object { 169 170 private final String name; 171 172 public NamedObject(String name) { 173 this.name = name; 174 } 175 176 public String toString() { 177 return name; 178 } 179 180 } 181 182 } 183 | Popular Tags |