1 5 package com.tc.object.bytecode; 6 7 import com.tc.object.TestClientObjectManager; 8 import com.tc.object.config.ConfigLockLevel; 9 import com.tc.object.config.DSOClientConfigHelper; 10 import com.tc.object.config.LockDefinition; 11 import com.tc.object.config.TransparencyClassSpec; 12 import com.tc.object.loaders.IsolationClassLoader; 13 import com.tc.object.tx.MockTransactionManager; 14 import com.tc.object.tx.MockTransactionManager.Begin; 15 import com.tctest.ClassAdapterTestTarget; 16 import com.tctest.ClassAdapterTestTargetBase; 17 import com.tctest.ClassAdapterTestTargetBaseBase; 18 import com.tctest.LockTestThrowsExceptionException; 19 20 import java.io.ObjectStreamField ; 21 import java.lang.reflect.Constructor ; 22 import java.lang.reflect.Field ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.Modifier ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import javax.swing.DefaultListModel ; 30 31 34 public class ClassAdapterTest extends ClassAdapterTestBase { 35 private static final Class [] WITH_ARGS_PARAMS = new Class [] { Integer.TYPE, String .class }; 36 private static final Object [] WITH_ARGS_ARGS = new Object [] { new Integer (1), "test string" }; 37 38 private DSOClientConfigHelper config; 39 private LockDefinition lockDefinition; 40 private IsolationClassLoader classLoader; 41 private TestClientObjectManager testClientObjectManager; 42 private MockTransactionManager testTransactionManager; 43 private String targetClassName = ClassAdapterTestTarget.class.getName(); private ClassLoader origThreadContextClassLoader; 45 46 protected void setUp() throws Exception { 47 System.getProperties().remove(ClassAdapterTestTarget.KEY); 48 initializeConfig(); 49 this.testClientObjectManager = new TestClientObjectManager(); 50 this.testTransactionManager = new MockTransactionManager(); 51 initClassLoader(); 52 this.origThreadContextClassLoader = Thread.currentThread().getContextClassLoader(); 53 Thread.currentThread().setContextClassLoader(this.classLoader); 54 } 55 56 private void initializeConfig() throws Exception { 57 this.config = createClientConfigHelper(); 58 } 59 60 private void createAutolockLockDefinition() { 61 this.config.addIncludePattern(this.targetClassName); 62 this.lockDefinition = new LockDefinition(LockDefinition.TC_AUTOLOCK_NAME, ConfigLockLevel.WRITE); 63 this.lockDefinition.commit(); 64 } 65 66 private void initClassLoader() { 67 this.classLoader = new IsolationClassLoader(config, testClientObjectManager, testTransactionManager); 68 this.classLoader.init(); 69 } 70 71 protected void tearDown() throws Exception { 72 super.tearDown(); 73 this.config = null; 74 this.lockDefinition = null; 75 this.testClientObjectManager = null; 76 this.testTransactionManager = null; 77 78 this.classLoader = null; 79 Thread.currentThread().setContextClassLoader(this.origThreadContextClassLoader); 80 } 81 82 public void testNamedLockInInstanceManagedConstructor() throws Exception { 83 String tcn = DefaultListModel .class.getName(); 84 config.addIncludePattern(tcn); 85 String methodPattern = "* " + tcn + ".*(..)"; 86 lockDefinition = new LockDefinition("doStuff", ConfigLockLevel.WRITE); 87 lockDefinition.commit(); 88 config.addLock(methodPattern, lockDefinition); 89 90 Class clazz = this.classLoader.loadClass(tcn); 91 clazz.newInstance(); 92 93 } 94 95 public void testSuperclassTransients() throws Exception { 96 String supersuperclass = ClassAdapterTestTargetBaseBase.class.getName(); 97 TransparencyClassSpec spec = config.getOrCreateSpec(supersuperclass); 98 spec.addTransient("myString"); 99 100 String superclass = ClassAdapterTestTargetBase.class.getName(); 101 spec = config.getOrCreateSpec(superclass); 102 103 spec = config.getOrCreateSpec(targetClassName); 104 105 String methodPattern = "public void " + targetClassName + ".doStuff()"; 107 lockDefinition = new LockDefinition("doStuff", ConfigLockLevel.WRITE); 108 lockDefinition.commit(); 109 config.addLock(methodPattern, lockDefinition); 110 111 Class clazz = this.classLoader.loadClass(targetClassName); 112 Object o = clazz.newInstance(); 113 Method m = clazz.getDeclaredMethod("doStuff", new Class [0]); 114 m.invoke(o, new Object [0]); 115 } 116 117 public void testAssertions() throws Exception { 118 LockDefinition bogus = new LockDefinition("fakeLock", ConfigLockLevel.WRITE); 120 bogus.commit(); 121 assertFalse(checkForLock(bogus)); 122 assertNoTransactions(); 123 assertTransactionCount(0); 124 } 125 126 public void testWildcardPatternWithNamedLocksAdaptsOK() throws Exception { 127 createNamedLockDefinition("test"); 128 createLockConfigurationForMethodExpression("*", "*", "(..)"); 129 callNoArgCtor(); 130 assertTransactionCount(1); 131 } 132 133 public void testWildcardPatternWithAutolockAdaptsOK() throws Exception { 134 createAutolockLockDefinition(); 135 createLockConfigurationForMethodExpression("* ", "*", "(..)"); 136 137 callNoArgCtor(); 138 } 139 140 public void testSynchronizedInstanceMethodWithWideArgs() throws Exception { 141 String methodName = "synchronizedInstanceMethodWithWideArgs"; 142 createAutolockLockDefinition(); 143 String modifiersPattern = "*"; 144 String parametersPattern = "(..)"; 145 createLockConfigurationForMethodExpression(modifiersPattern, methodName, parametersPattern); 146 147 this.testClientObjectManager.setIsManaged(true); 148 invokeWithArgs(methodName, new Class [] { Double.TYPE, Long.TYPE }, new Object [] { new Double (0), new Long (0) }); 149 } 150 151 public void testInstanceMethodWithNamedLocks() throws Exception { 152 String methodName = "instanceMethod"; 153 createNamedLockDefinition("testLock"); 154 createLockConfigurationForMethodExpression("void", methodName, "()"); 155 156 assertNoTransactions(); 157 158 invokeWithNoArgs(methodName); 159 160 assertNoAutolocks(); 161 162 assertNamedLockConditionsPostInvocation(1); 163 164 LockDefinition bogus = new LockDefinition("fakeLock", ConfigLockLevel.WRITE); 166 bogus.commit(); 167 assertFalse(checkForLock(bogus)); 168 169 } 170 171 public void testInstanceMethodThrowsExceptionWithNamedLocks() throws Exception { 172 String methodName = "instanceMethodThrowsException"; 173 174 createNamedLockDefinition("testLock"); 175 createLockConfigurationForMethodExpression("void", methodName, "()"); 176 177 assertNoTransactions(); 178 179 invokeWithNoArgsAndCheckForProperException(methodName); 180 181 assertNamedLockConditionsPostInvocation(1); 183 } 184 185 public void testInstanceMethodWithArgumentsWithNamedLocks() throws Exception { 186 187 String methodName = "instanceMethodWithArguments"; 188 189 createNamedLockDefinition("testLock"); 190 createLockConfigurationForMethodExpression("void", methodName, "(int, java.lang.String)"); 191 192 assertNoTransactions(); 193 194 invokeWithDefaultArgs(methodName); 195 196 assertNoAutolocks(); 198 assertNamedLockConditionsPostInvocation(1); 199 } 200 201 public void testInstanceMethodWithArgumentsThrowsExceptionWithNamedLocks() throws Exception { 202 String methodName = "instanceMethodWithArgumentsThrowsException"; 203 204 createNamedLockDefinition("testLock"); 205 createLockConfigurationForMethodExpression("void", methodName, "(int, java.lang.String)"); 206 207 assertNoTransactions(); 208 209 invokeWithDefaultArgsAndCheckForProperException(methodName); 210 211 assertNoAutolocks(); 213 assertNamedLockConditionsPostInvocation(1); 214 } 215 216 public void testSynchronizedInstanceMethodWithAutolock() throws Exception { 217 String testMethodName = "synchronizedInstanceMethod"; 218 219 createAutolockLockDefinition(); 220 createLockConfigurationForMethodExpression("public synchronized void", testMethodName, "()"); 221 222 assertNoTransactions(); 223 224 this.testClientObjectManager.setIsManaged(false); 226 invokeWithNoArgs(testMethodName); 227 assertNoTransactions(); 228 229 this.testClientObjectManager.setIsManaged(true); 231 initClassLoader(); 232 233 invokeWithNoArgs(testMethodName); 234 235 assertAutolockConditionsPostInvocation(1); 236 } 237 238 public void testSynchronizedInstanceMethodThrowsExceptionWithAutolock() throws Exception { 239 String testMethodName = "synchronizedInstanceMethodThrowsException"; 240 241 createAutolockLockDefinition(); 242 createLockConfigurationForMethodExpression("public synchronized void", testMethodName, "()"); 243 244 246 assertNoTransactions(); 251 252 this.testClientObjectManager.setIsManaged(false); 254 invokeWithNoArgsAndCheckForProperException(testMethodName); 255 256 assertNoTransactions(); 257 258 this.testClientObjectManager.setIsManaged(true); 260 initClassLoader(); 261 invokeWithNoArgsAndCheckForProperException(testMethodName); 262 263 assertAutolockConditionsPostInvocation(1); 264 } 265 266 public void testSynchronizedInstanceMethodWithArgumentsWithAutolock() throws Exception { 267 String methodName = "synchronizedInstanceMethodWithArguments"; 268 269 createAutolockLockDefinition(); 270 createLockConfigurationForMethodExpression("public synchronized void", methodName, "(int, java.lang.String)"); 271 272 274 276 assertNoTransactions(); 281 282 this.testClientObjectManager.setIsManaged(false); 284 invokeWithDefaultArgs(methodName); 285 assertNoTransactions(); 286 287 this.testClientObjectManager.setIsManaged(true); 289 initClassLoader(); 290 invokeWithDefaultArgs(methodName); 291 292 assertAutolockConditionsPostInvocation(1); 293 } 294 295 298 public void testSynchronizedInstanceMethodWithArgumentsThrowsExceptionWithAutolock() throws Exception { 299 String methodName = "synchronizedInstanceMethodWithArgumentsThrowsException"; 300 301 createAutolockLockDefinition(); 302 createLockConfigurationForMethodExpression("public synchronized void", methodName, "(int, java.lang.String)"); 303 304 308 assertNoTransactions(); 313 314 this.testClientObjectManager.setIsManaged(false); 316 317 invokeWithDefaultArgsAndCheckForProperException(methodName); 318 assertNoTransactions(); 319 320 this.testClientObjectManager.setIsManaged(true); 322 initClassLoader(); 323 invokeWithDefaultArgsAndCheckForProperException(methodName); 324 assertAutolockConditionsPostInvocation(1); 325 } 326 327 330 public void testInternalSynchronizedInstanceMethodWithAutolock() throws Exception { 331 String testMethodName = "internalSynchronizedInstanceMethod"; 332 333 createAutolockLockDefinition(); 334 createLockConfigurationForMethodExpression("public void", testMethodName, "()"); 335 336 assertNoTransactions(); 343 344 this.testClientObjectManager.setIsManaged(false); 346 invokeWithNoArgs(testMethodName); 347 assertNoTransactions(); 348 349 this.testClientObjectManager.setIsManaged(true); 351 initClassLoader(); 352 353 invokeWithNoArgs(testMethodName); 354 355 assertAutolockConditionsPostInvocation(1); 356 } 357 358 public void testInternalSynchronizedInstanceMethodWithNamedlockAndAutoLock() throws Exception { 359 config.addIncludePattern(this.targetClassName); 360 String methodName = "internalSynchronizedInstanceMethod"; 361 String methodExpression = "void " + targetClassName + "." + methodName + "()"; 363 LockDefinition ldnamed = new LockDefinition("test-lock", ConfigLockLevel.WRITE); 364 ldnamed.commit(); 365 LockDefinition ldautolock = new LockDefinition(LockDefinition.TC_AUTOLOCK_NAME, ConfigLockLevel.WRITE); 366 ldautolock.commit(); 367 368 config.getOrCreateSpec(targetClassName); 369 config.addLock(methodExpression, ldautolock); 370 config.addLock(methodExpression, ldnamed); 371 372 this.testClientObjectManager.setIsManaged(true); 373 374 assertNoTransactions(); 375 376 invokeWithNoArgs(methodName); 377 378 assertTrue(checkForLockName(ldnamed.getLockName(), ldnamed.getLockLevelAsInt())); 379 assertTrue(checkForLock(ldnamed)); 380 assertAutolockCount(1); 381 assertTransactionCount(2); 382 assertNoAutolockLiteral(); 383 } 384 385 public void testInternalSynchronizedInstanceMethodThrowsExceptionWithAutolock() throws Exception { 386 String testMethodName = "internalSynchronizedInstanceMethodThrowsException"; 387 388 createAutolockLockDefinition(); 389 createLockConfigurationForMethodExpression("public void", testMethodName, "()"); 390 391 assertNoTransactions(); 400 401 this.testClientObjectManager.setIsManaged(false); 403 invokeWithNoArgsAndCheckForProperException(testMethodName); 404 assertNoTransactions(); 405 406 this.testClientObjectManager.setIsManaged(true); 408 initClassLoader(); 409 410 invokeWithNoArgsAndCheckForProperException(testMethodName); 411 assertAutolockConditionsPostInvocation(1); 412 } 413 414 public void testInternalSynchronizedInstanceMethodWithArgumentsWithAutolock() throws Exception { 415 String testMethodName = "internalSynchronizedInstanceMethodWithArguments"; 416 createAutolockLockDefinition(); 417 createLockConfigurationForMethodExpression("public void", testMethodName, "(int, java.lang.String)"); 418 419 427 this.testClientObjectManager.setIsManaged(false); 429 invokeWithDefaultArgs(testMethodName); 430 assertNoTransactions(); 431 432 this.testClientObjectManager.setIsManaged(true); 434 initClassLoader(); 435 436 invokeWithDefaultArgs(testMethodName); 437 assertAutolockConditionsPostInvocation(1); 438 } 439 440 public void testInternalSynchronizedInstanceMethodWithArgumentsThrowsExceptionWithAutolock() throws Exception { 441 String testMethodName = "internalSynchronizedInstanceMethodWithArgumentsThrowsException"; 442 createAutolockLockDefinition(); 443 createLockConfigurationForMethodExpression("public void", testMethodName, "(int, java.lang.String)"); 444 445 453 this.testClientObjectManager.setIsManaged(false); 454 invokeWithDefaultArgsAndCheckForProperException(testMethodName); 455 assertNoTransactions(); 456 457 this.testClientObjectManager.setIsManaged(true); 458 initClassLoader(); 459 460 invokeWithDefaultArgsAndCheckForProperException(testMethodName); 461 assertAutolockConditionsPostInvocation(1); 462 } 463 464 public void testStaticMethodWithNamedLocks() throws Exception { 465 String methodName = "staticMethod"; 466 467 createNamedLockDefinition("testLock"); 468 createLockConfigurationForMethodExpression("void", methodName, "()"); 469 470 assertNoTransactions(); 471 472 invokeWithNoArgs(methodName); 473 assertAutolockCount(0); 474 assertNamedLockConditionsPostInvocation(1); 475 } 476 477 public void testStaticMethodThrowsExceptionWithNamedLocks() throws Exception { 478 String methodName = "staticMethodThrowsException"; 479 480 createNamedLockDefinition("testLock"); 481 createLockConfigurationForMethodExpression("void", methodName, "()"); 482 483 assertNoTransactions(); 484 invokeWithNoArgsAndCheckForProperException(methodName); 485 486 assertNamedLockConditionsPostInvocation(1); 488 } 489 490 public void testStaticMethodWithArgumentsWithNamedLocks() throws Exception { 491 String methodName = "staticMethodWithArguments"; 492 493 createNamedLockDefinition("testLock"); 494 createLockConfigurationForMethodExpression("void", methodName, "(int, java.lang.String)"); 495 496 assertNoTransactions(); 497 invokeWithDefaultArgs(methodName); 498 assertNamedLockConditionsPostInvocation(1); 499 } 500 501 public void testStaticMethodWithArgumentsThrowsExceptionWithNamedLocks() throws Exception { 502 String methodName = "staticMethodWithArgumentsThrowsException"; 503 504 createNamedLockDefinition("testLock"); 505 createLockConfigurationForMethodExpression("void", methodName, "(int, java.lang.String)"); 506 507 assertNoTransactions(); 508 invokeWithDefaultArgsAndCheckForProperException(methodName); 509 assertNamedLockConditionsPostInvocation(1); 510 } 511 512 public void testSynchronizedStaticMethodWithAutolock() throws Exception { 513 String testMethodName = "synchronizedStaticMethod"; 514 createAutolockLockDefinition(); 515 String modifiersPattern = "*"; 516 String parametersPattern = "(..)"; 517 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 518 519 this.testClientObjectManager.setIsManaged(true); 520 invokeWithNoArgs(testMethodName); 521 522 assertNoTransactions(); 523 assertNoAutolocks(); 524 525 checkWithNamedLockNoArgs(testMethodName, modifiersPattern, parametersPattern); 528 } 529 530 public void testSynchronizedStaticMethodThrowsExceptionWithAutolock() throws Exception { 531 String testMethodName = "synchronizedStaticMethodThrowsException"; 532 createAutolockLockDefinition(); 533 String modifiersPattern = "*"; 534 String parametersPattern = "(..)"; 535 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 536 537 this.testClientObjectManager.setIsManaged(true); 538 invokeWithNoArgsAndCheckForProperException(testMethodName); 539 540 assertNoTransactions(); 541 assertNoAutolocks(); 542 543 checkWithNamedLockNoArgsThrowsException(testMethodName, modifiersPattern, parametersPattern); 546 } 547 548 public void testSynchronizedStaticMethodWithArgumentsWithAutolock() throws Exception { 549 String testMethodName = "synchronizedStaticMethodWithArguments"; 550 createAutolockLockDefinition(); 551 String modifiersPattern = "*"; 552 String parametersPattern = "(..)"; 553 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 554 555 this.testClientObjectManager.setIsManaged(true); 556 invokeWithDefaultArgs(testMethodName); 557 558 assertNoTransactions(); 559 assertNoAutolocks(); 560 561 checkWithNamedLockDefaultArgs(testMethodName, modifiersPattern, parametersPattern); 564 } 565 566 public void testSynchronizedStaticMethodWithArgumentsThrowsExceptionWithAutolock() throws Exception { 567 String testMethodName = "synchronizedStaticMethodWithArgumentsThrowsException"; 568 createAutolockLockDefinition(); 569 String modifiersPattern = "*"; 570 String parametersPattern = "(..)"; 571 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 572 573 this.testClientObjectManager.setIsManaged(true); 574 invokeWithDefaultArgsAndCheckForProperException(testMethodName); 575 576 assertNoTransactions(); 577 assertNoAutolocks(); 578 579 checkWithnamedLockDefaultArgsThrowsException(testMethodName, modifiersPattern, parametersPattern); 580 } 581 582 public void testInternalSynchronizedStaticMethodWithAutolock() throws Exception { 583 String testMethodName = "internalSynchronizedStaticMethod"; 584 createAutolockLockDefinition(); 585 String modifiersPattern = "*"; 586 String parametersPattern = "(..)"; 587 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 588 589 this.testClientObjectManager.setIsManaged(true); 590 invokeWithNoArgs(testMethodName); 591 592 assertAutolockConditionsPostInvocation(1); 593 } 594 595 public void testInternalSynchronizedStaticMethodThrowsExceptionWithAutolock() throws Exception { 596 String testMethodName = "internalSynchronizedStaticMethodThrowsException"; 597 createAutolockLockDefinition(); 598 String modifiersPattern = "*"; 599 String parametersPattern = "(..)"; 600 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 601 602 this.testClientObjectManager.setIsManaged(true); 603 invokeWithNoArgsAndCheckForProperException(testMethodName); 604 605 assertAutolockConditionsPostInvocation(1); 606 } 607 608 public void testInternalSynchronizedStaticMethodWithArgumentsWithAutolock() throws Exception { 609 String testMethodName = "internalSynchronizedStaticMethodWithArguments"; 610 createAutolockLockDefinition(); 611 String modifiersPattern = "*"; 612 String parametersPattern = "(..)"; 613 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 614 615 this.testClientObjectManager.setIsManaged(true); 616 invokeWithDefaultArgs(testMethodName); 617 618 assertAutolockConditionsPostInvocation(1); 619 } 620 621 public void testInternalSynchronizedStaticMethodWithArgumentsThrowsExceptionWithAutolock() throws Exception { 622 String testMethodName = "internalSynchronizedStaticMethodWithArgumentsThrowsException"; 623 createAutolockLockDefinition(); 624 String modifiersPattern = "*"; 625 String parametersPattern = "(..)"; 626 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 627 628 this.testClientObjectManager.setIsManaged(true); 629 invokeWithDefaultArgsAndCheckForProperException(testMethodName); 630 631 assertAutolockConditionsPostInvocation(1); 632 } 633 634 public void testInstanceMethodReturnsAValueWithNamedLocks() throws Exception { 635 636 String methodName = "instanceMethodReturnsAValue"; 637 638 createNamedLockDefinition("testLock"); 639 createLockConfigurationForMethodExpression("*", methodName, "()"); 640 641 assertNoTransactions(); 642 643 invokeWithNoArgs(methodName); 644 645 assertNamedLockConditionsPostInvocation(1); 646 } 647 648 public void testInstanceMethodReturnsAValueThrowsExceptionWithNamedLocks() throws Exception { 649 String methodName = "instanceMethodReturnsAValueThrowsException"; 650 651 createNamedLockDefinition("testLock"); 652 createLockConfigurationForMethodExpression("*", methodName, "()"); 653 654 assertNoTransactions(); 655 656 invokeWithNoArgsAndCheckForProperException(methodName); 657 658 assertNamedLockConditionsPostInvocation(1); 659 } 660 661 public void testInstanceMethodWithArgumentsReturnsAValue() throws Exception { 662 String methodName = "instanceMethodWithArgumentsReturnsAValue"; 663 664 createNamedLockDefinition("testLock"); 665 createLockConfigurationForMethodExpression("*", methodName, "(..)"); 666 667 assertNoTransactions(); 668 669 invokeWithDefaultArgs(methodName); 670 671 assertNamedLockConditionsPostInvocation(1); 672 } 673 674 public void testInstanceMethodWithArgumentsReturnsAValueThrowsException() throws Exception { 675 String methodName = "instanceMethodWithArgumentsReturnsAValueThrowsException"; 676 677 createNamedLockDefinition("testLock"); 678 createLockConfigurationForMethodExpression("*", methodName, "(..)"); 679 680 assertNoTransactions(); 681 682 invokeWithDefaultArgsAndCheckForProperException(methodName); 683 684 assertNamedLockConditionsPostInvocation(1); 685 } 686 687 public void testSynchronizedInstanceMethodReturnsAValueWithAutolock() throws Exception { 688 String methodName = "synchronizedInstanceMethodReturnsAValue"; 689 690 createAutolockLockDefinition(); 691 createLockConfigurationForMethodExpression("String", methodName, "()"); 692 693 this.testClientObjectManager.setIsManaged(false); 694 invokeWithNoArgs(methodName); 695 696 assertNoTransactions(); 697 698 this.testClientObjectManager.setIsManaged(true); 699 initClassLoader(); 700 701 invokeWithNoArgs(methodName); 702 assertAutolockConditionsPostInvocation(1); 703 } 704 705 public void testSynchronizedInstanceMethodReturnsAValueThrowsExceptionWithAutolock() throws Exception { 706 String methodName = "synchronizedInstanceMethodReturnsAValueThrowsException"; 707 708 createAutolockLockDefinition(); 709 createLockConfigurationForMethodExpression("String", methodName, "()"); 710 711 this.testClientObjectManager.setIsManaged(false); 712 invokeWithNoArgsAndCheckForProperException(methodName); 713 714 assertNoTransactions(); 715 716 this.testClientObjectManager.setIsManaged(true); 717 initClassLoader(); 718 719 invokeWithNoArgsAndCheckForProperException(methodName); 720 assertAutolockConditionsPostInvocation(1); 721 } 722 723 public void testSynchronizedInstanceMethodWithArgumentsReturnsAValueWithAutolock() throws Exception { 724 String methodName = "synchronizedInstanceMethodWithArgumentsReturnsAValue"; 725 726 createAutolockLockDefinition(); 727 createLockConfigurationForMethodExpression("String", methodName, "(..)"); 728 729 this.testClientObjectManager.setIsManaged(false); 730 invokeWithDefaultArgs(methodName); 731 732 assertNoTransactions(); 733 734 this.testClientObjectManager.setIsManaged(true); 735 initClassLoader(); 736 737 invokeWithDefaultArgs(methodName); 738 assertAutolockConditionsPostInvocation(1); 739 } 740 741 public void testSynchronizedInstanceMethodWithArgumentsReturnsAValueThrowsExceptionWithAutolock() throws Exception { 742 743 String methodName = "synchronizedInstanceMethodWithArgumentsReturnsAValueThrowsException"; 744 745 createAutolockLockDefinition(); 746 createLockConfigurationForMethodExpression("String", methodName, "(..)"); 747 748 this.testClientObjectManager.setIsManaged(false); 749 invokeWithDefaultArgsAndCheckForProperException(methodName); 750 751 assertNoTransactions(); 752 753 this.testClientObjectManager.setIsManaged(true); 754 initClassLoader(); 755 756 invokeWithDefaultArgsAndCheckForProperException(methodName); 757 758 assertAutolockConditionsPostInvocation(1); 759 } 760 761 public void testInternalSynchronizedInstanceMethodReturnsAValueWithAutolock() throws Exception { 762 763 String methodName = "internalSynchronizedInstanceMethodReturnsAValue"; 764 765 createAutolockLockDefinition(); 766 createLockConfigurationForMethodExpression("String", methodName, "()"); 767 768 this.testClientObjectManager.setIsManaged(false); 769 invokeWithNoArgs(methodName); 770 771 assertNoTransactions(); 772 773 this.testClientObjectManager.setIsManaged(true); 774 initClassLoader(); 775 776 invokeWithNoArgs(methodName); 777 778 assertAutolockConditionsPostInvocation(1); 779 } 780 781 public void testInternalSynchronizedInstanceMethodReturnsAValueThrowsExceptionWithAutolock() throws Exception { 782 String methodName = "internalSynchronizedInstanceMethodReturnsAValueThrowsException"; 783 784 createAutolockLockDefinition(); 785 createLockConfigurationForMethodExpression("String", methodName, "()"); 786 787 this.testClientObjectManager.setIsManaged(false); 788 invokeWithNoArgsAndCheckForProperException(methodName); 789 790 assertNoTransactions(); 791 792 this.testClientObjectManager.setIsManaged(true); 793 initClassLoader(); 794 795 invokeWithNoArgsAndCheckForProperException(methodName); 796 797 assertAutolockConditionsPostInvocation(1); 798 } 799 800 public void testInternalSynchronizedInstanceMethodWithArgumentsReturnsAValueWithAutolock() throws Exception { 801 String methodName = "internalSynchronizedInstanceMethodWithArgumentsReturnsAValue"; 802 803 createAutolockLockDefinition(); 804 createLockConfigurationForMethodExpression("String", methodName, "(..)"); 805 806 this.testClientObjectManager.setIsManaged(false); 807 invokeWithDefaultArgs(methodName); 808 809 assertNoTransactions(); 810 811 this.testClientObjectManager.setIsManaged(true); 812 initClassLoader(); 813 814 invokeWithDefaultArgs(methodName); 815 816 assertAutolockConditionsPostInvocation(1); 817 } 818 819 public void testInternalSynchronizedInstanceMethodWithArgumentsReturnsAValueThrowsExceptionWithAutolock() 820 throws Exception { 821 String methodName = "internalSynchronizedInstanceMethodWithArgumentsReturnsAValueThrowsException"; 822 823 createAutolockLockDefinition(); 824 createLockConfigurationForMethodExpression("String", methodName, "(..)"); 825 826 this.testClientObjectManager.setIsManaged(false); 827 invokeWithDefaultArgsAndCheckForProperException(methodName); 828 829 assertNoTransactions(); 830 831 this.testClientObjectManager.setIsManaged(true); 832 initClassLoader(); 833 834 invokeWithDefaultArgsAndCheckForProperException(methodName); 835 836 assertAutolockConditionsPostInvocation(1); 837 } 838 839 public void testStaticMethodReturnsAValueWithNamedLocks() throws Exception { 840 String methodName = "staticMethodReturnsAValue"; 841 createNamedLockDefinition("testLock"); 842 createLockConfigurationForMethodExpression("*", methodName, "()"); 843 844 assertNoTransactions(); 845 846 invokeWithNoArgs(methodName); 847 848 assertNamedLockConditionsPostInvocation(1); 849 } 850 851 public void testStaticMethodReturnsAValueThrowsExceptionWithNamedLocks() throws Exception { 852 String methodName = "staticMethodReturnsAValueThrowsException"; 853 854 createNamedLockDefinition("testLock"); 855 createLockConfigurationForMethodExpression("*", methodName, "()"); 856 857 assertNoTransactions(); 858 859 invokeWithNoArgsAndCheckForProperException(methodName); 860 861 assertNamedLockConditionsPostInvocation(1); 862 } 863 864 public void testStaticMethodWithArgumentsReturnsAValueWithNamedLocks() throws Exception { 865 String methodName = "staticMethodWithArgumentsReturnsAValue"; 866 createNamedLockDefinition("testLock"); 867 createLockConfigurationForMethodExpression("*", methodName, "(..)"); 868 869 assertNoTransactions(); 870 871 invokeWithDefaultArgs(methodName); 872 873 assertNamedLockConditionsPostInvocation(1); 874 } 875 876 public void testStaticMethodWithArgumentsReturnsAValueThrowsExceptionWithNamedLocks() throws Exception { 877 878 String methodName = "staticMethodWithArgumentsReturnsAValueThrowsException"; 879 createNamedLockDefinition("testLock"); 880 createLockConfigurationForMethodExpression("*", methodName, "(..)"); 881 882 assertNoTransactions(); 883 884 invokeWithDefaultArgsAndCheckForProperException(methodName); 885 886 assertNamedLockConditionsPostInvocation(1); 887 } 888 889 893 public void testSynchronizedStaticMethodReturnsAValueWithAutolocks() throws Exception { 894 String testMethodName = "synchronizedStaticMethodReturnsAValue"; 895 createAutolockLockDefinition(); 896 String modifiersPattern = "*"; 897 String parametersPattern = "(..)"; 898 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 899 900 this.testClientObjectManager.setIsManaged(true); 901 invokeWithNoArgs(testMethodName); 902 903 assertNoTransactions(); 904 assertNoAutolocks(); 905 906 checkWithNamedLockNoArgs(testMethodName, modifiersPattern, parametersPattern); 909 } 910 911 public void testSynchronizedStaticMethodReturnsAValueThrowsExceptionWithAutolocks() throws Exception { 912 String testMethodName = "synchronizedStaticMethodReturnsAValueThrowsException"; 913 createAutolockLockDefinition(); 914 String modifiersPattern = "*"; 915 String parametersPattern = "(..)"; 916 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 917 918 this.testClientObjectManager.setIsManaged(true); 919 invokeWithNoArgsAndCheckForProperException(testMethodName); 920 921 assertNoTransactions(); 922 assertNoAutolocks(); 923 924 checkWithNamedLockNoArgsThrowsException(testMethodName, modifiersPattern, parametersPattern); 927 } 928 929 public void testSynchronizedStaticMethodWithArgumentsReturnsAValueWithAutolocks() throws Exception { 930 String testMethodName = "synchronizedStaticMethodWithArgumentsReturnsAValue"; 931 createAutolockLockDefinition(); 932 String modifiersPattern = "*"; 933 String parametersPattern = "(..)"; 934 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 935 936 this.testClientObjectManager.setIsManaged(true); 937 invokeWithDefaultArgs(testMethodName); 938 939 assertNoTransactions(); 940 assertNoAutolocks(); 941 942 checkWithNamedLockDefaultArgs(testMethodName, modifiersPattern, parametersPattern); 945 } 946 947 public void testSynchronizedStaticMethodWithArgumentsReturnsAValueThrowsExceptionWithAutolocks() throws Exception { 948 String testMethodName = "synchronizedStaticMethodWithArgumentsReturnsAValueThrowsException"; 949 createAutolockLockDefinition(); 950 String modifiersPattern = "*"; 951 String parametersPattern = "(..)"; 952 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 953 954 this.testClientObjectManager.setIsManaged(true); 955 invokeWithDefaultArgsAndCheckForProperException(testMethodName); 956 957 assertNoTransactions(); 958 assertNoAutolocks(); 959 960 checkWithnamedLockDefaultArgsThrowsException(testMethodName, modifiersPattern, parametersPattern); 961 } 962 963 966 public void testInternalSynchronizedStaticMethodReturnsAValueWithAutolocks() throws Exception { 967 968 String testMethodName = "internalSynchronizedStaticMethodReturnsAValue"; 969 createAutolockLockDefinition(); 970 String modifiersPattern = "*"; 971 String parametersPattern = "(..)"; 972 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 973 974 this.testClientObjectManager.setIsManaged(true); 975 invokeWithNoArgs(testMethodName); 976 977 assertAutolockConditionsPostInvocation(1); 978 } 979 980 public void testInternalSynchronizedStaticMethodReturnsAValueThrowsExceptionWithAutolocks() throws Exception { 981 982 String testMethodName = "internalSynchronizedStaticMethodReturnsAValueThrowsException"; 983 createAutolockLockDefinition(); 984 String modifiersPattern = "*"; 985 String parametersPattern = "(..)"; 986 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 987 988 this.testClientObjectManager.setIsManaged(true); 989 invokeWithNoArgsAndCheckForProperException(testMethodName); 990 991 assertAutolockConditionsPostInvocation(1); 992 } 993 994 public void testInternalSynchronizedStaticMethodWithArgumentsReturnsAValueWithAutolocks() throws Exception { 995 996 String testMethodName = "internalSynchronizedStaticMethodWithArgumentsReturnsAValue"; 997 createAutolockLockDefinition(); 998 String modifiersPattern = "*"; 999 String parametersPattern = "(..)"; 1000 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 1001 1002 this.testClientObjectManager.setIsManaged(true); 1003 invokeWithDefaultArgs(testMethodName); 1004 1005 assertAutolockConditionsPostInvocation(1); 1006 } 1007 1008 public void testInternalSynchronizedStaticMethodWithArgumentsReturnsAValueThrowsExceptionWithAutolocks() 1009 throws Exception { 1010 1011 String testMethodName = "internalSynchronizedStaticMethodWithArgumentsReturnsAValueThrowsException"; 1012 createAutolockLockDefinition(); 1013 String modifiersPattern = "*"; 1014 String parametersPattern = "(..)"; 1015 createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern); 1016 1017 this.testClientObjectManager.setIsManaged(true); 1018 invokeWithDefaultArgsAndCheckForProperException(testMethodName); 1019 1020 assertAutolockConditionsPostInvocation(1); 1021 } 1022 1023 public void testInstanceMethodWithAutolockAndNoSynchronizersDoesNothing() throws Exception { 1024 String methodName = "instanceMethod"; 1025 1026 createAutolockLockDefinition(); 1027 String modifiersPattern = "void"; 1028 String parametersPattern = "()"; 1029 createLockConfigurationForMethodExpression(modifiersPattern, methodName, parametersPattern); 1030 1031 this.testClientObjectManager.setIsManaged(true); 1032 invokeWithNoArgs(methodName); 1033 assertNoTransactions(); 1034 1035 checkWithNamedLockNoArgs(methodName, modifiersPattern, parametersPattern); 1039 } 1040 1041 public void testWildcardAutolock() throws Exception { 1042 1043 String testMethodName = "internalSynchronizedInstanceMethod"; 1044 1045 createAutolockLockDefinition(); 1046 createLockConfigurationForMethodExpression("*", "*", "(..)"); 1047 assertNoTransactions(); 1055 1056 this.testClientObjectManager.setIsManaged(false); 1058 invokeWithNoArgs(testMethodName); 1059 assertNoTransactions(); 1060 1061 this.testClientObjectManager.setIsManaged(true); 1063 initClassLoader(); 1064 1065 invokeWithNoArgs(testMethodName); 1066 1067 assertAutolockConditionsPostInvocation(1); 1068 } 1069 1070 public void testReadLock() throws Exception { 1071 config.addIncludePattern(this.targetClassName); 1072 String methodName = "instanceMethod"; 1073 1074 this.lockDefinition = new LockDefinition("testReadLock", ConfigLockLevel.READ); 1075 this.lockDefinition.commit(); 1076 1077 createLockConfigurationForMethodExpression("void", methodName, "()"); 1078 1079 assertNoTransactions(); 1080 1081 invokeWithNoArgs(methodName); 1082 assertNamedLockConditionsPostInvocation(1); 1083 } 1084 1085 public void testWithoutLocks() throws Exception { 1086 String methodName = "internalSynchronizedInstanceMethod"; 1087 1088 invokeWithNoArgs(methodName); 1089 1090 assertTransactionCount(0); 1091 } 1092 1093 public void testAutolockCtorNoException() throws Exception { 1094 String methodExpression = "* " + targetClassName + ".*(..)"; 1095 LockDefinition ld = new LockDefinition(LockDefinition.TC_AUTOLOCK_NAME, ConfigLockLevel.WRITE); 1096 ld.commit(); 1097 1098 config.getOrCreateSpec(targetClassName); 1099 config.addLock(methodExpression, ld); 1100 1101 this.testClientObjectManager.setIsManaged(true); 1102 1103 assertNoTransactions(); 1104 1105 System.setProperty(ClassAdapterTestTarget.KEY, ClassAdapterTestTarget.CSTR_AUTOLOCK_NO_EXCEPTION); 1106 1107 callNoArgCtor(); 1108 assertTransactionCount(1); 1109 assertAutolockCount(1); 1110 } 1111 1112 public void testNamedlockCtorNoException() throws Exception { 1113 String methodExpression = "* " + targetClassName + ".*(..)"; 1114 LockDefinition ld = new LockDefinition("test-lock", ConfigLockLevel.WRITE); 1115 ld.commit(); 1116 1117 config.getOrCreateSpec(targetClassName); 1118 config.addLock(methodExpression, ld); 1119 1120 assertNoTransactions(); 1121 1122 callNoArgCtor(); 1123 assertTransactionCount(1); 1124 assertAutolockCount(0); 1125 } 1126 1127 public void testNamedlockCtorThrowsException() throws Exception { 1128 String methodExpression = "* " + targetClassName + ".*(..)"; 1129 LockDefinition ld = new LockDefinition("test-lock", ConfigLockLevel.WRITE); 1130 ld.commit(); 1131 1132 config.getOrCreateSpec(targetClassName); 1133 config.addLock(methodExpression, ld); 1134 1135 assertNoTransactions(); 1136 1137 System.setProperty(ClassAdapterTestTarget.KEY, ClassAdapterTestTarget.CSTR_THROW_EXCEPTION); 1138 1139 try { 1140 callNoArgCtor(); 1141 throw new AssertionError (); 1142 } catch (RuntimeException re) { 1143 if ((re.getClass() != RuntimeException .class) 1144 || !re.getMessage().equals(ClassAdapterTestTarget.CSTR_THROW_EXCEPTION)) { throw re; } 1145 } 1146 1147 assertTransactionCount(1); 1148 assertAutolockCount(0); 1149 } 1150 1151 private void testAutolockCtorException(boolean inside) throws Exception { 1152 String methodExpression = "* " + targetClassName + ".*(..)"; 1153 LockDefinition ld = new LockDefinition(LockDefinition.TC_AUTOLOCK_NAME, ConfigLockLevel.WRITE); 1154 ld.commit(); 1155 1156 config.getOrCreateSpec(targetClassName); 1157 config.addLock(methodExpression, ld); 1158 1159 this.testClientObjectManager.setIsManaged(true); 1160 1161 assertNoTransactions(); 1162 1163 String cmd = inside ? ClassAdapterTestTarget.CSTR_AUTOLOCK_THROW_EXCEPTION_INSIDE 1164 : ClassAdapterTestTarget.CSTR_THROW_EXCEPTION; 1165 1166 System.setProperty(ClassAdapterTestTarget.KEY, cmd); 1167 1168 try { 1169 callNoArgCtor(); 1170 throw new AssertionError (); 1171 } catch (RuntimeException re) { 1172 if ((re.getClass() != RuntimeException .class) || !re.getMessage().equals(cmd)) { throw re; } 1173 } 1174 1175 int numTxn = inside ? 1 : 0; 1176 1177 assertTransactionCount(numTxn); 1178 assertAutolockCount(numTxn); 1179 } 1180 1181 public void testAutolockCtorExceptionOutsideSynch() throws Exception { 1182 testAutolockCtorException(false); 1183 } 1184 1185 public void testAutolockCtorExceptionInsideSynch() throws Exception { 1186 testAutolockCtorException(true); 1187 } 1188 1189 public void testNestedAutolocks() throws Exception { 1190 String methodName = "nestedInternalSynchronizedInstanceMethod"; 1191 1192 createAutolockLockDefinition(); 1193 createLockConfigurationForMethodExpression("*", "*", "(..)"); 1194 1195 this.testClientObjectManager.setIsManaged(true); 1196 assertNoTransactions(); 1197 1198 Object result = invokeWithNoArgs(methodName); 1199 int expectedTransactionCount = ((Integer ) result).intValue(); 1200 assertAutolockConditionsPostInvocation(expectedTransactionCount); 1201 } 1202 1203 public void testMultipleNamedLocks() throws Exception { 1204 config.addIncludePattern(this.targetClassName); 1205 String methodName = "instanceMethod"; 1206 String methodExpression = "* " + targetClassName + "." + methodName + "(..)"; 1207 LockDefinition ld1 = new LockDefinition("lock1", ConfigLockLevel.WRITE); 1208 LockDefinition ld2 = new LockDefinition("lock2", ConfigLockLevel.WRITE); 1209 ld1.commit(); 1210 ld2.commit(); 1211 LockDefinition[] declaredLockDefs = new LockDefinition[] { ld1, ld2 }; 1212 1213 config.getOrCreateSpec(targetClassName); 1214 config.addLock(methodExpression, ld1); 1215 config.addLock(methodExpression, ld2); 1216 this.testClientObjectManager.setIsManaged(true); 1217 1218 assertNoTransactions(); 1219 1220 invokeWithNoArgs(methodName); 1221 1222 assertTransactionCount(2); 1223 assertTrue(checkForLocks(declaredLockDefs)); 1224 } 1225 1226 public void testAutolockAndNamedLock() throws Exception { 1227 config.addIncludePattern(this.targetClassName); 1228 String methodName = "internalSynchronizedInstanceMethod"; 1229 String methodExpression = "void " + targetClassName + "." + methodName + "()"; 1231 LockDefinition ldnamed = new LockDefinition("test-lock", ConfigLockLevel.WRITE); 1232 ldnamed.commit(); 1233 LockDefinition ldautolock = new LockDefinition(LockDefinition.TC_AUTOLOCK_NAME, ConfigLockLevel.WRITE); 1234 ldautolock.commit(); 1235 1236 config.getOrCreateSpec(targetClassName); 1237 config.addLock(methodExpression, ldnamed); 1239 config.addLock(methodExpression, ldautolock); 1240 1241 this.testClientObjectManager.setIsManaged(true); 1242 1243 assertNoTransactions(); 1244 1245 invokeWithNoArgs(methodName); 1246 1247 assertTrue(checkForLockName(ldnamed.getLockName(), ldnamed.getLockLevelAsInt())); 1248 assertTrue(checkForLock(ldnamed)); 1249 assertAutolockCount(1); 1250 assertTransactionCount(2); 1251 assertNoAutolockLiteral(); 1252 } 1253 1254 public void testSynchronizedInstanceMethodWithAutolockAndNamedLock() throws Exception { 1255 config.addIncludePattern(this.targetClassName); 1256 String methodName = "synchronizedInstanceMethod"; 1257 String methodExpression = "void " + targetClassName + "." + methodName + "()"; 1259 LockDefinition ldnamed = new LockDefinition("test-lock", ConfigLockLevel.WRITE); 1260 ldnamed.commit(); 1261 LockDefinition ldautolock = new LockDefinition(LockDefinition.TC_AUTOLOCK_NAME, ConfigLockLevel.WRITE); 1262 ldautolock.commit(); 1263 1264 config.getOrCreateSpec(targetClassName); 1265 config.addLock(methodExpression, ldautolock); 1266 config.addLock(methodExpression, ldnamed); 1267 1268 this.testClientObjectManager.setIsManaged(true); 1269 1270 assertNoTransactions(); 1271 1272 invokeWithNoArgs(methodName); 1273 1274 assertTrue(checkForLockName(ldnamed.getLockName(), ldnamed.getLockLevelAsInt())); 1275 assertTrue(checkForLock(ldnamed)); 1276 assertAutolockCount(1); 1277 assertTransactionCount(2); 1278 assertNoAutolockLiteral(); 1279 } 1280 1281 public void testSerializationFields() throws Exception { 1282 config.getOrCreateSpec(targetClassName); 1283 Class notAdapted = Class.forName(targetClassName); 1284 Class adapted = classLoader.loadClass(targetClassName); 1285 assertNotSame(notAdapted, adapted); 1286 1287 Field f = notAdapted.getDeclaredField("serialVersionUID"); 1288 int mods = f.getModifiers(); 1289 assertTrue(Modifier.isPrivate(mods)); 1290 assertTrue(Modifier.isStatic(mods)); 1291 assertTrue(Modifier.isFinal(mods)); 1292 assertTrue(f.getType().equals(Long.TYPE)); 1293 1294 f = adapted.getDeclaredField("serialVersionUID"); 1295 mods = f.getModifiers(); 1296 assertTrue(Modifier.isPrivate(mods)); 1297 assertTrue(Modifier.isStatic(mods)); 1298 assertTrue(Modifier.isFinal(mods)); 1299 assertTrue(f.getType().equals(Long.TYPE)); 1300 1301 f = notAdapted.getDeclaredField("serialPersistentFields"); 1302 mods = f.getModifiers(); 1303 assertTrue(Modifier.isPrivate(mods)); 1304 assertTrue(Modifier.isStatic(mods)); 1305 assertTrue(Modifier.isFinal(mods)); 1306 assertTrue(f.getType().equals(ObjectStreamField [].class)); 1307 1308 f = adapted.getDeclaredField("serialPersistentFields"); 1309 mods = f.getModifiers(); 1310 assertTrue(Modifier.isPrivate(mods)); 1311 assertTrue(Modifier.isStatic(mods)); 1312 assertTrue(Modifier.isFinal(mods)); 1313 assertTrue(f.getType().equals(ObjectStreamField [].class)); 1314 } 1315 1316 private Object invokeWithDefaultArgs(String methodName) throws Exception { 1317 return invokeWithArgs(methodName, WITH_ARGS_PARAMS, WITH_ARGS_ARGS); 1318 } 1319 1320 private Object invokeWithArgs(String methodName, Class [] ptypes, Object [] args) throws Exception { 1321 Class c = classLoader.loadClass(targetClassName); 1322 Object instance = c.newInstance(); 1323 1324 if (instance instanceof Manageable) { 1325 if (testClientObjectManager.isManaged(instance)) { 1326 ((Manageable) instance).__tc_managed(testClientObjectManager.lookupOrCreate(instance)); 1327 } 1328 } 1329 1330 boolean failOnException = false; 1331 return invokeMethod(c, instance, methodName, ptypes, args, failOnException); 1332 } 1333 1334 private void invokeWithDefaultArgsAndCheckForProperException(String methodName) throws Exception { 1335 invokeWithArgsAndCheckForProperException(methodName, WITH_ARGS_PARAMS, WITH_ARGS_ARGS); 1336 } 1337 1338 private void invokeWithArgsAndCheckForProperException(String methodName, Class [] ptypes, Object [] args) 1339 throws Exception { 1340 try { 1341 invokeWithArgs(methodName, ptypes, args); 1342 fail("Should have thrown an exception"); 1343 } catch (InvocationTargetException e) { 1344 assertExceptionType(e); 1346 } 1347 } 1348 1349 private Object invokeWithNoArgs(String methodName) throws Exception { 1350 return invokeWithArgs(methodName, new Class [] {}, new Object [] {}); 1351 } 1352 1353 private void invokeWithNoArgsAndCheckForProperException(String methodName) throws Exception { 1354 try { 1355 invokeWithNoArgs(methodName); 1356 fail("Should have thrown an exception."); 1357 } catch (InvocationTargetException e) { 1358 assertExceptionType(e); 1360 } 1361 } 1362 1363 1366 private void checkWithNamedLock(String methodName, boolean withArgs, boolean checkForException, 1367 String modifiersPattern, String parametersPattern) throws Exception { 1368 initializeConfig(); 1369 createNamedLockDefinition("testLock"); 1370 createLockConfigurationForMethodExpression(modifiersPattern, methodName, parametersPattern); 1371 1372 initClassLoader(); 1373 1374 assertNoTransactions(); 1375 if (withArgs) { 1376 if (checkForException) { 1377 invokeWithDefaultArgsAndCheckForProperException(methodName); 1378 } else { 1379 invokeWithDefaultArgs(methodName); 1380 } 1381 } else { 1382 if (checkForException) { 1383 invokeWithNoArgsAndCheckForProperException(methodName); 1384 } else { 1385 invokeWithNoArgs(methodName); 1386 } 1387 } 1388 assertNoAutolocks(); 1389 assertNamedLockConditionsPostInvocation(1); 1390 } 1391 1392 private void checkWithNamedLockNoArgs(String methodName, String modifiersPattern, String parametersPattern) 1393 throws Exception { 1394 checkWithNamedLock(methodName, false, false, modifiersPattern, parametersPattern); 1395 } 1396 1397 private void checkWithNamedLockDefaultArgs(String methodName, String modifiersPattern, String parametersPattern) 1398 throws Exception { 1399 checkWithNamedLock(methodName, true, false, modifiersPattern, parametersPattern); 1400 } 1401 1402 private void checkWithNamedLockNoArgsThrowsException(String methodName, String modifiersPattern, 1403 String parametersPattern) throws Exception { 1404 checkWithNamedLock(methodName, false, true, modifiersPattern, parametersPattern); 1405 } 1406 1407 private void checkWithnamedLockDefaultArgsThrowsException(String methodName, String modifiersPattern, 1408 String parametersPattern) throws Exception { 1409 checkWithNamedLock(methodName, true, true, modifiersPattern, parametersPattern); 1410 } 1411 1412 private void assertAutolockConditionsPostInvocation(int expectedTransactionCount) { 1413 assertTrue("Transaction count " + getTransactionCount() + " should be greater than or equal to" 1414 + " expected autolocks: " + expectedTransactionCount, getTransactionCount() >= expectedTransactionCount); 1415 assertAutolockCount(expectedTransactionCount); 1416 assertTransactionCount(expectedTransactionCount); 1417 assertNoAutolockLiteral(); 1418 } 1419 1420 private void assertNamedLockConditionsPostInvocation(int expectedTransactionCount) { 1421 assertNamedLockConditionsPostInvocation(expectedTransactionCount, new LockDefinition[] { this.lockDefinition }); 1422 } 1423 1424 private void assertNamedLockConditionsPostInvocation(int expectedTransactionCount, LockDefinition[] lockDefs) { 1425 for (int i = 0; i < lockDefs.length; i++) { 1426 assertTrue(checkForLockName(lockDefs[i].getLockName(), lockDefs[i].getLockLevelAsInt())); 1427 assertTrue(checkForLock(lockDefs[i])); 1428 } 1429 assertTransactionCount(expectedTransactionCount); 1430 } 1431 1432 private void assertExceptionType(InvocationTargetException e) { 1433 Throwable target = e.getTargetException(); 1434 assertEquals(LockTestThrowsExceptionException.class.getName(), target.getClass().getName()); 1437 } 1438 1439 private void assertTransactionCount(int transactionCount) { 1440 assertEquals(transactionCount, getTransactionCount()); 1441 assertEquals(transactionCount, testTransactionManager.getCommitCount()); 1442 } 1443 1444 private int getTransactionCount() { 1445 return testTransactionManager.getBegins().size(); 1446 } 1447 1448 private void assertAutolockCount(int autolockCount) { 1449 assertEquals(autolockCount, getAutolockBeginCount()); 1450 } 1451 1452 private void assertNoTransactions() { 1453 assertTransactionCount(0); 1454 } 1455 1456 private void assertNoAutolocks() { 1457 assertAutolockCount(0); 1458 assertNoAutolockLiteral(); 1459 } 1460 1461 private int getAutolockBeginCount() { 1462 int rv = 0; 1463 for (Iterator i = testTransactionManager.getBegins().iterator(); i.hasNext();) { 1464 Begin b = (Begin) i.next(); 1465 if (b.lockName.startsWith("@")) { 1467 rv++; 1468 } 1469 } 1470 return rv; 1471 } 1472 1473 private boolean checkForLocks(LockDefinition[] lockDefs) { 1474 for (int i = 0; i < lockDefs.length; i++) { 1475 if (!checkForLock(lockDefs[i])) return false; 1476 } 1477 return true; 1478 } 1479 1480 private boolean checkForLock(LockDefinition lockdef) { 1481 return checkForLock(lockdef, this.testTransactionManager.getBegins()); 1482 } 1483 1484 1488 private boolean checkForLock(LockDefinition lockdef, List beginTransactions) { 1489 boolean rv = false; 1490 for (Iterator iter = beginTransactions.iterator(); iter.hasNext();) { 1491 rv = checkForLock(lockdef, (Begin) iter.next()); 1492 if (rv) break; 1493 } 1494 return rv; 1495 } 1496 1497 1500 private boolean checkForLock(LockDefinition lockdef, Begin lock) { 1501 boolean rv = false; 1502 if (lock != null) { 1503 rv = lockdef.getLockName().equals(ByteCodeUtil.stripGeneratedLockHeader(lock.lockName)); 1504 if (rv) { 1505 rv = checkLockType(lockdef, lock.lockType); 1507 } 1508 } 1509 return rv; 1510 } 1511 1512 private boolean checkForLockName(String lockName, int lockType) { 1513 List begins = this.testTransactionManager.getBegins(); 1514 for (Iterator i = begins.iterator(); i.hasNext();) { 1515 Begin lock = (Begin) i.next(); 1516 if (checkForLockName(lockName, lock)) return true; 1517 } 1518 return false; 1519 } 1520 1521 private static boolean checkForLockName(String lockName, Begin lock) { 1522 return ByteCodeUtil.stripGeneratedLockHeader(lock.lockName).equals(lockName); 1523 } 1524 1525 1528 private boolean checkLockType(LockDefinition lockdef, int lockType) { 1529 return (lockdef.getLockLevelAsInt() == lockType); 1530 } 1531 1532 private void assertNoAutolockLiteral() { 1533 assertFalse(checkForLockName(LockDefinition.TC_AUTOLOCK_NAME, com.tc.object.lockmanager.api.LockLevel.WRITE)); 1534 } 1535 1536 private void createNamedLockDefinition(String lockName) { 1537 this.config.addIncludePattern(this.targetClassName); 1538 this.lockDefinition = new LockDefinition(lockName, ConfigLockLevel.WRITE); 1539 this.lockDefinition.commit(); 1540 } 1541 1542 private void createLockConfigurationForMethodExpression(String modifiersPattern, String testMethodName, 1543 String parameterPattern) { 1544 String methodExpression = modifiersPattern + " " + targetClassName + "." + testMethodName + parameterPattern; 1545 1546 config.getOrCreateSpec(targetClassName); 1547 config.addLock(methodExpression, lockDefinition); 1548 } 1549 1550 private Object callNoArgCtor() throws Exception { 1551 assertNoTransactions(); 1553 1554 Class c = Class.forName(targetClassName, true, this.classLoader); 1556 1557 this.testTransactionManager.clearBegins(); 1559 this.testTransactionManager.clearCommitCount(); 1560 1561 Constructor ctor = c.getConstructor(new Class [] {}); 1562 try { 1563 return ctor.newInstance(new Object [] {}); 1564 } catch (InvocationTargetException ite) { 1565 Throwable t = ite.getTargetException(); 1566 if (t instanceof RuntimeException ) { throw (RuntimeException ) t; } 1567 if (t instanceof Error ) { throw (Error ) t; } 1568 throw new RuntimeException (t); 1569 } 1570 } 1571 1572} 1573 | Popular Tags |