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 NodeInterceptorGetKeyValTest extends AbstractOptimisticTestCase |
20 |
| { |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
4
| public NodeInterceptorGetKeyValTest(String name)
|
27 |
| { |
28 |
4
| super(name);
|
29 |
| } |
30 |
| |
31 |
1
| public void testTransactionGetKeyMethod() 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 |
| |
72 |
1
| assertEquals(pojo, cache.get("/one/two", "key1"));
|
73 |
1
| mgr.commit();
|
74 |
| |
75 |
| |
76 |
1
| assertEquals(3, workspace.getNodes().size());
|
77 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
78 |
1
| assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
79 |
1
| assertTrue(entry.getLocks().isEmpty());
|
80 |
1
| assertEquals(1, entry.getModifications().size());
|
81 |
1
| assertTrue(!cache.exists("/one/two"));
|
82 |
1
| assertEquals(null, dummy.getCalled());
|
83 |
| |
84 |
| |
85 |
| |
86 |
1
| mgr.begin();
|
87 |
| |
88 |
1
| Transaction tx2 = mgr.getTransaction();
|
89 |
| |
90 |
| |
91 |
1
| cache.getInvocationContext().setTransaction(tx2);
|
92 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx2));
|
93 |
| |
94 |
1
| assertNull(cache.get("/one/two", "key1"));
|
95 |
1
| mgr.commit();
|
96 |
1
| cache.stop();
|
97 |
| } |
98 |
| |
99 |
1
| public void testTransactionGetKeyValOverwriteMethod() throws Exception
|
100 |
| { |
101 |
| |
102 |
1
| TestListener listener = new TestListener();
|
103 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
104 |
| |
105 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
106 |
1
| interceptor.setCache(cache);
|
107 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
108 |
1
| nodeInterceptor.setCache(cache);
|
109 |
1
| MockInterceptor dummy = new MockInterceptor();
|
110 |
1
| dummy.setCache(cache);
|
111 |
| |
112 |
1
| interceptor.setNext(nodeInterceptor);
|
113 |
1
| nodeInterceptor.setNext(dummy);
|
114 |
| |
115 |
1
| cache.setInterceptorChain(interceptor);
|
116 |
| |
117 |
| |
118 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
119 |
1
| mgr.begin();
|
120 |
1
| Transaction tx = mgr.getTransaction();
|
121 |
| |
122 |
| |
123 |
1
| cache.getInvocationContext().setTransaction(tx);
|
124 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
125 |
| |
126 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
127 |
| |
128 |
1
| cache.put("/one/two", "key1", pojo);
|
129 |
| |
130 |
| |
131 |
1
| SamplePojo pojo2 = new SamplePojo(22, "test2");
|
132 |
| |
133 |
1
| cache.put("/one/two", "key1", pojo2);
|
134 |
| |
135 |
1
| assertEquals(null, dummy.getCalled());
|
136 |
1
| TransactionTable table = cache.getTransactionTable();
|
137 |
| |
138 |
1
| GlobalTransaction gtx = table.get(tx);
|
139 |
| |
140 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
141 |
| |
142 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
143 |
1
| assertEquals(pojo2, cache.get("/one/two", "key1"));
|
144 |
1
| mgr.commit();
|
145 |
| |
146 |
| |
147 |
1
| assertEquals(3, workspace.getNodes().size());
|
148 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
149 |
1
| assertEquals(pojo2, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
150 |
1
| assertTrue(entry.getLocks().isEmpty());
|
151 |
1
| assertEquals(2, entry.getModifications().size());
|
152 |
1
| assertTrue(!cache.exists("/one/two"));
|
153 |
1
| assertEquals(null, dummy.getCalled());
|
154 |
| |
155 |
1
| cache.stop();
|
156 |
| } |
157 |
| |
158 |
1
| public void testTransactionGetKeyValOverwriteNullMethod() throws Exception
|
159 |
| { |
160 |
| |
161 |
1
| TestListener listener = new TestListener();
|
162 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
163 |
| |
164 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
165 |
1
| interceptor.setCache(cache);
|
166 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
167 |
1
| nodeInterceptor.setCache(cache);
|
168 |
1
| MockInterceptor dummy = new MockInterceptor();
|
169 |
1
| dummy.setCache(cache);
|
170 |
| |
171 |
1
| interceptor.setNext(nodeInterceptor);
|
172 |
1
| nodeInterceptor.setNext(dummy);
|
173 |
| |
174 |
1
| cache.setInterceptorChain(interceptor);
|
175 |
| |
176 |
| |
177 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
178 |
1
| mgr.begin();
|
179 |
1
| Transaction tx = mgr.getTransaction();
|
180 |
| |
181 |
| |
182 |
1
| cache.getInvocationContext().setTransaction(tx);
|
183 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
184 |
| |
185 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
186 |
| |
187 |
1
| cache.put("/one/two", "key1", pojo);
|
188 |
| |
189 |
| |
190 |
1
| cache.put("/one/two", "key1", null);
|
191 |
| |
192 |
1
| assertEquals(null, dummy.getCalled());
|
193 |
1
| TransactionTable table = cache.getTransactionTable();
|
194 |
| |
195 |
1
| GlobalTransaction gtx = table.get(tx);
|
196 |
| |
197 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
198 |
| |
199 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
200 |
| |
201 |
1
| assertEquals(null, cache.get("/one/two", "key1"));
|
202 |
| |
203 |
1
| mgr.commit();
|
204 |
| |
205 |
1
| assertEquals(3, workspace.getNodes().size());
|
206 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
207 |
1
| assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
208 |
1
| assertTrue(entry.getLocks().isEmpty());
|
209 |
1
| assertEquals(2, entry.getModifications().size());
|
210 |
1
| assertTrue(!cache.exists("/one/two"));
|
211 |
1
| assertEquals(null, dummy.getCalled());
|
212 |
1
| cache.stop();
|
213 |
| } |
214 |
| |
215 |
| |
216 |
1
| public void testTwoTransactionGetIsolationKeyValMethod() throws Exception
|
217 |
| { |
218 |
| |
219 |
1
| TestListener listener = new TestListener();
|
220 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
221 |
| |
222 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
223 |
1
| interceptor.setCache(cache);
|
224 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
225 |
1
| nodeInterceptor.setCache(cache);
|
226 |
1
| MockInterceptor dummy = new MockInterceptor();
|
227 |
1
| dummy.setCache(cache);
|
228 |
| |
229 |
1
| interceptor.setNext(nodeInterceptor);
|
230 |
1
| nodeInterceptor.setNext(dummy);
|
231 |
| |
232 |
1
| cache.setInterceptorChain(interceptor);
|
233 |
| |
234 |
| |
235 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
236 |
1
| mgr.begin();
|
237 |
1
| Transaction tx = mgr.getTransaction();
|
238 |
| |
239 |
| |
240 |
1
| cache.getInvocationContext().setTransaction(tx);
|
241 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
242 |
| |
243 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
244 |
| |
245 |
1
| cache.put("/one/two", "key1", pojo);
|
246 |
| |
247 |
1
| assertEquals(pojo, cache.get("/one/two", "key1"));
|
248 |
| |
249 |
1
| mgr.suspend();
|
250 |
| |
251 |
| |
252 |
1
| mgr.begin();
|
253 |
1
| Transaction tx2 = mgr.getTransaction();
|
254 |
| |
255 |
1
| cache.getInvocationContext().setTransaction(tx2);
|
256 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx2));
|
257 |
| |
258 |
1
| SamplePojo pojo2 = new SamplePojo(22, "test2");
|
259 |
| |
260 |
1
| cache.put("/one/two", "key2", pojo2);
|
261 |
1
| assertEquals(null, cache.get("/one/two", "key1"));
|
262 |
1
| assertEquals(pojo2, cache.get("/one/two", "key2"));
|
263 |
| |
264 |
| |
265 |
1
| assertEquals(null, dummy.getCalled());
|
266 |
1
| TransactionTable table = cache.getTransactionTable();
|
267 |
| |
268 |
| |
269 |
1
| GlobalTransaction gtx = table.get(tx);
|
270 |
| |
271 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
272 |
| |
273 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
274 |
| |
275 |
| |
276 |
1
| GlobalTransaction gtx2 = table.get(tx2);
|
277 |
| |
278 |
1
| OptimisticTransactionEntry entry2 = (OptimisticTransactionEntry) table.get(gtx2);
|
279 |
| |
280 |
1
| TransactionWorkspace workspace2 = entry2.getTransactionWorkSpace();
|
281 |
| |
282 |
| |
283 |
1
| mgr.commit();
|
284 |
1
| mgr.resume(tx);
|
285 |
1
| mgr.commit();
|
286 |
| |
287 |
| |
288 |
1
| assertEquals(3, workspace.getNodes().size());
|
289 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
290 |
1
| assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
291 |
1
| assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key2"));
|
292 |
1
| assertTrue(entry.getLocks().isEmpty());
|
293 |
1
| assertEquals(1, entry.getModifications().size());
|
294 |
| |
295 |
| |
296 |
1
| assertEquals(3, workspace2.getNodes().size());
|
297 |
1
| assertNotNull(workspace2.getNode(Fqn.fromString("/one/two")));
|
298 |
1
| assertEquals(null, workspace2.getNode(Fqn.fromString("/one/two")).get("key1"));
|
299 |
1
| assertEquals(pojo2, workspace2.getNode(Fqn.fromString("/one/two")).get("key2"));
|
300 |
1
| assertTrue(entry2.getLocks().isEmpty());
|
301 |
1
| assertEquals(1, entry2.getModifications().size());
|
302 |
| |
303 |
1
| assertTrue(!cache.exists("/one/two"));
|
304 |
1
| assertEquals(null, dummy.getCalled());
|
305 |
1
| cache.stop();
|
306 |
| } |
307 |
| |
308 |
| |
309 |
| } |