1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| package org.jboss.cache.notifications; |
10 |
| |
11 |
| import junit.framework.TestCase; |
12 |
| import org.jboss.cache.AbstractCacheListener; |
13 |
| import org.jboss.cache.Cache; |
14 |
| import org.jboss.cache.DefaultCacheFactory; |
15 |
| import org.jboss.cache.Fqn; |
16 |
| import org.jboss.cache.Node; |
17 |
| import org.jboss.cache.config.Configuration; |
18 |
| import org.jboss.cache.lock.IsolationLevel; |
19 |
| |
20 |
| import javax.transaction.Transaction; |
21 |
| import javax.transaction.TransactionManager; |
22 |
| import java.util.ArrayList; |
23 |
| import java.util.Collections; |
24 |
| import java.util.HashMap; |
25 |
| import java.util.List; |
26 |
| import java.util.Map; |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public class CacheListenerTest extends TestCase |
35 |
| { |
36 |
| |
37 |
| protected boolean optLocking = false; |
38 |
| |
39 |
12
| public CacheListenerTest(String s)
|
40 |
| { |
41 |
12
| super(s);
|
42 |
| } |
43 |
| |
44 |
| private Cache cache; |
45 |
| private TransactionManager tm; |
46 |
| private EventLog eventLog = new EventLog(); |
47 |
| private Fqn fqn = Fqn.fromString("/test"); |
48 |
| |
49 |
12
| protected void setUp() throws Exception
|
50 |
| { |
51 |
12
| super.setUp();
|
52 |
12
| Configuration c = new Configuration();
|
53 |
12
| c.setCacheMode(Configuration.CacheMode.LOCAL);
|
54 |
12
| c.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
|
55 |
6
| if (optLocking) c.setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
|
56 |
12
| c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
57 |
12
| cache = DefaultCacheFactory.getInstance().createCache(c);
|
58 |
12
| tm = cache.getConfiguration().getRuntimeConfig().getTransactionManager();
|
59 |
12
| eventLog.events.clear();
|
60 |
12
| cache.addCacheListener(eventLog);
|
61 |
| } |
62 |
| |
63 |
12
| protected void tearDown() throws Exception
|
64 |
| { |
65 |
12
| super.tearDown();
|
66 |
12
| Transaction t = tm.getTransaction();
|
67 |
0
| if (t != null) t.rollback();
|
68 |
12
| cache.stop();
|
69 |
12
| cache.destroy();
|
70 |
| } |
71 |
| |
72 |
| |
73 |
| |
74 |
2
| public void testCreation() throws Exception
|
75 |
| { |
76 |
2
| assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
|
77 |
2
| cache.put(fqn, "key", "value");
|
78 |
2
| Map data = new HashMap();
|
79 |
2
| data.put("key", "value");
|
80 |
| |
81 |
| |
82 |
2
| List<CacheListenerEvent> expected = new ArrayList<CacheListenerEvent>();
|
83 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, true, true, null));
|
84 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, false, true, null));
|
85 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, Collections.emptyMap()));
|
86 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, data));
|
87 |
| |
88 |
2
| assertEquals(expected, eventLog.events);
|
89 |
2
| assertEquals("value", cache.get(fqn, "key"));
|
90 |
| } |
91 |
| |
92 |
2
| public void testOnlyModification() throws Exception
|
93 |
| { |
94 |
2
| assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
|
95 |
2
| cache.put(fqn, "key", "value");
|
96 |
2
| Map oldData = new HashMap();
|
97 |
2
| oldData.put("key", "value");
|
98 |
| |
99 |
| |
100 |
2
| eventLog.events.clear();
|
101 |
2
| assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
|
102 |
| |
103 |
| |
104 |
2
| cache.put(fqn, "key", "value2");
|
105 |
2
| Map newData = new HashMap();
|
106 |
2
| newData.put("key", "value2");
|
107 |
| |
108 |
| |
109 |
2
| List<CacheListenerEvent> expected = new ArrayList<CacheListenerEvent>();
|
110 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
|
111 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, newData));
|
112 |
| |
113 |
2
| assertEquals(expected.size(), eventLog.events.size());
|
114 |
2
| assertEquals(expected, eventLog.events);
|
115 |
| } |
116 |
| |
117 |
| |
118 |
2
| public void testOnlyRemoval() throws Exception
|
119 |
| { |
120 |
2
| assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
|
121 |
2
| cache.put(fqn, "key", "value");
|
122 |
2
| Map oldData = new HashMap();
|
123 |
2
| oldData.put("key", "value");
|
124 |
| |
125 |
2
| assertEquals("value", cache.get(fqn, "key"));
|
126 |
| |
127 |
| |
128 |
2
| eventLog.events.clear();
|
129 |
2
| assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
|
130 |
| |
131 |
| |
132 |
2
| cache.removeNode(fqn);
|
133 |
| |
134 |
| |
135 |
2
| List<CacheListenerEvent> expected = new ArrayList<CacheListenerEvent>();
|
136 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, true, true, oldData));
|
137 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, false, true, null));
|
138 |
| |
139 |
2
| assertEquals(expected, eventLog.events);
|
140 |
| |
141 |
| |
142 |
2
| assertNull("Should be null", cache.getRoot().getChild(fqn));
|
143 |
| } |
144 |
| |
145 |
| |
146 |
2
| public void testRemoveData() throws Exception
|
147 |
| { |
148 |
2
| assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
|
149 |
2
| cache.put(fqn, "key", "value");
|
150 |
2
| cache.put(fqn, "key2", "value2");
|
151 |
2
| Map oldData = new HashMap();
|
152 |
2
| oldData.put("key", "value");
|
153 |
2
| oldData.put("key2", "value2");
|
154 |
| |
155 |
| |
156 |
2
| eventLog.events.clear();
|
157 |
2
| assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
|
158 |
| |
159 |
| |
160 |
2
| cache.remove(fqn, "key2");
|
161 |
2
| Map removedData = new HashMap();
|
162 |
2
| removedData.put("key2", "value2");
|
163 |
| |
164 |
| |
165 |
2
| List<CacheListenerEvent> expected = new ArrayList<CacheListenerEvent>();
|
166 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
|
167 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, removedData));
|
168 |
| |
169 |
2
| assertEquals(expected, eventLog.events);
|
170 |
| } |
171 |
| |
172 |
2
| public void testPutMap() throws Exception
|
173 |
| { |
174 |
2
| assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
|
175 |
2
| Map oldData = new HashMap();
|
176 |
2
| oldData.put("key", "value");
|
177 |
2
| oldData.put("key2", "value2");
|
178 |
| |
179 |
| |
180 |
2
| eventLog.events.clear();
|
181 |
2
| assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
|
182 |
| |
183 |
| |
184 |
2
| cache.put(fqn, oldData);
|
185 |
| |
186 |
| |
187 |
2
| List<CacheListenerEvent> expected = new ArrayList<CacheListenerEvent>();
|
188 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, true, true, null));
|
189 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, false, true, null));
|
190 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, Collections.emptyMap()));
|
191 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, oldData));
|
192 |
| |
193 |
2
| assertEquals(expected, eventLog.events);
|
194 |
| } |
195 |
| |
196 |
2
| public void testMove()
|
197 |
| { |
198 |
2
| assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
|
199 |
2
| Fqn newParent = Fqn.fromString("/a");
|
200 |
2
| cache.put(fqn, "key", "value");
|
201 |
2
| cache.put(newParent, "key", "value");
|
202 |
| |
203 |
2
| Node n1 = cache.getRoot().getChild(fqn);
|
204 |
2
| Node n2 = cache.getRoot().getChild(newParent);
|
205 |
| |
206 |
2
| eventLog.events.clear();
|
207 |
2
| assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
|
208 |
| |
209 |
2
| cache.move(n1.getFqn(), n2.getFqn());
|
210 |
| |
211 |
2
| Fqn newFqn = new Fqn(newParent, fqn.getLastElement());
|
212 |
2
| List<CacheListenerEvent> expected = new ArrayList<CacheListenerEvent>();
|
213 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MOVED, fqn, newFqn, true, true));
|
214 |
2
| expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MOVED, fqn, newFqn, false, true));
|
215 |
| |
216 |
2
| assertEquals(expected, eventLog.events);
|
217 |
| } |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
| |
272 |
| |
273 |
| |
274 |
| |
275 |
| |
276 |
| |
277 |
| |
278 |
| |
279 |
| |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
| |
286 |
| |
287 |
| |
288 |
| |
289 |
| |
290 |
| |
291 |
| |
292 |
| |
293 |
| |
294 |
| |
295 |
| |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
| |
303 |
| |
304 |
| |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
| |
311 |
| |
312 |
| |
313 |
| |
314 |
| |
315 |
| |
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
| |
331 |
| |
332 |
| |
333 |
| |
334 |
| |
335 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
| |
340 |
| |
341 |
| |
342 |
| |
343 |
| |
344 |
| |
345 |
| |
346 |
| |
347 |
| |
348 |
| |
349 |
| public static class EventLog extends AbstractCacheListener |
350 |
| { |
351 |
| public final List<CacheListenerEvent> events = new ArrayList<CacheListenerEvent>(); |
352 |
| |
353 |
28
| public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
|
354 |
| { |
355 |
28
| CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, pre, isLocal, null);
|
356 |
28
| events.add(e);
|
357 |
| } |
358 |
| |
359 |
4
| public void nodeRemoved(Fqn fqn, boolean pre, boolean isLocal, Map data)
|
360 |
| { |
361 |
4
| CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, pre, isLocal, data);
|
362 |
4
| events.add(e);
|
363 |
| } |
364 |
| |
365 |
40
| public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, ModificationType modType, Map data)
|
366 |
| { |
367 |
40
| CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, pre, isLocal, data);
|
368 |
40
| events.add(e);
|
369 |
| } |
370 |
| |
371 |
12
| public void nodeVisited(Fqn fqn, boolean pre)
|
372 |
| { |
373 |
12
| CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_VISITED, fqn, pre, false, null);
|
374 |
12
| events.add(e);
|
375 |
| } |
376 |
| |
377 |
4
| public void nodeMoved(Fqn from, Fqn to, boolean pre, boolean isLocal)
|
378 |
| { |
379 |
4
| CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MOVED, from, to, pre, isLocal);
|
380 |
4
| events.add(e);
|
381 |
| } |
382 |
| |
383 |
| |
384 |
0
| public String toString()
|
385 |
| { |
386 |
0
| return "EventLog{" +
|
387 |
| "events=" + events + |
388 |
| '}'; |
389 |
| } |
390 |
| } |
391 |
| } |