KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.transaction;
8
9 import junit.framework.TestCase;
10 import org.jboss.cache.CacheImpl;
11 import org.jboss.cache.config.Configuration;
12 import org.jboss.cache.interceptors.OrderedSynchronizationHandler;
13 import org.jboss.cache.misc.TestingUtil;
14
15 import javax.transaction.Synchronization JavaDoc;
16 import javax.transaction.SystemException JavaDoc;
17 import javax.transaction.Transaction JavaDoc;
18 import javax.transaction.TransactionManager JavaDoc;
19
20 /**
21  * Tests cleaning of invocation contexts on completion of txs
22  *
23  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
24  */

25 public class InvocationContextCleanupTest extends TestCase
26 {
27    private CacheImpl[] caches;
28
29    private CacheImpl createCache(boolean optimistic) throws Exception JavaDoc
30    {
31       CacheImpl cache = new CacheImpl();
32       cache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
33       if (optimistic) cache.getConfiguration().setNodeLockingScheme("OPTIMISTIC");
34       cache.getConfiguration().setClusterName("InvocationContextCleanupTest");
35       cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
36       cache.getConfiguration().setLockAcquisitionTimeout(2000);
37       cache.start();
38       return cache;
39    }
40
41    protected void tearDown()
42    {
43       if (caches != null)
44       {
45          for (int i = 0; i < caches.length; i++)
46          {
47             if (caches[i] != null)
48             {
49                caches[i].stop();
50                caches[i] = null;
51             }
52          }
53          caches = null;
54       }
55    }
56
57    public void testInvocationContextCleanupPessimistic() throws Exception JavaDoc
58    {
59       test2CachesSync(false);
60    }
61
62    private void test2CachesSync(boolean optimistic) throws Exception JavaDoc
63    {
64       caches = new CacheImpl[2];
65       caches[0] = createCache(optimistic);
66       caches[1] = createCache(optimistic);
67
68       TestingUtil.blockUntilViewsReceived(caches, 2000);
69
70       TransactionManager JavaDoc mgr = caches[0].getTransactionManager();
71
72       mgr.begin();
73
74       OrderedSynchronizationHandler orderedHandler = OrderedSynchronizationHandler.getInstance(mgr.getTransaction());
75       orderedHandler.registerAtTail(new DummySynchronization(caches[0], mgr));
76
77       caches[0].put("/test", "x", "y");
78       try
79       {
80          mgr.commit();
81       }
82       finally
83       {
84       }
85
86       System.out.println(caches[0].printLockInfo());
87       System.out.println(caches[1].printLockInfo());
88       assertEquals("y", caches[0].get("/test", "x"));
89       assertEquals("y", caches[1].get("/test", "x"));
90    }
91
92    public static class DummySynchronization implements Synchronization JavaDoc
93    {
94       private CacheImpl cache;
95       private TransactionManager JavaDoc mgr;
96
97       public DummySynchronization(CacheImpl cache, TransactionManager JavaDoc mgr)
98       {
99          this.cache = cache;
100          this.mgr = mgr;
101       }
102
103       public void beforeCompletion()
104       {
105          // before returning, do a put (non-tx) on the cache!!
106
Transaction JavaDoc tx = null;
107          try
108          {
109             tx = mgr.suspend();
110          }
111          catch (SystemException JavaDoc e)
112          {
113             throw new RuntimeException JavaDoc("Unable to sustend transaction! " + e.getMessage());
114          }
115
116          try
117          {
118             cache.put("/test", "blah", "blahblah");
119             assertTrue("Should fail with a lock exception!", false);
120          }
121          catch (Exception JavaDoc e)
122          {
123             assertTrue("Should fail!", true);
124          }
125          finally
126          {
127             if (tx != null) try
128             {
129                mgr.resume(tx);
130             }
131             catch (Exception JavaDoc e)
132             {
133             }
134          }
135       }
136
137       public void afterCompletion(int i)
138       {
139          // do nothing
140
}
141    }
142 }
143
Popular Tags