KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > transaction > PrepareTxTest


1 package org.jboss.cache.tests.transaction;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.CacheException;
7 import org.jboss.cache.TransactionTable;
8 import org.jboss.cache.TreeCache;
9 import org.jboss.cache.transaction.DummyTransactionManager;
10
11 import javax.transaction.NotSupportedException JavaDoc;
12 import javax.transaction.Synchronization JavaDoc;
13 import javax.transaction.Transaction JavaDoc;
14
15 /**
16  * Created by IntelliJ IDEA.
17  * User: bela
18  * Date: Jun 9, 2004
19  * Time: 9:05:19 AM
20  */

21 public class PrepareTxTest extends TestCase {
22    TreeCache cache;
23
24    protected void setUp() throws Exception JavaDoc {
25       super.setUp();
26       cache=new TreeCache();
27       cache.setCacheMode("local");
28       cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
29       cache.createService();
30       cache.startService();
31    }
32
33    protected void tearDown() throws Exception JavaDoc {
34       super.tearDown();
35       cache.stopService();
36       cache.destroyService();
37    }
38
39
40
41
42    /** Tests cache modification <em>inside</em> the afterCompletion() callback. Reproduces a bug fixed in
43     * connection with JBossCache being used as Hibernate's second level cache
44     * @throws Exception
45     * @throws NotSupportedException
46     */

47    public void testCacheModificationInBeforeCompletionPhase() throws Exception JavaDoc, NotSupportedException JavaDoc {
48       int numLocks=0;
49       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
50       mgr.begin();
51       Transaction JavaDoc tx=mgr.getTransaction();
52
53       // this will cause the cache to register with TransactionManager for TX completion callbacks
54
cache.put("/one/two/three", "key1", "val1");
55       System.out.println("before commit:\n" + cache.printLockInfo());
56       numLocks=cache.getNumberOfLocksHeld();
57       assertEquals(3, numLocks);
58
59       // we register *second*
60
tx.registerSynchronization(new Synchronization JavaDoc() {
61
62          public void beforeCompletion() {
63             try {
64                cache.put("/a/b/c", null);
65                System.out.println("before commit:\n" + cache.printLockInfo());
66             }
67             catch(CacheException e) {
68                e.printStackTrace();
69             }
70          }
71
72          public void afterCompletion(int status) {
73          }
74       });
75
76       tx.commit();
77       System.out.println("after commit:\n" + cache.printLockInfo());
78       numLocks=cache.getNumberOfLocksHeld();
79       assertEquals(0, numLocks);
80
81       int num_local_txs, num_global_txs;
82       TransactionTable tx_table=cache.getTransactionTable();
83       num_local_txs=tx_table.getNumLocalTransactions();
84       num_global_txs=tx_table.getNumGlobalTransactions();
85       System.out.println("Number of Transactions: " + num_local_txs + "\nNumber of GlobalTransactions: " +
86                          num_global_txs + "\nTransactionTable:\n " + tx_table.toString(true));
87       assertEquals(num_local_txs, num_global_txs);
88       assertEquals(0, num_local_txs);
89    }
90
91
92
93    /** Tests cache modification <em>inside</em> the afterCompletion() callback. Reproduces a bug fixed in
94      * connection with JBossCache being used as Hibernate's second level cache
95      * @throws Exception
96      * @throws NotSupportedException
97      */

98     public void testCacheModificationInAfterCompletionPhase() throws Exception JavaDoc, NotSupportedException JavaDoc {
99        int numLocks=0;
100        DummyTransactionManager mgr=DummyTransactionManager.getInstance();
101        mgr.begin();
102        Transaction JavaDoc tx=mgr.getTransaction();
103
104        // this will cause the cache to register with TransactionManager for TX completion callbacks
105
cache.put("/one/two/three", "key1", "val1");
106        System.out.println("before commit:\n" + cache.printLockInfo());
107        numLocks=cache.getNumberOfLocksHeld();
108        assertEquals(3, numLocks);
109
110        // we register *second*
111
tx.registerSynchronization(new Synchronization JavaDoc() {
112
113           public void beforeCompletion() {
114           }
115
116           public void afterCompletion(int status) {
117              try {
118                 cache.put("/a/b/c", null);
119                 System.out.println("before commit:\n" + cache.printLockInfo());
120              }
121              catch(CacheException e) {
122                 e.printStackTrace();
123              }
124           }
125        });
126
127        tx.commit();
128        System.out.println("after commit:\n" + cache.printLockInfo());
129        numLocks=cache.getNumberOfLocksHeld();
130        assertEquals(0, numLocks);
131
132        int num_local_txs, num_global_txs;
133        TransactionTable tx_table=cache.getTransactionTable();
134        num_local_txs=tx_table.getNumLocalTransactions();
135        num_global_txs=tx_table.getNumGlobalTransactions();
136        System.out.println("Number of Transactions: " + num_local_txs + "\nNumber of GlobalTransactions: " +
137                           num_global_txs + "\nTransactionTable:\n " + tx_table.toString(true));
138        assertEquals(num_local_txs, num_global_txs);
139        assertEquals(0, num_local_txs);
140     }
141
142
143
144    public static Test suite() {
145       return new TestSuite(PrepareTxTest.class);
146    }
147
148    public static void main(String JavaDoc[] args) {
149       junit.textui.TestRunner.run(suite());
150    }
151
152 }
153
Popular Tags