KickJava   Java API By Example, From Geeks To Geeks.

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


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 NodeInterceptorRemoveDataTest extends AbstractOptimisticTestCase
22 {
23
24
25    /**
26     * @param name
27     */

28    public NodeInterceptorRemoveDataTest(String JavaDoc name)
29    {
30       super(name);
31    }
32
33    public void testTransactionRemoveNoNodeDataMethod() 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       cache.removeData("/one/two");
61
62       assertEquals(null, dummy.getCalled());
63       TransactionTable table = cache.getTransactionTable();
64
65       GlobalTransaction gtx = table.get(tx);
66
67       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
68
69       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
70
71
72       mgr.commit();
73
74       //assert what should be the results of our call
75
assertEquals(0, workspace.getNodes().size());
76       assertNull(workspace.getNode(Fqn.fromString("/one/two")));
77       assertTrue(entry.getLocks().isEmpty());
78       assertEquals(1, entry.getModifications().size());
79       assertTrue(!cache.exists("/one/two"));
80       assertEquals(null, dummy.getCalled());
81       cache.stop();
82    }
83
84    public void testTransactionRemoveEmptyMethod() throws Exception JavaDoc
85    {
86
87       TestListener listener = new TestListener();
88       final CacheImpl cache = createCacheWithListener(listener);
89
90       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
91       interceptor.setCache(cache);
92       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
93       nodeInterceptor.setCache(cache);
94       MockInterceptor dummy = new MockInterceptor();
95       dummy.setCache(cache);
96
97       interceptor.setNext(nodeInterceptor);
98       nodeInterceptor.setNext(dummy);
99
100       cache.setInterceptorChain(interceptor);
101
102 // first set up a node with a pojo
103
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
104       mgr.begin();
105       Transaction tx = mgr.getTransaction();
106
107       // inject InvocationContext
108
cache.getInvocationContext().setTransaction(tx);
109       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
110
111       Map JavaDoc temp = new HashMap JavaDoc();
112
113       cache.put("/one/two", temp);
114
115       cache.removeData("/one/two");
116
117       assertEquals(null, dummy.getCalled());
118       TransactionTable table = cache.getTransactionTable();
119
120       GlobalTransaction gtx = table.get(tx);
121
122       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
123
124       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
125
126
127       mgr.commit();
128
129       //assert what should be the results of our call
130
assertEquals(3, workspace.getNodes().size());
131       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
132       assertNull(workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
133       assertEquals(0, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
134       assertTrue(entry.getLocks().isEmpty());
135       assertEquals(2, entry.getModifications().size());
136       assertTrue(!cache.exists("/one/two"));
137       assertEquals(null, dummy.getCalled());
138       cache.stop();
139    }
140
141    public void testTransactionRemoveDataMethod() throws Exception JavaDoc
142    {
143
144       TestListener listener = new TestListener();
145       final CacheImpl cache = createCacheWithListener(listener);
146
147       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
148       interceptor.setCache(cache);
149       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
150       nodeInterceptor.setCache(cache);
151       MockInterceptor dummy = new MockInterceptor();
152       dummy.setCache(cache);
153
154       interceptor.setNext(nodeInterceptor);
155       nodeInterceptor.setNext(dummy);
156
157       cache.setInterceptorChain(interceptor);
158
159 // first set up a node with a pojo
160
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
161       mgr.begin();
162       Transaction tx = mgr.getTransaction();
163
164       // inject InvocationContext
165
cache.getInvocationContext().setTransaction(tx);
166       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
167
168       SamplePojo pojo = new SamplePojo(21, "test");
169       Map JavaDoc temp = new HashMap JavaDoc();
170       temp.put("key1", pojo);
171       cache.put("/one/two", temp);
172
173
174       assertEquals(null, dummy.getCalled());
175       TransactionTable table = cache.getTransactionTable();
176
177       GlobalTransaction gtx = table.get(tx);
178
179       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
180
181       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
182       assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
183
184       cache.removeData("/one/two");
185
186       mgr.commit();
187
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       assertNull(workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
192       assertEquals(0, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
193       assertTrue(entry.getLocks().isEmpty());
194       assertEquals(2, entry.getModifications().size());
195       assertTrue(!cache.exists("/one/two"));
196       assertEquals(null, dummy.getCalled());
197       cache.stop();
198    }
199
200
201    public void testTransactionRemoveOtherNodeDataMethod() throws Exception JavaDoc
202    {
203
204       TestListener listener = new TestListener();
205       final CacheImpl cache = createCacheWithListener(listener);
206
207       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
208       interceptor.setCache(cache);
209       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
210       nodeInterceptor.setCache(cache);
211       MockInterceptor dummy = new MockInterceptor();
212       dummy.setCache(cache);
213
214       interceptor.setNext(nodeInterceptor);
215       nodeInterceptor.setNext(dummy);
216
217       cache.setInterceptorChain(interceptor);
218
219 // first set up a node with a pojo
220
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
221       mgr.begin();
222       Transaction tx = mgr.getTransaction();
223
224       // inject InvocationContext
225
cache.getInvocationContext().setTransaction(tx);
226       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
227
228       SamplePojo pojo = new SamplePojo(21, "test");
229       Map JavaDoc temp = new HashMap JavaDoc();
230       temp.put("key1", pojo);
231       cache.put("/one/two", temp);
232
233
234       assertEquals(null, dummy.getCalled());
235       TransactionTable table = cache.getTransactionTable();
236
237       GlobalTransaction gtx = table.get(tx);
238
239       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
240
241       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
242       assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
243
244       cache.removeData("/one");
245
246       mgr.commit();
247
248       //assert what should be the results of our call
249
assertEquals(3, workspace.getNodes().size());
250       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
251       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
252       assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
253       assertTrue(entry.getLocks().isEmpty());
254       assertEquals(2, entry.getModifications().size());
255       assertTrue(!cache.exists("/one/two"));
256       assertEquals(null, dummy.getCalled());
257       cache.stop();
258    }
259 }
260
Popular Tags