KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > invocationcontext > TransactionTest


1 package org.jboss.cache.invocationcontext;
2
3 import junit.framework.TestCase;
4 import org.jboss.cache.CacheSPI;
5 import org.jboss.cache.Fqn;
6 import org.jboss.cache.factories.DefaultCacheFactory;
7
8 import javax.transaction.TransactionManager JavaDoc;
9 import java.util.Map JavaDoc;
10
11 /**
12  * A test to ensure the transactional context is properly set up in the IC
13  *
14  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani</a>
15  */

16 public class TransactionTest extends TestCase
17 {
18    private CacheSPI cache;
19    private TransactionManager JavaDoc tm;
20
21    protected void setUp()
22    {
23       cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache("META-INF/local-tx-service.xml");
24       tm = cache.getTransactionManager();
25    }
26
27    protected void tearDown()
28    {
29       if (cache != null)
30       {
31          cache.stop();
32          cache = null;
33       }
34    }
35
36    public void testTxExistenceAfterWrite() throws Exception JavaDoc
37    {
38       // make sure we have a running transaction.
39
tm.begin();
40
41       assertNull("Tx should not have been set up yet", cache.getInvocationContext().getTransaction());
42       assertNull("Gtx should not have been set up yet", cache.getInvocationContext().getGlobalTransaction());
43
44       // now make a WRITE call into the cache (should start a tx)
45
cache.getRoot().put("k", "v");
46       Map JavaDoc data = cache.getRoot().getData();
47       assertEquals("Data map should not empty", 1, data.size());
48
49       // but now we should have a local tx registered in the invocation context
50
assertNotNull("Tx should have been set up by now", cache.getInvocationContext().getTransaction());
51       assertEquals("The same current transaction should be associated with this invocation ctx.", tm.getTransaction(), cache.getInvocationContext().getTransaction());
52       assertNotNull("Gtx should have been set up by now", cache.getInvocationContext().getGlobalTransaction());
53
54       tm.commit();
55    }
56
57    public void testTxExistenceAfterRead() throws Exception JavaDoc
58    {
59       // make sure we have a running transaction.
60
tm.begin();
61
62       assertNull("Tx should not have been set up yet", cache.getInvocationContext().getTransaction());
63       assertNull("Gtx should not have been set up yet", cache.getInvocationContext().getGlobalTransaction());
64
65       // now make a WRITE call into the cache (should start a tx)
66
Object JavaDoc value = cache.get(Fqn.ROOT, "k");
67       assertNull("Value should be null", value);
68
69       // but now we should have a local tx registered in the invocation context
70
assertNotNull("Tx should have been set up by now", cache.getInvocationContext().getTransaction());
71       assertEquals("The same current transaction should be associated with this invocation ctx.", tm.getTransaction(), cache.getInvocationContext().getTransaction());
72       assertNotNull("Gtx should have been set up by now", cache.getInvocationContext().getGlobalTransaction());
73
74       tm.commit();
75    }
76
77    public void testScrubbingAfterCommit() throws Exception JavaDoc
78    {
79       doScrubbingTest(true);
80    }
81
82    public void testScrubbingAfterRollback() throws Exception JavaDoc
83    {
84       doScrubbingTest(false);
85    }
86
87    private void doScrubbingTest(boolean commit) throws Exception JavaDoc
88    {
89       tm.begin();
90       cache.getRoot().put("key", "value");
91       assertNotNull("Tx should have been set up by now", cache.getInvocationContext().getTransaction());
92       assertEquals("The same current transaction should be associated with this invocation ctx.", tm.getTransaction(), cache.getInvocationContext().getTransaction());
93       assertNotNull("Gtx should have been set up by now", cache.getInvocationContext().getGlobalTransaction());
94
95       if (commit) tm.commit();
96       else tm.rollback();
97
98       assertNull("Tx should have been scrubbed", cache.getInvocationContext().getTransaction());
99       assertNull("Gtx should have been scrubbed", cache.getInvocationContext().getGlobalTransaction());
100    }
101 }
102
Popular Tags