1 /* TestRegistry.java */ 2 3 package org.quilt.reg; 4 5 import junit.framework.*; 6 7 public class TestRegistry extends TestCase { 8 9 private Registry reg = null; 10 11 public TestRegistry(String name) { 12 super(name); 13 } 14 15 String salutation[] = {"hello", "there"}; 16 17 String key1[] = {"Quilt", "ARG0"}; 18 String key2[] = {"Elvis", "lives"}; 19 String key3[] = {"One plus ", "one"}; 20 String key4[] = {"Elvis", "wombat"}; 21 String key5[] = {"Elvis", "heart-throb"}; 22 String obj1 = "froggy boy"; 23 String obj2 = "Topeka, Kansas"; 24 Integer obj3 = new Integer(3); 25 Boolean obj4 = new Boolean(true); 26 Float obj5 = new Float ((float)5.47); 27 28 public void setUp () { 29 reg = new Registry(); 30 reg.clear(); 31 } 32 33 public void testEmpty () { 34 assertEquals ("Empty registry has items in it", 0, reg.size()); 35 assertNull ("Retrieved item from empty registry", 36 reg.get(salutation) ); 37 assertTrue ("Empty registry is not empty", reg.isEmpty() ); 38 } 39 40 public void testSimple () { 41 assertNull ("Put with no previous value - ", reg.put(key1, obj1)); 42 assertEquals (1, reg.size()); 43 assertTrue (reg.containsKey(key1)); 44 assertNull ("Put with no previous value - ", reg.put(key2, obj2)); 45 assertEquals (2, reg.size()); 46 assertTrue (reg.containsKey(key2)); 47 assertNull ("Put with no previous value - ", reg.put(key3, obj3)); 48 assertEquals (3, reg.size()); 49 assertTrue (reg.containsKey(key3)); 50 51 // make sure all three are present and have correct values 52 assertEquals ("'Quilt' get returns wrong value", obj1, reg.get(key1)); 53 assertEquals ("'Elvis' get returns wrong value", obj2, reg.get(key2)); 54 assertEquals ("'One plus' get returns wrong value", 55 obj3, reg.get(key3)); 56 57 // overwrite, check return value is previous value 58 assertEquals ("Put did not return previous value", 59 obj1, reg.put(key1, obj2)); 60 assertEquals (3, reg.size()); 61 assertEquals ("Put did not return previous value", 62 obj2, reg.put(key2, obj3)); 63 assertEquals (3, reg.size()); 64 assertEquals ("Put did not return previous value", 65 obj3, reg.put(key3, obj1)); 66 assertEquals (3, reg.size()); 67 68 // check value returned on deletion is stored value 69 assertEquals ("remove did not return value stored", 70 obj2, reg.remove (key1)); 71 assertTrue ("Item deleted but still present", !reg.containsKey(key1)); 72 assertEquals (2, reg.size()); 73 74 assertEquals ("remove did not return value stored", 75 obj3, reg.remove (key2)); 76 assertTrue ("Item deleted but still present", !reg.containsKey(key2)); 77 assertEquals (1, reg.size()); 78 79 assertEquals ("remove did not return value stored", 80 obj1, reg.remove (key3)); 81 assertTrue ("Item deleted but still present", !reg.containsKey(key3)); 82 assertEquals (0, reg.size()); 83 assertTrue ("Empty registry is not empty", reg.isEmpty() ); 84 } 85 86 public void testBoolean () { 87 // put some random stuff in the registry ... 88 assertNull ("Put with no previous value - ", reg.put(key1, obj1)); 89 assertEquals (1, reg.size()); 90 assertTrue (reg.containsKey(key1)); 91 // first Elvis 92 assertNull ("Put with no previous value - ", reg.put(key2, obj2)); 93 assertEquals (2, reg.size()); 94 assertTrue (reg.containsKey(key2)); 95 assertNull ("Put with no previous value - ", reg.put(key3, obj3)); 96 assertEquals (3, reg.size()); 97 assertTrue (reg.containsKey(key3)); 98 99 Boolean t = new Boolean(true); 100 Boolean f = new Boolean(false); 101 String tKey[] = {"the", "truth"}; 102 String fKey[] = {"a", "falsehood"}; 103 assertNull ("Put with no previous value - ", reg.put(tKey, t)); 104 assertEquals (4, reg.size()); 105 assertTrue ("No truth in the database", reg.containsKey(tKey)); 106 assertNull ("Put with no previous value - ", reg.put(fKey, f)); 107 assertEquals (5, reg.size()); 108 assertTrue ("No lies in the database", reg.containsKey(fKey)); 109 110 assertEquals ("Can't get truth out of the registry", t, reg.get(tKey)); 111 assertEquals ("Can't get lies out of the registry" , f, reg.get(fKey)); 112 113 assertTrue ( t.booleanValue() ); 114 assertTrue ( ((Boolean)reg.get(tKey)).booleanValue() ); 115 assertTrue (!f.booleanValue() ); 116 assertTrue (!((Boolean)reg.get(fKey)).booleanValue() ); 117 } 118 public void testPartialOverlap() { 119 assertNull ("Put with no previous value - ", reg.put(key1, obj1)); 120 assertEquals (1, reg.size()); 121 assertTrue (reg.containsKey(key1)); 122 // Elvis/lives --> Topeka, Kansas 123 assertNull ("Put with no previous value - ", reg.put(key2, obj2)); 124 assertEquals (2, reg.size()); 125 assertTrue (reg.containsKey(key2)); 126 assertNull ("Put with no previous value - ", reg.put(key3, obj3)); 127 assertEquals (3, reg.size()); 128 assertTrue (reg.containsKey(key3)); 129 130 // the next two have the same value (Elvis) in key[0] 131 // Elvis/wombat --> Boolean(true) 132 assertNull ("Put with no previous value - ", reg.put(key4, obj4)); 133 assertEquals (4, reg.size()); 134 assertTrue (reg.containsKey(key4)); 135 // Elvis/heart-throb --> Float(5.47) 136 assertNull ("Put with no previous value - ", reg.put(key5, obj5)); 137 assertEquals (5, reg.size()); 138 assertTrue (reg.containsKey(key5)); 139 140 // overwrite, check return value is previous value 141 assertEquals ("Put did not return previous value", 142 obj1, reg.put(key1, obj2)); 143 assertEquals (5, reg.size()); 144 assertEquals ("Put did not return previous value", 145 obj2, reg.put(key2, obj3)); 146 assertEquals (5, reg.size()); 147 assertEquals ("Put did not return previous value", 148 obj3, reg.put(key3, obj1)); 149 assertEquals (5, reg.size()); 150 151 // Elvis/wombat returns Integer(3) 152 assertEquals ("Elvis/wombat put did not return previous value", 153 obj4, reg.put(key4, obj5)); 154 assertEquals (5, reg.size()); 155 assertEquals ("Elvis/heart-throb put did not return previous value", 156 obj5, reg.put(key5, obj4)); 157 assertEquals (5, reg.size()); 158 159 // check value returned on deletion is stored value 160 // first Elvis ... 161 assertEquals ("remove did not return value stored", 162 obj4, reg.remove (key5)); 163 assertTrue ("Item deleted but still present", !reg.containsKey(key5)); 164 assertEquals (4, reg.size()); 165 assertEquals ("remove did not return value stored", 166 obj5, reg.remove (key4)); 167 assertTrue ("Item deleted but still present", !reg.containsKey(key4)); 168 assertEquals (3, reg.size()); 169 // then the rest 170 assertEquals ("remove did not return value stored", 171 obj2, reg.remove (key1)); 172 assertTrue ("Item deleted but still present", !reg.containsKey(key1)); 173 assertEquals (2, reg.size()); 174 175 assertEquals ("remove did not return value stored", 176 obj3, reg.remove (key2)); 177 assertTrue ("Item deleted but still present", !reg.containsKey(key2)); 178 assertEquals (1, reg.size()); 179 180 assertEquals ("remove did not return value stored", 181 obj1, reg.remove (key3)); 182 assertTrue ("Item deleted but still present", !reg.containsKey(key3)); 183 assertEquals (0, reg.size()); 184 assertTrue ("Empty registry is not empty", reg.isEmpty() ); // GEEP 185 } 186 public void testElvis () { 187 // keys{2,4,5} have "Elvis" as key[0] 188 189 // Elvis/lives --> Topeka, Kansas 190 assertNull ("Put with no previous value - ", reg.put(key2, obj2)); 191 assertEquals (1, reg.size()); 192 assertTrue (reg.containsKey(key2)); 193 194 // Elvis/wombat --> Boolean(true) 195 assertNull ("Put with no previous value - ", reg.put(key4, obj4)); 196 assertEquals (2, reg.size()); 197 assertTrue (reg.containsKey(key4)); 198 199 // Elvis/heart-throb --> Float(5.47) 200 assertNull ("Put with no previous value - ", reg.put(key5, obj5)); 201 assertEquals (3, reg.size()); 202 assertTrue (reg.containsKey(key5)); 203 204 // make sure all three are present and have correct values 205 assertEquals ("Elvis/lives get error", obj2, reg.get(key2)); 206 assertEquals ("Elvis/wombat get error", obj4, reg.get(key4)); 207 assertEquals ("Elvis/heart-throb get error", obj5, reg.get(key5)); 208 209 // overwrite, check return value is previous value 210 assertEquals ("Put did not return previous value", 211 obj2, reg.put(key2, obj4)); 212 assertEquals (3, reg.size()); 213 assertEquals ("Put did not return previous value", 214 obj4, reg.put(key4, obj5)); 215 assertEquals (3, reg.size()); 216 assertEquals ("Put did not return previous value", 217 obj5, reg.put(key5, obj2)); 218 assertEquals (3, reg.size()); 219 220 // check value returned on deletion is stored value 221 assertEquals ("remove did not return value stored", 222 obj4, reg.remove (key2)); 223 assertTrue ("Item deleted but still present", !reg.containsKey(key2)); 224 assertEquals (2, reg.size()); 225 226 assertEquals ("remove did not return value stored", 227 obj5, reg.remove (key4)); 228 assertTrue ("Item deleted but still present", !reg.containsKey(key4)); 229 assertEquals (1, reg.size()); 230 231 assertEquals ("remove did not return value stored", 232 obj2, reg.remove (key5)); 233 assertTrue ("Item deleted but still present", !reg.containsKey(key5)); 234 assertEquals (0, reg.size()); 235 assertTrue ("Empty registry is not empty", reg.isEmpty() ); // GEEP 236 } 237 public void testRG2 () { 238 // duplicating bug found in the field 239 String keys1[] = {"rg2", "msg"}; 240 Boolean val1 = new Boolean(true); 241 String keys2[] = {"rg2", "test"}; 242 String val2 = "hope this works"; 243 244 assertEquals(0, reg.size()); 245 246 assertNull ("Put with no previous value - ", reg.put(keys1, val1)); 247 assertEquals (1, reg.size()); 248 assertTrue (reg.containsKey(keys1)); 249 250 assertNull ("Put with no previous value - ", reg.put(keys2, val2)); 251 assertEquals (2, reg.size()); 252 assertTrue (reg.containsKey(keys2)); 253 254 assertEquals ("rg2/msg get error", val1, reg.get(keys1)); 255 assertEquals ("rg2/test get error", val2, reg.get(keys2)); 256 } 257 public void testExceptions () { 258 // database is empty 259 try { 260 reg.get(null); 261 fail ("get with null key did not throw exception"); 262 } catch (IllegalArgumentException e) { } 263 264 try { 265 reg.put(key1, obj1); 266 reg.put(null, obj2); 267 fail ("put with null key did not throw exception"); 268 } catch (IllegalArgumentException e) { 269 assertEquals (1, reg.size()); 270 } 271 try { 272 reg.put(key2, obj2); 273 reg.containsKey(null); 274 fail ("containsKey with null key did not throw exception"); 275 } catch (IllegalArgumentException e) { 276 assertEquals (2, reg.size()); 277 } 278 try { 279 reg.put(key3, obj3); 280 reg.remove(null); 281 fail ("remove with null key did not throw exception"); 282 } catch (IllegalArgumentException e) { 283 assertEquals (3, reg.size()); 284 } 285 } 286 // COMING ATTRACTIONS: 287 // 288 // test sequential operations 289 // 290 // test set-wise operations 291 // 292 // test sub-map operations 293 294 } 295

This page was automatically generated by Maven