KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.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.CacheImpl;
8 import org.jboss.cache.TransactionTable;
9
10 import javax.transaction.NotSupportedException JavaDoc;
11 import javax.transaction.Synchronization JavaDoc;
12 import javax.transaction.Transaction JavaDoc;
13
14 /**
15  * Created by IntelliJ IDEA.
16  * User: bela
17  * Date: Jun 9, 2004
18  * Time: 9:05:19 AM
19  */

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

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

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