KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > optimistic > OpLockingInterceptorTest


1 /*
2  * Created on 17-Feb-2005
3  *
4  *
5  *
6  */

7 package org.jboss.cache.optimistic;
8
9 import org.jboss.cache.CacheImpl;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.GlobalTransaction;
12 import org.jboss.cache.NodeSPI;
13 import org.jboss.cache.OptimisticTransactionEntry;
14 import org.jboss.cache.TransactionTable;
15 import org.jboss.cache.interceptors.Interceptor;
16 import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
17 import org.jboss.cache.interceptors.OptimisticLockingInterceptor;
18 import org.jboss.cache.interceptors.OptimisticNodeInterceptor;
19 import org.jboss.cache.loader.SamplePojo;
20 import org.jboss.cache.lock.NodeLock;
21 import org.jboss.cache.marshall.MethodCall;
22 import org.jboss.cache.marshall.MethodCallFactory;
23 import org.jboss.cache.marshall.MethodDeclarations;
24 import org.jboss.cache.transaction.DummyTransactionManager;
25
26 import javax.transaction.Transaction JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * @author xenephon
33  */

34 public class OpLockingInterceptorTest extends AbstractOptimisticTestCase
35 {
36
37
38    /**
39     * @param name
40     */

41    public OpLockingInterceptorTest(String JavaDoc name)
42    {
43       super(name);
44
45    }
46
47    public void testTransactionPrepareMethod() throws Exception JavaDoc
48    {
49
50       TestListener listener = new TestListener();
51       final CacheImpl cache = createCacheWithListener(listener);
52
53       Interceptor lockingInterceptor = new OptimisticLockingInterceptor();
54       lockingInterceptor.setCache(cache);
55       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
56       interceptor.setCache(cache);
57       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
58       nodeInterceptor.setCache(cache);
59       MockInterceptor dummy = new MockInterceptor();
60       dummy.setCache(cache);
61       lockingInterceptor.setNext(interceptor);
62       interceptor.setNext(nodeInterceptor);
63       nodeInterceptor.setNext(dummy);
64
65       cache.setInterceptorChain(lockingInterceptor);
66
67       // first set up a node with a pojo
68
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
69       mgr.begin();
70       Transaction tx = mgr.getTransaction();
71
72       // inject InvocationContext
73
cache.getInvocationContext().setTransaction(tx);
74       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
75
76       SamplePojo pojo = new SamplePojo(21, "test");
77       Map JavaDoc temp = new HashMap JavaDoc();
78       temp.put("key1", pojo);
79       cache.put("/one/two", temp);
80
81       assertEquals(null, dummy.getCalled());
82       TransactionTable table = cache.getTransactionTable();
83
84       GlobalTransaction gtx = table.get(tx);
85
86       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
87
88       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
89
90       /*GlobalTransaction.class,
91       List.class,
92       Address.class,
93       boolean.class*/

94
95       assertEquals(3, workspace.getNodes().size());
96       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
97       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
98       assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
99       assertTrue(entry.getLocks().isEmpty());
100       assertEquals(1, entry.getModifications().size());
101       assertTrue(!cache.exists("/one/two"));
102       assertEquals(null, dummy.getCalled());
103
104       //now let us do a prepare
105
MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object JavaDoc[]{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE});
106       try
107       {
108          cache._replicate(prepareMethod);
109       }
110       catch (Throwable JavaDoc t)
111       {
112
113       }
114
115
116       assertEquals(3, workspace.getNodes().size());
117       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
118       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
119       assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
120       assertEquals(3, entry.getLocks().size());
121       for (Iterator JavaDoc it = entry.getLocks().iterator(); it.hasNext();)
122       {
123          NodeLock lock = (NodeLock) it.next();
124          assertTrue(lock.isWriteLocked());
125          assertEquals(gtx, lock.getWriterOwner());
126       }
127       assertEquals(1, entry.getModifications().size());
128       assertTrue(!cache.exists("/one/two"));
129       //assertEquals(null,dummy.getCalled());
130

131
132       mgr.commit();
133
134       cache.stop();
135
136    }
137
138    public void testTransactionCommitMethod() throws Exception JavaDoc
139    {
140
141       TestListener listener = new TestListener();
142       final CacheImpl cache = createCacheWithListener(listener);
143
144       Interceptor lockingInterceptor = new OptimisticLockingInterceptor();
145       lockingInterceptor.setCache(cache);
146       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
147       interceptor.setCache(cache);
148       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
149       nodeInterceptor.setCache(cache);
150       MockInterceptor dummy = new MockInterceptor();
151       dummy.setCache(cache);
152       lockingInterceptor.setNext(interceptor);
153       interceptor.setNext(nodeInterceptor);
154       nodeInterceptor.setNext(dummy);
155
156       cache.setInterceptorChain(lockingInterceptor);
157
158       // first set up a node with a pojo
159
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
160       mgr.begin();
161       Transaction tx = mgr.getTransaction();
162
163       // inject InvocationContext
164
cache.getInvocationContext().setTransaction(tx);
165       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
166
167       SamplePojo pojo = new SamplePojo(21, "test");
168       Map JavaDoc temp = new HashMap JavaDoc();
169       temp.put("key1", pojo);
170       cache.put("/one/two", temp);
171
172       assertEquals(null, dummy.getCalled());
173       TransactionTable table = cache.getTransactionTable();
174
175       GlobalTransaction gtx = table.get(tx);
176
177       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
178
179       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
180
181       /*GlobalTransaction.class,
182       List.class,
183       Address.class,
184       boolean.class*/

185
186       assertEquals(3, workspace.getNodes().size());
187       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
188       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
189       assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
190       assertTrue(entry.getLocks().isEmpty());
191       assertEquals(1, entry.getModifications().size());
192       assertTrue(!cache.exists("/one/two"));
193       assertEquals(null, dummy.getCalled());
194
195       //now let us do a prepare
196
MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object JavaDoc[]{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE});
197       try
198       {
199          cache._replicate(prepareMethod);
200       }
201       catch (Throwable JavaDoc t)
202       {
203
204       }
205
206
207       assertEquals(3, workspace.getNodes().size());
208       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
209       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
210       assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
211       assertEquals(3, entry.getLocks().size());
212       for (Iterator JavaDoc it = entry.getLocks().iterator(); it.hasNext();)
213       {
214          NodeLock lock = (NodeLock) it.next();
215          assertTrue(lock.isWriteLocked());
216          assertEquals(gtx, lock.getWriterOwner());
217       }
218       assertEquals(1, entry.getModifications().size());
219       assertTrue(!cache.exists("/one/two"));
220       //assertEquals(null,dummy.getCalled());
221
assertEquals(MethodDeclarations.optimisticPrepareMethod, dummy.getCalled());
222
223
224       MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, new Object JavaDoc[]{gtx});
225       try
226       {
227          cache._replicate(commitMethod);
228       }
229       catch (Throwable JavaDoc t)
230       {
231          fail();
232       }
233       assertEquals(3, entry.getLocks().size());
234       for (Iterator JavaDoc it = entry.getLocks().iterator(); it.hasNext();)
235       {
236          NodeLock lock = (NodeLock) it.next();
237          assertEquals(false, lock.isLocked());
238
239       }
240       //make sure the nodes and locks are the same order
241
int i = 0;
242       for (Iterator JavaDoc it = workspace.getNodes().values().iterator(); it.hasNext();)
243       {
244          NodeSPI node = ((WorkspaceNode) it.next()).getNode();
245          assertEquals(node.getLock(), entry.getLocks().get(i));
246          i++;
247       }
248       assertEquals(MethodDeclarations.commitMethod, dummy.getCalled());
249       mgr.commit();
250
251       cache.stop();
252
253    }
254
255    public void testTransactionRollbackMethod() throws Exception JavaDoc
256    {
257
258       TestListener listener = new TestListener();
259       final CacheImpl cache = createCacheWithListener(listener);
260
261       Interceptor lockingInterceptor = new OptimisticLockingInterceptor();
262       lockingInterceptor.setCache(cache);
263       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
264       interceptor.setCache(cache);
265       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
266       nodeInterceptor.setCache(cache);
267       MockInterceptor dummy = new MockInterceptor();
268       dummy.setCache(cache);
269       lockingInterceptor.setNext(interceptor);
270       interceptor.setNext(nodeInterceptor);
271       nodeInterceptor.setNext(dummy);
272
273       cache.setInterceptorChain(lockingInterceptor);
274
275       // first set up a node with a pojo
276
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
277       mgr.begin();
278       Transaction tx = mgr.getTransaction();
279
280       // inject InvocationContext
281
cache.getInvocationContext().setTransaction(tx);
282       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
283
284       SamplePojo pojo = new SamplePojo(21, "test");
285       Map JavaDoc temp = new HashMap JavaDoc();
286       temp.put("key1", pojo);
287       cache.put("/one/two", temp);
288
289       assertEquals(null, dummy.getCalled());
290       TransactionTable table = cache.getTransactionTable();
291
292       GlobalTransaction gtx = table.get(tx);
293
294       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
295
296       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
297
298       /*GlobalTransaction.class,
299       List.class,
300       Address.class,
301       boolean.class*/

302
303       assertEquals(3, workspace.getNodes().size());
304       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
305       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
306       assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
307       assertTrue(entry.getLocks().isEmpty());
308       assertEquals(1, entry.getModifications().size());
309       assertTrue(!cache.exists("/one/two"));
310       assertEquals(null, dummy.getCalled());
311
312       //now let us do a prepare
313
MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object JavaDoc[]{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE});
314       try
315       {
316          cache._replicate(prepareMethod);
317       }
318       catch (Throwable JavaDoc t)
319       {
320
321       }
322
323
324       assertEquals(3, workspace.getNodes().size());
325       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
326       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
327       assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
328       assertEquals(3, entry.getLocks().size());
329       for (Iterator JavaDoc it = entry.getLocks().iterator(); it.hasNext();)
330       {
331          NodeLock lock = (NodeLock) it.next();
332          assertTrue(lock.isWriteLocked());
333          assertEquals(gtx, lock.getWriterOwner());
334       }
335       assertEquals(1, entry.getModifications().size());
336       assertTrue(!cache.exists("/one/two"));
337       assertEquals(MethodDeclarations.optimisticPrepareMethod, dummy.getCalled());
338
339
340       MethodCall rollbackMethod = MethodCallFactory.create(MethodDeclarations.rollbackMethod, new Object JavaDoc[]{gtx});
341       try
342       {
343          cache._replicate(rollbackMethod);
344       }
345       catch (Throwable JavaDoc t)
346       {
347          fail();
348       }
349       assertEquals(3, entry.getLocks().size());
350       for (Iterator JavaDoc it = entry.getLocks().iterator(); it.hasNext();)
351       {
352          NodeLock lock = (NodeLock) it.next();
353          assertEquals(false, lock.isLocked());
354
355       }
356       //make sure the nodes and locks are the same order
357
int i = 0;
358       for (Iterator JavaDoc it = workspace.getNodes().values().iterator(); it.hasNext();)
359       {
360          NodeSPI node = ((WorkspaceNode) it.next()).getNode();
361          assertEquals(node.getLock(), entry.getLocks().get(i));
362          i++;
363       }
364       assertEquals(MethodDeclarations.rollbackMethod, dummy.getCalled());
365       mgr.commit();
366
367       cache.stop();
368
369    }
370
371 }
372
Popular Tags