KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
16
17 /**
18  * @author xenephon
19  */

20 public class NodeInterceptorGetKeysTest extends AbstractOptimisticTestCase
21 {
22
23
24    /**
25     * @param name
26     */

27    public NodeInterceptorGetKeysTest(String JavaDoc name)
28    {
29       super(name);
30    }
31
32
33    public void testTransactionGetKeysMethod() 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       SamplePojo pojo = new SamplePojo(21, "test");
61
62       cache.put("/one/two", "key1", pojo);
63
64       assertEquals(null, dummy.getCalled());
65       TransactionTable table = cache.getTransactionTable();
66
67       GlobalTransaction gtx = table.get(tx);
68
69       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
70
71       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
72
73       //assert we can see this with a key value get in the transaction
74
assertEquals(1, cache.getKeys("/one/two").size());
75       mgr.commit();
76
77       //assert what should be the results of our call
78
assertEquals(3, workspace.getNodes().size());
79       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
80       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
81       assertTrue(entry.getLocks().isEmpty());
82       assertEquals(1, entry.getModifications().size());
83       assertTrue(!cache.exists("/one/two"));
84       assertEquals(null, dummy.getCalled());
85
86       //assert that we cannot see the change if we have not put it into the cache
87
// we need to do this as we do not have the tx interceptor in this stack
88
mgr.begin();
89
90       Transaction tx2 = mgr.getTransaction();
91
92       // inject InvocationContext
93
cache.getInvocationContext().setTransaction(tx2);
94       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx2));
95
96       assertNull(cache.get("/one/two", "key1"));
97       mgr.commit();
98       cache.stop();
99    }
100
101
102    public void testTransactionGetNoKeysMethod() throws Exception JavaDoc
103    {
104
105       TestListener listener = new TestListener();
106       final CacheImpl cache = createCacheWithListener(listener);
107
108       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
109       interceptor.setCache(cache);
110       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
111       nodeInterceptor.setCache(cache);
112       MockInterceptor dummy = new MockInterceptor();
113       dummy.setCache(cache);
114
115       interceptor.setNext(nodeInterceptor);
116       nodeInterceptor.setNext(dummy);
117
118       cache.setInterceptorChain(interceptor);
119
120 // first set up a node with a pojo
121
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
122       mgr.begin();
123       Transaction tx = mgr.getTransaction();
124
125       // inject InvocationContext
126
cache.getInvocationContext().setTransaction(tx);
127       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
128
129
130       assertEquals(null, dummy.getCalled());
131       TransactionTable table = cache.getTransactionTable();
132
133       GlobalTransaction gtx = table.get(tx);
134
135       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
136
137       //assert we can see this with a key value get in the transaction
138
assertEquals(0, cache.getKeys("/").size());
139       mgr.commit();
140
141
142       assertTrue(entry.getLocks().isEmpty());
143       assertEquals(0, entry.getModifications().size());
144       assertTrue(!cache.exists("/one/two"));
145       assertEquals(null, dummy.getCalled());
146
147
148       cache.stop();
149    }
150
151    public void testTransactionGetKeysIteratorMethod() throws Exception JavaDoc
152    {
153
154       TestListener listener = new TestListener();
155       final CacheImpl cache = createCacheWithListener(listener);
156
157       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
158       interceptor.setCache(cache);
159       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
160       nodeInterceptor.setCache(cache);
161       MockInterceptor dummy = new MockInterceptor();
162       dummy.setCache(cache);
163
164       interceptor.setNext(nodeInterceptor);
165       nodeInterceptor.setNext(dummy);
166
167       cache.setInterceptorChain(interceptor);
168
169 // first set up a node with a pojo
170
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
171       mgr.begin();
172       Transaction tx = mgr.getTransaction();
173
174       // inject InvocationContext
175
cache.getInvocationContext().setTransaction(tx);
176       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
177
178
179       SamplePojo pojo = new SamplePojo(21, "test");
180
181       cache.put("/one/two", "key1", pojo);
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       //assert we can see this with a key value get in the transaction
191
assertEquals(1, cache.getKeys("/one/two").size());
192
193       for (Iterator JavaDoc it = cache.getKeys("/one/two").iterator(); it.hasNext();)
194       {
195          it.next();
196          it.remove();
197       }
198       assertEquals(0, cache.getKeys("/one/two").size());
199       mgr.commit();
200
201
202       assertTrue(entry.getLocks().isEmpty());
203       assertEquals(1, entry.getModifications().size());
204       assertTrue(!cache.exists("/one/two"));
205       assertEquals(null, dummy.getCalled());
206
207
208       cache.stop();
209    }
210
211
212 }
213
Popular Tags