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; |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| public class NodeInterceptorKeyValTest extends AbstractOptimisticTestCase |
20 |
| { |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
5
| public NodeInterceptorKeyValTest(String name)
|
27 |
| { |
28 |
5
| super(name);
|
29 |
| } |
30 |
| |
31 |
1
| public void testTransactionPutKeyMethod() throws Exception
|
32 |
| { |
33 |
| |
34 |
1
| TestListener listener = new TestListener();
|
35 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
36 |
| |
37 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
38 |
1
| interceptor.setCache(cache);
|
39 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
40 |
1
| nodeInterceptor.setCache(cache);
|
41 |
1
| MockInterceptor dummy = new MockInterceptor();
|
42 |
1
| dummy.setCache(cache);
|
43 |
| |
44 |
1
| interceptor.setNext(nodeInterceptor);
|
45 |
1
| nodeInterceptor.setNext(dummy);
|
46 |
| |
47 |
1
| cache.setInterceptorChain(interceptor);
|
48 |
| |
49 |
| |
50 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
51 |
1
| mgr.begin();
|
52 |
1
| Transaction tx = mgr.getTransaction();
|
53 |
| |
54 |
| |
55 |
1
| cache.getInvocationContext().setTransaction(tx);
|
56 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
57 |
| |
58 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
59 |
| |
60 |
1
| cache.put("/one/two", "key1", pojo);
|
61 |
| |
62 |
1
| assertEquals(null, dummy.getCalled());
|
63 |
1
| TransactionTable table = cache.getTransactionTable();
|
64 |
| |
65 |
1
| GlobalTransaction gtx = table.get(tx);
|
66 |
| |
67 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
68 |
| |
69 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
70 |
| |
71 |
1
| mgr.commit();
|
72 |
| |
73 |
| |
74 |
1
| assertEquals(3, workspace.getNodes().size());
|
75 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
76 |
1
| assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
77 |
1
| assertTrue(entry.getLocks().isEmpty());
|
78 |
1
| assertEquals(1, entry.getModifications().size());
|
79 |
1
| assertTrue(!cache.exists("/one/two"));
|
80 |
1
| assertEquals(null, dummy.getCalled());
|
81 |
1
| cache.stop();
|
82 |
| } |
83 |
| |
84 |
1
| public void testTransactionKeyValOverwriteMethod() throws Exception
|
85 |
| { |
86 |
| |
87 |
1
| TestListener listener = new TestListener();
|
88 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
89 |
| |
90 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
91 |
1
| interceptor.setCache(cache);
|
92 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
93 |
1
| nodeInterceptor.setCache(cache);
|
94 |
1
| MockInterceptor dummy = new MockInterceptor();
|
95 |
1
| dummy.setCache(cache);
|
96 |
| |
97 |
1
| interceptor.setNext(nodeInterceptor);
|
98 |
1
| nodeInterceptor.setNext(dummy);
|
99 |
| |
100 |
1
| cache.setInterceptorChain(interceptor);
|
101 |
| |
102 |
| |
103 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
104 |
1
| mgr.begin();
|
105 |
1
| Transaction tx = mgr.getTransaction();
|
106 |
| |
107 |
| |
108 |
1
| cache.getInvocationContext().setTransaction(tx);
|
109 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
110 |
| |
111 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
112 |
| |
113 |
1
| cache.put("/one/two", "key1", pojo);
|
114 |
| |
115 |
| |
116 |
1
| SamplePojo pojo2 = new SamplePojo(21, "test");
|
117 |
| |
118 |
1
| cache.put("/one/two", "key1", pojo2);
|
119 |
| |
120 |
1
| assertEquals(null, dummy.getCalled());
|
121 |
1
| TransactionTable table = cache.getTransactionTable();
|
122 |
| |
123 |
1
| GlobalTransaction gtx = table.get(tx);
|
124 |
| |
125 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
126 |
| |
127 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
128 |
| |
129 |
1
| mgr.commit();
|
130 |
| |
131 |
1
| assertEquals(3, workspace.getNodes().size());
|
132 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
133 |
1
| assertEquals(pojo2, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
134 |
1
| assertTrue(entry.getLocks().isEmpty());
|
135 |
1
| assertEquals(2, entry.getModifications().size());
|
136 |
1
| assertTrue(!cache.exists("/one/two"));
|
137 |
1
| assertEquals(null, dummy.getCalled());
|
138 |
1
| cache.stop();
|
139 |
| } |
140 |
| |
141 |
1
| public void testTransactionKeyValOverwriteNullMethod() throws Exception
|
142 |
| { |
143 |
| |
144 |
1
| TestListener listener = new TestListener();
|
145 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
146 |
| |
147 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
148 |
1
| interceptor.setCache(cache);
|
149 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
150 |
1
| nodeInterceptor.setCache(cache);
|
151 |
1
| MockInterceptor dummy = new MockInterceptor();
|
152 |
1
| dummy.setCache(cache);
|
153 |
| |
154 |
1
| interceptor.setNext(nodeInterceptor);
|
155 |
1
| nodeInterceptor.setNext(dummy);
|
156 |
| |
157 |
1
| cache.setInterceptorChain(interceptor);
|
158 |
| |
159 |
| |
160 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
161 |
1
| mgr.begin();
|
162 |
1
| Transaction tx = mgr.getTransaction();
|
163 |
| |
164 |
| |
165 |
1
| cache.getInvocationContext().setTransaction(tx);
|
166 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
167 |
| |
168 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
169 |
| |
170 |
1
| cache.put("/one/two", "key1", pojo);
|
171 |
| |
172 |
| |
173 |
1
| cache.put("/one/two", "key1", null);
|
174 |
| |
175 |
1
| assertEquals(null, dummy.getCalled());
|
176 |
1
| TransactionTable table = cache.getTransactionTable();
|
177 |
| |
178 |
1
| GlobalTransaction gtx = table.get(tx);
|
179 |
| |
180 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
181 |
| |
182 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
183 |
| |
184 |
1
| mgr.commit();
|
185 |
| |
186 |
1
| assertEquals(3, workspace.getNodes().size());
|
187 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
188 |
1
| assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
189 |
1
| assertTrue(entry.getLocks().isEmpty());
|
190 |
1
| assertEquals(2, entry.getModifications().size());
|
191 |
1
| assertTrue(!cache.exists("/one/two"));
|
192 |
1
| assertEquals(null, dummy.getCalled());
|
193 |
1
| cache.stop();
|
194 |
| } |
195 |
| |
196 |
| |
197 |
1
| public void testTransactionAdditionlaKeyValMethod() throws Exception
|
198 |
| { |
199 |
| |
200 |
1
| TestListener listener = new TestListener();
|
201 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
202 |
| |
203 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
204 |
1
| interceptor.setCache(cache);
|
205 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
206 |
1
| nodeInterceptor.setCache(cache);
|
207 |
1
| MockInterceptor dummy = new MockInterceptor();
|
208 |
1
| dummy.setCache(cache);
|
209 |
| |
210 |
1
| interceptor.setNext(nodeInterceptor);
|
211 |
1
| nodeInterceptor.setNext(dummy);
|
212 |
| |
213 |
1
| cache.setInterceptorChain(interceptor);
|
214 |
| |
215 |
| |
216 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
217 |
1
| mgr.begin();
|
218 |
1
| Transaction tx = mgr.getTransaction();
|
219 |
| |
220 |
| |
221 |
1
| cache.getInvocationContext().setTransaction(tx);
|
222 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
223 |
| |
224 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
225 |
| |
226 |
1
| cache.put("/one/two", "key1", pojo);
|
227 |
| |
228 |
1
| SamplePojo pojo2 = new SamplePojo(21, "test");
|
229 |
| |
230 |
1
| cache.put("/one/two", "key2", pojo2);
|
231 |
| |
232 |
1
| assertEquals(null, dummy.getCalled());
|
233 |
1
| TransactionTable table = cache.getTransactionTable();
|
234 |
| |
235 |
1
| GlobalTransaction gtx = table.get(tx);
|
236 |
| |
237 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
238 |
| |
239 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
240 |
| |
241 |
1
| mgr.commit();
|
242 |
| |
243 |
1
| assertEquals(3, workspace.getNodes().size());
|
244 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
245 |
| |
246 |
1
| assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
247 |
1
| assertEquals(pojo2, workspace.getNode(Fqn.fromString("/one/two")).get("key2"));
|
248 |
1
| assertTrue(entry.getLocks().isEmpty());
|
249 |
1
| assertEquals(2, entry.getModifications().size());
|
250 |
1
| assertTrue(!cache.exists("/one/two"));
|
251 |
1
| assertEquals(null, dummy.getCalled());
|
252 |
1
| cache.stop();
|
253 |
| } |
254 |
| |
255 |
1
| public void testTwoTransactionAdditionKeyValMethod() throws Exception
|
256 |
| { |
257 |
| |
258 |
1
| TestListener listener = new TestListener();
|
259 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
260 |
| |
261 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
262 |
1
| interceptor.setCache(cache);
|
263 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
264 |
1
| nodeInterceptor.setCache(cache);
|
265 |
1
| MockInterceptor dummy = new MockInterceptor();
|
266 |
1
| dummy.setCache(cache);
|
267 |
| |
268 |
1
| interceptor.setNext(nodeInterceptor);
|
269 |
1
| nodeInterceptor.setNext(dummy);
|
270 |
| |
271 |
1
| cache.setInterceptorChain(interceptor);
|
272 |
| |
273 |
| |
274 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
275 |
1
| mgr.begin();
|
276 |
1
| Transaction tx = mgr.getTransaction();
|
277 |
| |
278 |
| |
279 |
1
| cache.getInvocationContext().setTransaction(tx);
|
280 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
281 |
| |
282 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
283 |
| |
284 |
1
| cache.put("/one/two", "key1", pojo);
|
285 |
| |
286 |
| |
287 |
1
| mgr.suspend();
|
288 |
| |
289 |
| |
290 |
1
| mgr.begin();
|
291 |
1
| Transaction tx2 = mgr.getTransaction();
|
292 |
1
| cache.getInvocationContext().setTransaction(tx2);
|
293 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx2));
|
294 |
| |
295 |
1
| SamplePojo pojo2 = new SamplePojo(21, "test");
|
296 |
| |
297 |
1
| cache.put("/one/two", "key2", pojo2);
|
298 |
| |
299 |
1
| assertEquals(null, dummy.getCalled());
|
300 |
1
| TransactionTable table = cache.getTransactionTable();
|
301 |
| |
302 |
| |
303 |
1
| GlobalTransaction gtx = table.get(tx);
|
304 |
| |
305 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
306 |
| |
307 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
308 |
| |
309 |
| |
310 |
1
| GlobalTransaction gtx2 = table.get(tx2);
|
311 |
| |
312 |
1
| OptimisticTransactionEntry entry2 = (OptimisticTransactionEntry) table.get(gtx2);
|
313 |
| |
314 |
1
| TransactionWorkspace workspace2 = entry2.getTransactionWorkSpace();
|
315 |
| |
316 |
| |
317 |
1
| mgr.commit();
|
318 |
1
| mgr.resume(tx);
|
319 |
1
| mgr.commit();
|
320 |
| |
321 |
| |
322 |
1
| assertEquals(3, workspace.getNodes().size());
|
323 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
324 |
1
| assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
325 |
1
| assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key2"));
|
326 |
1
| assertTrue(entry.getLocks().isEmpty());
|
327 |
1
| assertEquals(1, entry.getModifications().size());
|
328 |
| |
329 |
| |
330 |
1
| assertEquals(3, workspace2.getNodes().size());
|
331 |
1
| assertNotNull(workspace2.getNode(Fqn.fromString("/one/two")));
|
332 |
1
| assertEquals(null, workspace2.getNode(Fqn.fromString("/one/two")).get("key1"));
|
333 |
1
| assertEquals(pojo2, workspace2.getNode(Fqn.fromString("/one/two")).get("key2"));
|
334 |
1
| assertTrue(entry2.getLocks().isEmpty());
|
335 |
1
| assertEquals(1, entry2.getModifications().size());
|
336 |
| |
337 |
1
| assertTrue(!cache.exists("/one/two"));
|
338 |
1
| assertEquals(null, dummy.getCalled());
|
339 |
1
| cache.stop();
|
340 |
| } |
341 |
| |
342 |
| |
343 |
| } |