1 5 package com.tctest; 6 7 import com.tc.object.config.ConfigVisitor; 8 import com.tc.object.config.DSOClientConfigHelper; 9 import com.tc.object.tx.ReadOnlyException; 10 import com.tc.simulator.app.ApplicationConfig; 11 import com.tc.simulator.listener.ListenerProvider; 12 import com.tc.util.Assert; 13 14 import gnu.trove.THashSet; 15 16 import java.util.Arrays ; 17 import java.util.Comparator ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.LinkedHashSet ; 21 import java.util.List ; 22 import java.util.Set ; 23 import java.util.TreeSet ; 24 25 public class GenericSetTestApp extends GenericTestApp { 26 27 public GenericSetTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 28 super(appId, cfg, listenerProvider, Set.class); 29 } 30 31 protected Object getTestObject(String testName) { 32 Set sets = (Set) sharedMap.get("sets"); 33 return sets.iterator(); 34 } 35 36 protected void setupTestObject(String testName) { 37 Set sets = new HashSet(); 38 sets.add(new HashSet()); 39 sets.add(new LinkedHashSet ()); 40 sets.add(new THashSet()); 41 sets.add(new TreeSet (new NullTolerantComparator())); 42 sets.add(new MyHashSet()); 43 sets.add(new MyLinkedHashSet()); 44 sets.add(new MyTHashSet()); 45 sets.add(new MyTreeSet(new NullTolerantComparator())); 46 48 sharedMap.put("sets", sets); 49 sharedMap.put("arrayforHashSet", new Object [2]); 50 sharedMap.put("arrayforTHashSet", new Object [2]); 51 sharedMap.put("arrayforTreeSet", new Object [2]); 52 sharedMap.put("arrayforLinkedHashSet", new Object [2]); 53 sharedMap.put("arrayforMyHashSet", new Object [2]); 54 sharedMap.put("arrayforMyTHashSet", new Object [2]); 55 sharedMap.put("arrayforMyTreeSet", new Object [2]); 56 sharedMap.put("arrayforMyLinkedHashSet", new Object [2]); 57 } 58 59 void testBasicAdd(Set set, boolean validate) { 61 if (validate) { 62 assertSetsEqual(Arrays.asList(new Object [] { "January", "February" }), set); 63 } else { 64 synchronized (set) { 65 boolean added = set.add("January"); 66 Assert.assertTrue(added); 67 added = set.add("February"); 68 Assert.assertTrue(added); 69 } 70 } 71 } 72 73 void testAddAll(Set set, boolean validate) { 74 if (validate) { 75 assertSetsEqual(Arrays.asList(new Object [] { "January", "February" }), set); 76 } else { 77 Set toAdd = new LinkedHashSet (); 78 toAdd.add("January"); 79 toAdd.add("February"); 80 synchronized (set) { 81 boolean added = set.addAll(toAdd); 82 Assert.assertTrue(added); 83 } 84 } 85 } 86 87 void testClear(Set set, boolean validate) { 88 if (validate) { 89 assertEmptySet(set); 90 } else { 91 Set toAdd = new HashSet(); 92 toAdd.add("first element"); 93 toAdd.add("second element"); 94 synchronized (set) { 95 set.clear(); 96 } 97 } 98 } 99 100 void testRemove(Set set, boolean validate) { 101 if (validate) { 102 assertSetsEqual(Arrays.asList(new Object [] { "January", "March" }), set); 103 } else { 104 synchronized (set) { 105 set.add("January"); 106 set.add("February"); 107 set.add("March"); 108 } 109 synchronized (set) { 110 set.remove("February"); 111 } 112 } 113 } 114 115 void testRemoveAll(Set set, boolean validate) { 116 if (validate) { 117 assertSetsEqual(Arrays.asList(new Object [] { "January", "April" }), set); 118 } else { 119 synchronized (set) { 120 set.add("January"); 121 set.add("February"); 122 set.add("March"); 123 set.add("April"); 124 } 125 Set toRemove = new HashSet(); 126 toRemove.add("February"); 127 toRemove.add("March"); 128 synchronized (set) { 129 set.removeAll(toRemove); 130 } 131 } 132 } 133 134 void testRetainAll(Set set, boolean validate) { 135 if (validate) { 136 assertSetsEqual(Arrays.asList(new Object [] { "February", "March" }), set); 137 } else { 138 synchronized (set) { 139 set.add("January"); 140 set.add("February"); 141 set.add("March"); 142 set.add("April"); 143 } 144 Set toRetain = new HashSet(); 145 toRetain.add("February"); 146 toRetain.add("March"); 147 synchronized (set) { 148 set.retainAll(toRetain); 149 } 150 } 151 } 152 153 void testToArray(Set set, boolean validate) { 154 Object [] array = getArray(set); 155 156 if (validate) { 157 assertSetsEqual(Arrays.asList(array), set); 158 } else { 159 synchronized (set) { 160 set.add("January"); 161 set.add("February"); 162 } 163 synchronized (array) { 164 Object [] returnArray = set.toArray(array); 165 Assert.assertTrue(returnArray == array); 166 } 167 } 168 } 169 170 void testIteratorRemove(Set set, boolean validate) { 172 if (validate) { 173 assertSetsEqual(Arrays.asList(new Object [] { "January", "February" }), set); 174 } else { 175 synchronized (set) { 176 set.add("January"); 177 set.add("February"); 178 set.add("March"); 179 } 180 String element; 181 synchronized (set) { 182 for (Iterator iterator = set.iterator(); iterator.hasNext();) { 183 element = (String ) iterator.next(); 184 if ("March".equals(element)) { 185 iterator.remove(); 186 } 187 } 188 } 189 } 190 } 191 192 void testIteratorRemoveNull(Set set, boolean validate) { 193 if (validate) { 194 assertSetsEqual(Arrays.asList(new Object [] { "January", "February" }), set); 195 } else { 196 synchronized (set) { 197 set.add("January"); 198 set.add(null); 199 set.add("February"); 200 } 201 synchronized (set) { 202 Iterator iterator = set.iterator(); 203 Assert.assertEquals(true, iterator.hasNext()); 204 while (iterator.hasNext()) { 205 Object o = iterator.next(); 206 if (o == null) { 207 iterator.remove(); 208 } 209 } 210 } 211 } 212 } 213 214 void testReadOnlyAdd(Set set, boolean validate) { 216 if (validate) { 217 assertEmptySet(set); 218 } else { 219 synchronized (set) { 220 try { 221 set.add("first element"); 222 throw new AssertionError ("Should have thrown a ReadOnlyException"); 223 } catch (ReadOnlyException e) { 224 } 226 } 227 } 228 } 229 230 void testReadOnlyAddAll(Set set, boolean validate) { 231 if (validate) { 232 assertEmptySet(set); 233 } else { 234 Set toAdd = new HashSet(); 235 toAdd.add("first element"); 236 toAdd.add("second element"); 237 synchronized (set) { 238 try { 239 set.addAll(toAdd); 240 throw new AssertionError ("Should have thrown a ReadOnlyException"); 241 } catch (ReadOnlyException r) { 242 } 244 } 245 } 246 } 247 248 void testSetUpRemove(Set set, boolean validate) { 250 if (validate) { 251 assertSetsEqual(Arrays.asList(new Object [] { "January", "February" }), set); 252 } else { 253 synchronized (set) { 254 set.add("January"); 255 set.add("February"); 256 } 257 tryReadOnlyRemove(set); 258 } 259 } 260 261 private void tryReadOnlyRemove(Set set) { 263 synchronized (set) { 264 try { 265 set.remove("February"); 266 throw new AssertionError ("Should have thrown a ReadOnlyException"); 267 } catch (ReadOnlyException t) { 268 } 270 } 271 } 272 273 void testSetUpIteratorRemove(Set set, boolean validate) { 275 if (validate) { 276 assertSetsEqual(Arrays.asList(new Object [] { "January", "February" }), set); 277 } else { 278 synchronized (set) { 279 set.add("January"); 280 set.add("February"); 281 } 282 tryReadOnlyIteratorRemove(set); 283 } 284 } 285 286 private void tryReadOnlyIteratorRemove(Set set) { 288 synchronized (set) { 289 try { 290 Iterator iterator = set.iterator(); 291 iterator.next(); 292 iterator.remove(); 293 throw new AssertionError ("Should have thrown a ReadOnlyException"); 294 } catch (ReadOnlyException r) { 295 } 297 } 298 } 299 300 void testSetUpClear(Set set, boolean validate) { 302 if (validate) { 303 assertSetsEqual(Arrays.asList(new Object [] { "January", "February" }), set); 304 } else { 305 synchronized (set) { 306 set.add("January"); 307 set.add("February"); 308 } 309 tryReadOnlyClear(set); 310 } 311 } 312 313 private void tryReadOnlyClear(Set set) { 315 synchronized (set) { 316 try { 317 set.clear(); 318 throw new AssertionError ("Should have thrown a ReadOnlyException"); 319 } catch (ReadOnlyException t) { 320 } 322 } 323 } 324 325 void testSetUpToArray(Set set, boolean validate) { 327 if (validate) { 328 Object [] array = getArray(set); 329 assertEmptyObjectArray(array); 330 } else { 331 synchronized (set) { 332 set.add("January"); 333 set.add("February"); 334 } 335 tryReadOnlyToArray(set); 336 } 337 } 338 339 void tryReadOnlyToArray(Set set) { 341 Object [] array = getArray(set); 342 synchronized (array) { 343 try { 344 Object [] returnArray = set.toArray(array); 345 Assert.assertTrue(returnArray == array); 346 throw new AssertionError ("Should have thrown a ReadOnlyException"); 347 } catch (ReadOnlyException t) { 348 } 350 } 351 } 352 353 void testSetUpRetainAll(Set set, boolean validate) { 355 if (validate) { 356 assertSetsEqual(Arrays.asList(new Object [] { "January", "February" }), set); 357 } else { 358 synchronized (set) { 359 set.add("January"); 360 set.add("February"); 361 } 362 tryReadOnlyRetainAll(set); 363 } 364 } 365 366 void tryReadOnlyRetainAll(Set set) { 368 synchronized (set) { 369 Set toRetain = new HashSet(); 370 toRetain.add("January"); 371 try { 372 set.retainAll(toRetain); 373 throw new AssertionError ("Should have thrown a ReadOnlyException"); 374 } catch (ReadOnlyException t) { 375 } 377 } 378 } 379 380 void testSetUpRemoveAll(Set set, boolean validate) { 382 if (validate) { 383 assertSetsEqual(Arrays.asList(new Object [] { "January", "February" }), set); 384 } else { 385 synchronized (set) { 386 set.add("January"); 387 set.add("February"); 388 } 389 tryReadOnlyRemoveAll(set); 390 } 391 } 392 393 void tryReadOnlyRemoveAll(Set set) { 395 synchronized (set) { 396 Set toRemove = new HashSet(); 397 toRemove.add("January"); 398 try { 399 set.removeAll(toRemove); 400 throw new AssertionError ("Should have thrown a ReadOnlyException"); 401 } catch (ReadOnlyException t) { 402 } 404 } 405 } 406 407 private Object getMySubclassArray(Set set) { 408 if (set instanceof MyLinkedHashSet) { return sharedMap.get("arrayforMyLinkedHashSet"); } 409 if (set instanceof MyHashSet) { return sharedMap.get("arrayforMyHashSet"); } 410 if (set instanceof MyTHashSet) { return sharedMap.get("arrayforMyTHashSet"); } 411 if (set instanceof MyTreeSet) { return sharedMap.get("arrayforMyTreeSet"); } 412 return null; 413 } 414 415 private Object [] getArray(Set set) { 416 Object o = getMySubclassArray(set); 417 if (o != null) { return (Object []) o; } 418 419 if (set instanceof LinkedHashSet ) { return (Object []) sharedMap.get("arrayforLinkedHashSet"); } 420 if (set instanceof HashSet) { return (Object []) sharedMap.get("arrayforHashSet"); } 421 if (set instanceof THashSet) { return (Object []) sharedMap.get("arrayforTHashSet"); } 422 if (set instanceof TreeSet ) { return (Object []) sharedMap.get("arrayforTreeSet"); } 423 return null; 424 } 425 426 private static void assertEmptyObjectArray(Object [] array) { 427 for (int i = 0; i < array.length; i++) { 428 Assert.assertNull(array[i]); 429 } 430 } 431 432 private static void assertEmptySet(Set set) { 433 Assert.assertEquals(0, set.size()); 434 Assert.assertTrue(set.isEmpty()); 435 436 int count = 0; 437 for (Iterator i = set.iterator(); i.hasNext();) { 438 count++; 439 } 440 Assert.assertEquals(0, count); 441 } 442 443 private static void assertSetsEqual(List expectElements, Set actual) { 444 Assert.assertEquals(expectElements.size(), actual.size()); 445 446 if (actual instanceof LinkedHashSet ) { 447 for (Iterator iExpect = expectElements.iterator(), iActual = actual.iterator(); iExpect.hasNext();) { 448 Assert.assertEquals(iExpect.next(), iActual.next()); 449 } 450 } 451 452 Assert.assertTrue(expectElements.containsAll(actual)); 453 Assert.assertTrue(actual.containsAll(expectElements)); 454 455 if (expectElements.isEmpty()) { 456 Assert.assertTrue(actual.isEmpty()); 457 } else { 458 Assert.assertFalse(actual.isEmpty()); 459 } 460 } 461 462 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 463 String testClass = GenericSetTestApp.class.getName(); 464 config.addIncludePattern(testClass + "$*"); 465 config.getOrCreateSpec(testClass); 466 String writeAllowedMethodExpression = "* " + testClass + "*.*(..)"; 467 config.addWriteAutolock(writeAllowedMethodExpression); 468 String readOnlyMethodExpression = "* " + testClass + "*.*ReadOnly*(..)"; 469 config.addReadAutolock(readOnlyMethodExpression); 470 } 471 472 private static class NullTolerantComparator implements Comparator { 473 474 public int compare(Object o1, Object o2) { 475 if (o1 == null && o2 == null) { return 0; } 476 if (o1 == null && o2 != null) { return -1; } 477 if (o1 != null && o2 == null) { return 1; } 478 return ((Comparable ) o1).compareTo(o2); 479 } 480 } 481 482 private static class MyHashSet extends HashSet { 483 public MyHashSet() { 484 super(); 485 } 486 } 487 488 private static class MyLinkedHashSet extends LinkedHashSet { 489 public MyLinkedHashSet() { 490 super(); 491 } 492 } 493 494 private static class MyTHashSet extends THashSet { 495 public MyTHashSet() { 496 super(); 497 } 498 } 499 500 private static class MyTreeSet extends TreeSet { 501 public MyTreeSet(Comparator comparator) { 502 super(comparator); 503 } 504 } 505 } 506 | Popular Tags |