KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

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

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

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

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