KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on 17-Feb-2005
3  *
4  *
5  *
6  */

7 package org.jboss.cache.optimistic;
8
9 import org.jboss.cache.CacheImpl;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.GlobalTransaction;
12 import org.jboss.cache.OptimisticTransactionEntry;
13 import org.jboss.cache.TransactionTable;
14 import org.jboss.cache.interceptors.Interceptor;
15 import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
16 import org.jboss.cache.interceptors.OptimisticNodeInterceptor;
17 import org.jboss.cache.loader.SamplePojo;
18 import org.jboss.cache.transaction.DummyTransactionManager;
19
20 import javax.transaction.Transaction JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * @author xenephon
26  */

27 public class NodeInterceptorRemoveKeyValTest extends AbstractOptimisticTestCase
28 {
29
30
31    /**
32     * @param name
33     */

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