KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > mgmt > TxTest


1 package org.jboss.cache.mgmt;
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 import org.jboss.cache.config.Configuration;
9 import org.jboss.cache.factories.XmlConfigurationParser;
10 import org.jboss.cache.interceptors.TxInterceptor;
11
12 import javax.transaction.TransactionManager JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.List JavaDoc;
15
16 /**
17  * Simple functional tests for TxInterceptor statistics
18  *
19  * @author Jerry Gauthier
20  * @version $Id: TxTest.java,v 1.10 2006/12/30 19:48:46 msurtani Exp $
21  */

22 public class TxTest extends TestCase
23 {
24    private static final String JavaDoc CLUSTER_NAME = "TxTestCluster";
25    private static final String JavaDoc CAPITAL = "capital";
26    private static final String JavaDoc CURRENCY = "currency";
27    private static final String JavaDoc POPULATION = "population";
28    private static final String JavaDoc AREA = "area";
29
30    CacheImpl cache1 = null;
31    CacheImpl cache2 = null;
32
33    protected void setUp() throws Exception JavaDoc
34    {
35       super.setUp();
36       cache1 = createCache(CLUSTER_NAME);
37       cache2 = createCache(CLUSTER_NAME);
38    }
39
40    protected void tearDown() throws Exception JavaDoc
41    {
42       super.tearDown();
43       if (cache1 != null)
44       {
45          cache1.stop();
46          cache1.destroy();
47          cache1 = null;
48       }
49       if (cache2 != null)
50       {
51          cache2.stop();
52          cache2.destroy();
53          cache2 = null;
54       }
55    }
56
57    public void testTxMgmt() throws Exception JavaDoc
58    {
59       assertNotNull("Cache1 is null.", cache1);
60       assertNotNull("Cache2 is null.", cache2);
61
62       // Note: because these tests are normally executed without a server, the interceptor
63
// MBeans are usually not available for use in the tests. Consequently it's necessary
64
// to obtain a reference to the interceptor and work with it directly.
65
TxInterceptor tx1 = getTxInterceptor(cache1);
66       assertNotNull("Cache1 InvalidationInterceptor not found.", tx1);
67       TxInterceptor tx2 = getTxInterceptor(cache2);
68       assertNotNull("Cache2 InvalidationInterceptor not found.", tx2);
69
70       TransactionManager JavaDoc tm1 = cache1.getTransactionManager();
71       assertNotNull("TransactionManager is null.", tm1);
72
73       // populate cache1 with test data - no transaction
74
loadCacheNoTx(cache1);
75
76       // confirm that data is in cache1 and in cache2
77
Fqn key = Fqn.fromString("Europe/Austria");
78       assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
79       assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
80       key = Fqn.fromString("Europe/Albania");
81       assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
82       assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
83
84       // verify basic statistics for entries loaded into cache
85
assertEquals("Cache1 Tx Prepares error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx1.getPrepares()));
86       assertEquals("Cache1 Tx Commits error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx1.getCommits()));
87       assertEquals("Cache1 Tx Rollbacks error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx1.getRollbacks()));
88       assertEquals("Cache2 Tx Prepares error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx2.getPrepares()));
89       assertEquals("Cache2 Tx Commits error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx2.getCommits()));
90       assertEquals("Cache2 Tx Rollbacks error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx2.getRollbacks()));
91
92       // populate cache1 with test data - then transaction commit
93
loadCacheTxCommit(cache1, tm1);
94       loadCacheTxCommit2(cache1, tm1);
95
96       // confirm that committed data is in cache1 and in cache2
97
key = Fqn.fromString("Europe/England");
98       assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
99       assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
100       key = Fqn.fromString("Europe/Hungary");
101       assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
102       assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
103
104       // populate cache1 with test data - then transaction rollback
105
loadCacheTxRollback(cache1, tm1);
106
107       // confirm that rolled back data is not in cache1 or cache2
108
key = Fqn.fromString("Europe/France");
109       assertNull("Cache1 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
110       assertNull("Cache2 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
111       key = Fqn.fromString("Europe/Germany");
112       assertNull("Cache1 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
113       assertNull("Cache2 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
114
115       // check the statistics - transactions are only handled by JBoss Cache on the remote node (i.e., node2)
116
assertEquals("Cache1 Tx Prepares error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx1.getPrepares()));
117       assertEquals("Cache1 Tx Commits error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx1.getCommits()));
118       assertEquals("Cache1 Tx Rollbacks error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx1.getRollbacks()));
119       assertEquals("Cache2 Tx Prepares error after reset: ", new Long JavaDoc(2), new Long JavaDoc(tx2.getPrepares()));
120       assertEquals("Cache2 Tx Commits error after reset: ", new Long JavaDoc(2), new Long JavaDoc(tx2.getCommits()));
121       // rollbacks don't currently get propagated so the counter will be 0, not 1
122
assertEquals("Cache2 Tx Rollbacks error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx2.getRollbacks()));
123
124       // reset statistics
125
tx1.resetStatistics();
126       tx2.resetStatistics();
127
128       // check the statistics again
129
assertEquals("Cache1 Tx Prepares error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx1.getPrepares()));
130       assertEquals("Cache1 Tx Commits error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx1.getCommits()));
131       assertEquals("Cache1 Tx Rollbacks error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx1.getRollbacks()));
132       assertEquals("Cache2 Tx Prepares error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx2.getPrepares()));
133       assertEquals("Cache2 Tx Commits error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx2.getCommits()));
134       assertEquals("Cache2 Tx Rollbacks error after reset: ", new Long JavaDoc(0), new Long JavaDoc(tx2.getRollbacks()));
135    }
136
137    private void loadCacheNoTx(CacheImpl cache) throws Exception JavaDoc
138    {
139       cache.put("Europe", new HashMap JavaDoc());
140       cache.put("Europe/Austria", new HashMap JavaDoc());
141       cache.put("Europe/Austria", CAPITAL, "Vienna");
142       cache.put("Europe/Austria", CURRENCY, "Euro");
143       cache.put("Europe/Austria", POPULATION, 8184691);
144
145       HashMap JavaDoc albania = new HashMap JavaDoc(4);
146       albania.put(CAPITAL, "Tirana");
147       albania.put(CURRENCY, "Lek");
148       albania.put(POPULATION, 3563112);
149       albania.put(AREA, 28748);
150       cache.put("Europe/Albania", albania);
151    }
152
153    private void loadCacheTxCommit(CacheImpl cache, TransactionManager JavaDoc tm) throws Exception JavaDoc
154    {
155       tm.begin();
156
157       cache.put("Europe/Czech Republic", new HashMap JavaDoc());
158       cache.put("Europe/Czech Republic", CAPITAL, "Prague");
159       cache.put("Europe/Czech Republic", CURRENCY, "Czech Koruna");
160       cache.put("Europe/Czech Republic", POPULATION, 10241138);
161
162       cache.put("Europe/England", new HashMap JavaDoc());
163       cache.put("Europe/England", CAPITAL, "London");
164       cache.put("Europe/England", CURRENCY, "British Pound");
165       cache.put("Europe/England", POPULATION, 60441457);
166
167       tm.commit();
168    }
169
170    private void loadCacheTxCommit2(CacheImpl cache, TransactionManager JavaDoc tm) throws Exception JavaDoc
171    {
172       tm.begin();
173
174       HashMap JavaDoc hungary = new HashMap JavaDoc(4);
175       hungary.put(CAPITAL, "Budapest");
176       hungary.put(CURRENCY, "Forint");
177       hungary.put(POPULATION, 10006835);
178       hungary.put(AREA, 93030);
179       cache.put("Europe/Hungary", hungary);
180
181       HashMap JavaDoc romania = new HashMap JavaDoc(4);
182       romania.put(CAPITAL, "Bucharest");
183       romania.put(CURRENCY, "Leu");
184       romania.put(POPULATION, 22329977);
185       romania.put(AREA, 237500);
186       cache.put("Europe/Romania", romania);
187
188       tm.commit();
189    }
190
191    private void loadCacheTxRollback(CacheImpl cache, TransactionManager JavaDoc tm) throws Exception JavaDoc
192    {
193       tm.begin();
194
195       cache.put("Europe/France", new HashMap JavaDoc());
196       cache.put("Europe/France", CAPITAL, "Paris");
197       cache.put("Europe/France", CURRENCY, "Euro");
198       cache.put("Europe/France", POPULATION, 60656178);
199
200       cache.put("Europe/Germany", new HashMap JavaDoc());
201       cache.put("Europe/Germany", CAPITAL, "Berlin");
202       cache.put("Europe/Germany", CURRENCY, "Euro");
203       cache.put("Europe/Germany", POPULATION, 82431390);
204
205       tm.rollback();
206    }
207
208    private CacheImpl createCache(String JavaDoc clusterName) throws Exception JavaDoc
209    {
210       CacheImpl cache = new CacheImpl();
211       cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml"));
212       cache.getConfiguration().setUseRegionBasedMarshalling(false);
213       cache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
214       cache.getConfiguration().setExposeManagementStatistics(true);
215       cache.getConfiguration().setClusterName(clusterName);
216       cache.create();
217       cache.start();
218       return cache;
219    }
220
221    private TxInterceptor getTxInterceptor(CacheImpl cache)
222    {
223       List JavaDoc interceptors = cache.getInterceptors();
224       if (interceptors.isEmpty())
225          return null;
226
227       for (int i = 0; i < interceptors.size(); i++)
228       {
229          Object JavaDoc o = interceptors.get(i);
230          if (o instanceof TxInterceptor)
231             return (TxInterceptor) o;
232       }
233       return null;
234    }
235
236    public static Test suite()
237    {
238       return new TestSuite(TxTest.class);
239    }
240
241 }
242
Popular Tags