1 4 package com.tctest.transparency; 5 6 import org.apache.commons.collections.ArrayStack; 7 import org.apache.commons.collections.MultiHashMap; 8 9 import com.tc.exception.TCNonPortableObjectError; 10 import com.tc.object.config.ConfigVisitor; 11 import com.tc.object.config.DSOClientConfigHelper; 12 import com.tc.object.config.TransparencyClassSpec; 13 import com.tc.simulator.app.ApplicationConfig; 14 import com.tc.simulator.listener.ListenerProvider; 15 import com.tc.util.SequenceID; 16 import com.tctest.runner.AbstractTransparentApp; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Date ; 21 import java.util.HashMap ; 22 import java.util.LinkedList ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Stack ; 26 import java.util.TreeMap ; 27 import java.util.Vector ; 28 29 public class ClassNotPortableTestApp extends AbstractTransparentApp { 30 31 SubClassA root1; 32 SubClassB root2; 33 SubClassC root3; 34 35 SuperClassWithFields root4; 36 37 Object root5; 38 List root6; 39 40 Collection root7; 41 Map root8; 42 Worker root9; 43 44 public ClassNotPortableTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 45 super(appId, cfg, listenerProvider); 46 } 47 48 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 49 String testClass = ClassNotPortableTestApp.class.getName(); 50 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 51 String methodExpression = "* " + testClass + "*.*(..)"; 52 config.addIncludePattern(testClass); 53 config.addWriteAutolock(methodExpression); 54 spec.addRoot("root1", "root1"); 55 spec.addRoot("root2", "root2"); 56 spec.addRoot("root3", "root3"); 57 spec.addRoot("root4", "root4"); 58 spec.addRoot("root5", "root5"); 59 spec.addRoot("root6", "root6"); 60 spec.addRoot("root7", "root7"); 61 spec.addRoot("root8", "root8"); 62 spec.addRoot("root9", "root9"); 63 64 String classname1 = SubClassA.class.getName(); 66 String classname2 = SubClassB.class.getName(); 67 String classname3 = SubClassC.class.getName(); 68 69 String classname4 = Worker.class.getName(); 70 71 String classname5 = ArrayStack.class.getName(); 72 String classname6 = MultiHashMap.class.getName(); 73 74 String classname7 = Thread .class.getName(); 75 76 config.addIncludePattern(classname1); 77 config.addIncludePattern(classname2); 78 config.addIncludePattern(classname3); 79 config.addIncludePattern(classname4); 80 config.addIncludePattern(classname5); 81 config.addIncludePattern(classname6); 82 83 config.addIncludePattern(classname7); 84 config.addIncludePattern(ReferenceHolder.class.getName()); 85 config.addIncludePattern(TreeNode.class.getName()); 86 87 config.addWriteAutolock("* " + classname1 + ".*(..)"); 88 config.addWriteAutolock("* " + classname2 + ".*(..)"); 89 config.addWriteAutolock("* " + classname3 + ".*(..)"); 90 } 91 92 public void run() { 93 94 testSuperClassNotPortable(); 95 96 testNonPortableClassAddedToSharedGraph(); 97 98 100 testThreadAndSubClassOfThreads(); 101 102 testSetAndGetOnPortableAdaptableClasses(); 103 104 testComplexReferenceGraphAddedToSharedMap(); 105 } 106 107 private void testSetAndGetOnPortableAdaptableClasses() { 111 SubClassC cc = new SubClassC(); 112 cc.checkedUnCheckedSetsAndGets(); 113 114 Worker w = new Worker(); 115 w.checkedUnCheckedSetsAndGets(); 116 } 117 118 private void testSuperClassNotPortable() { 119 try { 121 root1 = new SubClassA(); 122 root1.method1(); 123 throw new AssertionError ("Should have failed"); 124 } catch (TCNonPortableObjectError tcp) { 125 } 127 128 try { 130 root2 = new SubClassB(); 131 root2.method1(); 132 throw new AssertionError ("Should have failed"); 133 } catch (TCNonPortableObjectError tcp) { 134 } 136 137 try { 139 root3 = new SubClassC(); 140 root3.method1(); 141 throw new AssertionError ("Should have failed"); 142 } catch (TCNonPortableObjectError tcp) { 143 } 145 146 } 147 148 private void testNonPortableClassAddedToSharedGraph() { 149 try { 150 root4 = new SuperClassWithFields(); 151 throw new AssertionError ("Should have failed"); 152 } catch (TCNonPortableObjectError tcp) { 153 } 155 156 try { 157 root5 = new SuperClassWithFields(); 158 throw new AssertionError ("Should have failed"); 159 } catch (TCNonPortableObjectError tcp) { 160 } 162 163 try { 164 root5 = new SubClassD(); 165 throw new AssertionError ("Should have failed"); 166 } catch (TCNonPortableObjectError tcp) { 167 } 169 170 root6 = new ArrayList (); 171 172 synchronized (root6) { 173 try { 174 root6.add(new SuperClassWithNoFields()); 176 throw new AssertionError ("Should have failed"); 177 } catch (TCNonPortableObjectError tcp) { 178 } 180 181 Map m = new HashMap(); 182 m.put(new Integer (10), new SuperClassWithFields()); 183 184 try { 185 root6.add(m); 187 throw new AssertionError ("Should have failed"); 188 } catch (TCNonPortableObjectError tcp) { 189 } 191 192 Map tm = new TreeMap (); 193 root6.add(tm); 194 195 try { 196 tm.put(new Integer (10), new SuperClassWithFields()); 197 throw new AssertionError ("Should have failed"); 199 } catch (TCNonPortableObjectError tcp) { 200 } 202 203 ReferenceHolder ref = new ReferenceHolder(new Integer (120)); 204 root6.add(ref); 205 try { 206 ref.setReference(new SuperClassWithNoFields()); 207 throw new AssertionError ("Should have failed"); 208 } catch (TCNonPortableObjectError tcp) { 209 } 211 212 ReferenceHolder refs[] = new ReferenceHolder[2]; 213 root6.add(refs); 214 refs[0] = new ReferenceHolder("Hello String literal !"); 215 try { 216 refs[1] = new ReferenceHolder(new SuperClassWithFields()); 218 throw new AssertionError ("Should have failed"); 219 } catch (TCNonPortableObjectError tcp) { 220 } 222 223 try { 225 root6.add(new SequenceID(10)); 226 throw new AssertionError ("Should have failed"); 227 } catch (TCNonPortableObjectError tcp) { 228 } 230 } 231 } 232 233 private void testSubclassOfCollections() { 234 int count = 13; 235 try { 236 root7 = new SubClassOfArrayList(count); 237 addSomething(root7, count); 238 System.err.println(root7); 239 throw new AssertionError ("Should have failed"); 240 } catch (TCNonPortableObjectError tcp) { 241 } 243 244 root7 = new Stack(); 246 synchronized (root7) { 247 addSomething(root7, count); 248 System.err.println(root7); 249 250 root7.add(addSomething(new ArrayList (), 2)); 251 root7.add(addSomething(new LinkedList (), 2)); 252 253 try { 254 root7.add(addSomething(new ArrayStack(), 9)); 255 throw new AssertionError ("Should have failed"); 256 } catch (TCNonPortableObjectError tcp) { 257 } 259 260 root8 = new HashMap(); 261 putSomething(root8, 3); 262 synchronized (root8) { 263 264 root8.put(new Long (count++), new TreeMap ()); 265 267 try { 268 root8.put(new Long (count++), new MultiHashMap()); 269 throw new AssertionError ("Should have failed"); 270 } catch (TCNonPortableObjectError tcp) { 271 } 273 } 274 275 } 277 } 278 279 private void testThreadAndSubClassOfThreads() { 280 if (root8 == null) { 281 root8 = new HashMap(); 282 } 283 try { 284 synchronized (root8) { 285 root8.put("Hello Thread ", new Thread ("hello (1)\n")); 286 } 287 throw new AssertionError ("Should have failed"); 288 } catch (TCNonPortableObjectError tcp) { 289 } 291 292 try { 293 root9 = new Worker(); 294 throw new AssertionError ("Should have failed"); 295 } catch (TCNonPortableObjectError tcp) { 296 } 298 } 299 300 private void testComplexReferenceGraphAddedToSharedMap() { 301 if (root8 == null) { 302 root8 = new HashMap(); 303 } 304 Vector v = new Vector (); 305 synchronized (root8) { 306 root8.put("Shared Vector", v); 307 } 308 Object o = buildComplexStructure(); 309 try { 310 synchronized (v) { 311 v.add(o); 312 } 313 throw new AssertionError ("Should have failed"); 314 } catch (TCNonPortableObjectError ex) { 315 } 317 } 318 319 private Object buildComplexStructure() { 320 int level = 6; 321 TreeNode root = new TreeNode(); 322 buildRecursively(root, level); 323 return new ReferenceHolder(root); 324 } 325 326 private void buildRecursively(TreeNode root, int level) { 327 root.left = new TreeNode(); 328 root.right = new TreeNode(); 329 level--; 330 if (level == 1) { 331 root.left = new Date (); 332 root.right = getNonPortableList(); 333 } else { 334 buildRecursively((TreeNode) root.left, level); 335 buildRecursively((TreeNode) root.right, level); 336 } 337 } 338 339 private Object getNonPortableList() { 340 List l = new ArrayList (); 341 int c = 10; 342 while (c-- > 0) { 343 l.add(new Date ()); 344 } 345 l.add(new SubClassC()); 346 return l; 347 } 348 349 private Collection addSomething(Collection root, int count) { 350 while (count > 0) { 351 synchronized (root) { 352 root.add(new Long (count--)); 353 } 354 } 355 return root; 356 } 357 358 private Map putSomething(Map root, int count) { 359 while (count > 0) { 360 synchronized (root) { 361 root.put(new Long (count), new Long (count--)); 362 } 363 } 364 return root; 365 } 366 367 static class TreeNode { 368 Object left; 369 Object right; 370 371 TreeNode() { 372 super(); 373 } 374 375 TreeNode(Object l, Object r) { 376 this.left = l; 377 this.right = r; 378 } 379 } 380 381 static class ReferenceHolder { 382 Object reference; 383 384 public ReferenceHolder(Object o) { 385 setReference(o); 386 } 387 388 public Object getReference() { 389 return reference; 390 } 391 392 public void setReference(Object reference) { 393 this.reference = reference; 394 } 395 396 } 397 398 static class SubClassOfArrayList extends ArrayList { 399 private int localint; 400 401 SubClassOfArrayList(int localint) { 402 this.localint = localint; 403 } 404 405 int getLocalInt() { 406 return this.localint; 407 } 408 409 void setLocalInt(int i) { 410 this.localint = i; 411 } 412 413 public String toString() { 414 return "SubClassOfArrayList(" + localint + "):" + super.toString(); 415 } 416 } 417 418 static class SubClassOfHashMap extends HashMap { 419 private int localint; 420 421 SubClassOfHashMap(int localint) { 422 this.localint = localint; 423 } 424 425 int getLocalInt() { 426 return this.localint; 427 } 428 429 void setLocalInt(int i) { 430 this.localint = i; 431 } 432 433 public String toString() { 434 return "SubClassOfHashMap(" + localint + "):" + super.toString(); 435 } 436 } 437 438 static class Worker extends Thread { 439 private List works = new ArrayList (); 440 private boolean stop = false; 441 442 private int k; 443 444 public synchronized void addWork(Runnable r) { 445 works.add(r); 446 notifyAll(); 447 } 448 449 public void checkedUnCheckedSetsAndGets() { 450 int newk = k + 109; 452 k = newk; 453 454 SubClassD d = new SubClassD(); 456 d.d = d.d++; 457 } 458 459 public void notSynchronizedOnMethod(Object o) { 460 synchronized (o) { 461 o.notifyAll(); 462 } 463 } 464 465 public synchronized void requestStop() { 466 stop = true; 467 notifyAll(); 468 } 469 470 public void run() { 471 while (true) { 472 while (works.isEmpty() && !stop) { 473 try { 474 wait(); 475 } catch (InterruptedException e) { 476 throw new RuntimeException (e); 477 } 478 } 479 if (stop) break; 480 Runnable r = (Runnable ) works.remove(0); 481 r.run(); 482 } 483 } 484 } 485 486 } 487
| Popular Tags
|