1 4 package com.tctest; 5 6 import org.apache.commons.collections.FastHashMap; 7 8 import com.tc.object.bytecode.Manageable; 9 import com.tc.object.config.ConfigVisitor; 10 import com.tc.object.config.DSOClientConfigHelper; 11 import com.tc.object.tx.UnlockedSharedObjectException; 12 import com.tc.simulator.app.ApplicationConfig; 13 import com.tc.simulator.listener.ListenerProvider; 14 import com.tc.util.Assert; 15 16 import java.io.ByteArrayInputStream ; 17 import java.io.ByteArrayOutputStream ; 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 import java.util.Set ; 28 import java.util.Map.Entry; 29 30 public class AutoLockMapTestApp extends GenericTestApp { 31 32 public AutoLockMapTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 33 super(appId, cfg, listenerProvider, Map.class); 34 } 35 36 protected Object getTestObject(String test) { 37 List maps = (List ) sharedMap.get("maps"); 38 39 return maps.iterator(); 40 } 41 42 protected void setupTestObject(String test) { 43 List maps = new ArrayList (); 44 maps.add(new HashMap()); 45 maps.add(new Hashtable ()); 46 maps.add(new FastHashMap()); 47 FastHashMap fm = new FastHashMap(); 48 fm.setFast(true); 49 maps.add(fm); 50 maps.add(new Properties ()); 51 52 sharedMap.put("maps", maps); 53 sharedMap.put("arrayforHashtable", new Object [4]); 54 sharedMap.put("arrayforFastHashMap", new Object [4]); 55 sharedMap.put("arrayforFastHashMapWithFast", new Object [4]); 56 sharedMap.put("arrayforProperties", new Object [4]); 57 } 58 59 private void initialize(Map map) { 60 map.putAll(getInitialData()); 61 } 62 63 private Hashtable getInitialData() { 64 Hashtable table = new Hashtable (); 65 table.put("January", "Jan"); 66 table.put("February", "Feb"); 67 table.put("March", "Mar"); 68 table.put("April", "Apr"); 69 return table; 70 } 71 72 void testDisableAutoLocks(Map map, boolean validate) throws Exception { 73 if (! (map instanceof Hashtable ) || (map instanceof HashMap)) { 74 return; 75 } 76 77 if (validate) { 78 return; 79 } 80 81 Hashtable ht = (Hashtable )map; 82 ((Manageable)ht).__tc_managed().disableAutoLocking(); 83 try { 84 ht.put("saravan", "smells"); 85 throw new AssertionError ("put() did not fail"); 86 } catch (UnlockedSharedObjectException use) { 87 } 89 } 90 91 void testPut(Map map, boolean validate) throws Exception { 92 if (map instanceof HashMap) { return; } 93 94 if (validate) { 95 assertMappings(getInitialData(), map); 96 } else { 97 initialize(map); 98 } 99 } 100 101 void testEntrySetRemove(Map map, boolean validate) throws Exception { 102 if (map instanceof HashMap) { return; } 103 104 if (validate) { 105 Hashtable expect = getInitialData(); 106 expect.remove("February"); 107 assertMappings(expect, map); 108 } else { 109 initialize(map); 110 111 Set entrySet = map.entrySet(); 112 entrySet.remove(new SimpleEntry("February", "Feb")); 113 } 114 } 115 116 void testEntrySetClear(Map map, boolean validate) throws Exception { 117 if (map instanceof HashMap) { return; } 118 119 if (validate) { 120 Assert.assertEquals(0, map.size()); 121 } else { 122 initialize(map); 123 Set entrySet = map.entrySet(); 124 entrySet.clear(); 125 } 126 } 127 128 void testEntrySetRetainAll(Map map, boolean validate) throws Exception { 129 if (map instanceof HashMap) { return; } 130 131 if (validate) { 132 Hashtable expect = getInitialData(); 133 expect.remove("January"); 134 expect.remove("April"); 135 assertMappings(expect, map); 136 } else { 137 initialize(map); 138 Set entrySet = map.entrySet(); 139 140 Collection toRetain = new ArrayList (); 141 toRetain.add(new SimpleEntry("February", "Feb")); 142 toRetain.add(new SimpleEntry("March", "Mar")); 143 144 entrySet.retainAll(toRetain); 145 } 146 } 147 148 void testEntrySetRemoveAll(Map map, boolean validate) throws Exception { 149 if (map instanceof HashMap) { return; } 150 151 if (validate) { 152 Hashtable expect = getInitialData(); 153 expect.remove("February"); 154 expect.remove("March"); 155 assertMappings(expect, map); 156 } else { 157 initialize(map); 158 Set entrySet = map.entrySet(); 159 160 Collection toRemove = new ArrayList (); 161 toRemove.add(new SimpleEntry("February", "Feb")); 162 toRemove.add(new SimpleEntry("March", "Mar")); 163 164 entrySet.removeAll(toRemove); 165 } 166 } 167 168 173 187 188 void testEntrySetIterator(Map map, boolean validate) throws Exception { 189 if ((map instanceof FastHashMap) && (!((FastHashMap) map).getFast())) { return; } 190 if (map instanceof HashMap) { return; } 191 192 if (validate) { 193 Hashtable expect = getInitialData(); 194 expect.remove("February"); 195 196 assertMappings(expect, map); 197 } else { 198 initialize(map); 199 Set entrySet = map.entrySet(); 200 for (Iterator iterator = entrySet.iterator(); iterator.hasNext();) { 201 Entry entry = (Entry) iterator.next(); 202 if ("February".equals(entry.getKey())) { 203 iterator.remove(); 204 break; 205 } 206 } 207 } 208 } 209 210 void testKeySetRemove(Map map, boolean validate) throws Exception { 211 if (map instanceof HashMap) { return; } 212 213 if (validate) { 214 Hashtable expect = getInitialData(); 215 expect.remove("February"); 216 assertMappings(expect, map); 217 } else { 218 initialize(map); 219 Set keySet = map.keySet(); 220 keySet.remove("February"); 221 } 222 } 223 224 void testKeySetClear(Map map, boolean validate) throws Exception { 225 if (map instanceof HashMap) { return; } 226 227 if (validate) { 228 Assert.assertEquals(0, map.size()); 229 } else { 230 initialize(map); 231 Set keySet = map.keySet(); 232 keySet.clear(); 233 } 234 } 235 236 void testKeySetIterator(Map map, boolean validate) throws Exception { 237 if ((map instanceof FastHashMap) && (!((FastHashMap) map).getFast())) { return; } 238 if (map instanceof HashMap) { return; } 239 240 if (validate) { 241 Hashtable expect = getInitialData(); 242 expect.remove("February"); 243 assertMappings(expect, map); 244 } else { 245 initialize(map); 246 Set keySet = map.keySet(); 247 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) { 248 String key = (String ) iterator.next(); 249 if ("February".equals(key)) { 250 iterator.remove(); 251 break; 252 } 253 } 254 } 255 } 256 257 void testKeySetRetainAll(Map map, boolean validate) throws Exception { 258 if (map instanceof HashMap) { return; } 259 260 if (validate) { 261 Hashtable expect = getInitialData(); 262 expect.remove("January"); 263 expect.remove("April"); 264 assertMappings(expect, map); 265 } else { 266 initialize(map); 267 Set keySet = map.keySet(); 268 269 Collection toRetain = new ArrayList (); 270 toRetain.add("February"); 271 toRetain.add("March"); 272 273 keySet.retainAll(toRetain); 274 } 275 } 276 277 void testKeySetRemoveAll(Map map, boolean validate) throws Exception { 278 if (map instanceof HashMap) { return; } 279 280 if (validate) { 281 Hashtable expect = getInitialData(); 282 expect.remove("February"); 283 expect.remove("March"); 284 assertMappings(expect, map); 285 } else { 286 initialize(map); 287 Set keySet = map.keySet(); 288 289 Collection toRemove = new ArrayList (); 290 toRemove.add("February"); 291 toRemove.add("March"); 292 293 keySet.removeAll(toRemove); 294 } 295 } 296 297 void testKeySetToArray(Map map, boolean validate) { 298 if (map instanceof HashMap) { return; } 299 300 Object [] array = getArray(map); 301 302 if (validate) { 303 assertArray(array, map.keySet()); 304 } else { 305 initialize(map); 306 307 synchronized (array) { 308 Object [] returnArray = map.keySet().toArray(array); 309 Assert.assertTrue(returnArray == array); 310 } 311 } 312 } 313 314 void testValuesRemove(Map map, boolean validate) throws Exception { 315 if (map instanceof HashMap) { return; } 316 317 if (validate) { 318 Hashtable expect = getInitialData(); 319 expect.remove("February"); 320 assertMappings(expect, map); 321 } else { 322 initialize(map); 323 Collection values = map.values(); 324 values.remove("Feb"); 325 } 326 } 327 328 void testValuesClear(Map map, boolean validate) throws Exception { 329 if (map instanceof HashMap) { return; } 330 331 if (validate) { 332 Assert.assertEquals(0, map.size()); 333 } else { 334 initialize(map); 335 Collection values = map.values(); 336 values.clear(); 337 } 338 } 339 340 void testValuesIterator(Map map, boolean validate) throws Exception { 341 if ((map instanceof FastHashMap) && (!((FastHashMap) map).getFast())) { return; } 342 if (map instanceof HashMap) { return; } 343 344 if (validate) { 345 Hashtable expect = getInitialData(); 346 expect.remove("February"); 347 assertMappings(expect, map); 348 } else { 349 initialize(map); 350 351 Collection values = map.values(); 352 for (Iterator iterator = values.iterator(); iterator.hasNext();) { 353 String value = (String ) iterator.next(); 354 if ("Feb".equals(value)) { 355 iterator.remove(); 356 break; 357 } 358 } 359 } 360 } 361 362 void testValuesRetainAll(Map map, boolean validate) throws Exception { 363 if (map instanceof HashMap) { return; } 364 365 if (validate) { 366 Hashtable expect = getInitialData(); 367 expect.remove("January"); 368 expect.remove("April"); 369 assertMappings(expect, map); 370 } else { 371 initialize(map); 372 Collection values = map.values(); 373 374 Collection toRetain = new ArrayList (); 375 toRetain.add("Feb"); 376 toRetain.add("Mar"); 377 378 values.retainAll(toRetain); 379 } 380 } 381 382 void testValuesRemoveAll(Map map, boolean validate) throws Exception { 383 if (map instanceof HashMap) { return; } 384 385 if (validate) { 386 Hashtable expect = getInitialData(); 387 expect.remove("February"); 388 expect.remove("March"); 389 assertMappings(expect, map); 390 } else { 391 initialize(map); 392 Collection values = map.values(); 393 394 Collection toRemove = new ArrayList (); 395 toRemove.add("Feb"); 396 toRemove.add("Mar"); 397 398 values.removeAll(toRemove); 399 } 400 } 401 402 void testValuesToArray(Map map, boolean validate) { 403 if (map instanceof HashMap) { return; } 404 405 Object [] array = getArray(map); 406 407 if (validate) { 408 assertArray(array, map.values()); 409 } else { 410 initialize(map); 411 412 synchronized (array) { 413 Object [] returnArray = map.values().toArray(array); 414 Assert.assertTrue(returnArray == array); 415 } 416 } 417 } 418 419 void testBasicSetProperty(Map map, boolean validate) { 420 if (map instanceof HashMap) { return; } 421 if (!(map instanceof Properties )) { 422 return; 423 } 424 425 if(validate) { 426 assertMappings(getInitialData(), map); 427 } else { 428 ((Properties )map).setProperty("January", "Jan"); 429 ((Properties )map).setProperty("February", "Feb"); 430 ((Properties )map).setProperty("March", "Mar"); 431 ((Properties )map).setProperty("April", "Apr"); 432 } 433 } 434 435 void testBasicGetProperty(Map map, boolean validate) { 436 if (map instanceof HashMap) { return; } 437 if (!(map instanceof Properties )) { 438 return; 439 } 440 441 if(validate) { 442 Assert.assertEquals("value", ((Properties )map).getProperty("key")); 443 Assert.assertEquals("defaultValue", ((Properties )map).getProperty("nonsense", "defaultValue")); 444 Assert.assertEquals("value", ((Properties )map).getProperty("key", "defaultValue")); 445 } else { 446 ((Properties )map).setProperty("key", "value"); 447 } 448 } 449 450 void testBasicLoad(Map map, boolean validate) { 451 if (map instanceof HashMap) { return; } 452 if (!(map instanceof Properties )) { 453 return; 454 } 455 456 if(validate) { 457 Map expectedMap = new Properties (); 458 expectedMap.put("key1", "val1"); 459 expectedMap.put("key2", "val2"); 460 expectedMap.put("key3", "val3"); 461 assertMappings(expectedMap, map); 462 } else { 463 Properties data = new Properties (); 464 data.setProperty("key1", "val1"); 465 data.setProperty("key2", "val2"); 466 data.setProperty("key3", "val3"); 467 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 468 try { 469 data.store(outputStream, null); 470 } catch (IOException ioe) { 471 Assert.fail(); 472 } 473 ByteArrayInputStream inputStream = new ByteArrayInputStream (outputStream.toByteArray()); 474 try { 475 ((Properties )map).load(inputStream); 476 } catch (IOException ioe) { 477 Assert.fail(); 478 } 479 } 480 } 481 482 void testHashMapPut(Map map, boolean validate) { 483 if (!(map instanceof HashMap) || (map instanceof FastHashMap)) { return; } 484 485 if (validate) { 486 Assert.assertEquals(0, map.size()); 487 } else { 488 try { 489 map.put("key1", "value1"); 490 throw new AssertionError ("Should have thrown an UnlockedSharedObjectException."); 491 } catch (UnlockedSharedObjectException e) { 492 } 494 } 495 } 496 497 void assertArray(Object [] expect, Collection collection) { 498 Assert.assertEquals(expect.length, collection.size()); 499 for (int i = 0; i < expect.length; i++) { 500 String val = (String )expect[i]; 501 Assert.assertTrue(collection.contains(val)); 502 } 503 } 504 505 void assertArray(Object [] expect, Map map) { 506 Assert.assertEquals(expect.length, map.size()); 507 for (int i = 0; i < expect.length; i++) { 508 Entry entry = (Entry) expect[i]; 509 Object val = map.get(entry.getKey()); 510 Assert.assertEquals(entry.getValue(), val); 511 } 512 } 513 514 void assertMappings(Map expect, Map actual) { 515 Assert.assertEquals(expect.size(), actual.size()); 516 517 Set expectEntries = expect.entrySet(); 518 Set actualEntries = actual.entrySet(); 519 520 for (Iterator i = expectEntries.iterator(); i.hasNext();) { 521 Entry entry = (Entry) i.next(); 522 Assert.assertEquals(entry.getValue(), actual.get(entry.getKey())); 523 } 524 525 for (Iterator i = actualEntries.iterator(); i.hasNext();) { 526 Entry entry = (Entry) i.next(); 527 Assert.assertEquals(entry.getValue(), expect.get(entry.getKey())); 528 } 529 } 530 531 private Object [] getArray(Map map) { 532 if (map instanceof Properties ) { return (Object []) sharedMap.get("arrayforProperties"); } 533 if (map instanceof FastHashMap) { 534 if (((FastHashMap) map).getFast()) { 535 return (Object []) sharedMap.get("arrayforFastHashMapWithFast"); 536 } else { 537 return (Object []) sharedMap.get("arrayforFastHashMap"); 538 } 539 } else if (map instanceof Hashtable ) { return (Object []) sharedMap.get("arrayforHashtable"); } 540 541 return null; 542 } 543 544 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 545 config.addNewModule("clustered-commons-collections-3.1", "1.0.0"); 546 547 String testClass = AutoLockMapTestApp.class.getName(); 548 config.getOrCreateSpec(testClass); 549 String readOnlyMethodExpression = "* " + testClass + "*.*ReadOnly*(..)"; 550 config.addReadAutolock(readOnlyMethodExpression); 551 String methodExpression = "* " + testClass + "*.*(..)"; 552 config.addWriteAutolock(methodExpression); 553 } 554 555 private static class SimpleEntry implements Map.Entry { 556 557 private final Object key; 558 private Object value; 559 560 public SimpleEntry(Object key, Object value) { 561 this.key = key; 562 this.value = value; 563 } 564 565 public SimpleEntry(Map.Entry e) { 566 this.key = e.getKey(); 567 this.value = e.getValue(); 568 } 569 570 public Object getKey() { 571 return key; 572 } 573 574 public Object getValue() { 575 return value; 576 } 577 578 public Object setValue(Object value) { 579 Object oldValue = this.value; 580 this.value = value; 581 return oldValue; 582 } 583 584 public boolean equals(Object o) { 585 if (!(o instanceof Map.Entry)) return false; 586 Map.Entry e = (Map.Entry) o; 587 return eq(key, e.getKey()) && eq(value, e.getValue()); 588 } 589 590 public int hashCode() { 591 return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode()); 592 } 593 594 public String toString() { 595 return key + "=" + value; 596 } 597 598 private static boolean eq(Object o1, Object o2) { 599 return (o1 == null ? o2 == null : o1.equals(o2)); 600 } 601 } 602 } 603 | Popular Tags |