KickJava   Java API By Example, From Geeks To Geeks.

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


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.CacheImpl;
7 import org.jboss.cache.Fqn;
8
9 import javax.transaction.Transaction JavaDoc;
10 import javax.transaction.TransactionManager JavaDoc;
11
12 /**
13  * Based on a contribution by Owen Taylor
14  *
15  * @author otaylor@redhat.com
16  * @author Manik Surtani (manik@jboss.org)
17  */

18 public class SuspendTxTest extends TestCase
19 {
20    CacheImpl cache;
21    TransactionManager JavaDoc mgr;
22
23    protected void setUp() throws Exception JavaDoc
24    {
25       super.setUp();
26       cache = new CacheImpl();
27       cache.getConfiguration().setCacheMode("local");
28       cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
29       cache.start();
30       mgr = cache.getTransactionManager();
31    }
32
33    protected void tearDown() throws Exception JavaDoc
34    {
35       super.tearDown();
36       cache.stop();
37       if (mgr.getTransaction() != null)
38       {
39          mgr.rollback();
40       }
41       mgr = null;
42    }
43
44    /**
45     * Tests that locks created when a transaction is suspended are independent
46     * from the transaction.
47     */

48    public void testSuspendedLocks() throws Exception JavaDoc
49    {
50       // create /one first
51
cache.put("/one", null);
52       cache.put("/a", null);
53       mgr.begin();
54
55       cache.put("/one/two", "key1", "val1");
56       int numLocksBefore = cache.getNumberOfLocksHeld();
57
58       Transaction JavaDoc tx = mgr.suspend();
59
60       cache.put("/a/b", "key1", "val1");
61       mgr.resume(tx);
62
63       assertEquals(numLocksBefore, cache.getNumberOfLocksHeld());
64    }
65
66    /**
67     * Tests that locks created when a transaction is suspended are independent
68     * from the transaction.
69     */

70    public void testSuspendedUsingOptionsLocks() throws Exception JavaDoc
71    {
72       mgr.begin();
73
74       cache.put("/one/two", "key1", "val1");
75       int numLocksBefore = cache.getNumberOfLocksHeld();
76
77       cache.getInvocationContext().getOptionOverrides().setFailSilently(true);// will cause any current txs to be suspended for the duration of this call.
78
cache.put(Fqn.fromString("/a/b"), "key1", "val1");
79
80       assertEquals(numLocksBefore, cache.getNumberOfLocksHeld());
81    }
82
83
84    public static Test suite()
85    {
86       return new TestSuite(SuspendTxTest.class);
87    }
88
89    public static void main(String JavaDoc[] args)
90    {
91       junit.textui.TestRunner.run(suite());
92    }
93
94 }
95
Popular Tags