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