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; |
15 |
| import java.util.HashMap; |
16 |
| import java.util.Map; |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| public class NodeInterceptorPutEraseTest extends AbstractOptimisticTestCase |
22 |
| { |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
3
| public NodeInterceptorPutEraseTest(String name)
|
29 |
| { |
30 |
3
| super(name);
|
31 |
| } |
32 |
| |
33 |
1
| public void testTransactionPutKeyMethod() throws Exception
|
34 |
| { |
35 |
| |
36 |
1
| TestListener listener = new TestListener();
|
37 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
38 |
| |
39 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
40 |
1
| interceptor.setCache(cache);
|
41 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
42 |
1
| nodeInterceptor.setCache(cache);
|
43 |
1
| MockInterceptor dummy = new MockInterceptor();
|
44 |
1
| dummy.setCache(cache);
|
45 |
| |
46 |
1
| interceptor.setNext(nodeInterceptor);
|
47 |
1
| nodeInterceptor.setNext(dummy);
|
48 |
| |
49 |
1
| cache.setInterceptorChain(interceptor);
|
50 |
| |
51 |
| |
52 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
53 |
1
| mgr.begin();
|
54 |
1
| Transaction tx = mgr.getTransaction();
|
55 |
| |
56 |
| |
57 |
1
| cache.getInvocationContext().setTransaction(tx);
|
58 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
59 |
| |
60 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
61 |
1
| Map temp = new HashMap();
|
62 |
1
| temp.put("key1", pojo);
|
63 |
1
| cache.put("/one/two", temp);
|
64 |
| |
65 |
1
| assertEquals(null, dummy.getCalled());
|
66 |
1
| TransactionTable table = cache.getTransactionTable();
|
67 |
| |
68 |
1
| GlobalTransaction gtx = table.get(tx);
|
69 |
| |
70 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
71 |
| |
72 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
73 |
| |
74 |
1
| mgr.commit();
|
75 |
| |
76 |
| |
77 |
1
| assertEquals(3, workspace.getNodes().size());
|
78 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
79 |
1
| assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
80 |
1
| assertTrue(entry.getLocks().isEmpty());
|
81 |
1
| assertEquals(1, entry.getModifications().size());
|
82 |
1
| assertTrue(!cache.exists("/one/two"));
|
83 |
1
| assertEquals(null, dummy.getCalled());
|
84 |
1
| cache.stop();
|
85 |
| } |
86 |
| |
87 |
1
| public void testTransactionKeyValOverwriteMethod() throws Exception
|
88 |
| { |
89 |
| |
90 |
1
| TestListener listener = new TestListener();
|
91 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
92 |
| |
93 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
94 |
1
| interceptor.setCache(cache);
|
95 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
96 |
1
| nodeInterceptor.setCache(cache);
|
97 |
1
| MockInterceptor dummy = new MockInterceptor();
|
98 |
1
| dummy.setCache(cache);
|
99 |
| |
100 |
1
| interceptor.setNext(nodeInterceptor);
|
101 |
1
| nodeInterceptor.setNext(dummy);
|
102 |
| |
103 |
1
| cache.setInterceptorChain(interceptor);
|
104 |
| |
105 |
| |
106 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
107 |
1
| mgr.begin();
|
108 |
1
| Transaction tx = mgr.getTransaction();
|
109 |
| |
110 |
| |
111 |
1
| cache.getInvocationContext().setTransaction(tx);
|
112 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
113 |
| |
114 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
115 |
| |
116 |
1
| cache.put("/one/two", "key1", pojo);
|
117 |
| |
118 |
| |
119 |
1
| SamplePojo pojo2 = new SamplePojo(21, "test");
|
120 |
| |
121 |
1
| cache.put("/one/two", "key1", pojo2);
|
122 |
| |
123 |
1
| assertEquals(null, dummy.getCalled());
|
124 |
1
| TransactionTable table = cache.getTransactionTable();
|
125 |
| |
126 |
1
| GlobalTransaction gtx = table.get(tx);
|
127 |
| |
128 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
129 |
| |
130 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
131 |
| |
132 |
1
| mgr.commit();
|
133 |
| |
134 |
1
| assertEquals(3, workspace.getNodes().size());
|
135 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
136 |
1
| assertEquals(pojo2, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
137 |
1
| assertTrue(entry.getLocks().isEmpty());
|
138 |
1
| assertEquals(2, entry.getModifications().size());
|
139 |
1
| assertTrue(!cache.exists("/one/two"));
|
140 |
1
| assertEquals(null, dummy.getCalled());
|
141 |
1
| cache.stop();
|
142 |
| } |
143 |
| |
144 |
1
| public void testTransactionKeyValOverwriteNullMethod() throws Exception
|
145 |
| { |
146 |
| |
147 |
1
| TestListener listener = new TestListener();
|
148 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
149 |
| |
150 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
151 |
1
| interceptor.setCache(cache);
|
152 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
153 |
1
| nodeInterceptor.setCache(cache);
|
154 |
1
| MockInterceptor dummy = new MockInterceptor();
|
155 |
1
| dummy.setCache(cache);
|
156 |
| |
157 |
1
| interceptor.setNext(nodeInterceptor);
|
158 |
1
| nodeInterceptor.setNext(dummy);
|
159 |
| |
160 |
1
| cache.setInterceptorChain(interceptor);
|
161 |
| |
162 |
| |
163 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
164 |
1
| mgr.begin();
|
165 |
1
| Transaction tx = mgr.getTransaction();
|
166 |
| |
167 |
| |
168 |
1
| cache.getInvocationContext().setTransaction(tx);
|
169 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
170 |
| |
171 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
172 |
| |
173 |
1
| cache.put("/one/two", "key1", pojo);
|
174 |
| |
175 |
| |
176 |
1
| cache.put("/one/two", "key1", null);
|
177 |
| |
178 |
1
| assertEquals(null, dummy.getCalled());
|
179 |
1
| TransactionTable table = cache.getTransactionTable();
|
180 |
| |
181 |
1
| GlobalTransaction gtx = table.get(tx);
|
182 |
| |
183 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
184 |
| |
185 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
186 |
| |
187 |
1
| mgr.commit();
|
188 |
| |
189 |
1
| assertEquals(3, workspace.getNodes().size());
|
190 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
191 |
1
| assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
192 |
1
| assertTrue(entry.getLocks().isEmpty());
|
193 |
1
| assertEquals(2, entry.getModifications().size());
|
194 |
1
| assertTrue(!cache.exists("/one/two"));
|
195 |
1
| assertEquals(null, dummy.getCalled());
|
196 |
1
| cache.stop();
|
197 |
| } |
198 |
| |
199 |
| |
200 |
| } |