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 |
| import java.util.HashMap; |
16 |
| import java.util.Map; |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| public class NodeInterceptorRemoveDataTest extends AbstractOptimisticTestCase |
22 |
| { |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
4
| public NodeInterceptorRemoveDataTest(String name)
|
29 |
| { |
30 |
4
| super(name);
|
31 |
| } |
32 |
| |
33 |
1
| public void testTransactionRemoveNoNodeDataMethod() throws Exception
|
34 |
| { |
35 |
| |
36 |
1
| TestListener listener = new TestListener();
|
37 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
38 |
| |
39 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
40 |
1
| interceptor.setCache(cache);
|
41 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
42 |
1
| nodeInterceptor.setCache(cache);
|
43 |
1
| MockInterceptor dummy = new MockInterceptor();
|
44 |
1
| dummy.setCache(cache);
|
45 |
| |
46 |
1
| interceptor.setNext(nodeInterceptor);
|
47 |
1
| nodeInterceptor.setNext(dummy);
|
48 |
| |
49 |
1
| cache.setInterceptorChain(interceptor);
|
50 |
| |
51 |
| |
52 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
53 |
1
| mgr.begin();
|
54 |
1
| Transaction tx = mgr.getTransaction();
|
55 |
| |
56 |
| |
57 |
1
| cache.getInvocationContext().setTransaction(tx);
|
58 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
59 |
| |
60 |
1
| cache.removeData("/one/two");
|
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
| mgr.commit();
|
73 |
| |
74 |
| |
75 |
1
| assertEquals(0, workspace.getNodes().size());
|
76 |
1
| assertNull(workspace.getNode(Fqn.fromString("/one/two")));
|
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 testTransactionRemoveEmptyMethod() 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
| Map temp = new HashMap();
|
112 |
| |
113 |
1
| cache.put("/one/two", temp);
|
114 |
| |
115 |
1
| cache.removeData("/one/two");
|
116 |
| |
117 |
1
| assertEquals(null, dummy.getCalled());
|
118 |
1
| TransactionTable table = cache.getTransactionTable();
|
119 |
| |
120 |
1
| GlobalTransaction gtx = table.get(tx);
|
121 |
| |
122 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
123 |
| |
124 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
125 |
| |
126 |
| |
127 |
1
| mgr.commit();
|
128 |
| |
129 |
| |
130 |
1
| assertEquals(3, workspace.getNodes().size());
|
131 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
132 |
1
| assertNull(workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
133 |
1
| assertEquals(0, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
|
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 testTransactionRemoveDataMethod() 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 |
1
| Map temp = new HashMap();
|
170 |
1
| temp.put("key1", pojo);
|
171 |
1
| cache.put("/one/two", temp);
|
172 |
| |
173 |
| |
174 |
1
| assertEquals(null, dummy.getCalled());
|
175 |
1
| TransactionTable table = cache.getTransactionTable();
|
176 |
| |
177 |
1
| GlobalTransaction gtx = table.get(tx);
|
178 |
| |
179 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
180 |
| |
181 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
182 |
1
| assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
|
183 |
| |
184 |
1
| cache.removeData("/one/two");
|
185 |
| |
186 |
1
| mgr.commit();
|
187 |
| |
188 |
| |
189 |
1
| assertEquals(3, workspace.getNodes().size());
|
190 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
191 |
1
| assertNull(workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
192 |
1
| assertEquals(0, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
|
193 |
1
| assertTrue(entry.getLocks().isEmpty());
|
194 |
1
| assertEquals(2, entry.getModifications().size());
|
195 |
1
| assertTrue(!cache.exists("/one/two"));
|
196 |
1
| assertEquals(null, dummy.getCalled());
|
197 |
1
| cache.stop();
|
198 |
| } |
199 |
| |
200 |
| |
201 |
1
| public void testTransactionRemoveOtherNodeDataMethod() throws Exception
|
202 |
| { |
203 |
| |
204 |
1
| TestListener listener = new TestListener();
|
205 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
206 |
| |
207 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
208 |
1
| interceptor.setCache(cache);
|
209 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
210 |
1
| nodeInterceptor.setCache(cache);
|
211 |
1
| MockInterceptor dummy = new MockInterceptor();
|
212 |
1
| dummy.setCache(cache);
|
213 |
| |
214 |
1
| interceptor.setNext(nodeInterceptor);
|
215 |
1
| nodeInterceptor.setNext(dummy);
|
216 |
| |
217 |
1
| cache.setInterceptorChain(interceptor);
|
218 |
| |
219 |
| |
220 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
221 |
1
| mgr.begin();
|
222 |
1
| Transaction tx = mgr.getTransaction();
|
223 |
| |
224 |
| |
225 |
1
| cache.getInvocationContext().setTransaction(tx);
|
226 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
227 |
| |
228 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
229 |
1
| Map temp = new HashMap();
|
230 |
1
| temp.put("key1", pojo);
|
231 |
1
| cache.put("/one/two", temp);
|
232 |
| |
233 |
| |
234 |
1
| assertEquals(null, dummy.getCalled());
|
235 |
1
| TransactionTable table = cache.getTransactionTable();
|
236 |
| |
237 |
1
| GlobalTransaction gtx = table.get(tx);
|
238 |
| |
239 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
240 |
| |
241 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
242 |
1
| assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
|
243 |
| |
244 |
1
| cache.removeData("/one");
|
245 |
| |
246 |
1
| mgr.commit();
|
247 |
| |
248 |
| |
249 |
1
| assertEquals(3, workspace.getNodes().size());
|
250 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
251 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
252 |
1
| assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
|
253 |
1
| assertTrue(entry.getLocks().isEmpty());
|
254 |
1
| assertEquals(2, entry.getModifications().size());
|
255 |
1
| assertTrue(!cache.exists("/one/two"));
|
256 |
1
| assertEquals(null, dummy.getCalled());
|
257 |
1
| cache.stop();
|
258 |
| } |
259 |
| } |