0001: /*
0002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
0003: * notice. All rights reserved.
0004: */
0005: package com.tc.object.bytecode;
0006:
0007: import com.tc.object.TestClientObjectManager;
0008: import com.tc.object.config.ConfigLockLevel;
0009: import com.tc.object.config.DSOClientConfigHelper;
0010: import com.tc.object.config.LockDefinition;
0011: import com.tc.object.config.LockDefinitionImpl;
0012: import com.tc.object.config.TransparencyClassSpec;
0013: import com.tc.object.loaders.IsolationClassLoader;
0014: import com.tc.object.tx.MockTransactionManager;
0015: import com.tc.object.tx.MockTransactionManager.Begin;
0016: import com.tctest.ClassAdapterTestTarget;
0017: import com.tctest.ClassAdapterTestTargetBase;
0018: import com.tctest.ClassAdapterTestTargetBaseBase;
0019: import com.tctest.LockTestThrowsExceptionException;
0020:
0021: import java.io.ObjectStreamField;
0022: import java.lang.reflect.Constructor;
0023: import java.lang.reflect.Field;
0024: import java.lang.reflect.InvocationTargetException;
0025: import java.lang.reflect.Method;
0026: import java.lang.reflect.Modifier;
0027: import java.util.Iterator;
0028: import java.util.List;
0029:
0030: import javax.swing.DefaultListModel;
0031:
0032: /**
0033: * Test to see if an adapted class has all of the adaptations we expect.
0034: */
0035: public class ClassAdapterTest extends ClassAdapterTestBase {
0036: private static final Class[] WITH_ARGS_PARAMS = new Class[] {
0037: Integer.TYPE, String.class };
0038: private static final Object[] WITH_ARGS_ARGS = new Object[] {
0039: new Integer(1), "test string" };
0040:
0041: private DSOClientConfigHelper config;
0042: private LockDefinition lockDefinition;
0043: private IsolationClassLoader classLoader;
0044: private TestClientObjectManager testClientObjectManager;
0045: private MockTransactionManager testTransactionManager;
0046: private String targetClassName = ClassAdapterTestTarget.class
0047: .getName(); // "com.tctest.ClassAdapterTestTarget";
0048: private ClassLoader origThreadContextClassLoader;
0049:
0050: protected void setUp() throws Exception {
0051: System.getProperties().remove(ClassAdapterTestTarget.KEY);
0052: initializeConfig();
0053: this .testClientObjectManager = new TestClientObjectManager();
0054: this .testTransactionManager = new MockTransactionManager();
0055: initClassLoader();
0056: this .origThreadContextClassLoader = Thread.currentThread()
0057: .getContextClassLoader();
0058: Thread.currentThread().setContextClassLoader(this .classLoader);
0059: }
0060:
0061: private void initializeConfig() throws Exception {
0062: this .config = createClientConfigHelper();
0063: }
0064:
0065: private void createAutolockLockDefinition() {
0066: this .config.addIncludePattern(this .targetClassName);
0067: this .lockDefinition = new LockDefinitionImpl(
0068: LockDefinition.TC_AUTOLOCK_NAME, ConfigLockLevel.WRITE);
0069: this .lockDefinition.commit();
0070: }
0071:
0072: private void initClassLoader() {
0073: boolean isManaged = testClientObjectManager.getIsManaged();
0074: testClientObjectManager.setIsManaged(false);
0075:
0076: try {
0077: this .classLoader = new IsolationClassLoader(config,
0078: testClientObjectManager, testTransactionManager);
0079: this .classLoader.init();
0080: } finally {
0081: testClientObjectManager.setIsManaged(isManaged);
0082: }
0083: }
0084:
0085: private void setTargetClientObjectManager(Class clazz)
0086: throws Exception {
0087: Method m = clazz.getDeclaredMethod(
0088: "setTestClientObjectManager",
0089: new Class[] { testClientObjectManager.getClass() });
0090: m.invoke(null, new Object[] { testClientObjectManager });
0091: }
0092:
0093: protected void tearDown() throws Exception {
0094: super .tearDown();
0095: this .config = null;
0096: this .lockDefinition = null;
0097: this .testClientObjectManager = null;
0098: this .testTransactionManager = null;
0099:
0100: this .classLoader = null;
0101: Thread.currentThread().setContextClassLoader(
0102: this .origThreadContextClassLoader);
0103: }
0104:
0105: public void testNamedLockInInstanceManagedConstructor()
0106: throws Exception {
0107: String tcn = DefaultListModel.class.getName();
0108: config.addIncludePattern(tcn);
0109: String methodPattern = "* " + tcn + ".*(..)";
0110: lockDefinition = new LockDefinitionImpl("doStuff",
0111: ConfigLockLevel.WRITE);
0112: lockDefinition.commit();
0113: config.addLock(methodPattern, lockDefinition);
0114:
0115: Class clazz = this .classLoader.loadClass(tcn);
0116: clazz.newInstance();
0117:
0118: }
0119:
0120: public void testSuperclassTransients() throws Exception {
0121: String super super class = ClassAdapterTestTargetBaseBase.class
0122: .getName();
0123: TransparencyClassSpec spec = config
0124: .getOrCreateSpec(super super class);
0125: spec.addTransient("myString");
0126:
0127: String super class = ClassAdapterTestTargetBase.class.getName();
0128: spec = config.getOrCreateSpec(super class);
0129:
0130: spec = config.getOrCreateSpec(targetClassName);
0131:
0132: // this.config.addAutolock("public void " + targetClassName + ".doStuff()");
0133: String methodPattern = "public void " + targetClassName
0134: + ".doStuff()";
0135: lockDefinition = new LockDefinitionImpl("doStuff",
0136: ConfigLockLevel.WRITE);
0137: lockDefinition.commit();
0138: config.addLock(methodPattern, lockDefinition);
0139:
0140: Class clazz = this .classLoader.loadClass(targetClassName);
0141: Object o = clazz.newInstance();
0142: Method m = clazz.getDeclaredMethod("doStuff", new Class[0]);
0143: m.invoke(o, new Object[0]);
0144: }
0145:
0146: public void testAssertions() throws Exception {
0147: // Make sure that a bogus lock doesn't pass the test...
0148: LockDefinition bogus = new LockDefinitionImpl("fakeLock",
0149: ConfigLockLevel.WRITE);
0150: bogus.commit();
0151: assertFalse(checkForLock(bogus));
0152: assertNoTransactions();
0153: assertTransactionCount(0);
0154: }
0155:
0156: public void testWildcardPatternWithNamedLocksAdaptsOK()
0157: throws Exception {
0158: createNamedLockDefinition("test");
0159: createLockConfigurationForMethodExpression("*", "*", "(..)");
0160: callNoArgCtor();
0161: assertTransactionCount(1);
0162: }
0163:
0164: public void testWildcardPatternWithAutolockAdaptsOK()
0165: throws Exception {
0166: createAutolockLockDefinition();
0167: createLockConfigurationForMethodExpression("* ", "*", "(..)");
0168:
0169: callNoArgCtor();
0170: }
0171:
0172: public void testSynchronizedInstanceMethodWithWideArgs()
0173: throws Exception {
0174: String methodName = "synchronizedInstanceMethodWithWideArgs";
0175: createAutolockLockDefinition();
0176: String modifiersPattern = "*";
0177: String parametersPattern = "(..)";
0178: createLockConfigurationForMethodExpression(modifiersPattern,
0179: methodName, parametersPattern);
0180:
0181: this .testClientObjectManager.setIsManaged(true);
0182: invokeWithArgs(methodName,
0183: new Class[] { Double.TYPE, Long.TYPE }, new Object[] {
0184: new Double(0), new Long(0) });
0185: }
0186:
0187: public void testInstanceMethodWithNamedLocks() throws Exception {
0188: String methodName = "instanceMethod";
0189: createNamedLockDefinition("testLock");
0190: createLockConfigurationForMethodExpression("void", methodName,
0191: "()");
0192:
0193: assertNoTransactions();
0194:
0195: invokeWithNoArgs(methodName);
0196:
0197: assertNoAutolocks();
0198:
0199: assertNamedLockConditionsPostInvocation(1);
0200:
0201: // Make sure that a bogus lock doesn't pass the test...
0202: LockDefinition bogus = new LockDefinitionImpl("fakeLock",
0203: ConfigLockLevel.WRITE);
0204: bogus.commit();
0205: assertFalse(checkForLock(bogus));
0206:
0207: }
0208:
0209: public void testInstanceMethodThrowsExceptionWithNamedLocks()
0210: throws Exception {
0211: String methodName = "instanceMethodThrowsException";
0212:
0213: createNamedLockDefinition("testLock");
0214: createLockConfigurationForMethodExpression("void", methodName,
0215: "()");
0216:
0217: assertNoTransactions();
0218:
0219: invokeWithNoArgsAndCheckForProperException(methodName);
0220:
0221: // make sure we recorded the lock we expected.
0222: assertNamedLockConditionsPostInvocation(1);
0223: }
0224:
0225: public void testInstanceMethodWithArgumentsWithNamedLocks()
0226: throws Exception {
0227:
0228: String methodName = "instanceMethodWithArguments";
0229:
0230: createNamedLockDefinition("testLock");
0231: createLockConfigurationForMethodExpression("void", methodName,
0232: "(int, java.lang.String)");
0233:
0234: assertNoTransactions();
0235:
0236: invokeWithDefaultArgs(methodName);
0237:
0238: // check for begin transaction.
0239: assertNoAutolocks();
0240: assertNamedLockConditionsPostInvocation(1);
0241: }
0242:
0243: public void testInstanceMethodWithArgumentsThrowsExceptionWithNamedLocks()
0244: throws Exception {
0245: String methodName = "instanceMethodWithArgumentsThrowsException";
0246:
0247: createNamedLockDefinition("testLock");
0248: createLockConfigurationForMethodExpression("void", methodName,
0249: "(int, java.lang.String)");
0250:
0251: assertNoTransactions();
0252:
0253: invokeWithDefaultArgsAndCheckForProperException(methodName);
0254:
0255: // check for begin transaction.
0256: assertNoAutolocks();
0257: assertNamedLockConditionsPostInvocation(1);
0258: }
0259:
0260: public void testSynchronizedInstanceMethodWithAutolock()
0261: throws Exception {
0262: String testMethodName = "synchronizedInstanceMethod";
0263:
0264: createAutolockLockDefinition();
0265: createLockConfigurationForMethodExpression(
0266: "public synchronized void", testMethodName, "()");
0267:
0268: assertNoTransactions();
0269:
0270: // unmanaged objects shouldn't be autolocked.
0271: this .testClientObjectManager.setIsManaged(false);
0272: invokeWithNoArgs(testMethodName);
0273: assertNoTransactions();
0274:
0275: // managed objects SHOULD be autolocked.
0276: this .testClientObjectManager.setIsManaged(true);
0277: initClassLoader();
0278:
0279: invokeWithNoArgs(testMethodName);
0280:
0281: assertAutolockConditionsPostInvocation(1);
0282: }
0283:
0284: public void testSynchronizedInstanceMethodThrowsExceptionWithAutolock()
0285: throws Exception {
0286: String testMethodName = "synchronizedInstanceMethodThrowsException";
0287:
0288: createAutolockLockDefinition();
0289: createLockConfigurationForMethodExpression(
0290: "public synchronized void", testMethodName, "()");
0291:
0292: // int modifiers = getModifiers(testMethodName, new Class[] {});
0293:
0294: // assertTrue(config.isLockMethod(modifiers, targetClassName,
0295: // testMethodName, "()V", null));
0296: // assertTrue(config.isAutolock(modifiers, targetClassName, testMethodName,
0297: // "()V", null));
0298: assertNoTransactions();
0299:
0300: // unmanaged objects shouldn't be autolocked.
0301: this .testClientObjectManager.setIsManaged(false);
0302: invokeWithNoArgsAndCheckForProperException(testMethodName);
0303:
0304: assertNoTransactions();
0305:
0306: // managed objects SHOULD be autolocked.
0307: this .testClientObjectManager.setIsManaged(true);
0308: initClassLoader();
0309: invokeWithNoArgsAndCheckForProperException(testMethodName);
0310:
0311: assertAutolockConditionsPostInvocation(1);
0312: }
0313:
0314: public void testSynchronizedInstanceMethodWithArgumentsWithAutolock()
0315: throws Exception {
0316: String methodName = "synchronizedInstanceMethodWithArguments";
0317:
0318: createAutolockLockDefinition();
0319: createLockConfigurationForMethodExpression(
0320: "public synchronized void", methodName,
0321: "(int, java.lang.String)");
0322:
0323: // Class[] params = new Class[] { Integer.TYPE, String.class };
0324:
0325: // int modifiers = getModifiers(methodName, params);
0326:
0327: // assertTrue(config.isLockMethod(modifiers, targetClassName, methodName,
0328: // "(ILjava/lang/String;)V", null));
0329: // assertTrue(config.isAutolock(modifiers, targetClassName, methodName,
0330: // "(ILjava/lang/String;)V", null));
0331: assertNoTransactions();
0332:
0333: // unmanaged objects shouldn't be autolocked.
0334: this .testClientObjectManager.setIsManaged(false);
0335: invokeWithDefaultArgs(methodName);
0336: assertNoTransactions();
0337:
0338: // managed objects SHOULD be autolocked.
0339: this .testClientObjectManager.setIsManaged(true);
0340: initClassLoader();
0341: invokeWithDefaultArgs(methodName);
0342:
0343: assertAutolockConditionsPostInvocation(1);
0344: }
0345:
0346: /**
0347: * Tests that autolocks work on synchronized methods.
0348: */
0349: public void testSynchronizedInstanceMethodWithArgumentsThrowsExceptionWithAutolock()
0350: throws Exception {
0351: String methodName = "synchronizedInstanceMethodWithArgumentsThrowsException";
0352:
0353: createAutolockLockDefinition();
0354: createLockConfigurationForMethodExpression(
0355: "public synchronized void", methodName,
0356: "(int, java.lang.String)");
0357:
0358: // Class[] params = new Class[] { Integer.TYPE, String.class };
0359: //
0360: // int modifiers = getModifiers(methodName, params);
0361:
0362: // assertTrue(config.isLockMethod(modifiers, targetClassName, methodName,
0363: // "(ILjava/lang/String;)V", null));
0364: // assertTrue(config.isAutolock(modifiers, targetClassName, methodName,
0365: // "(ILjava/lang/String;)V", null));
0366: assertNoTransactions();
0367:
0368: // unmanaged objects shouldn't be autolocked.
0369: this .testClientObjectManager.setIsManaged(false);
0370:
0371: invokeWithDefaultArgsAndCheckForProperException(methodName);
0372: assertNoTransactions();
0373:
0374: // managed objects SHOULD be autolocked.
0375: this .testClientObjectManager.setIsManaged(true);
0376: initClassLoader();
0377: invokeWithDefaultArgsAndCheckForProperException(methodName);
0378: assertAutolockConditionsPostInvocation(1);
0379: }
0380:
0381: /**
0382: * Tests that autolocks work on synchronization inside methods.
0383: */
0384: public void testInternalSynchronizedInstanceMethodWithAutolock()
0385: throws Exception {
0386: String testMethodName = "internalSynchronizedInstanceMethod";
0387:
0388: createAutolockLockDefinition();
0389: createLockConfigurationForMethodExpression("public void",
0390: testMethodName, "()");
0391:
0392: // int modifiers = getModifiers(testMethodName, new Class[] {});
0393: //
0394: // assertFalse(config.isLockMethod(modifiers, targetClassName,
0395: // testMethodName, "()V", null));
0396: // assertTrue(config.isAutolock(modifiers, targetClassName, testMethodName,
0397: // "()V", null));
0398: assertNoTransactions();
0399:
0400: // unmanaged objects shouldn't be autolocked.
0401: this .testClientObjectManager.setIsManaged(false);
0402: invokeWithNoArgs(testMethodName);
0403: assertNoTransactions();
0404:
0405: // managed objects SHOULD be autolocked.
0406: this .testClientObjectManager.setIsManaged(true);
0407: initClassLoader();
0408:
0409: invokeWithNoArgs(testMethodName);
0410:
0411: assertAutolockConditionsPostInvocation(1);
0412: }
0413:
0414: public void testInternalSynchronizedInstanceMethodWithNamedlockAndAutoLock()
0415: throws Exception {
0416: config.addIncludePattern(this .targetClassName);
0417: String methodName = "internalSynchronizedInstanceMethod";
0418: // set up locks
0419: String methodExpression = "void " + targetClassName + "."
0420: + methodName + "()";
0421: LockDefinition ldnamed = new LockDefinitionImpl("test-lock",
0422: ConfigLockLevel.WRITE);
0423: ldnamed.commit();
0424: LockDefinition ldautolock = new LockDefinitionImpl(
0425: LockDefinition.TC_AUTOLOCK_NAME, ConfigLockLevel.WRITE);
0426: ldautolock.commit();
0427:
0428: config.getOrCreateSpec(targetClassName);
0429: config.addLock(methodExpression, ldautolock);
0430: config.addLock(methodExpression, ldnamed);
0431:
0432: this .testClientObjectManager.setIsManaged(true);
0433:
0434: assertNoTransactions();
0435:
0436: invokeWithNoArgs(methodName);
0437:
0438: assertTrue(checkForLockName(ldnamed.getLockName(), ldnamed
0439: .getLockLevelAsInt()));
0440: assertTrue(checkForLock(ldnamed));
0441: assertAutolockCount(1);
0442: assertTransactionCount(2);
0443: assertNoAutolockLiteral();
0444: }
0445:
0446: public void testInternalSynchronizedInstanceMethodThrowsExceptionWithAutolock()
0447: throws Exception {
0448: String testMethodName = "internalSynchronizedInstanceMethodThrowsException";
0449:
0450: createAutolockLockDefinition();
0451: createLockConfigurationForMethodExpression("public void",
0452: testMethodName, "()");
0453:
0454: // int modifiers = getModifiers(testMethodName, new Class[] {});
0455: //
0456: // assertFalse(config.isLockMethod(modifiers, targetClassName,
0457: // testMethodName, NO_ARGS_RETURNS_VOID_DESCRIPTION,
0458: // null));
0459: // assertTrue(config.isAutolock(modifiers, targetClassName, testMethodName,
0460: // NO_ARGS_RETURNS_VOID_DESCRIPTION,
0461: // null));
0462: assertNoTransactions();
0463:
0464: // unmanaged objects shouldn't be autolocked.
0465: this .testClientObjectManager.setIsManaged(false);
0466: invokeWithNoArgsAndCheckForProperException(testMethodName);
0467: assertNoTransactions();
0468:
0469: // managed objects SHOULD be autolocked.
0470: this .testClientObjectManager.setIsManaged(true);
0471: initClassLoader();
0472:
0473: invokeWithNoArgsAndCheckForProperException(testMethodName);
0474: assertAutolockConditionsPostInvocation(1);
0475: }
0476:
0477: public void testInternalSynchronizedInstanceMethodWithArgumentsWithAutolock()
0478: throws Exception {
0479: String testMethodName = "internalSynchronizedInstanceMethodWithArguments";
0480: createAutolockLockDefinition();
0481: createLockConfigurationForMethodExpression("public void",
0482: testMethodName, "(int, java.lang.String)");
0483:
0484: // int modifiers = getModifiers(testMethodName, WITH_ARGS_PARAMS);
0485: // assertFalse(config.isLockMethod(modifiers, targetClassName,
0486: // testMethodName, WITH_ARGS_RETURNS_VOID_DESCRIPTION,
0487: // null));
0488: // assertTrue(config.isAutolock(modifiers, targetClassName, testMethodName,
0489: // WITH_ARGS_RETURNS_VOID_DESCRIPTION,
0490: // null));
0491:
0492: // unmanaged objects shouldn't be autolocked.
0493: this .testClientObjectManager.setIsManaged(false);
0494: invokeWithDefaultArgs(testMethodName);
0495: assertNoTransactions();
0496:
0497: // managed objects should be autolocked
0498: this .testClientObjectManager.setIsManaged(true);
0499: initClassLoader();
0500:
0501: invokeWithDefaultArgs(testMethodName);
0502: assertAutolockConditionsPostInvocation(1);
0503: }
0504:
0505: public void testInternalSynchronizedInstanceMethodWithArgumentsThrowsExceptionWithAutolock()
0506: throws Exception {
0507: String testMethodName = "internalSynchronizedInstanceMethodWithArgumentsThrowsException";
0508: createAutolockLockDefinition();
0509: createLockConfigurationForMethodExpression("public void",
0510: testMethodName, "(int, java.lang.String)");
0511:
0512: // int modifiers = getModifiers(testMethodName, WITH_ARGS_PARAMS);
0513: // assertFalse(config.isLockMethod(modifiers, targetClassName,
0514: // testMethodName, WITH_ARGS_RETURNS_VOID_DESCRIPTION,
0515: // null));
0516: // assertTrue(config.isAutolock(modifiers, targetClassName, testMethodName,
0517: // WITH_ARGS_RETURNS_VOID_DESCRIPTION,
0518: // null));
0519:
0520: this .testClientObjectManager.setIsManaged(false);
0521: invokeWithDefaultArgsAndCheckForProperException(testMethodName);
0522: assertNoTransactions();
0523:
0524: this .testClientObjectManager.setIsManaged(true);
0525: initClassLoader();
0526:
0527: invokeWithDefaultArgsAndCheckForProperException(testMethodName);
0528: assertAutolockConditionsPostInvocation(1);
0529: }
0530:
0531: public void testStaticMethodWithNamedLocks() throws Exception {
0532: String methodName = "staticMethod";
0533:
0534: createNamedLockDefinition("testLock");
0535: createLockConfigurationForMethodExpression("void", methodName,
0536: "()");
0537:
0538: assertNoTransactions();
0539:
0540: invokeWithNoArgs(methodName);
0541: assertAutolockCount(0);
0542: assertNamedLockConditionsPostInvocation(1);
0543: }
0544:
0545: public void testStaticMethodThrowsExceptionWithNamedLocks()
0546: throws Exception {
0547: String methodName = "staticMethodThrowsException";
0548:
0549: createNamedLockDefinition("testLock");
0550: createLockConfigurationForMethodExpression("void", methodName,
0551: "()");
0552:
0553: assertNoTransactions();
0554: invokeWithNoArgsAndCheckForProperException(methodName);
0555:
0556: // make sure we recorded the lock we expected.
0557: assertNamedLockConditionsPostInvocation(1);
0558: }
0559:
0560: public void testStaticMethodWithArgumentsWithNamedLocks()
0561: throws Exception {
0562: String methodName = "staticMethodWithArguments";
0563:
0564: createNamedLockDefinition("testLock");
0565: createLockConfigurationForMethodExpression("void", methodName,
0566: "(int, java.lang.String)");
0567:
0568: assertNoTransactions();
0569: invokeWithDefaultArgs(methodName);
0570: assertNamedLockConditionsPostInvocation(1);
0571: }
0572:
0573: public void testStaticMethodWithArgumentsThrowsExceptionWithNamedLocks()
0574: throws Exception {
0575: String methodName = "staticMethodWithArgumentsThrowsException";
0576:
0577: createNamedLockDefinition("testLock");
0578: createLockConfigurationForMethodExpression("void", methodName,
0579: "(int, java.lang.String)");
0580:
0581: assertNoTransactions();
0582: invokeWithDefaultArgsAndCheckForProperException(methodName);
0583: assertNamedLockConditionsPostInvocation(1);
0584: }
0585:
0586: public void testSynchronizedStaticMethodWithAutolock()
0587: throws Exception {
0588: String testMethodName = "synchronizedStaticMethod";
0589: createAutolockLockDefinition();
0590: String modifiersPattern = "*";
0591: String parametersPattern = "(..)";
0592: createLockConfigurationForMethodExpression(modifiersPattern,
0593: testMethodName, parametersPattern);
0594:
0595: this .testClientObjectManager.setIsManaged(true);
0596: invokeWithNoArgs(testMethodName);
0597:
0598: assertNoTransactions();
0599: assertNoAutolocks();
0600:
0601: // make sure that the pattern used to test autolocks actually picks out a
0602: // real method.
0603: checkWithNamedLockNoArgs(testMethodName, modifiersPattern,
0604: parametersPattern);
0605: }
0606:
0607: public void testSynchronizedStaticMethodThrowsExceptionWithAutolock()
0608: throws Exception {
0609: String testMethodName = "synchronizedStaticMethodThrowsException";
0610: createAutolockLockDefinition();
0611: String modifiersPattern = "*";
0612: String parametersPattern = "(..)";
0613: createLockConfigurationForMethodExpression(modifiersPattern,
0614: testMethodName, parametersPattern);
0615:
0616: this .testClientObjectManager.setIsManaged(true);
0617: invokeWithNoArgsAndCheckForProperException(testMethodName);
0618:
0619: assertNoTransactions();
0620: assertNoAutolocks();
0621:
0622: // make sure that the pattern used to test autolocks actually picks out a
0623: // real method.
0624: checkWithNamedLockNoArgsThrowsException(testMethodName,
0625: modifiersPattern, parametersPattern);
0626: }
0627:
0628: public void testSynchronizedStaticMethodWithArgumentsWithAutolock()
0629: throws Exception {
0630: String testMethodName = "synchronizedStaticMethodWithArguments";
0631: createAutolockLockDefinition();
0632: String modifiersPattern = "*";
0633: String parametersPattern = "(..)";
0634: createLockConfigurationForMethodExpression(modifiersPattern,
0635: testMethodName, parametersPattern);
0636:
0637: this .testClientObjectManager.setIsManaged(true);
0638: invokeWithDefaultArgs(testMethodName);
0639:
0640: assertNoTransactions();
0641: assertNoAutolocks();
0642:
0643: // make sure that the pattern used to test autolocks actually picks out a
0644: // real method.
0645: checkWithNamedLockDefaultArgs(testMethodName, modifiersPattern,
0646: parametersPattern);
0647: }
0648:
0649: public void testSynchronizedStaticMethodWithArgumentsThrowsExceptionWithAutolock()
0650: throws Exception {
0651: String testMethodName = "synchronizedStaticMethodWithArgumentsThrowsException";
0652: createAutolockLockDefinition();
0653: String modifiersPattern = "*";
0654: String parametersPattern = "(..)";
0655: createLockConfigurationForMethodExpression(modifiersPattern,
0656: testMethodName, parametersPattern);
0657:
0658: this .testClientObjectManager.setIsManaged(true);
0659: invokeWithDefaultArgsAndCheckForProperException(testMethodName);
0660:
0661: assertNoTransactions();
0662: assertNoAutolocks();
0663:
0664: checkWithnamedLockDefaultArgsThrowsException(testMethodName,
0665: modifiersPattern, parametersPattern);
0666: }
0667:
0668: public void testInternalSynchronizedStaticMethodWithAutolock()
0669: throws Exception {
0670: String testMethodName = "internalSynchronizedStaticMethod";
0671: createAutolockLockDefinition();
0672: String modifiersPattern = "*";
0673: String parametersPattern = "(..)";
0674: createLockConfigurationForMethodExpression(modifiersPattern,
0675: testMethodName, parametersPattern);
0676:
0677: this .testClientObjectManager.setIsManaged(true);
0678: invokeWithNoArgs(testMethodName);
0679:
0680: assertAutolockConditionsPostInvocation(1);
0681: }
0682:
0683: public void testInternalSynchronizedStaticMethodThrowsExceptionWithAutolock()
0684: throws Exception {
0685: String testMethodName = "internalSynchronizedStaticMethodThrowsException";
0686: createAutolockLockDefinition();
0687: String modifiersPattern = "*";
0688: String parametersPattern = "(..)";
0689: createLockConfigurationForMethodExpression(modifiersPattern,
0690: testMethodName, parametersPattern);
0691:
0692: this .testClientObjectManager.setIsManaged(true);
0693: invokeWithNoArgsAndCheckForProperException(testMethodName);
0694:
0695: assertAutolockConditionsPostInvocation(1);
0696: }
0697:
0698: public void testInternalSynchronizedStaticMethodWithArgumentsWithAutolock()
0699: throws Exception {
0700: String testMethodName = "internalSynchronizedStaticMethodWithArguments";
0701: createAutolockLockDefinition();
0702: String modifiersPattern = "*";
0703: String parametersPattern = "(..)";
0704: createLockConfigurationForMethodExpression(modifiersPattern,
0705: testMethodName, parametersPattern);
0706:
0707: this .testClientObjectManager.setIsManaged(true);
0708: invokeWithDefaultArgs(testMethodName);
0709:
0710: assertAutolockConditionsPostInvocation(1);
0711: }
0712:
0713: public void testInternalSynchronizedStaticMethodWithArgumentsThrowsExceptionWithAutolock()
0714: throws Exception {
0715: String testMethodName = "internalSynchronizedStaticMethodWithArgumentsThrowsException";
0716: createAutolockLockDefinition();
0717: String modifiersPattern = "*";
0718: String parametersPattern = "(..)";
0719: createLockConfigurationForMethodExpression(modifiersPattern,
0720: testMethodName, parametersPattern);
0721:
0722: this .testClientObjectManager.setIsManaged(true);
0723: invokeWithDefaultArgsAndCheckForProperException(testMethodName);
0724:
0725: assertAutolockConditionsPostInvocation(1);
0726: }
0727:
0728: public void testInstanceMethodReturnsAValueWithNamedLocks()
0729: throws Exception {
0730:
0731: String methodName = "instanceMethodReturnsAValue";
0732:
0733: createNamedLockDefinition("testLock");
0734: createLockConfigurationForMethodExpression("*", methodName,
0735: "()");
0736:
0737: assertNoTransactions();
0738:
0739: invokeWithNoArgs(methodName);
0740:
0741: assertNamedLockConditionsPostInvocation(1);
0742: }
0743:
0744: public void testInstanceMethodReturnsAValueThrowsExceptionWithNamedLocks()
0745: throws Exception {
0746: String methodName = "instanceMethodReturnsAValueThrowsException";
0747:
0748: createNamedLockDefinition("testLock");
0749: createLockConfigurationForMethodExpression("*", methodName,
0750: "()");
0751:
0752: assertNoTransactions();
0753:
0754: invokeWithNoArgsAndCheckForProperException(methodName);
0755:
0756: assertNamedLockConditionsPostInvocation(1);
0757: }
0758:
0759: public void testInstanceMethodWithArgumentsReturnsAValue()
0760: throws Exception {
0761: String methodName = "instanceMethodWithArgumentsReturnsAValue";
0762:
0763: createNamedLockDefinition("testLock");
0764: createLockConfigurationForMethodExpression("*", methodName,
0765: "(..)");
0766:
0767: assertNoTransactions();
0768:
0769: invokeWithDefaultArgs(methodName);
0770:
0771: assertNamedLockConditionsPostInvocation(1);
0772: }
0773:
0774: public void testInstanceMethodWithArgumentsReturnsAValueThrowsException()
0775: throws Exception {
0776: String methodName = "instanceMethodWithArgumentsReturnsAValueThrowsException";
0777:
0778: createNamedLockDefinition("testLock");
0779: createLockConfigurationForMethodExpression("*", methodName,
0780: "(..)");
0781:
0782: assertNoTransactions();
0783:
0784: invokeWithDefaultArgsAndCheckForProperException(methodName);
0785:
0786: assertNamedLockConditionsPostInvocation(1);
0787: }
0788:
0789: public void testSynchronizedInstanceMethodReturnsAValueWithAutolock()
0790: throws Exception {
0791: String methodName = "synchronizedInstanceMethodReturnsAValue";
0792:
0793: createAutolockLockDefinition();
0794: createLockConfigurationForMethodExpression("String",
0795: methodName, "()");
0796:
0797: this .testClientObjectManager.setIsManaged(false);
0798: invokeWithNoArgs(methodName);
0799:
0800: assertNoTransactions();
0801:
0802: this .testClientObjectManager.setIsManaged(true);
0803: initClassLoader();
0804:
0805: invokeWithNoArgs(methodName);
0806: assertAutolockConditionsPostInvocation(1);
0807: }
0808:
0809: public void testSynchronizedInstanceMethodReturnsAValueThrowsExceptionWithAutolock()
0810: throws Exception {
0811: String methodName = "synchronizedInstanceMethodReturnsAValueThrowsException";
0812:
0813: createAutolockLockDefinition();
0814: createLockConfigurationForMethodExpression("String",
0815: methodName, "()");
0816:
0817: this .testClientObjectManager.setIsManaged(false);
0818: invokeWithNoArgsAndCheckForProperException(methodName);
0819:
0820: assertNoTransactions();
0821:
0822: this .testClientObjectManager.setIsManaged(true);
0823: initClassLoader();
0824:
0825: invokeWithNoArgsAndCheckForProperException(methodName);
0826: assertAutolockConditionsPostInvocation(1);
0827: }
0828:
0829: public void testSynchronizedInstanceMethodWithArgumentsReturnsAValueWithAutolock()
0830: throws Exception {
0831: String methodName = "synchronizedInstanceMethodWithArgumentsReturnsAValue";
0832:
0833: createAutolockLockDefinition();
0834: createLockConfigurationForMethodExpression("String",
0835: methodName, "(..)");
0836:
0837: this .testClientObjectManager.setIsManaged(false);
0838: invokeWithDefaultArgs(methodName);
0839:
0840: assertNoTransactions();
0841:
0842: this .testClientObjectManager.setIsManaged(true);
0843: initClassLoader();
0844:
0845: invokeWithDefaultArgs(methodName);
0846: assertAutolockConditionsPostInvocation(1);
0847: }
0848:
0849: public void testSynchronizedInstanceMethodWithArgumentsReturnsAValueThrowsExceptionWithAutolock()
0850: throws Exception {
0851:
0852: String methodName = "synchronizedInstanceMethodWithArgumentsReturnsAValueThrowsException";
0853:
0854: createAutolockLockDefinition();
0855: createLockConfigurationForMethodExpression("String",
0856: methodName, "(..)");
0857:
0858: this .testClientObjectManager.setIsManaged(false);
0859: invokeWithDefaultArgsAndCheckForProperException(methodName);
0860:
0861: assertNoTransactions();
0862:
0863: this .testClientObjectManager.setIsManaged(true);
0864: initClassLoader();
0865:
0866: invokeWithDefaultArgsAndCheckForProperException(methodName);
0867:
0868: assertAutolockConditionsPostInvocation(1);
0869: }
0870:
0871: public void testInternalSynchronizedInstanceMethodReturnsAValueWithAutolock()
0872: throws Exception {
0873:
0874: String methodName = "internalSynchronizedInstanceMethodReturnsAValue";
0875:
0876: createAutolockLockDefinition();
0877: createLockConfigurationForMethodExpression("String",
0878: methodName, "()");
0879:
0880: this .testClientObjectManager.setIsManaged(false);
0881: invokeWithNoArgs(methodName);
0882:
0883: assertNoTransactions();
0884:
0885: this .testClientObjectManager.setIsManaged(true);
0886: initClassLoader();
0887:
0888: invokeWithNoArgs(methodName);
0889:
0890: assertAutolockConditionsPostInvocation(1);
0891: }
0892:
0893: public void testInternalSynchronizedInstanceMethodReturnsAValueThrowsExceptionWithAutolock()
0894: throws Exception {
0895: String methodName = "internalSynchronizedInstanceMethodReturnsAValueThrowsException";
0896:
0897: createAutolockLockDefinition();
0898: createLockConfigurationForMethodExpression("String",
0899: methodName, "()");
0900:
0901: this .testClientObjectManager.setIsManaged(false);
0902: invokeWithNoArgsAndCheckForProperException(methodName);
0903:
0904: assertNoTransactions();
0905:
0906: this .testClientObjectManager.setIsManaged(true);
0907: initClassLoader();
0908:
0909: invokeWithNoArgsAndCheckForProperException(methodName);
0910:
0911: assertAutolockConditionsPostInvocation(1);
0912: }
0913:
0914: public void testInternalSynchronizedInstanceMethodWithArgumentsReturnsAValueWithAutolock()
0915: throws Exception {
0916: String methodName = "internalSynchronizedInstanceMethodWithArgumentsReturnsAValue";
0917:
0918: createAutolockLockDefinition();
0919: createLockConfigurationForMethodExpression("String",
0920: methodName, "(..)");
0921:
0922: this .testClientObjectManager.setIsManaged(false);
0923: invokeWithDefaultArgs(methodName);
0924:
0925: assertNoTransactions();
0926:
0927: this .testClientObjectManager.setIsManaged(true);
0928: initClassLoader();
0929:
0930: invokeWithDefaultArgs(methodName);
0931:
0932: assertAutolockConditionsPostInvocation(1);
0933: }
0934:
0935: public void testInternalSynchronizedInstanceMethodWithArgumentsReturnsAValueThrowsExceptionWithAutolock()
0936: throws Exception {
0937: String methodName = "internalSynchronizedInstanceMethodWithArgumentsReturnsAValueThrowsException";
0938:
0939: createAutolockLockDefinition();
0940: createLockConfigurationForMethodExpression("String",
0941: methodName, "(..)");
0942:
0943: this .testClientObjectManager.setIsManaged(false);
0944: invokeWithDefaultArgsAndCheckForProperException(methodName);
0945:
0946: assertNoTransactions();
0947:
0948: this .testClientObjectManager.setIsManaged(true);
0949: initClassLoader();
0950:
0951: invokeWithDefaultArgsAndCheckForProperException(methodName);
0952:
0953: assertAutolockConditionsPostInvocation(1);
0954: }
0955:
0956: public void testStaticMethodReturnsAValueWithNamedLocks()
0957: throws Exception {
0958: String methodName = "staticMethodReturnsAValue";
0959: createNamedLockDefinition("testLock");
0960: createLockConfigurationForMethodExpression("*", methodName,
0961: "()");
0962:
0963: assertNoTransactions();
0964:
0965: invokeWithNoArgs(methodName);
0966:
0967: assertNamedLockConditionsPostInvocation(1);
0968: }
0969:
0970: public void testStaticMethodReturnsAValueThrowsExceptionWithNamedLocks()
0971: throws Exception {
0972: String methodName = "staticMethodReturnsAValueThrowsException";
0973:
0974: createNamedLockDefinition("testLock");
0975: createLockConfigurationForMethodExpression("*", methodName,
0976: "()");
0977:
0978: assertNoTransactions();
0979:
0980: invokeWithNoArgsAndCheckForProperException(methodName);
0981:
0982: assertNamedLockConditionsPostInvocation(1);
0983: }
0984:
0985: public void testStaticMethodWithArgumentsReturnsAValueWithNamedLocks()
0986: throws Exception {
0987: String methodName = "staticMethodWithArgumentsReturnsAValue";
0988: createNamedLockDefinition("testLock");
0989: createLockConfigurationForMethodExpression("*", methodName,
0990: "(..)");
0991:
0992: assertNoTransactions();
0993:
0994: invokeWithDefaultArgs(methodName);
0995:
0996: assertNamedLockConditionsPostInvocation(1);
0997: }
0998:
0999: public void testStaticMethodWithArgumentsReturnsAValueThrowsExceptionWithNamedLocks()
1000: throws Exception {
1001:
1002: String methodName = "staticMethodWithArgumentsReturnsAValueThrowsException";
1003: createNamedLockDefinition("testLock");
1004: createLockConfigurationForMethodExpression("*", methodName,
1005: "(..)");
1006:
1007: assertNoTransactions();
1008:
1009: invokeWithDefaultArgsAndCheckForProperException(methodName);
1010:
1011: assertNamedLockConditionsPostInvocation(1);
1012: }
1013:
1014: /**
1015: * Autolocks should not apply to synchronized static methods because they are synchronized on the Class object which
1016: * can't be distributed... we don't think.
1017: */
1018: public void testSynchronizedStaticMethodReturnsAValueWithAutolocks()
1019: throws Exception {
1020: String testMethodName = "synchronizedStaticMethodReturnsAValue";
1021: createAutolockLockDefinition();
1022: String modifiersPattern = "*";
1023: String parametersPattern = "(..)";
1024: createLockConfigurationForMethodExpression(modifiersPattern,
1025: testMethodName, parametersPattern);
1026:
1027: this .testClientObjectManager.setIsManaged(true);
1028: invokeWithNoArgs(testMethodName);
1029:
1030: assertNoTransactions();
1031: assertNoAutolocks();
1032:
1033: // make sure that the pattern used to test autolocks actually picks out a
1034: // real method.
1035: checkWithNamedLockNoArgs(testMethodName, modifiersPattern,
1036: parametersPattern);
1037: }
1038:
1039: public void testSynchronizedStaticMethodReturnsAValueThrowsExceptionWithAutolocks()
1040: throws Exception {
1041: String testMethodName = "synchronizedStaticMethodReturnsAValueThrowsException";
1042: createAutolockLockDefinition();
1043: String modifiersPattern = "*";
1044: String parametersPattern = "(..)";
1045: createLockConfigurationForMethodExpression(modifiersPattern,
1046: testMethodName, parametersPattern);
1047:
1048: this .testClientObjectManager.setIsManaged(true);
1049: invokeWithNoArgsAndCheckForProperException(testMethodName);
1050:
1051: assertNoTransactions();
1052: assertNoAutolocks();
1053:
1054: // make sure that the pattern used to test autolocks actually picks out a
1055: // real method.
1056: checkWithNamedLockNoArgsThrowsException(testMethodName,
1057: modifiersPattern, parametersPattern);
1058: }
1059:
1060: public void testSynchronizedStaticMethodWithArgumentsReturnsAValueWithAutolocks()
1061: throws Exception {
1062: String testMethodName = "synchronizedStaticMethodWithArgumentsReturnsAValue";
1063: createAutolockLockDefinition();
1064: String modifiersPattern = "*";
1065: String parametersPattern = "(..)";
1066: createLockConfigurationForMethodExpression(modifiersPattern,
1067: testMethodName, parametersPattern);
1068:
1069: this .testClientObjectManager.setIsManaged(true);
1070: invokeWithDefaultArgs(testMethodName);
1071:
1072: assertNoTransactions();
1073: assertNoAutolocks();
1074:
1075: // make sure that the pattern used to test autolocks actually picks out a
1076: // real method.
1077: checkWithNamedLockDefaultArgs(testMethodName, modifiersPattern,
1078: parametersPattern);
1079: }
1080:
1081: public void testSynchronizedStaticMethodWithArgumentsReturnsAValueThrowsExceptionWithAutolocks()
1082: throws Exception {
1083: String testMethodName = "synchronizedStaticMethodWithArgumentsReturnsAValueThrowsException";
1084: createAutolockLockDefinition();
1085: String modifiersPattern = "*";
1086: String parametersPattern = "(..)";
1087: createLockConfigurationForMethodExpression(modifiersPattern,
1088: testMethodName, parametersPattern);
1089:
1090: this .testClientObjectManager.setIsManaged(true);
1091: invokeWithDefaultArgsAndCheckForProperException(testMethodName);
1092:
1093: assertNoTransactions();
1094: assertNoAutolocks();
1095:
1096: checkWithnamedLockDefaultArgsThrowsException(testMethodName,
1097: modifiersPattern, parametersPattern);
1098: }
1099:
1100: /**
1101: * Autolocks SHOULD apply to internally synchronized static methods.
1102: */
1103: public void testInternalSynchronizedStaticMethodReturnsAValueWithAutolocks()
1104: throws Exception {
1105:
1106: String testMethodName = "internalSynchronizedStaticMethodReturnsAValue";
1107: createAutolockLockDefinition();
1108: String modifiersPattern = "*";
1109: String parametersPattern = "(..)";
1110: createLockConfigurationForMethodExpression(modifiersPattern,
1111: testMethodName, parametersPattern);
1112:
1113: this .testClientObjectManager.setIsManaged(true);
1114: invokeWithNoArgs(testMethodName);
1115:
1116: assertAutolockConditionsPostInvocation(1);
1117: }
1118:
1119: public void testInternalSynchronizedStaticMethodReturnsAValueThrowsExceptionWithAutolocks()
1120: throws Exception {
1121:
1122: String testMethodName = "internalSynchronizedStaticMethodReturnsAValueThrowsException";
1123: createAutolockLockDefinition();
1124: String modifiersPattern = "*";
1125: String parametersPattern = "(..)";
1126: createLockConfigurationForMethodExpression(modifiersPattern,
1127: testMethodName, parametersPattern);
1128:
1129: this .testClientObjectManager.setIsManaged(true);
1130: invokeWithNoArgsAndCheckForProperException(testMethodName);
1131:
1132: assertAutolockConditionsPostInvocation(1);
1133: }
1134:
1135: public void testInternalSynchronizedStaticMethodWithArgumentsReturnsAValueWithAutolocks()
1136: throws Exception {
1137:
1138: String testMethodName = "internalSynchronizedStaticMethodWithArgumentsReturnsAValue";
1139: createAutolockLockDefinition();
1140: String modifiersPattern = "*";
1141: String parametersPattern = "(..)";
1142: createLockConfigurationForMethodExpression(modifiersPattern,
1143: testMethodName, parametersPattern);
1144:
1145: this .testClientObjectManager.setIsManaged(true);
1146: invokeWithDefaultArgs(testMethodName);
1147:
1148: assertAutolockConditionsPostInvocation(1);
1149: }
1150:
1151: public void testInternalSynchronizedStaticMethodWithArgumentsReturnsAValueThrowsExceptionWithAutolocks()
1152: throws Exception {
1153:
1154: String testMethodName = "internalSynchronizedStaticMethodWithArgumentsReturnsAValueThrowsException";
1155: createAutolockLockDefinition();
1156: String modifiersPattern = "*";
1157: String parametersPattern = "(..)";
1158: createLockConfigurationForMethodExpression(modifiersPattern,
1159: testMethodName, parametersPattern);
1160:
1161: this .testClientObjectManager.setIsManaged(true);
1162: invokeWithDefaultArgsAndCheckForProperException(testMethodName);
1163:
1164: assertAutolockConditionsPostInvocation(1);
1165: }
1166:
1167: public void testInstanceMethodWithAutolockAndNoSynchronizersDoesNothing()
1168: throws Exception {
1169: String methodName = "instanceMethod";
1170:
1171: createAutolockLockDefinition();
1172: String modifiersPattern = "void";
1173: String parametersPattern = "()";
1174: createLockConfigurationForMethodExpression(modifiersPattern,
1175: methodName, parametersPattern);
1176:
1177: this .testClientObjectManager.setIsManaged(true);
1178: invokeWithNoArgs(methodName);
1179: assertNoTransactions();
1180:
1181: // add a regular-old named lock to make sure that the method expression
1182: // actually picks out a method in order
1183: // to validate that the test above actually tested something...
1184: checkWithNamedLockNoArgs(methodName, modifiersPattern,
1185: parametersPattern);
1186: }
1187:
1188: public void testWildcardAutolock() throws Exception {
1189:
1190: String testMethodName = "internalSynchronizedInstanceMethod";
1191:
1192: createAutolockLockDefinition();
1193: createLockConfigurationForMethodExpression("*", "*", "(..)");
1194: //
1195: // int modifiers = getModifiers(testMethodName, new Class[] {});
1196: //
1197: // assertFalse(config.isLockMethod(modifiers, targetClassName,
1198: // testMethodName, "()V", null));
1199: // assertTrue(config.isAutolock(modifiers, targetClassName, testMethodName,
1200: // "()V", null));
1201: assertNoTransactions();
1202:
1203: // unmanaged objects shouldn't be autolocked.
1204: this .testClientObjectManager.setIsManaged(false);
1205: invokeWithNoArgs(testMethodName);
1206: assertNoTransactions();
1207:
1208: // managed objects SHOULD be autolocked.
1209: this .testClientObjectManager.setIsManaged(true);
1210: initClassLoader();
1211:
1212: invokeWithNoArgs(testMethodName);
1213:
1214: assertAutolockConditionsPostInvocation(1);
1215: }
1216:
1217: public void testReadLock() throws Exception {
1218: config.addIncludePattern(this .targetClassName);
1219: String methodName = "instanceMethod";
1220:
1221: this .lockDefinition = new LockDefinitionImpl("testReadLock",
1222: ConfigLockLevel.READ);
1223: this .lockDefinition.commit();
1224:
1225: createLockConfigurationForMethodExpression("void", methodName,
1226: "()");
1227:
1228: assertNoTransactions();
1229:
1230: invokeWithNoArgs(methodName);
1231: assertNamedLockConditionsPostInvocation(1);
1232: }
1233:
1234: public void testWithoutLocks() throws Exception {
1235: String methodName = "internalSynchronizedInstanceMethod";
1236:
1237: invokeWithNoArgs(methodName);
1238:
1239: assertTransactionCount(0);
1240: }
1241:
1242: public void testAutolockCtorNoException() throws Exception {
1243: String methodExpression = "* " + targetClassName + ".*(..)";
1244: LockDefinition ld = new LockDefinitionImpl(
1245: LockDefinition.TC_AUTOLOCK_NAME, ConfigLockLevel.WRITE);
1246: ld.commit();
1247:
1248: config.getOrCreateSpec(targetClassName);
1249: config.addLock(methodExpression, ld);
1250:
1251: this .testClientObjectManager.setIsManaged(true);
1252:
1253: assertNoTransactions();
1254:
1255: System.setProperty(ClassAdapterTestTarget.KEY,
1256: ClassAdapterTestTarget.CSTR_AUTOLOCK_NO_EXCEPTION);
1257:
1258: callNoArgCtor();
1259: assertTransactionCount(1);
1260: assertAutolockCount(1);
1261: }
1262:
1263: public void testNamedlockCtorNoException() throws Exception {
1264: String methodExpression = "* " + targetClassName + ".*(..)";
1265: LockDefinition ld = new LockDefinitionImpl("test-lock",
1266: ConfigLockLevel.WRITE);
1267: ld.commit();
1268:
1269: config.getOrCreateSpec(targetClassName);
1270: config.addLock(methodExpression, ld);
1271:
1272: assertNoTransactions();
1273:
1274: callNoArgCtor();
1275: assertTransactionCount(1);
1276: assertAutolockCount(0);
1277: }
1278:
1279: public void testNamedlockCtorThrowsException() throws Exception {
1280: String methodExpression = "* " + targetClassName + ".*(..)";
1281: LockDefinition ld = new LockDefinitionImpl("test-lock",
1282: ConfigLockLevel.WRITE);
1283: ld.commit();
1284:
1285: config.getOrCreateSpec(targetClassName);
1286: config.addLock(methodExpression, ld);
1287:
1288: assertNoTransactions();
1289:
1290: System.setProperty(ClassAdapterTestTarget.KEY,
1291: ClassAdapterTestTarget.CSTR_THROW_EXCEPTION);
1292:
1293: try {
1294: callNoArgCtor();
1295: throw new AssertionError();
1296: } catch (RuntimeException re) {
1297: if ((re.getClass() != RuntimeException.class)
1298: || !re
1299: .getMessage()
1300: .equals(
1301: ClassAdapterTestTarget.CSTR_THROW_EXCEPTION)) {
1302: throw re;
1303: }
1304: }
1305:
1306: assertTransactionCount(1);
1307: assertAutolockCount(0);
1308: }
1309:
1310: private void testAutolockCtorException(boolean inside)
1311: throws Exception {
1312: String methodExpression = "* " + targetClassName + ".*(..)";
1313: LockDefinition ld = new LockDefinitionImpl(
1314: LockDefinition.TC_AUTOLOCK_NAME, ConfigLockLevel.WRITE);
1315: ld.commit();
1316:
1317: config.getOrCreateSpec(targetClassName);
1318: config.addLock(methodExpression, ld);
1319:
1320: this .testClientObjectManager.setIsManaged(true);
1321:
1322: assertNoTransactions();
1323:
1324: String cmd = inside ? ClassAdapterTestTarget.CSTR_AUTOLOCK_THROW_EXCEPTION_INSIDE
1325: : ClassAdapterTestTarget.CSTR_THROW_EXCEPTION;
1326:
1327: System.setProperty(ClassAdapterTestTarget.KEY, cmd);
1328:
1329: try {
1330: callNoArgCtor();
1331: throw new AssertionError();
1332: } catch (RuntimeException re) {
1333: if ((re.getClass() != RuntimeException.class)
1334: || !re.getMessage().equals(cmd)) {
1335: throw re;
1336: }
1337: }
1338:
1339: int numTxn = inside ? 1 : 0;
1340:
1341: assertTransactionCount(numTxn);
1342: assertAutolockCount(numTxn);
1343: }
1344:
1345: public void testAutolockCtorExceptionOutsideSynch()
1346: throws Exception {
1347: testAutolockCtorException(false);
1348: }
1349:
1350: public void testAutolockCtorExceptionInsideSynch() throws Exception {
1351: testAutolockCtorException(true);
1352: }
1353:
1354: public void testNestedAutolocks() throws Exception {
1355: String methodName = "nestedInternalSynchronizedInstanceMethod";
1356:
1357: createAutolockLockDefinition();
1358: createLockConfigurationForMethodExpression("*", "*", "(..)");
1359:
1360: this .testClientObjectManager.setIsManaged(true);
1361: assertNoTransactions();
1362:
1363: Object result = invokeWithNoArgs(methodName);
1364: int expectedTransactionCount = ((Integer) result).intValue();
1365: assertAutolockConditionsPostInvocation(expectedTransactionCount);
1366: }
1367:
1368: public void testMultipleNamedLocks() throws Exception {
1369: config.addIncludePattern(this .targetClassName);
1370: String methodName = "instanceMethod";
1371: String methodExpression = "* " + targetClassName + "."
1372: + methodName + "(..)";
1373: LockDefinition ld1 = new LockDefinitionImpl("lock1",
1374: ConfigLockLevel.WRITE);
1375: LockDefinition ld2 = new LockDefinitionImpl("lock2",
1376: ConfigLockLevel.WRITE);
1377: ld1.commit();
1378: ld2.commit();
1379: LockDefinition[] declaredLockDefs = new LockDefinition[] { ld1,
1380: ld2 };
1381:
1382: config.getOrCreateSpec(targetClassName);
1383: config.addLock(methodExpression, ld1);
1384: config.addLock(methodExpression, ld2);
1385: this .testClientObjectManager.setIsManaged(true);
1386:
1387: assertNoTransactions();
1388:
1389: invokeWithNoArgs(methodName);
1390:
1391: assertTransactionCount(2);
1392: assertTrue(checkForLocks(declaredLockDefs));
1393: }
1394:
1395: public void testAutolockAndNamedLock() throws Exception {
1396: config.addIncludePattern(this .targetClassName);
1397: String methodName = "internalSynchronizedInstanceMethod";
1398: // set up locks
1399: String methodExpression = "void " + targetClassName + "."
1400: + methodName + "()";
1401: LockDefinition ldnamed = new LockDefinitionImpl("test-lock",
1402: ConfigLockLevel.WRITE);
1403: ldnamed.commit();
1404: LockDefinition ldautolock = new LockDefinitionImpl(
1405: LockDefinition.TC_AUTOLOCK_NAME, ConfigLockLevel.WRITE);
1406: ldautolock.commit();
1407:
1408: config.getOrCreateSpec(targetClassName);
1409: // config.addLock(methodExpression, ldautolock);
1410: config.addLock(methodExpression, ldnamed);
1411: config.addLock(methodExpression, ldautolock);
1412:
1413: this .testClientObjectManager.setIsManaged(true);
1414:
1415: assertNoTransactions();
1416:
1417: invokeWithNoArgs(methodName);
1418:
1419: assertTrue(checkForLockName(ldnamed.getLockName(), ldnamed
1420: .getLockLevelAsInt()));
1421: assertTrue(checkForLock(ldnamed));
1422: assertAutolockCount(1);
1423: assertTransactionCount(2);
1424: assertNoAutolockLiteral();
1425: }
1426:
1427: public void testSynchronizedInstanceMethodWithAutolockAndNamedLock()
1428: throws Exception {
1429: config.addIncludePattern(this .targetClassName);
1430: String methodName = "synchronizedInstanceMethod";
1431: // set up locks
1432: String methodExpression = "void " + targetClassName + "."
1433: + methodName + "()";
1434: LockDefinition ldnamed = new LockDefinitionImpl("test-lock",
1435: ConfigLockLevel.WRITE);
1436: ldnamed.commit();
1437: LockDefinition ldautolock = new LockDefinitionImpl(
1438: LockDefinition.TC_AUTOLOCK_NAME, ConfigLockLevel.WRITE);
1439: ldautolock.commit();
1440:
1441: config.getOrCreateSpec(targetClassName);
1442: config.addLock(methodExpression, ldautolock);
1443: config.addLock(methodExpression, ldnamed);
1444:
1445: this .testClientObjectManager.setIsManaged(true);
1446:
1447: assertNoTransactions();
1448:
1449: invokeWithNoArgs(methodName);
1450:
1451: assertTrue(checkForLockName(ldnamed.getLockName(), ldnamed
1452: .getLockLevelAsInt()));
1453: assertTrue(checkForLock(ldnamed));
1454: assertAutolockCount(1);
1455: assertTransactionCount(2);
1456: assertNoAutolockLiteral();
1457: }
1458:
1459: public void testSerializationFields() throws Exception {
1460: config.getOrCreateSpec(targetClassName);
1461: Class notAdapted = Class.forName(targetClassName);
1462: Class adapted = classLoader.loadClass(targetClassName);
1463: assertNotSame(notAdapted, adapted);
1464:
1465: Field f = notAdapted.getDeclaredField("serialVersionUID");
1466: int mods = f.getModifiers();
1467: assertTrue(Modifier.isPrivate(mods));
1468: assertTrue(Modifier.isStatic(mods));
1469: assertTrue(Modifier.isFinal(mods));
1470: assertTrue(f.getType().equals(Long.TYPE));
1471:
1472: f = adapted.getDeclaredField("serialVersionUID");
1473: mods = f.getModifiers();
1474: assertTrue(Modifier.isPrivate(mods));
1475: assertTrue(Modifier.isStatic(mods));
1476: assertTrue(Modifier.isFinal(mods));
1477: assertTrue(f.getType().equals(Long.TYPE));
1478:
1479: f = notAdapted.getDeclaredField("serialPersistentFields");
1480: mods = f.getModifiers();
1481: assertTrue(Modifier.isPrivate(mods));
1482: assertTrue(Modifier.isStatic(mods));
1483: assertTrue(Modifier.isFinal(mods));
1484: assertTrue(f.getType().equals(ObjectStreamField[].class));
1485:
1486: f = adapted.getDeclaredField("serialPersistentFields");
1487: mods = f.getModifiers();
1488: assertTrue(Modifier.isPrivate(mods));
1489: assertTrue(Modifier.isStatic(mods));
1490: assertTrue(Modifier.isFinal(mods));
1491: assertTrue(f.getType().equals(ObjectStreamField[].class));
1492: }
1493:
1494: private Object invokeWithDefaultArgs(String methodName)
1495: throws Exception {
1496: return invokeWithArgs(methodName, WITH_ARGS_PARAMS,
1497: WITH_ARGS_ARGS);
1498: }
1499:
1500: private Object invokeWithArgs(String methodName, Class[] ptypes,
1501: Object[] args) throws Exception {
1502: Class c = classLoader.loadClass(targetClassName);
1503: setTargetClientObjectManager(c);
1504: Object instance = c.newInstance();
1505:
1506: if (instance instanceof Manageable) {
1507: if (testClientObjectManager.isManaged(instance)) {
1508: ((Manageable) instance)
1509: .__tc_managed(testClientObjectManager
1510: .lookupOrCreate(instance));
1511: }
1512: }
1513:
1514: boolean failOnException = false;
1515: return invokeMethod(c, instance, methodName, ptypes, args,
1516: failOnException);
1517: }
1518:
1519: private void invokeWithDefaultArgsAndCheckForProperException(
1520: String methodName) throws Exception {
1521: invokeWithArgsAndCheckForProperException(methodName,
1522: WITH_ARGS_PARAMS, WITH_ARGS_ARGS);
1523: }
1524:
1525: private void invokeWithArgsAndCheckForProperException(
1526: String methodName, Class[] ptypes, Object[] args)
1527: throws Exception {
1528: try {
1529: invokeWithArgs(methodName, ptypes, args);
1530: fail("Should have thrown an exception");
1531: } catch (InvocationTargetException e) {
1532: // expected
1533: assertExceptionType(e);
1534: }
1535: }
1536:
1537: private Object invokeWithNoArgs(String methodName) throws Exception {
1538: return invokeWithArgs(methodName, new Class[] {},
1539: new Object[] {});
1540: }
1541:
1542: private void invokeWithNoArgsAndCheckForProperException(
1543: String methodName) throws Exception {
1544: try {
1545: invokeWithNoArgs(methodName);
1546: fail("Should have thrown an exception.");
1547: } catch (InvocationTargetException e) {
1548: // expected
1549: assertExceptionType(e);
1550: }
1551: }
1552:
1553: /**
1554: * Adds and
1555: */
1556: private void checkWithNamedLock(String methodName,
1557: boolean withArgs, boolean checkForException,
1558: String modifiersPattern, String parametersPattern)
1559: throws Exception {
1560: initializeConfig();
1561: createNamedLockDefinition("testLock");
1562: createLockConfigurationForMethodExpression(modifiersPattern,
1563: methodName, parametersPattern);
1564:
1565: initClassLoader();
1566:
1567: assertNoTransactions();
1568: if (withArgs) {
1569: if (checkForException) {
1570: invokeWithDefaultArgsAndCheckForProperException(methodName);
1571: } else {
1572: invokeWithDefaultArgs(methodName);
1573: }
1574: } else {
1575: if (checkForException) {
1576: invokeWithNoArgsAndCheckForProperException(methodName);
1577: } else {
1578: invokeWithNoArgs(methodName);
1579: }
1580: }
1581: assertNoAutolocks();
1582: assertNamedLockConditionsPostInvocation(1);
1583: }
1584:
1585: private void checkWithNamedLockNoArgs(String methodName,
1586: String modifiersPattern, String parametersPattern)
1587: throws Exception {
1588: checkWithNamedLock(methodName, false, false, modifiersPattern,
1589: parametersPattern);
1590: }
1591:
1592: private void checkWithNamedLockDefaultArgs(String methodName,
1593: String modifiersPattern, String parametersPattern)
1594: throws Exception {
1595: checkWithNamedLock(methodName, true, false, modifiersPattern,
1596: parametersPattern);
1597: }
1598:
1599: private void checkWithNamedLockNoArgsThrowsException(
1600: String methodName, String modifiersPattern,
1601: String parametersPattern) throws Exception {
1602: checkWithNamedLock(methodName, false, true, modifiersPattern,
1603: parametersPattern);
1604: }
1605:
1606: private void checkWithnamedLockDefaultArgsThrowsException(
1607: String methodName, String modifiersPattern,
1608: String parametersPattern) throws Exception {
1609: checkWithNamedLock(methodName, true, true, modifiersPattern,
1610: parametersPattern);
1611: }
1612:
1613: private void assertAutolockConditionsPostInvocation(
1614: int expectedTransactionCount) {
1615: assertTrue("Transaction count " + getTransactionCount()
1616: + " should be greater than or equal to"
1617: + " expected autolocks: " + expectedTransactionCount,
1618: getTransactionCount() >= expectedTransactionCount);
1619: assertAutolockCount(expectedTransactionCount);
1620: assertTransactionCount(expectedTransactionCount);
1621: assertNoAutolockLiteral();
1622: }
1623:
1624: private void assertNamedLockConditionsPostInvocation(
1625: int expectedTransactionCount) {
1626: assertNamedLockConditionsPostInvocation(
1627: expectedTransactionCount,
1628: new LockDefinition[] { this .lockDefinition });
1629: }
1630:
1631: private void assertNamedLockConditionsPostInvocation(
1632: int expectedTransactionCount, LockDefinition[] lockDefs) {
1633: for (int i = 0; i < lockDefs.length; i++) {
1634: assertTrue(checkForLockName(lockDefs[i].getLockName(),
1635: lockDefs[i].getLockLevelAsInt()));
1636: assertTrue(checkForLock(lockDefs[i]));
1637: }
1638: assertTransactionCount(expectedTransactionCount);
1639: }
1640:
1641: private void assertExceptionType(InvocationTargetException e) {
1642: Throwable target = e.getTargetException();
1643: // Must compare names, since the classes aren't the same (different
1644: // classloaders).
1645: assertEquals(LockTestThrowsExceptionException.class.getName(),
1646: target.getClass().getName());
1647: }
1648:
1649: private void assertTransactionCount(int transactionCount) {
1650: assertEquals(transactionCount, getTransactionCount());
1651: assertEquals(transactionCount, testTransactionManager
1652: .getCommitCount());
1653: }
1654:
1655: private int getTransactionCount() {
1656: return testTransactionManager.getBegins().size();
1657: }
1658:
1659: private void assertAutolockCount(int autolockCount) {
1660: assertEquals(autolockCount, getAutolockBeginCount());
1661: }
1662:
1663: private void assertNoTransactions() {
1664: assertTransactionCount(0);
1665: }
1666:
1667: private void assertNoAutolocks() {
1668: assertAutolockCount(0);
1669: assertNoAutolockLiteral();
1670: }
1671:
1672: private int getAutolockBeginCount() {
1673: int rv = 0;
1674: for (Iterator i = testTransactionManager.getBegins().iterator(); i
1675: .hasNext();) {
1676: Begin b = (Begin) i.next();
1677: // hack
1678: if (b.lockName.startsWith("@")) {
1679: rv++;
1680: }
1681: }
1682: return rv;
1683: }
1684:
1685: private boolean checkForLocks(LockDefinition[] lockDefs) {
1686: for (int i = 0; i < lockDefs.length; i++) {
1687: if (!checkForLock(lockDefs[i]))
1688: return false;
1689: }
1690: return true;
1691: }
1692:
1693: private boolean checkForLock(LockDefinition lockdef) {
1694: return checkForLock(lockdef, this .testTransactionManager
1695: .getBegins());
1696: }
1697:
1698: /**
1699: * Returns true if the lock name of the LockDefinition is found in the List. The List should be a list of String
1700: * arrays, each corresponding to a call to TransactionManager.begin(String[] locks)
1701: */
1702: private boolean checkForLock(LockDefinition lockdef,
1703: List beginTransactions) {
1704: boolean rv = false;
1705: for (Iterator iter = beginTransactions.iterator(); iter
1706: .hasNext();) {
1707: rv = checkForLock(lockdef, (Begin) iter.next());
1708: if (rv)
1709: break;
1710: }
1711: return rv;
1712: }
1713:
1714: /**
1715: * Returns true if the lock name of the LockDefinition is found in the String array.
1716: */
1717: private boolean checkForLock(LockDefinition lockdef, Begin lock) {
1718: boolean rv = false;
1719: if (lock != null) {
1720: rv = lockdef.getLockName().equals(
1721: ByteCodeUtil
1722: .stripGeneratedLockHeader(lock.lockName));
1723: if (rv) {
1724: // make sure that the lock type is the same
1725: rv = checkLockType(lockdef, lock.lockType);
1726: }
1727: }
1728: return rv;
1729: }
1730:
1731: private boolean checkForLockName(String lockName, int lockType) {
1732: List begins = this .testTransactionManager.getBegins();
1733: for (Iterator i = begins.iterator(); i.hasNext();) {
1734: Begin lock = (Begin) i.next();
1735: if (checkForLockName(lockName, lock))
1736: return true;
1737: }
1738: return false;
1739: }
1740:
1741: private static boolean checkForLockName(String lockName, Begin lock) {
1742: return ByteCodeUtil.stripGeneratedLockHeader(lock.lockName)
1743: .equals(lockName);
1744: }
1745:
1746: /**
1747: * Check the string representing the actual lock used to make sure that it is the same type
1748: */
1749: private boolean checkLockType(LockDefinition lockdef, int lockType) {
1750: return (lockdef.getLockLevelAsInt() == lockType);
1751: }
1752:
1753: private void assertNoAutolockLiteral() {
1754: assertFalse(checkForLockName(LockDefinition.TC_AUTOLOCK_NAME,
1755: com.tc.object.lockmanager.api.LockLevel.WRITE));
1756: }
1757:
1758: private void createNamedLockDefinition(String lockName) {
1759: this .config.addIncludePattern(this .targetClassName);
1760: this .lockDefinition = new LockDefinitionImpl(lockName,
1761: ConfigLockLevel.WRITE);
1762: this .lockDefinition.commit();
1763: }
1764:
1765: private void createLockConfigurationForMethodExpression(
1766: String modifiersPattern, String testMethodName,
1767: String parameterPattern) {
1768: String methodExpression = modifiersPattern + " "
1769: + targetClassName + "." + testMethodName
1770: + parameterPattern;
1771:
1772: config.getOrCreateSpec(targetClassName);
1773: config.addLock(methodExpression, lockDefinition);
1774: }
1775:
1776: private Object callNoArgCtor() throws Exception {
1777: // we're going to be clearing the counts, so make sure the caller assumes no TXNs to start with
1778: assertNoTransactions();
1779:
1780: // this makes sure the class initializer is called
1781: Class c = Class
1782: .forName(targetClassName, true, this .classLoader);
1783: setTargetClientObjectManager(c);
1784:
1785: // depending on the compiler (eclipse vs. javac, there might methods called that obtain locks)
1786: this .testTransactionManager.clearBegins();
1787: this .testTransactionManager.clearCommitCount();
1788:
1789: Constructor ctor = c.getConstructor(new Class[] {});
1790: try {
1791: return ctor.newInstance(new Object[] {});
1792: } catch (InvocationTargetException ite) {
1793: Throwable t = ite.getTargetException();
1794: if (t instanceof RuntimeException) {
1795: throw (RuntimeException) t;
1796: }
1797: if (t instanceof Error) {
1798: throw (Error) t;
1799: }
1800: throw new RuntimeException(t);
1801: }
1802: }
1803:
1804: }
|