KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > optimistic > TxInterceptorTest


1 /*
2  * Created on 17-Feb-2005
3  *
4  *
5  *
6  */

7 package org.jboss.cache.optimistic;
8
9 import org.jboss.cache.CacheImpl;
10 import org.jboss.cache.GlobalTransaction;
11 import org.jboss.cache.OptimisticTransactionEntry;
12 import org.jboss.cache.TransactionTable;
13 import org.jboss.cache.loader.SamplePojo;
14 import org.jboss.cache.marshall.MethodCall;
15 import org.jboss.cache.marshall.MethodCallFactory;
16 import org.jboss.cache.marshall.MethodDeclarations;
17 import org.jboss.cache.transaction.DummyTransactionManager;
18
19 import javax.transaction.Transaction JavaDoc;
20 import java.util.List JavaDoc;
21
22 public class TxInterceptorTest extends AbstractOptimisticTestCase
23 {
24    public TxInterceptorTest(String JavaDoc name)
25    {
26       super(name);
27    }
28
29    public void testNoTransaction() throws Exception JavaDoc
30    {
31
32       CacheImpl cache = createCache();
33       MockInterceptor dummy = new MockInterceptor();
34       dummy.setCache(cache);
35
36       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
37       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
38       assertNull(mgr.getTransaction());
39       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
40       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
41
42       SamplePojo pojo = new SamplePojo(21, "test");
43
44       cache.put("/one/two", "key1", pojo);
45       assertNull(mgr.getTransaction());
46       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
47       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
48
49       //make sure all calls were done in right order
50

51       List JavaDoc calls = dummy.getAllCalled();
52
53       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
54       assertEquals(MethodDeclarations.commitMethod, calls.get(1));
55       //flesh this out a bit more
56

57    }
58
59    public void testLocalTransactionExists() throws Exception JavaDoc
60    {
61
62       CacheImpl cache = createCache();
63       MockInterceptor dummy = new MockInterceptor();
64       dummy.setCache(cache);
65
66       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
67
68       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
69
70       //start local transaction
71
mgr.begin();
72       Transaction tx = mgr.getTransaction();
73
74       SamplePojo pojo = new SamplePojo(21, "test");
75
76       assertNotNull(mgr.getTransaction());
77       TransactionTable txTable = cache.getTransactionTable();
78       assertNull(txTable.get(tx));
79
80       cache.put("/one/two", "key1", pojo);
81
82       assertNotNull(mgr.getTransaction());
83       mgr.commit();
84
85       assertNull(mgr.getTransaction());
86
87       List JavaDoc calls = dummy.getAllCalled();
88       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
89       assertEquals(MethodDeclarations.commitMethod, calls.get(1));
90
91       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
92       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
93       cache.stop();
94
95    }
96
97    public void testRollbackTransaction() throws Exception JavaDoc
98    {
99
100       CacheImpl cache = createCache();
101       MockInterceptor dummy = new MockInterceptor();
102       dummy.setCache(cache);
103
104       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
105       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
106
107       //start local transaction
108
mgr.begin();
109
110       SamplePojo pojo = new SamplePojo(21, "test");
111
112       cache.put("/one/two", "key1", pojo);
113
114       assertNotNull(mgr.getTransaction());
115       mgr.rollback();
116
117       assertNull(mgr.getTransaction());
118
119       List JavaDoc calls = dummy.getAllCalled();
120       assertEquals(1, calls.size());
121       assertEquals(MethodDeclarations.rollbackMethod, calls.get(0));
122
123
124       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
125       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
126       cache.stop();
127
128    }
129
130    public void testEmptyLocalTransaction() throws Exception JavaDoc
131    {
132       CacheImpl cache = createCache();
133       MockInterceptor dummy = new MockInterceptor();
134       dummy.setCache(cache);
135
136       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
137       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
138
139       //start local transaction
140
mgr.begin();
141
142       assertNotNull(mgr.getTransaction());
143       mgr.commit();
144
145       assertNull(mgr.getTransaction());
146
147       List JavaDoc calls = dummy.getAllCalled();
148       assertEquals(0, calls.size());
149
150       assertNull(mgr.getTransaction());
151
152       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
153       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
154
155       cache.stop();
156    }
157
158    public void testEmptyRollbackLocalTransaction() throws Exception JavaDoc
159    {
160
161       CacheImpl cache = createCache();
162       MockInterceptor dummy = new MockInterceptor();
163       dummy.setCache(cache);
164
165       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
166
167       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
168
169       //start local transaction
170
mgr.begin();
171
172       assertNotNull(mgr.getTransaction());
173       mgr.rollback();
174
175       assertNull(mgr.getTransaction());
176
177       List JavaDoc calls = dummy.getAllCalled();
178       assertEquals(0, calls.size());
179
180       assertNull(mgr.getTransaction());
181
182       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
183       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
184       cache.stop();
185
186    }
187
188    public void testLocalRollbackAftercommitTransaction() throws Exception JavaDoc
189    {
190
191       CacheImpl cache = createCache();
192       MockInterceptor dummy = new MockInterceptor();
193       dummy.setCache(cache);
194
195       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
196
197       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
198
199       //start local transaction
200
mgr.begin();
201
202       SamplePojo pojo = new SamplePojo(21, "test");
203
204       cache.put("/one/two", "key1", pojo);
205
206       assertNotNull(mgr.getTransaction());
207       mgr.commit();
208
209       assertNull(mgr.getTransaction());
210
211       List JavaDoc calls = dummy.getAllCalled();
212       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
213       assertEquals(MethodDeclarations.commitMethod, calls.get(1));
214       boolean failed = false;
215       try
216       {
217          mgr.rollback();
218          fail();
219       }
220       catch (Exception JavaDoc e)
221       {
222          e.printStackTrace();
223          failed = true;
224          assertTrue(true);
225       }
226       assertTrue(failed);
227       assertNull(mgr.getTransaction());
228       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
229       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
230       cache.stop();
231    }
232
233
234    public void testgtxTransactionExists() throws Exception JavaDoc
235    {
236       CacheImpl cache = createCache();
237       MockInterceptor dummy = new MockInterceptor();
238       dummy.setCache(cache);
239
240       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
241       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
242
243       //start local transaction
244
mgr.begin();
245       Transaction tx = mgr.getTransaction();
246
247       //this sets
248
cache.getCurrentTransaction(tx);
249
250       SamplePojo pojo = new SamplePojo(21, "test");
251
252       cache.put("/one/two", "key1", pojo);
253
254       assertNotNull(mgr.getTransaction());
255       mgr.commit();
256
257       assertNull(mgr.getTransaction());
258
259       List JavaDoc calls = dummy.getAllCalled();
260       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
261       assertEquals(MethodDeclarations.commitMethod, calls.get(1));
262
263
264       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
265       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
266
267       cache.stop();
268    }
269
270
271    public void testRemotePrepareTransaction() throws Exception JavaDoc
272    {
273
274       CacheImpl cache = createCache();
275       MockInterceptor dummy = new MockInterceptor();
276       dummy.setCache(cache);
277
278       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
279       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
280
281       //start local transaction
282
mgr.begin();
283       Transaction tx = mgr.getTransaction();
284
285
286       SamplePojo pojo = new SamplePojo(21, "test");
287
288       cache.put("/one/two", "key1", pojo);
289
290       GlobalTransaction gtx = cache.getCurrentTransaction(tx);
291       TransactionTable table = cache.getTransactionTable();
292       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
293       assertNotNull(mgr.getTransaction());
294       mgr.commit();
295
296       //test local calls
297
List JavaDoc calls = dummy.getAllCalled();
298       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
299       assertEquals(MethodDeclarations.commitMethod, calls.get(1));
300       assertNull(mgr.getTransaction());
301
302       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
303       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
304
305
306       GlobalTransaction remoteGtx = new GlobalTransaction();
307
308       remoteGtx.setAddress(new DummyAddress());
309       //hack the method call to make it have the remote gtx
310
MethodCall meth = entry.getModifications().get(0);
311
312       meth.getArgs()[0] = remoteGtx;
313       //call our remote method
314
MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
315       try
316       {
317          cache._replicate(prepareMethod);
318       }
319       catch (Throwable JavaDoc t)
320       {
321          fail();
322       }
323
324       //our thread should still be null
325
assertNull(mgr.getTransaction());
326
327       //there should be a registration for the remote gtx
328
assertNotNull(table.get(remoteGtx));
329       assertNotNull(table.getLocalTransaction(remoteGtx));
330
331       //assert that the method has been passed up the stack
332
calls = dummy.getAllCalled();
333       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
334
335       //assert we have the tx in th table
336
assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
337       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
338       cache.stop();
339    }
340
341    public void testRemotePrepareSuspendTransaction() throws Exception JavaDoc
342    {
343
344       CacheImpl cache = createCache();
345       MockInterceptor dummy = new MockInterceptor();
346       dummy.setCache(cache);
347
348       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
349       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
350
351       //start local transaction
352
mgr.begin();
353       Transaction tx = mgr.getTransaction();
354
355
356       SamplePojo pojo = new SamplePojo(21, "test");
357
358       cache.put("/one/two", "key1", pojo);
359
360       GlobalTransaction gtx = cache.getCurrentTransaction(tx);
361       TransactionTable table = cache.getTransactionTable();
362       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
363       assertEquals(tx, mgr.getTransaction());
364
365       //now send the remote prepare
366

367       GlobalTransaction remoteGtx = new GlobalTransaction();
368
369       remoteGtx.setAddress(new DummyAddress());
370       //hack the method call to make it have the remote gtx
371
MethodCall meth = entry.getModifications().get(0);
372
373       meth.getArgs()[0] = remoteGtx;
374       //call our remote method
375
MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
376       try
377       {
378          cache._replicate(prepareMethod);
379       }
380       catch (Throwable JavaDoc t)
381       {
382          t.printStackTrace();
383          fail();
384       }
385
386       //we should have the same transaction back again
387
assertEquals(tx, mgr.getTransaction());
388
389       // there should be a registration for the remote gtx
390
assertNotNull(table.get(remoteGtx));
391       assertNotNull(table.getLocalTransaction(remoteGtx));
392
393
394       List JavaDoc calls = dummy.getAllCalled();
395       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
396
397       //assert we have two current transactions
398
assertEquals(2, cache.getTransactionTable().getNumGlobalTransactions());
399       assertEquals(2, cache.getTransactionTable().getNumLocalTransactions());
400
401       //commit the local tx
402
mgr.commit();
403
404       //check local calls
405
calls = dummy.getAllCalled();
406       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(1));
407       assertEquals(MethodDeclarations.commitMethod, calls.get(2));
408
409       //assert we have only 1 transaction left
410

411       assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
412       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
413
414       assertNotNull(table.get(remoteGtx));
415       assertNotNull(table.getLocalTransaction(remoteGtx));
416
417       assertNull(table.get(gtx));
418       assertNull(table.getLocalTransaction(gtx));
419       //assert we are no longer associated
420
assertEquals(null, mgr.getTransaction());
421       cache.stop();
422    }
423
424    public void testRemoteCommitSuspendTransaction() throws Exception JavaDoc
425    {
426
427       CacheImpl cache = createCache();
428       MockInterceptor dummy = new MockInterceptor();
429       dummy.setCache(cache);
430
431       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true));
432
433       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
434
435       //start local transaction
436
mgr.begin();
437       Transaction tx = mgr.getTransaction();
438
439       //this sets
440
cache.getCurrentTransaction(tx);
441
442       SamplePojo pojo = new SamplePojo(21, "test");
443
444       cache.put("/one/two", "key1", pojo);
445
446       GlobalTransaction gtx = cache.getCurrentTransaction(tx);
447       TransactionTable table = cache.getTransactionTable();
448       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
449       assertEquals(tx, mgr.getTransaction());
450
451       //now send the remote prepare
452

453       GlobalTransaction remoteGtx = new GlobalTransaction();
454
455       remoteGtx.setAddress(new DummyAddress());
456       //hack the method call to make it have the remote gtx
457
MethodCall meth = entry.getModifications().get(0);
458
459       meth.getArgs()[0] = remoteGtx;
460       //call our remote method
461
MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
462       try
463       {
464          cache._replicate(prepareMethod);
465       }
466       catch (Throwable JavaDoc t)
467       {
468          fail();
469       }
470
471       assertEquals(2, cache.getTransactionTable().getNumGlobalTransactions());
472       assertEquals(2, cache.getTransactionTable().getNumLocalTransactions());
473
474 // call our remote method
475
MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, remoteGtx);
476       try
477       {
478          cache._replicate(commitMethod);
479       }
480       catch (Throwable JavaDoc t)
481       {
482          t.printStackTrace();
483          fail();
484       }
485
486       //we should have the same transaction back again
487
assertEquals(tx, mgr.getTransaction());
488
489       // there should be a registration for the remote gtx
490
assertNull(table.get(remoteGtx));
491       assertNull(table.getLocalTransaction(remoteGtx));
492
493       List JavaDoc calls = dummy.getAllCalled();
494       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
495       assertEquals(MethodDeclarations.commitMethod, calls.get(1));
496
497       assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
498       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
499
500       //commit the local tx
501
mgr.commit();
502
503       calls = dummy.getAllCalled();
504       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
505       assertEquals(MethodDeclarations.commitMethod, calls.get(3));
506
507
508       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
509       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
510
511       assertEquals(null, mgr.getTransaction());
512       cache.stop();
513    }
514
515    public void testRemoteRollbackSuspendTransaction() throws Exception JavaDoc
516    {
517
518       CacheImpl cache = createCache();
519       MockInterceptor dummy = new MockInterceptor();
520       dummy.setCache(cache);
521
522       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true));
523       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
524
525       //start local transaction
526
mgr.begin();
527       Transaction tx = mgr.getTransaction();
528
529       //this sets
530
cache.getCurrentTransaction(tx);
531
532       SamplePojo pojo = new SamplePojo(21, "test");
533
534       cache.put("/one/two", "key1", pojo);
535
536       GlobalTransaction gtx = cache.getCurrentTransaction(tx);
537       TransactionTable table = cache.getTransactionTable();
538       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
539       assertEquals(tx, mgr.getTransaction());
540
541       //now send the remote prepare
542

543       GlobalTransaction remoteGtx = new GlobalTransaction();
544
545       remoteGtx.setAddress(new DummyAddress());
546       //hack the method call to make it have the remote gtx
547
MethodCall meth = entry.getModifications().get(0);
548
549       meth.getArgs()[0] = remoteGtx;
550       //call our remote method
551
MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
552       try
553       {
554          cache._replicate(prepareMethod);
555       }
556       catch (Throwable JavaDoc t)
557       {
558          fail();
559       }
560
561       assertEquals(2, cache.getTransactionTable().getNumGlobalTransactions());
562       assertEquals(2, cache.getTransactionTable().getNumLocalTransactions());
563
564 // call our remote method
565
MethodCall rollbackMethod = MethodCallFactory.create(MethodDeclarations.rollbackMethod, remoteGtx);
566       try
567       {
568          cache._replicate(rollbackMethod);
569       }
570       catch (Throwable JavaDoc t)
571       {
572          fail();
573       }
574
575       //we should have the same transaction back again
576
assertEquals(tx, mgr.getTransaction());
577
578       // there should be a registration for the remote gtx
579
assertNull(table.get(remoteGtx));
580       assertNull(table.getLocalTransaction(remoteGtx));
581
582       List JavaDoc calls = dummy.getAllCalled();
583       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
584       assertEquals(MethodDeclarations.rollbackMethod, calls.get(1));
585
586       assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
587       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
588
589       //commit the local tx
590
mgr.commit();
591
592       calls = dummy.getAllCalled();
593       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
594       assertEquals(MethodDeclarations.commitMethod, calls.get(3));
595
596
597       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
598       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
599
600       assertEquals(null, mgr.getTransaction());
601       cache.stop();
602    }
603
604    public void testRemoteCommitTransaction() throws Exception JavaDoc
605    {
606
607       CacheImpl cache = createCache();
608       MockInterceptor dummy = new MockInterceptor();
609       dummy.setCache(cache);
610
611       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true));
612       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
613
614       //start local transaction
615
mgr.begin();
616       Transaction tx = mgr.getTransaction();
617
618       //this sets
619
cache.getCurrentTransaction(tx);
620
621       SamplePojo pojo = new SamplePojo(21, "test");
622
623       cache.put("/one/two", "key1", pojo);
624
625       GlobalTransaction gtx = cache.getCurrentTransaction(tx);
626       TransactionTable table = cache.getTransactionTable();
627       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
628       assertNotNull(mgr.getTransaction());
629       mgr.commit();
630
631       //test local calls
632
List JavaDoc calls = dummy.getAllCalled();
633       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
634       assertEquals(MethodDeclarations.commitMethod, calls.get(1));
635
636       assertNull(mgr.getTransaction());
637
638       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
639       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
640
641
642       GlobalTransaction remoteGtx = new GlobalTransaction();
643
644       remoteGtx.setAddress(new DummyAddress());
645
646 // hack the method call to make it have the remote gtx
647
MethodCall meth = entry.getModifications().get(0);
648
649       meth.getArgs()[0] = remoteGtx;
650       //call our remote method
651
MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
652       try
653       {
654          cache._replicate(prepareMethod);
655       }
656       catch (Throwable JavaDoc t)
657       {
658          fail();
659       }
660
661       //our thread should be null
662
assertNull(mgr.getTransaction());
663       assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
664       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
665
666       // there should be a registration for the remote gtx
667
assertNotNull(table.get(remoteGtx));
668       assertNotNull(table.getLocalTransaction(remoteGtx));
669       //this is not populated until replication interceptor is used
670
assertEquals(1, table.get(remoteGtx).getModifications().size());
671
672       calls = dummy.getAllCalled();
673       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
674
675       assertNull(mgr.getTransaction());
676 // call our remote method
677
MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, remoteGtx, Boolean.TRUE);
678       try
679       {
680          cache._replicate(commitMethod);
681       }
682       catch (Throwable JavaDoc t)
683       {
684          fail();
685       }
686
687       assertNull(table.get(remoteGtx));
688       assertNull(table.getLocalTransaction(remoteGtx));
689
690       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
691       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
692       assertNull(mgr.getTransaction());
693       cache.stop();
694
695    }
696
697    public void testRemoteRollbackTransaction() throws Exception JavaDoc
698    {
699
700       CacheImpl cache = createCache();
701       MockInterceptor dummy = new MockInterceptor();
702       dummy.setCache(cache);
703
704       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true));
705       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
706
707       //start local transaction
708
mgr.begin();
709       Transaction tx = mgr.getTransaction();
710
711       //this sets
712
cache.getCurrentTransaction(tx);
713
714       SamplePojo pojo = new SamplePojo(21, "test");
715
716       cache.put("/one/two", "key1", pojo);
717
718       GlobalTransaction gtx = cache.getCurrentTransaction(tx);
719       TransactionTable table = cache.getTransactionTable();
720       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
721       assertNotNull(mgr.getTransaction());
722       mgr.commit();
723
724       //test local calls
725
List JavaDoc calls = dummy.getAllCalled();
726       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
727       assertEquals(MethodDeclarations.commitMethod, calls.get(1));
728
729       GlobalTransaction remoteGtx = new GlobalTransaction();
730
731       remoteGtx.setAddress(new DummyAddress());
732
733 // hack the method call to make it have the remote gtx
734
MethodCall meth = entry.getModifications().get(0);
735
736       meth.getArgs()[0] = remoteGtx;
737       //call our remote method
738
MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
739       try
740       {
741          cache._replicate(prepareMethod);
742       }
743       catch (Throwable JavaDoc t)
744       {
745          fail();
746       }
747
748       //our thread should be null
749
assertNull(mgr.getTransaction());
750
751       // there should be a registration for the remote gtx
752
assertNotNull(table.get(remoteGtx));
753       assertNotNull(table.getLocalTransaction(remoteGtx));
754       //this is not populated until replication interceptor is used
755
assertEquals(1, table.get(remoteGtx).getModifications().size());
756
757       calls = dummy.getAllCalled();
758       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
759
760 // call our remote method
761
MethodCall rollbackMethod = MethodCallFactory.create(MethodDeclarations.rollbackMethod, remoteGtx);
762       try
763       {
764          cache._replicate(rollbackMethod);
765       }
766       catch (Throwable JavaDoc t)
767       {
768          fail();
769       }
770
771       calls = dummy.getAllCalled();
772       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
773       assertEquals(MethodDeclarations.rollbackMethod, calls.get(3));
774
775       assertNull(table.get(remoteGtx));
776       assertNull(table.getLocalTransaction(remoteGtx));
777
778       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
779       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
780       cache.stop();
781
782    }
783
784
785    public void testSequentialTransactionExists() throws Exception JavaDoc
786    {
787
788       CacheImpl cache = createCache();
789       MockInterceptor dummy = new MockInterceptor();
790       dummy.setCache(cache);
791
792       cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
793
794       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
795
796       mgr.begin();
797       Transaction tx = mgr.getTransaction();
798       SamplePojo pojo = new SamplePojo(21, "test");
799
800       cache.put("/one/two", "key1", pojo);
801
802       assertNotNull(mgr.getTransaction());
803       mgr.commit();
804
805
806       mgr.begin();
807       Transaction tx1 = mgr.getTransaction();
808       assertNotNull(tx1);
809       assertNotSame(tx, tx1);
810       cache.put("/one/two", "key1", pojo);
811       mgr.commit();
812
813
814       List JavaDoc calls = dummy.getAllCalled();
815       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
816       assertEquals(MethodDeclarations.commitMethod, calls.get(1));
817
818       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
819       assertEquals(MethodDeclarations.commitMethod, calls.get(3));
820       cache.stop();
821    }
822
823 }
824
Popular Tags