KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > bytecode > ClassAdapterTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
21 import java.lang.reflect.Constructor JavaDoc;
22 import java.lang.reflect.Field JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.Modifier JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.swing.DefaultListModel JavaDoc;
30
31 /**
32  * Test to see if an adapted class has all of the adaptations we expect.
33  */

34 public class ClassAdapterTest extends ClassAdapterTestBase {
35   private static final Class JavaDoc[] WITH_ARGS_PARAMS = new Class JavaDoc[] { Integer.TYPE, String JavaDoc.class };
36   private static final Object JavaDoc[] WITH_ARGS_ARGS = new Object JavaDoc[] { new Integer JavaDoc(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 JavaDoc targetClassName = ClassAdapterTestTarget.class.getName(); // "com.tctest.ClassAdapterTestTarget";
44
private ClassLoader JavaDoc origThreadContextClassLoader;
45
46   protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
83     String JavaDoc tcn = DefaultListModel JavaDoc.class.getName();
84     config.addIncludePattern(tcn);
85     String JavaDoc methodPattern = "* " + tcn + ".*(..)";
86     lockDefinition = new LockDefinition("doStuff", ConfigLockLevel.WRITE);
87     lockDefinition.commit();
88     config.addLock(methodPattern, lockDefinition);
89
90     Class JavaDoc clazz = this.classLoader.loadClass(tcn);
91     clazz.newInstance();
92
93   }
94
95   public void testSuperclassTransients() throws Exception JavaDoc {
96     String JavaDoc supersuperclass = ClassAdapterTestTargetBaseBase.class.getName();
97     TransparencyClassSpec spec = config.getOrCreateSpec(supersuperclass);
98     spec.addTransient("myString");
99
100     String JavaDoc superclass = ClassAdapterTestTargetBase.class.getName();
101     spec = config.getOrCreateSpec(superclass);
102
103     spec = config.getOrCreateSpec(targetClassName);
104
105     // this.config.addAutolock("public void " + targetClassName + ".doStuff()");
106
String JavaDoc methodPattern = "public void " + targetClassName + ".doStuff()";
107     lockDefinition = new LockDefinition("doStuff", ConfigLockLevel.WRITE);
108     lockDefinition.commit();
109     config.addLock(methodPattern, lockDefinition);
110
111     Class JavaDoc clazz = this.classLoader.loadClass(targetClassName);
112     Object JavaDoc o = clazz.newInstance();
113     Method JavaDoc m = clazz.getDeclaredMethod("doStuff", new Class JavaDoc[0]);
114     m.invoke(o, new Object JavaDoc[0]);
115   }
116
117   public void testAssertions() throws Exception JavaDoc {
118     // Make sure that a bogus lock doesn't pass the test...
119
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 JavaDoc {
127     createNamedLockDefinition("test");
128     createLockConfigurationForMethodExpression("*", "*", "(..)");
129     callNoArgCtor();
130     assertTransactionCount(1);
131   }
132
133   public void testWildcardPatternWithAutolockAdaptsOK() throws Exception JavaDoc {
134     createAutolockLockDefinition();
135     createLockConfigurationForMethodExpression("* ", "*", "(..)");
136
137     callNoArgCtor();
138   }
139
140   public void testSynchronizedInstanceMethodWithWideArgs() throws Exception JavaDoc {
141     String JavaDoc methodName = "synchronizedInstanceMethodWithWideArgs";
142     createAutolockLockDefinition();
143     String JavaDoc modifiersPattern = "*";
144     String JavaDoc parametersPattern = "(..)";
145     createLockConfigurationForMethodExpression(modifiersPattern, methodName, parametersPattern);
146
147     this.testClientObjectManager.setIsManaged(true);
148     invokeWithArgs(methodName, new Class JavaDoc[] { Double.TYPE, Long.TYPE }, new Object JavaDoc[] { new Double JavaDoc(0), new Long JavaDoc(0) });
149   }
150
151   public void testInstanceMethodWithNamedLocks() throws Exception JavaDoc {
152     String JavaDoc 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     // Make sure that a bogus lock doesn't pass the test...
165
LockDefinition bogus = new LockDefinition("fakeLock", ConfigLockLevel.WRITE);
166     bogus.commit();
167     assertFalse(checkForLock(bogus));
168
169   }
170
171   public void testInstanceMethodThrowsExceptionWithNamedLocks() throws Exception JavaDoc {
172     String JavaDoc methodName = "instanceMethodThrowsException";
173
174     createNamedLockDefinition("testLock");
175     createLockConfigurationForMethodExpression("void", methodName, "()");
176
177     assertNoTransactions();
178
179     invokeWithNoArgsAndCheckForProperException(methodName);
180
181     // make sure we recorded the lock we expected.
182
assertNamedLockConditionsPostInvocation(1);
183   }
184
185   public void testInstanceMethodWithArgumentsWithNamedLocks() throws Exception JavaDoc {
186
187     String JavaDoc methodName = "instanceMethodWithArguments";
188
189     createNamedLockDefinition("testLock");
190     createLockConfigurationForMethodExpression("void", methodName, "(int, java.lang.String)");
191
192     assertNoTransactions();
193
194     invokeWithDefaultArgs(methodName);
195
196     // check for begin transaction.
197
assertNoAutolocks();
198     assertNamedLockConditionsPostInvocation(1);
199   }
200
201   public void testInstanceMethodWithArgumentsThrowsExceptionWithNamedLocks() throws Exception JavaDoc {
202     String JavaDoc methodName = "instanceMethodWithArgumentsThrowsException";
203
204     createNamedLockDefinition("testLock");
205     createLockConfigurationForMethodExpression("void", methodName, "(int, java.lang.String)");
206
207     assertNoTransactions();
208
209     invokeWithDefaultArgsAndCheckForProperException(methodName);
210
211     // check for begin transaction.
212
assertNoAutolocks();
213     assertNamedLockConditionsPostInvocation(1);
214   }
215
216   public void testSynchronizedInstanceMethodWithAutolock() throws Exception JavaDoc {
217     String JavaDoc testMethodName = "synchronizedInstanceMethod";
218
219     createAutolockLockDefinition();
220     createLockConfigurationForMethodExpression("public synchronized void", testMethodName, "()");
221
222     assertNoTransactions();
223
224     // unmanaged objects shouldn't be autolocked.
225
this.testClientObjectManager.setIsManaged(false);
226     invokeWithNoArgs(testMethodName);
227     assertNoTransactions();
228
229     // managed objects SHOULD be autolocked.
230
this.testClientObjectManager.setIsManaged(true);
231     initClassLoader();
232
233     invokeWithNoArgs(testMethodName);
234
235     assertAutolockConditionsPostInvocation(1);
236   }
237
238   public void testSynchronizedInstanceMethodThrowsExceptionWithAutolock() throws Exception JavaDoc {
239     String JavaDoc testMethodName = "synchronizedInstanceMethodThrowsException";
240
241     createAutolockLockDefinition();
242     createLockConfigurationForMethodExpression("public synchronized void", testMethodName, "()");
243
244     // int modifiers = getModifiers(testMethodName, new Class[] {});
245

246     // assertTrue(config.isLockMethod(modifiers, targetClassName,
247
// testMethodName, "()V", null));
248
// assertTrue(config.isAutolock(modifiers, targetClassName, testMethodName,
249
// "()V", null));
250
assertNoTransactions();
251
252     // unmanaged objects shouldn't be autolocked.
253
this.testClientObjectManager.setIsManaged(false);
254     invokeWithNoArgsAndCheckForProperException(testMethodName);
255
256     assertNoTransactions();
257
258     // managed objects SHOULD be autolocked.
259
this.testClientObjectManager.setIsManaged(true);
260     initClassLoader();
261     invokeWithNoArgsAndCheckForProperException(testMethodName);
262
263     assertAutolockConditionsPostInvocation(1);
264   }
265
266   public void testSynchronizedInstanceMethodWithArgumentsWithAutolock() throws Exception JavaDoc {
267     String JavaDoc methodName = "synchronizedInstanceMethodWithArguments";
268
269     createAutolockLockDefinition();
270     createLockConfigurationForMethodExpression("public synchronized void", methodName, "(int, java.lang.String)");
271
272     // Class[] params = new Class[] { Integer.TYPE, String.class };
273

274     // int modifiers = getModifiers(methodName, params);
275

276     // assertTrue(config.isLockMethod(modifiers, targetClassName, methodName,
277
// "(ILjava/lang/String;)V", null));
278
// assertTrue(config.isAutolock(modifiers, targetClassName, methodName,
279
// "(ILjava/lang/String;)V", null));
280
assertNoTransactions();
281
282     // unmanaged objects shouldn't be autolocked.
283
this.testClientObjectManager.setIsManaged(false);
284     invokeWithDefaultArgs(methodName);
285     assertNoTransactions();
286
287     // managed objects SHOULD be autolocked.
288
this.testClientObjectManager.setIsManaged(true);
289     initClassLoader();
290     invokeWithDefaultArgs(methodName);
291
292     assertAutolockConditionsPostInvocation(1);
293   }
294
295   /**
296    * Tests that autolocks work on synchronized methods.
297    */

298   public void testSynchronizedInstanceMethodWithArgumentsThrowsExceptionWithAutolock() throws Exception JavaDoc {
299     String JavaDoc methodName = "synchronizedInstanceMethodWithArgumentsThrowsException";
300
301     createAutolockLockDefinition();
302     createLockConfigurationForMethodExpression("public synchronized void", methodName, "(int, java.lang.String)");
303
304     // Class[] params = new Class[] { Integer.TYPE, String.class };
305
//
306
// int modifiers = getModifiers(methodName, params);
307

308     // assertTrue(config.isLockMethod(modifiers, targetClassName, methodName,
309
// "(ILjava/lang/String;)V", null));
310
// assertTrue(config.isAutolock(modifiers, targetClassName, methodName,
311
// "(ILjava/lang/String;)V", null));
312
assertNoTransactions();
313
314     // unmanaged objects shouldn't be autolocked.
315
this.testClientObjectManager.setIsManaged(false);
316
317     invokeWithDefaultArgsAndCheckForProperException(methodName);
318     assertNoTransactions();
319
320     // managed objects SHOULD be autolocked.
321
this.testClientObjectManager.setIsManaged(true);
322     initClassLoader();
323     invokeWithDefaultArgsAndCheckForProperException(methodName);
324     assertAutolockConditionsPostInvocation(1);
325   }
326
327   /**
328    * Tests that autolocks work on synchronization inside methods.
329    */

330   public void testInternalSynchronizedInstanceMethodWithAutolock() throws Exception JavaDoc {
331     String JavaDoc testMethodName = "internalSynchronizedInstanceMethod";
332
333     createAutolockLockDefinition();
334     createLockConfigurationForMethodExpression("public void", testMethodName, "()");
335
336     // int modifiers = getModifiers(testMethodName, new Class[] {});
337
//
338
// assertFalse(config.isLockMethod(modifiers, targetClassName,
339
// testMethodName, "()V", null));
340
// assertTrue(config.isAutolock(modifiers, targetClassName, testMethodName,
341
// "()V", null));
342
assertNoTransactions();
343
344     // unmanaged objects shouldn't be autolocked.
345
this.testClientObjectManager.setIsManaged(false);
346     invokeWithNoArgs(testMethodName);
347     assertNoTransactions();
348
349     // managed objects SHOULD be autolocked.
350
this.testClientObjectManager.setIsManaged(true);
351     initClassLoader();
352
353     invokeWithNoArgs(testMethodName);
354
355     assertAutolockConditionsPostInvocation(1);
356   }
357
358   public void testInternalSynchronizedInstanceMethodWithNamedlockAndAutoLock() throws Exception JavaDoc {
359     config.addIncludePattern(this.targetClassName);
360     String JavaDoc methodName = "internalSynchronizedInstanceMethod";
361     // set up locks
362
String JavaDoc 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 JavaDoc {
386     String JavaDoc testMethodName = "internalSynchronizedInstanceMethodThrowsException";
387
388     createAutolockLockDefinition();
389     createLockConfigurationForMethodExpression("public void", testMethodName, "()");
390
391     // int modifiers = getModifiers(testMethodName, new Class[] {});
392
//
393
// assertFalse(config.isLockMethod(modifiers, targetClassName,
394
// testMethodName, NO_ARGS_RETURNS_VOID_DESCRIPTION,
395
// null));
396
// assertTrue(config.isAutolock(modifiers, targetClassName, testMethodName,
397
// NO_ARGS_RETURNS_VOID_DESCRIPTION,
398
// null));
399
assertNoTransactions();
400
401     // unmanaged objects shouldn't be autolocked.
402
this.testClientObjectManager.setIsManaged(false);
403     invokeWithNoArgsAndCheckForProperException(testMethodName);
404     assertNoTransactions();
405
406     // managed objects SHOULD be autolocked.
407
this.testClientObjectManager.setIsManaged(true);
408     initClassLoader();
409
410     invokeWithNoArgsAndCheckForProperException(testMethodName);
411     assertAutolockConditionsPostInvocation(1);
412   }
413
414   public void testInternalSynchronizedInstanceMethodWithArgumentsWithAutolock() throws Exception JavaDoc {
415     String JavaDoc testMethodName = "internalSynchronizedInstanceMethodWithArguments";
416     createAutolockLockDefinition();
417     createLockConfigurationForMethodExpression("public void", testMethodName, "(int, java.lang.String)");
418
419     // int modifiers = getModifiers(testMethodName, WITH_ARGS_PARAMS);
420
// assertFalse(config.isLockMethod(modifiers, targetClassName,
421
// testMethodName, WITH_ARGS_RETURNS_VOID_DESCRIPTION,
422
// null));
423
// assertTrue(config.isAutolock(modifiers, targetClassName, testMethodName,
424
// WITH_ARGS_RETURNS_VOID_DESCRIPTION,
425
// null));
426

427     // unmanaged objects shouldn't be autolocked.
428
this.testClientObjectManager.setIsManaged(false);
429     invokeWithDefaultArgs(testMethodName);
430     assertNoTransactions();
431
432     // managed objects should be autolocked
433
this.testClientObjectManager.setIsManaged(true);
434     initClassLoader();
435
436     invokeWithDefaultArgs(testMethodName);
437     assertAutolockConditionsPostInvocation(1);
438   }
439
440   public void testInternalSynchronizedInstanceMethodWithArgumentsThrowsExceptionWithAutolock() throws Exception JavaDoc {
441     String JavaDoc testMethodName = "internalSynchronizedInstanceMethodWithArgumentsThrowsException";
442     createAutolockLockDefinition();
443     createLockConfigurationForMethodExpression("public void", testMethodName, "(int, java.lang.String)");
444
445     // int modifiers = getModifiers(testMethodName, WITH_ARGS_PARAMS);
446
// assertFalse(config.isLockMethod(modifiers, targetClassName,
447
// testMethodName, WITH_ARGS_RETURNS_VOID_DESCRIPTION,
448
// null));
449
// assertTrue(config.isAutolock(modifiers, targetClassName, testMethodName,
450
// WITH_ARGS_RETURNS_VOID_DESCRIPTION,
451
// null));
452

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 JavaDoc {
465     String JavaDoc 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 JavaDoc {
478     String JavaDoc methodName = "staticMethodThrowsException";
479
480     createNamedLockDefinition("testLock");
481     createLockConfigurationForMethodExpression("void", methodName, "()");
482
483     assertNoTransactions();
484     invokeWithNoArgsAndCheckForProperException(methodName);
485
486     // make sure we recorded the lock we expected.
487
assertNamedLockConditionsPostInvocation(1);
488   }
489
490   public void testStaticMethodWithArgumentsWithNamedLocks() throws Exception JavaDoc {
491     String JavaDoc 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 JavaDoc {
502     String JavaDoc 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 JavaDoc {
513     String JavaDoc testMethodName = "synchronizedStaticMethod";
514     createAutolockLockDefinition();
515     String JavaDoc modifiersPattern = "*";
516     String JavaDoc parametersPattern = "(..)";
517     createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern);
518
519     this.testClientObjectManager.setIsManaged(true);
520     invokeWithNoArgs(testMethodName);
521
522     assertNoTransactions();
523     assertNoAutolocks();
524
525     // make sure that the pattern used to test autolocks actually picks out a
526
// real method.
527
checkWithNamedLockNoArgs(testMethodName, modifiersPattern, parametersPattern);
528   }
529
530   public void testSynchronizedStaticMethodThrowsExceptionWithAutolock() throws Exception JavaDoc {
531     String JavaDoc testMethodName = "synchronizedStaticMethodThrowsException";
532     createAutolockLockDefinition();
533     String JavaDoc modifiersPattern = "*";
534     String JavaDoc parametersPattern = "(..)";
535     createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern);
536
537     this.testClientObjectManager.setIsManaged(true);
538     invokeWithNoArgsAndCheckForProperException(testMethodName);
539
540     assertNoTransactions();
541     assertNoAutolocks();
542
543     // make sure that the pattern used to test autolocks actually picks out a
544
// real method.
545
checkWithNamedLockNoArgsThrowsException(testMethodName, modifiersPattern, parametersPattern);
546   }
547
548   public void testSynchronizedStaticMethodWithArgumentsWithAutolock() throws Exception JavaDoc {
549     String JavaDoc testMethodName = "synchronizedStaticMethodWithArguments";
550     createAutolockLockDefinition();
551     String JavaDoc modifiersPattern = "*";
552     String JavaDoc parametersPattern = "(..)";
553     createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern);
554
555     this.testClientObjectManager.setIsManaged(true);
556     invokeWithDefaultArgs(testMethodName);
557
558     assertNoTransactions();
559     assertNoAutolocks();
560
561     // make sure that the pattern used to test autolocks actually picks out a
562
// real method.
563
checkWithNamedLockDefaultArgs(testMethodName, modifiersPattern, parametersPattern);
564   }
565
566   public void testSynchronizedStaticMethodWithArgumentsThrowsExceptionWithAutolock() throws Exception JavaDoc {
567     String JavaDoc testMethodName = "synchronizedStaticMethodWithArgumentsThrowsException";
568     createAutolockLockDefinition();
569     String JavaDoc modifiersPattern = "*";
570     String JavaDoc 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 JavaDoc {
583     String JavaDoc testMethodName = "internalSynchronizedStaticMethod";
584     createAutolockLockDefinition();
585     String JavaDoc modifiersPattern = "*";
586     String JavaDoc 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 JavaDoc {
596     String JavaDoc testMethodName = "internalSynchronizedStaticMethodThrowsException";
597     createAutolockLockDefinition();
598     String JavaDoc modifiersPattern = "*";
599     String JavaDoc 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 JavaDoc {
609     String JavaDoc testMethodName = "internalSynchronizedStaticMethodWithArguments";
610     createAutolockLockDefinition();
611     String JavaDoc modifiersPattern = "*";
612     String JavaDoc 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 JavaDoc {
622     String JavaDoc testMethodName = "internalSynchronizedStaticMethodWithArgumentsThrowsException";
623     createAutolockLockDefinition();
624     String JavaDoc modifiersPattern = "*";
625     String JavaDoc 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 JavaDoc {
635
636     String JavaDoc 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 JavaDoc {
649     String JavaDoc 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 JavaDoc {
662     String JavaDoc 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 JavaDoc {
675     String JavaDoc 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 JavaDoc {
688     String JavaDoc 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 JavaDoc {
706     String JavaDoc 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 JavaDoc {
724     String JavaDoc 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 JavaDoc {
742
743     String JavaDoc 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 JavaDoc {
762
763     String JavaDoc 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 JavaDoc {
782     String JavaDoc 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 JavaDoc {
801     String JavaDoc 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 JavaDoc {
821     String JavaDoc 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 JavaDoc {
840     String JavaDoc 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 JavaDoc {
852     String JavaDoc 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 JavaDoc {
865     String JavaDoc 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 JavaDoc {
877
878     String JavaDoc methodName = "staticMethodWithArgumentsReturnsAValueThrowsException";
879     createNamedLockDefinition("testLock");
880     createLockConfigurationForMethodExpression("*", methodName, "(..)");
881
882     assertNoTransactions();
883
884     invokeWithDefaultArgsAndCheckForProperException(methodName);
885
886     assertNamedLockConditionsPostInvocation(1);
887   }
888
889   /**
890    * Autolocks should not apply to synchronized static methods because they are synchronized on the Class object which
891    * can't be distributed... we don't think.
892    */

893   public void testSynchronizedStaticMethodReturnsAValueWithAutolocks() throws Exception JavaDoc {
894     String JavaDoc testMethodName = "synchronizedStaticMethodReturnsAValue";
895     createAutolockLockDefinition();
896     String JavaDoc modifiersPattern = "*";
897     String JavaDoc parametersPattern = "(..)";
898     createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern);
899
900     this.testClientObjectManager.setIsManaged(true);
901     invokeWithNoArgs(testMethodName);
902
903     assertNoTransactions();
904     assertNoAutolocks();
905
906     // make sure that the pattern used to test autolocks actually picks out a
907
// real method.
908
checkWithNamedLockNoArgs(testMethodName, modifiersPattern, parametersPattern);
909   }
910
911   public void testSynchronizedStaticMethodReturnsAValueThrowsExceptionWithAutolocks() throws Exception JavaDoc {
912     String JavaDoc testMethodName = "synchronizedStaticMethodReturnsAValueThrowsException";
913     createAutolockLockDefinition();
914     String JavaDoc modifiersPattern = "*";
915     String JavaDoc parametersPattern = "(..)";
916     createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern);
917
918     this.testClientObjectManager.setIsManaged(true);
919     invokeWithNoArgsAndCheckForProperException(testMethodName);
920
921     assertNoTransactions();
922     assertNoAutolocks();
923
924     // make sure that the pattern used to test autolocks actually picks out a
925
// real method.
926
checkWithNamedLockNoArgsThrowsException(testMethodName, modifiersPattern, parametersPattern);
927   }
928
929   public void testSynchronizedStaticMethodWithArgumentsReturnsAValueWithAutolocks() throws Exception JavaDoc {
930     String JavaDoc testMethodName = "synchronizedStaticMethodWithArgumentsReturnsAValue";
931     createAutolockLockDefinition();
932     String JavaDoc modifiersPattern = "*";
933     String JavaDoc parametersPattern = "(..)";
934     createLockConfigurationForMethodExpression(modifiersPattern, testMethodName, parametersPattern);
935
936     this.testClientObjectManager.setIsManaged(true);
937     invokeWithDefaultArgs(testMethodName);
938
939     assertNoTransactions();
940     assertNoAutolocks();
941
942     // make sure that the pattern used to test autolocks actually picks out a
943
// real method.
944
checkWithNamedLockDefaultArgs(testMethodName, modifiersPattern, parametersPattern);
945   }
946
947   public void testSynchronizedStaticMethodWithArgumentsReturnsAValueThrowsExceptionWithAutolocks() throws Exception JavaDoc {
948     String JavaDoc testMethodName = "synchronizedStaticMethodWithArgumentsReturnsAValueThrowsException";
949     createAutolockLockDefinition();
950     String JavaDoc modifiersPattern = "*";
951     String JavaDoc 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   /**
964    * Autolocks SHOULD apply to internally synchronized static methods.
965    */

966   public void testInternalSynchronizedStaticMethodReturnsAValueWithAutolocks() throws Exception JavaDoc {
967
968     String JavaDoc testMethodName = "internalSynchronizedStaticMethodReturnsAValue";
969     createAutolockLockDefinition();
970     String JavaDoc modifiersPattern = "*";
971     String JavaDoc 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 JavaDoc {
981
982     String JavaDoc testMethodName = "internalSynchronizedStaticMethodReturnsAValueThrowsException";
983     createAutolockLockDefinition();
984     String JavaDoc modifiersPattern = "*";
985     String JavaDoc 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 JavaDoc {
995
996     String JavaDoc testMethodName = "internalSynchronizedStaticMethodWithArgumentsReturnsAValue";
997     createAutolockLockDefinition();
998     String JavaDoc modifiersPattern = "*";
999     String JavaDoc 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 JavaDoc {
1010
1011    String JavaDoc testMethodName = "internalSynchronizedStaticMethodWithArgumentsReturnsAValueThrowsException";
1012    createAutolockLockDefinition();
1013    String JavaDoc modifiersPattern = "*";
1014    String JavaDoc 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 JavaDoc {
1024    String JavaDoc methodName = "instanceMethod";
1025
1026    createAutolockLockDefinition();
1027    String JavaDoc modifiersPattern = "void";
1028    String JavaDoc parametersPattern = "()";
1029    createLockConfigurationForMethodExpression(modifiersPattern, methodName, parametersPattern);
1030
1031    this.testClientObjectManager.setIsManaged(true);
1032    invokeWithNoArgs(methodName);
1033    assertNoTransactions();
1034
1035    // add a regular-old named lock to make sure that the method expression
1036
// actually picks out a method in order
1037
// to validate that the test above actually tested something...
1038
checkWithNamedLockNoArgs(methodName, modifiersPattern, parametersPattern);
1039  }
1040
1041  public void testWildcardAutolock() throws Exception JavaDoc {
1042
1043    String JavaDoc testMethodName = "internalSynchronizedInstanceMethod";
1044
1045    createAutolockLockDefinition();
1046    createLockConfigurationForMethodExpression("*", "*", "(..)");
1047    //
1048
// int modifiers = getModifiers(testMethodName, new Class[] {});
1049
//
1050
// assertFalse(config.isLockMethod(modifiers, targetClassName,
1051
// testMethodName, "()V", null));
1052
// assertTrue(config.isAutolock(modifiers, targetClassName, testMethodName,
1053
// "()V", null));
1054
assertNoTransactions();
1055
1056    // unmanaged objects shouldn't be autolocked.
1057
this.testClientObjectManager.setIsManaged(false);
1058    invokeWithNoArgs(testMethodName);
1059    assertNoTransactions();
1060
1061    // managed objects SHOULD be autolocked.
1062
this.testClientObjectManager.setIsManaged(true);
1063    initClassLoader();
1064
1065    invokeWithNoArgs(testMethodName);
1066
1067    assertAutolockConditionsPostInvocation(1);
1068  }
1069
1070  public void testReadLock() throws Exception JavaDoc {
1071    config.addIncludePattern(this.targetClassName);
1072    String JavaDoc 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 JavaDoc {
1086    String JavaDoc methodName = "internalSynchronizedInstanceMethod";
1087
1088    invokeWithNoArgs(methodName);
1089
1090    assertTransactionCount(0);
1091  }
1092
1093  public void testAutolockCtorNoException() throws Exception JavaDoc {
1094    String JavaDoc 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 JavaDoc {
1113    String JavaDoc 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 JavaDoc {
1128    String JavaDoc 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 JavaDoc();
1142    } catch (RuntimeException JavaDoc re) {
1143      if ((re.getClass() != RuntimeException JavaDoc.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 JavaDoc {
1152    String JavaDoc 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 JavaDoc 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 JavaDoc();
1171    } catch (RuntimeException JavaDoc re) {
1172      if ((re.getClass() != RuntimeException JavaDoc.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 JavaDoc {
1182    testAutolockCtorException(false);
1183  }
1184
1185  public void testAutolockCtorExceptionInsideSynch() throws Exception JavaDoc {
1186    testAutolockCtorException(true);
1187  }
1188
1189  public void testNestedAutolocks() throws Exception JavaDoc {
1190    String JavaDoc methodName = "nestedInternalSynchronizedInstanceMethod";
1191
1192    createAutolockLockDefinition();
1193    createLockConfigurationForMethodExpression("*", "*", "(..)");
1194
1195    this.testClientObjectManager.setIsManaged(true);
1196    assertNoTransactions();
1197
1198    Object JavaDoc result = invokeWithNoArgs(methodName);
1199    int expectedTransactionCount = ((Integer JavaDoc) result).intValue();
1200    assertAutolockConditionsPostInvocation(expectedTransactionCount);
1201  }
1202
1203  public void testMultipleNamedLocks() throws Exception JavaDoc {
1204    config.addIncludePattern(this.targetClassName);
1205    String JavaDoc methodName = "instanceMethod";
1206    String JavaDoc 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 JavaDoc {
1227    config.addIncludePattern(this.targetClassName);
1228    String JavaDoc methodName = "internalSynchronizedInstanceMethod";
1229    // set up locks
1230
String JavaDoc 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, ldautolock);
1238
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 JavaDoc {
1255    config.addIncludePattern(this.targetClassName);
1256    String JavaDoc methodName = "synchronizedInstanceMethod";
1257    // set up locks
1258
String JavaDoc 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 JavaDoc {
1282    config.getOrCreateSpec(targetClassName);
1283    Class JavaDoc notAdapted = Class.forName(targetClassName);
1284    Class JavaDoc adapted = classLoader.loadClass(targetClassName);
1285    assertNotSame(notAdapted, adapted);
1286
1287    Field JavaDoc 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 JavaDoc[].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 JavaDoc[].class));
1314  }
1315
1316  private Object JavaDoc invokeWithDefaultArgs(String JavaDoc methodName) throws Exception JavaDoc {
1317    return invokeWithArgs(methodName, WITH_ARGS_PARAMS, WITH_ARGS_ARGS);
1318  }
1319
1320  private Object JavaDoc invokeWithArgs(String JavaDoc methodName, Class JavaDoc[] ptypes, Object JavaDoc[] args) throws Exception JavaDoc {
1321    Class JavaDoc c = classLoader.loadClass(targetClassName);
1322    Object JavaDoc 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 JavaDoc methodName) throws Exception JavaDoc {
1335    invokeWithArgsAndCheckForProperException(methodName, WITH_ARGS_PARAMS, WITH_ARGS_ARGS);
1336  }
1337
1338  private void invokeWithArgsAndCheckForProperException(String JavaDoc methodName, Class JavaDoc[] ptypes, Object JavaDoc[] args)
1339      throws Exception JavaDoc {
1340    try {
1341      invokeWithArgs(methodName, ptypes, args);
1342      fail("Should have thrown an exception");
1343    } catch (InvocationTargetException JavaDoc e) {
1344      // expected
1345
assertExceptionType(e);
1346    }
1347  }
1348
1349  private Object JavaDoc invokeWithNoArgs(String JavaDoc methodName) throws Exception JavaDoc {
1350    return invokeWithArgs(methodName, new Class JavaDoc[] {}, new Object JavaDoc[] {});
1351  }
1352
1353  private void invokeWithNoArgsAndCheckForProperException(String JavaDoc methodName) throws Exception JavaDoc {
1354    try {
1355      invokeWithNoArgs(methodName);
1356      fail("Should have thrown an exception.");
1357    } catch (InvocationTargetException JavaDoc e) {
1358      // expected
1359
assertExceptionType(e);
1360    }
1361  }
1362
1363  /**
1364   * Adds and
1365   */

1366  private void checkWithNamedLock(String JavaDoc methodName, boolean withArgs, boolean checkForException,
1367                                  String JavaDoc modifiersPattern, String JavaDoc parametersPattern) throws Exception JavaDoc {
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 JavaDoc methodName, String JavaDoc modifiersPattern, String JavaDoc parametersPattern)
1393      throws Exception JavaDoc {
1394    checkWithNamedLock(methodName, false, false, modifiersPattern, parametersPattern);
1395  }
1396
1397  private void checkWithNamedLockDefaultArgs(String JavaDoc methodName, String JavaDoc modifiersPattern, String JavaDoc parametersPattern)
1398      throws Exception JavaDoc {
1399    checkWithNamedLock(methodName, true, false, modifiersPattern, parametersPattern);
1400  }
1401
1402  private void checkWithNamedLockNoArgsThrowsException(String JavaDoc methodName, String JavaDoc modifiersPattern,
1403                                                       String JavaDoc parametersPattern) throws Exception JavaDoc {
1404    checkWithNamedLock(methodName, false, true, modifiersPattern, parametersPattern);
1405  }
1406
1407  private void checkWithnamedLockDefaultArgsThrowsException(String JavaDoc methodName, String JavaDoc modifiersPattern,
1408                                                            String JavaDoc parametersPattern) throws Exception JavaDoc {
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 JavaDoc e) {
1433    Throwable JavaDoc target = e.getTargetException();
1434    // Must compare names, since the classes aren't the same (different
1435
// classloaders).
1436
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 JavaDoc i = testTransactionManager.getBegins().iterator(); i.hasNext();) {
1464      Begin b = (Begin) i.next();
1465      // hack
1466
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  /**
1485   * Returns true if the lock name of the LockDefinition is found in the List. The List should be a list of String
1486   * arrays, each corresponding to a call to TransactionManager.begin(String[] locks)
1487   */

1488  private boolean checkForLock(LockDefinition lockdef, List JavaDoc beginTransactions) {
1489    boolean rv = false;
1490    for (Iterator JavaDoc iter = beginTransactions.iterator(); iter.hasNext();) {
1491      rv = checkForLock(lockdef, (Begin) iter.next());
1492      if (rv) break;
1493    }
1494    return rv;
1495  }
1496
1497  /**
1498   * Returns true if the lock name of the LockDefinition is found in the String array.
1499   */

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        // make sure that the lock type is the same
1506
rv = checkLockType(lockdef, lock.lockType);
1507      }
1508    }
1509    return rv;
1510  }
1511
1512  private boolean checkForLockName(String JavaDoc lockName, int lockType) {
1513    List JavaDoc begins = this.testTransactionManager.getBegins();
1514    for (Iterator JavaDoc 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 JavaDoc lockName, Begin lock) {
1522    return ByteCodeUtil.stripGeneratedLockHeader(lock.lockName).equals(lockName);
1523  }
1524
1525  /**
1526   * Check the string representing the actual lock used to make sure that it is the same type
1527   */

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 JavaDoc 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 JavaDoc modifiersPattern, String JavaDoc testMethodName,
1543                                                          String JavaDoc parameterPattern) {
1544    String JavaDoc methodExpression = modifiersPattern + " " + targetClassName + "." + testMethodName + parameterPattern;
1545
1546    config.getOrCreateSpec(targetClassName);
1547    config.addLock(methodExpression, lockDefinition);
1548  }
1549
1550  private Object JavaDoc callNoArgCtor() throws Exception JavaDoc {
1551    // we're going to be clearing the counts, so make sure the caller assumes no TXNs to start with
1552
assertNoTransactions();
1553
1554    // this makes sure the class initializer is called
1555
Class JavaDoc c = Class.forName(targetClassName, true, this.classLoader);
1556
1557    // depending on the compiler (eclipse vs. javac, there might methods called that obtain locks)
1558
this.testTransactionManager.clearBegins();
1559    this.testTransactionManager.clearCommitCount();
1560
1561    Constructor JavaDoc ctor = c.getConstructor(new Class JavaDoc[] {});
1562    try {
1563      return ctor.newInstance(new Object JavaDoc[] {});
1564    } catch (InvocationTargetException JavaDoc ite) {
1565      Throwable JavaDoc t = ite.getTargetException();
1566      if (t instanceof RuntimeException JavaDoc) { throw (RuntimeException JavaDoc) t; }
1567      if (t instanceof Error JavaDoc) { throw (Error JavaDoc) t; }
1568      throw new RuntimeException JavaDoc(t);
1569    }
1570  }
1571
1572}
1573
Popular Tags