KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.optimistic;
2
3 import org.jboss.cache.CacheImpl;
4 import org.jboss.cache.Fqn;
5 import org.jboss.cache.GlobalTransaction;
6 import org.jboss.cache.OptimisticTransactionEntry;
7 import org.jboss.cache.TransactionTable;
8 import org.jboss.cache.interceptors.Interceptor;
9 import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
10 import org.jboss.cache.interceptors.OptimisticNodeInterceptor;
11 import org.jboss.cache.loader.SamplePojo;
12 import org.jboss.cache.transaction.DummyTransactionManager;
13
14 import javax.transaction.Transaction JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 /**
19  * @author xenephon
20  */

21 public class NodeInterceptorPutEraseTest extends AbstractOptimisticTestCase
22 {
23
24
25    /**
26     * @param name
27     */

28    public NodeInterceptorPutEraseTest(String JavaDoc name)
29    {
30       super(name);
31    }
32
33    public void testTransactionPutKeyMethod() throws Exception JavaDoc
34    {
35
36       TestListener listener = new TestListener();
37       final CacheImpl cache = createCacheWithListener(listener);
38
39       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
40       interceptor.setCache(cache);
41       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
42       nodeInterceptor.setCache(cache);
43       MockInterceptor dummy = new MockInterceptor();
44       dummy.setCache(cache);
45
46       interceptor.setNext(nodeInterceptor);
47       nodeInterceptor.setNext(dummy);
48
49       cache.setInterceptorChain(interceptor);
50
51 // first set up a node with a pojo
52
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
53       mgr.begin();
54       Transaction tx = mgr.getTransaction();
55
56       // inject InvocationContext
57
cache.getInvocationContext().setTransaction(tx);
58       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
59
60       SamplePojo pojo = new SamplePojo(21, "test");
61       Map JavaDoc temp = new HashMap JavaDoc();
62       temp.put("key1", pojo);
63       cache.put("/one/two", temp);
64
65       assertEquals(null, dummy.getCalled());
66       TransactionTable table = cache.getTransactionTable();
67
68       GlobalTransaction gtx = table.get(tx);
69
70       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
71
72       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
73
74       mgr.commit();
75
76       //assert what should be the results of our call
77
assertEquals(3, workspace.getNodes().size());
78       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
79       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
80       assertTrue(entry.getLocks().isEmpty());
81       assertEquals(1, entry.getModifications().size());
82       assertTrue(!cache.exists("/one/two"));
83       assertEquals(null, dummy.getCalled());
84       cache.stop();
85    }
86
87    public void testTransactionKeyValOverwriteMethod() throws Exception JavaDoc
88    {
89
90       TestListener listener = new TestListener();
91       final CacheImpl cache = createCacheWithListener(listener);
92
93       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
94       interceptor.setCache(cache);
95       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
96       nodeInterceptor.setCache(cache);
97       MockInterceptor dummy = new MockInterceptor();
98       dummy.setCache(cache);
99
100       interceptor.setNext(nodeInterceptor);
101       nodeInterceptor.setNext(dummy);
102
103       cache.setInterceptorChain(interceptor);
104
105 // first set up a node with a pojo
106
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
107       mgr.begin();
108       Transaction tx = mgr.getTransaction();
109
110       // inject InvocationContext
111
cache.getInvocationContext().setTransaction(tx);
112       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
113
114       SamplePojo pojo = new SamplePojo(21, "test");
115
116       cache.put("/one/two", "key1", pojo);
117
118       //overwrite the map we just put in
119
SamplePojo pojo2 = new SamplePojo(21, "test");
120
121       cache.put("/one/two", "key1", pojo2);
122
123       assertEquals(null, dummy.getCalled());
124       TransactionTable table = cache.getTransactionTable();
125
126       GlobalTransaction gtx = table.get(tx);
127
128       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
129
130       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
131
132       mgr.commit();
133       //assert what should be the results of our call
134
assertEquals(3, workspace.getNodes().size());
135       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
136       assertEquals(pojo2, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
137       assertTrue(entry.getLocks().isEmpty());
138       assertEquals(2, entry.getModifications().size());
139       assertTrue(!cache.exists("/one/two"));
140       assertEquals(null, dummy.getCalled());
141       cache.stop();
142    }
143
144    public void testTransactionKeyValOverwriteNullMethod() throws Exception JavaDoc
145    {
146
147       TestListener listener = new TestListener();
148       final CacheImpl cache = createCacheWithListener(listener);
149
150       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
151       interceptor.setCache(cache);
152       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
153       nodeInterceptor.setCache(cache);
154       MockInterceptor dummy = new MockInterceptor();
155       dummy.setCache(cache);
156
157       interceptor.setNext(nodeInterceptor);
158       nodeInterceptor.setNext(dummy);
159
160       cache.setInterceptorChain(interceptor);
161
162 // first set up a node with a pojo
163
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
164       mgr.begin();
165       Transaction tx = mgr.getTransaction();
166
167       // inject InvocationContext
168
cache.getInvocationContext().setTransaction(tx);
169       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
170
171       SamplePojo pojo = new SamplePojo(21, "test");
172
173       cache.put("/one/two", "key1", pojo);
174
175
176       cache.put("/one/two", "key1", null);
177
178       assertEquals(null, dummy.getCalled());
179       TransactionTable table = cache.getTransactionTable();
180
181       GlobalTransaction gtx = table.get(tx);
182
183       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
184
185       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
186
187       mgr.commit();
188       //assert what should be the results of our call
189
assertEquals(3, workspace.getNodes().size());
190       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
191       assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
192       assertTrue(entry.getLocks().isEmpty());
193       assertEquals(2, entry.getModifications().size());
194       assertTrue(!cache.exists("/one/two"));
195       assertEquals(null, dummy.getCalled());
196       cache.stop();
197    }
198
199
200 }
201
Popular Tags