KickJava   Java API By Example, From Geeks To Geeks.

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


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

27    public NodeInterceptorGetChildrenNamesTest(String JavaDoc name)
28    {
29       super(name);
30    }
31
32
33    public void testTransactionGetNamesMethod() 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
61       SamplePojo pojo = new SamplePojo(21, "test");
62
63       cache.put("/one/two", "key1", pojo);
64
65       assertEquals(null, dummy.getCalled());
66       TransactionTable table = cache.getTransactionTable();
67
68       GlobalTransaction gtx = table.get(tx);
69
70       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
71
72       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
73
74       //assert we can see this with a key value get in the transaction
75
assertEquals(1, cache.getChildrenNames("/one").size());
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       assertTrue(entry.getLocks().isEmpty());
83       assertEquals(1, entry.getModifications().size());
84       assertTrue(!cache.exists("/one/two"));
85       assertEquals(null, dummy.getCalled());
86
87       //assert that we cannot see the change if we have not put it into the cache
88
// we need to do this as we do not have the tx interceptor in this stack
89
mgr.begin();
90
91       Transaction tx2 = mgr.getTransaction();
92
93       // inject InvocationContext
94
cache.getInvocationContext().setTransaction(tx2);
95       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx2));
96
97       assertEquals(0, cache.getChildrenNames("/").size());
98       mgr.commit();
99       cache.stop();
100    }
101
102
103    public void testTransactionGetNoNamesMethod() throws Exception JavaDoc
104    {
105
106       TestListener listener = new TestListener();
107       final CacheImpl cache = createCacheWithListener(listener);
108
109       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
110       interceptor.setCache(cache);
111       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
112       nodeInterceptor.setCache(cache);
113       MockInterceptor dummy = new MockInterceptor();
114       dummy.setCache(cache);
115
116       interceptor.setNext(nodeInterceptor);
117       nodeInterceptor.setNext(dummy);
118
119       cache.setInterceptorChain(interceptor);
120
121 // first set up a node with a pojo
122
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
123       mgr.begin();
124       Transaction tx = mgr.getTransaction();
125
126       // inject InvocationContext
127
cache.getInvocationContext().setTransaction(tx);
128       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
129
130
131       assertEquals(null, dummy.getCalled());
132       TransactionTable table = cache.getTransactionTable();
133
134       GlobalTransaction gtx = table.get(tx);
135
136       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
137
138       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
139
140       //assert we can see this with a key value get in the transaction
141
assertEquals(0, cache.getChildrenNames("/").size());
142       mgr.commit();
143
144
145       assertTrue(entry.getLocks().isEmpty());
146       assertEquals(0, entry.getModifications().size());
147       assertTrue(!cache.exists("/one/two"));
148       assertEquals(null, dummy.getCalled());
149
150
151       cache.stop();
152    }
153
154    public void testTransactionGetNamesIteratorMethod() throws Exception JavaDoc
155    {
156
157       TestListener listener = new TestListener();
158       final CacheImpl cache = createCacheWithListener(listener);
159
160       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
161       interceptor.setCache(cache);
162       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
163       nodeInterceptor.setCache(cache);
164       MockInterceptor dummy = new MockInterceptor();
165       dummy.setCache(cache);
166
167       interceptor.setNext(nodeInterceptor);
168       nodeInterceptor.setNext(dummy);
169
170       cache.setInterceptorChain(interceptor);
171
172 // first set up a node with a pojo
173
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
174       mgr.begin();
175       Transaction tx = mgr.getTransaction();
176
177       // inject InvocationContext
178
cache.getInvocationContext().setTransaction(tx);
179       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
180
181       SamplePojo pojo = new SamplePojo(21, "test");
182
183       cache.put("/one/two", "key1", pojo);
184
185       assertEquals(null, dummy.getCalled());
186       TransactionTable table = cache.getTransactionTable();
187
188       GlobalTransaction gtx = table.get(tx);
189
190       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
191
192       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
193
194       //assert we can see this
195
assertEquals(1, cache.getChildrenNames("/one").size());
196
197       try
198       {
199          for (Iterator JavaDoc it = cache.getChildrenNames("/one").iterator(); it.hasNext();)
200          {
201             Object JavaDoc temp = it.next();
202             it.remove();
203          }
204          fail("Should not be allowed to modify elements in the set returned by getChildrenNames()");
205       }
206       catch (UnsupportedOperationException JavaDoc uoe)
207       {
208          // the returned set should be unmodifiable and a remove on the iterator should fail.
209
}
210
211       //assert the removal has had no effect
212
assertEquals(1, cache.getChildrenNames("/one").size());
213       assertNotNull(workspace.getNode(Fqn.fromString("/one")).getChild(new Fqn("two")));
214
215       mgr.commit();
216
217
218       assertTrue(entry.getLocks().isEmpty());
219       assertEquals(1, entry.getModifications().size());
220       assertTrue(!cache.exists("/one/two"));
221       assertEquals(null, dummy.getCalled());
222
223
224       cache.stop();
225    }
226
227
228 }
229
Popular Tags