KickJava   Java API By Example, From Geeks To Geeks.

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


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

28    public NodeInterceptorPutMapTest(String JavaDoc name)
29    {
30       super(name);
31    }
32
33    public void testTransactionPutDataMethod() 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
47       interceptor.setNext(nodeInterceptor);
48       nodeInterceptor.setNext(dummy);
49
50       cache.setInterceptorChain(interceptor);
51
52 // first set up a node with a pojo
53
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
54       mgr.begin();
55       Transaction tx = mgr.getTransaction();
56
57       // inject InvocationContext
58
cache.getInvocationContext().setTransaction(tx);
59       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
60
61       SamplePojo pojo = new SamplePojo(21, "test");
62       Map JavaDoc temp = new HashMap JavaDoc();
63       temp.put("key1", pojo);
64       cache.put("/one/two", temp);
65
66       assertEquals(null, dummy.getCalled());
67       TransactionTable table = cache.getTransactionTable();
68
69       GlobalTransaction gtx = table.get(tx);
70
71       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
72
73       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
74
75
76       mgr.commit();
77
78       //assert what should be the results of our call
79
assertEquals(3, workspace.getNodes().size());
80       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
81       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
82       assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
83       assertTrue(entry.getLocks().isEmpty());
84       assertEquals(1, entry.getModifications().size());
85       assertTrue(!cache.exists("/one/two"));
86       assertEquals(null, dummy.getCalled());
87       cache.stop();
88    }
89
90    public void testTransactionPutLocalOverwriteDataMethod() throws Exception JavaDoc
91    {
92
93       TestListener listener = new TestListener();
94       final CacheImpl cache = createCacheWithListener(listener);
95
96       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
97       interceptor.setCache(cache);
98       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
99       nodeInterceptor.setCache(cache);
100       MockInterceptor dummy = new MockInterceptor();
101       dummy.setCache(cache);
102
103       interceptor.setNext(nodeInterceptor);
104       nodeInterceptor.setNext(dummy);
105
106       cache.setInterceptorChain(interceptor);
107
108 // first set up a node with a pojo
109
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
110       mgr.begin();
111       Transaction tx = mgr.getTransaction();
112
113       // inject InvocationContext
114
cache.getInvocationContext().setTransaction(tx);
115       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
116
117       SamplePojo pojo = new SamplePojo(21, "test");
118       Map JavaDoc temp = new HashMap JavaDoc();
119       temp.put("key1", pojo);
120       cache.put("/one/two", temp);
121
122       //overwrite the map we just put in
123
SamplePojo pojo2 = new SamplePojo(22, "test");
124       Map JavaDoc temp2 = new HashMap JavaDoc();
125       temp2.put("key1", pojo2);
126       cache.put("/one/two", temp2);
127
128       assertEquals(null, dummy.getCalled());
129       TransactionTable table = cache.getTransactionTable();
130
131       GlobalTransaction gtx = table.get(tx);
132
133       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
134
135       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
136
137       mgr.commit();
138       //assert what should be the results of our call
139
assertEquals(3, workspace.getNodes().size());
140       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
141       assertEquals(pojo2, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
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 testTransactionPutLocalEmptyMethod() 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
182       Map JavaDoc temp2 = new HashMap JavaDoc();
183
184       cache.put("/one/two", temp2);
185
186       assertEquals(null, dummy.getCalled());
187       TransactionTable table = cache.getTransactionTable();
188
189       GlobalTransaction gtx = table.get(tx);
190
191       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
192
193       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
194
195       mgr.commit();
196       //assert what should be the results of our call
197
assertEquals(3, workspace.getNodes().size());
198       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
199       assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
200       assertTrue(entry.getLocks().isEmpty());
201       assertEquals(2, entry.getModifications().size());
202       assertTrue(!cache.exists("/one/two"));
203       assertEquals(null, dummy.getCalled());
204       cache.stop();
205    }
206 }
207
Popular Tags