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.DefaultCacheFactory; |
8 |
| import org.jboss.cache.Fqn; |
9 |
| import org.jboss.cache.config.Configuration; |
10 |
| import org.jboss.cache.config.Configuration.CacheMode; |
11 |
| import org.jboss.cache.factories.UnitTestCacheFactory; |
12 |
| import org.jboss.cache.interceptors.TxInterceptor; |
13 |
| |
14 |
| import javax.transaction.TransactionManager; |
15 |
| import java.util.HashMap; |
16 |
| import java.util.List; |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| public class TxTest extends TestCase |
25 |
| { |
26 |
| private static final String CLUSTER_NAME = "TxTestCluster"; |
27 |
| private static final String CAPITAL = "capital"; |
28 |
| private static final String CURRENCY = "currency"; |
29 |
| private static final String POPULATION = "population"; |
30 |
| private static final String AREA = "area"; |
31 |
| |
32 |
| CacheImpl cache1 = null; |
33 |
| CacheImpl cache2 = null; |
34 |
| |
35 |
1
| protected void setUp() throws Exception
|
36 |
| { |
37 |
1
| super.setUp();
|
38 |
1
| cache1 = createCache(CLUSTER_NAME);
|
39 |
1
| cache2 = createCache(CLUSTER_NAME);
|
40 |
| } |
41 |
| |
42 |
1
| protected void tearDown() throws Exception
|
43 |
| { |
44 |
1
| super.tearDown();
|
45 |
1
| if (cache1 != null)
|
46 |
| { |
47 |
1
| cache1.stop();
|
48 |
1
| cache1.destroy();
|
49 |
1
| cache1 = null;
|
50 |
| } |
51 |
1
| if (cache2 != null)
|
52 |
| { |
53 |
1
| cache2.stop();
|
54 |
1
| cache2.destroy();
|
55 |
1
| cache2 = null;
|
56 |
| } |
57 |
| } |
58 |
| |
59 |
1
| public void testTxMgmt() throws Exception
|
60 |
| { |
61 |
1
| assertNotNull("Cache1 is null.", cache1);
|
62 |
1
| assertNotNull("Cache2 is null.", cache2);
|
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
1
| TxInterceptor tx1 = getTxInterceptor(cache1);
|
68 |
1
| assertNotNull("Cache1 InvalidationInterceptor not found.", tx1);
|
69 |
1
| TxInterceptor tx2 = getTxInterceptor(cache2);
|
70 |
1
| assertNotNull("Cache2 InvalidationInterceptor not found.", tx2);
|
71 |
| |
72 |
1
| TransactionManager tm1 = cache1.getTransactionManager();
|
73 |
1
| assertNotNull("TransactionManager is null.", tm1);
|
74 |
| |
75 |
| |
76 |
1
| loadCacheNoTx(cache1);
|
77 |
| |
78 |
| |
79 |
1
| Fqn key = Fqn.fromString("Europe/Austria");
|
80 |
1
| assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
|
81 |
1
| assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
|
82 |
1
| key = Fqn.fromString("Europe/Albania");
|
83 |
1
| assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
|
84 |
1
| assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
|
85 |
| |
86 |
| |
87 |
1
| assertEquals("Cache1 Tx Prepares error after reset: ", new Long(0), new Long(tx1.getPrepares()));
|
88 |
1
| assertEquals("Cache1 Tx Commits error after reset: ", new Long(0), new Long(tx1.getCommits()));
|
89 |
1
| assertEquals("Cache1 Tx Rollbacks error after reset: ", new Long(0), new Long(tx1.getRollbacks()));
|
90 |
1
| assertEquals("Cache2 Tx Prepares error after reset: ", new Long(0), new Long(tx2.getPrepares()));
|
91 |
1
| assertEquals("Cache2 Tx Commits error after reset: ", new Long(0), new Long(tx2.getCommits()));
|
92 |
1
| assertEquals("Cache2 Tx Rollbacks error after reset: ", new Long(0), new Long(tx2.getRollbacks()));
|
93 |
| |
94 |
| |
95 |
1
| loadCacheTxCommit(cache1, tm1);
|
96 |
1
| loadCacheTxCommit2(cache1, tm1);
|
97 |
| |
98 |
| |
99 |
1
| key = Fqn.fromString("Europe/England");
|
100 |
1
| assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
|
101 |
1
| assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
|
102 |
1
| key = Fqn.fromString("Europe/Hungary");
|
103 |
1
| assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
|
104 |
1
| assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
|
105 |
| |
106 |
| |
107 |
1
| loadCacheTxRollback(cache1, tm1);
|
108 |
| |
109 |
| |
110 |
1
| key = Fqn.fromString("Europe/France");
|
111 |
1
| assertNull("Cache1 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
|
112 |
1
| assertNull("Cache2 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
|
113 |
1
| key = Fqn.fromString("Europe/Germany");
|
114 |
1
| assertNull("Cache1 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
|
115 |
1
| assertNull("Cache2 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
|
116 |
| |
117 |
| |
118 |
1
| assertEquals("Cache1 Tx Prepares error after reset: ", new Long(0), new Long(tx1.getPrepares()));
|
119 |
1
| assertEquals("Cache1 Tx Commits error after reset: ", new Long(0), new Long(tx1.getCommits()));
|
120 |
1
| assertEquals("Cache1 Tx Rollbacks error after reset: ", new Long(0), new Long(tx1.getRollbacks()));
|
121 |
1
| assertEquals("Cache2 Tx Prepares error after reset: ", new Long(2), new Long(tx2.getPrepares()));
|
122 |
1
| assertEquals("Cache2 Tx Commits error after reset: ", new Long(2), new Long(tx2.getCommits()));
|
123 |
| |
124 |
0
| assertEquals("Cache2 Tx Rollbacks error after reset: ", new Long(0), new Long(tx2.getRollbacks()));
|
125 |
| |
126 |
| |
127 |
0
| tx1.resetStatistics();
|
128 |
0
| tx2.resetStatistics();
|
129 |
| |
130 |
| |
131 |
0
| assertEquals("Cache1 Tx Prepares error after reset: ", new Long(0), new Long(tx1.getPrepares()));
|
132 |
0
| assertEquals("Cache1 Tx Commits error after reset: ", new Long(0), new Long(tx1.getCommits()));
|
133 |
0
| assertEquals("Cache1 Tx Rollbacks error after reset: ", new Long(0), new Long(tx1.getRollbacks()));
|
134 |
0
| assertEquals("Cache2 Tx Prepares error after reset: ", new Long(0), new Long(tx2.getPrepares()));
|
135 |
0
| assertEquals("Cache2 Tx Commits error after reset: ", new Long(0), new Long(tx2.getCommits()));
|
136 |
0
| assertEquals("Cache2 Tx Rollbacks error after reset: ", new Long(0), new Long(tx2.getRollbacks()));
|
137 |
| } |
138 |
| |
139 |
1
| private void loadCacheNoTx(CacheImpl cache) throws Exception
|
140 |
| { |
141 |
1
| cache.put("Europe", new HashMap());
|
142 |
1
| cache.put("Europe/Austria", new HashMap());
|
143 |
1
| cache.put("Europe/Austria", CAPITAL, "Vienna");
|
144 |
1
| cache.put("Europe/Austria", CURRENCY, "Euro");
|
145 |
1
| cache.put("Europe/Austria", POPULATION, 8184691);
|
146 |
| |
147 |
1
| HashMap albania = new HashMap(4);
|
148 |
1
| albania.put(CAPITAL, "Tirana");
|
149 |
1
| albania.put(CURRENCY, "Lek");
|
150 |
1
| albania.put(POPULATION, 3563112);
|
151 |
1
| albania.put(AREA, 28748);
|
152 |
1
| cache.put("Europe/Albania", albania);
|
153 |
| } |
154 |
| |
155 |
1
| private void loadCacheTxCommit(CacheImpl cache, TransactionManager tm) throws Exception
|
156 |
| { |
157 |
1
| tm.begin();
|
158 |
| |
159 |
1
| cache.put("Europe/Czech Republic", new HashMap());
|
160 |
1
| cache.put("Europe/Czech Republic", CAPITAL, "Prague");
|
161 |
1
| cache.put("Europe/Czech Republic", CURRENCY, "Czech Koruna");
|
162 |
1
| cache.put("Europe/Czech Republic", POPULATION, 10241138);
|
163 |
| |
164 |
1
| cache.put("Europe/England", new HashMap());
|
165 |
1
| cache.put("Europe/England", CAPITAL, "London");
|
166 |
1
| cache.put("Europe/England", CURRENCY, "British Pound");
|
167 |
1
| cache.put("Europe/England", POPULATION, 60441457);
|
168 |
| |
169 |
1
| tm.commit();
|
170 |
| } |
171 |
| |
172 |
1
| private void loadCacheTxCommit2(CacheImpl cache, TransactionManager tm) throws Exception
|
173 |
| { |
174 |
1
| tm.begin();
|
175 |
| |
176 |
1
| HashMap hungary = new HashMap(4);
|
177 |
1
| hungary.put(CAPITAL, "Budapest");
|
178 |
1
| hungary.put(CURRENCY, "Forint");
|
179 |
1
| hungary.put(POPULATION, 10006835);
|
180 |
1
| hungary.put(AREA, 93030);
|
181 |
1
| cache.put("Europe/Hungary", hungary);
|
182 |
| |
183 |
1
| HashMap romania = new HashMap(4);
|
184 |
1
| romania.put(CAPITAL, "Bucharest");
|
185 |
1
| romania.put(CURRENCY, "Leu");
|
186 |
1
| romania.put(POPULATION, 22329977);
|
187 |
1
| romania.put(AREA, 237500);
|
188 |
1
| cache.put("Europe/Romania", romania);
|
189 |
| |
190 |
1
| tm.commit();
|
191 |
| } |
192 |
| |
193 |
1
| private void loadCacheTxRollback(CacheImpl cache, TransactionManager tm) throws Exception
|
194 |
| { |
195 |
1
| tm.begin();
|
196 |
| |
197 |
1
| cache.put("Europe/France", new HashMap());
|
198 |
1
| cache.put("Europe/France", CAPITAL, "Paris");
|
199 |
1
| cache.put("Europe/France", CURRENCY, "Euro");
|
200 |
1
| cache.put("Europe/France", POPULATION, 60656178);
|
201 |
| |
202 |
1
| cache.put("Europe/Germany", new HashMap());
|
203 |
1
| cache.put("Europe/Germany", CAPITAL, "Berlin");
|
204 |
1
| cache.put("Europe/Germany", CURRENCY, "Euro");
|
205 |
1
| cache.put("Europe/Germany", POPULATION, 82431390);
|
206 |
| |
207 |
1
| tm.rollback();
|
208 |
| } |
209 |
| |
210 |
2
| private CacheImpl createCache(String clusterName) throws Exception
|
211 |
| { |
212 |
2
| CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
213 |
2
| cache.setConfiguration(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC));
|
214 |
2
| cache.getConfiguration().setUseRegionBasedMarshalling(false);
|
215 |
2
| cache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
216 |
2
| cache.getConfiguration().setExposeManagementStatistics(true);
|
217 |
2
| cache.getConfiguration().setClusterName(clusterName);
|
218 |
2
| cache.create();
|
219 |
2
| cache.start();
|
220 |
2
| return cache;
|
221 |
| } |
222 |
| |
223 |
2
| private TxInterceptor getTxInterceptor(CacheImpl cache)
|
224 |
| { |
225 |
2
| List interceptors = cache.getInterceptors();
|
226 |
2
| if (interceptors.isEmpty())
|
227 |
| { |
228 |
0
| return null;
|
229 |
| } |
230 |
| |
231 |
6
| for (int i = 0; i < interceptors.size(); i++)
|
232 |
| { |
233 |
6
| Object o = interceptors.get(i);
|
234 |
6
| if (o instanceof TxInterceptor)
|
235 |
| { |
236 |
2
| return (TxInterceptor) o;
|
237 |
| } |
238 |
| } |
239 |
0
| return null;
|
240 |
| } |
241 |
| |
242 |
1
| public static Test suite()
|
243 |
| { |
244 |
1
| return new TestSuite(TxTest.class);
|
245 |
| } |
246 |
| |
247 |
| } |