KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > notifications > CacheListenerTest


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
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.Fqn;
15 import org.jboss.cache.Node;
16 import org.jboss.cache.config.Configuration;
17 import org.jboss.cache.factories.DefaultCacheFactory;
18 import org.jboss.cache.lock.IsolationLevel;
19
20 import javax.transaction.Transaction JavaDoc;
21 import javax.transaction.TransactionManager JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * Note that this is significantly different from the old <b>TreeCacheListenerTest</b> of the JBoss Cache 1.x series, and
30  * exercises the new {@link org.jboss.cache.CacheListener} interface.
31  *
32  * @since 2.0.0
33  */

34 public class CacheListenerTest extends TestCase
35 {
36
37    protected boolean optLocking = false;
38
39    public CacheListenerTest(String JavaDoc s)
40    {
41       super(s);
42    }
43
44    private Cache cache;
45    private TransactionManager JavaDoc tm;
46    private EventLog eventLog = new EventLog();
47    private Fqn fqn = Fqn.fromString("/test");
48
49    protected void setUp() throws Exception JavaDoc
50    {
51       super.setUp();
52       Configuration c = new Configuration();
53       c.setCacheMode(Configuration.CacheMode.LOCAL);
54       c.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
55       if (optLocking) c.setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
56       c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
57       cache = DefaultCacheFactory.getInstance().createCache(c);
58       tm = cache.getConfiguration().getRuntimeConfig().getTransactionManager();
59       eventLog.events.clear();
60       cache.addCacheListener(eventLog);
61    }
62
63    protected void tearDown() throws Exception JavaDoc
64    {
65       super.tearDown();
66       Transaction JavaDoc t = tm.getTransaction();
67       if (t != null) t.rollback();
68       cache.stop();
69       cache.destroy();
70    }
71
72    // simple tests first
73

74    public void testCreation() throws Exception JavaDoc
75    {
76       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
77       cache.put(fqn, "key", "value");
78       Map JavaDoc data = new HashMap JavaDoc();
79       data.put("key", "value");
80
81       //expected
82
List JavaDoc<CacheListenerEvent> expected = new ArrayList JavaDoc<CacheListenerEvent>();
83       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, true, true, null));
84       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, false, true, null));
85       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, Collections.emptyMap()));
86       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, data));
87
88       assertEquals(expected, eventLog.events);
89       assertEquals("value", cache.get(fqn, "key"));
90    }
91
92    public void testOnlyModification() throws Exception JavaDoc
93    {
94       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
95       cache.put(fqn, "key", "value");
96       Map JavaDoc oldData = new HashMap JavaDoc();
97       oldData.put("key", "value");
98
99       // clear CacheListenerEvent log
100
eventLog.events.clear();
101       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
102
103       // modify existing node
104
cache.put(fqn, "key", "value2");
105       Map JavaDoc newData = new HashMap JavaDoc();
106       newData.put("key", "value2");
107
108       //expected
109
List JavaDoc<CacheListenerEvent> expected = new ArrayList JavaDoc<CacheListenerEvent>();
110       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
111       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, newData));
112
113       assertEquals(expected.size(), eventLog.events.size());
114       assertEquals(expected, eventLog.events);
115    }
116
117
118    public void testOnlyRemoval() throws Exception JavaDoc
119    {
120       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
121       cache.put(fqn, "key", "value");
122       Map JavaDoc oldData = new HashMap JavaDoc();
123       oldData.put("key", "value");
124
125       assertEquals("value", cache.get(fqn, "key"));
126
127       // clear CacheListenerEvent log
128
eventLog.events.clear();
129       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
130
131       // modify existing node
132
cache.removeNode(fqn);
133
134       //expected
135
List JavaDoc<CacheListenerEvent> expected = new ArrayList JavaDoc<CacheListenerEvent>();
136       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, true, true, oldData));
137       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, false, true, null));
138
139       assertEquals(expected, eventLog.events);
140
141       // test that the node has in fact been removed.
142
assertNull("Should be null", cache.getRoot().getChild(fqn));
143    }
144
145
146    public void testRemoveData() throws Exception JavaDoc
147    {
148       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
149       cache.put(fqn, "key", "value");
150       cache.put(fqn, "key2", "value2");
151       Map JavaDoc oldData = new HashMap JavaDoc();
152       oldData.put("key", "value");
153       oldData.put("key2", "value2");
154
155       // clear CacheListenerEvent log
156
eventLog.events.clear();
157       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
158
159       // modify existing node
160
cache.remove(fqn, "key2");
161       Map JavaDoc removedData = new HashMap JavaDoc();
162       removedData.put("key2", "value2");
163
164       //expected
165
List JavaDoc<CacheListenerEvent> expected = new ArrayList JavaDoc<CacheListenerEvent>();
166       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
167       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, removedData));
168
169       assertEquals(expected, eventLog.events);
170    }
171
172    public void testPutMap() throws Exception JavaDoc
173    {
174       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
175       Map JavaDoc oldData = new HashMap JavaDoc();
176       oldData.put("key", "value");
177       oldData.put("key2", "value2");
178
179       // clear CacheListenerEvent log
180
eventLog.events.clear();
181       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
182
183       // modify existing node
184
cache.put(fqn, oldData);
185
186       //expected
187
List JavaDoc<CacheListenerEvent> expected = new ArrayList JavaDoc<CacheListenerEvent>();
188       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, true, true, null));
189       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, false, true, null));
190       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, Collections.emptyMap()));
191       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, oldData));
192
193       assertEquals(expected, eventLog.events);
194    }
195
196    public void testMove()
197    {
198       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
199       Fqn newParent = Fqn.fromString("/a");
200       cache.put(fqn, "key", "value");
201       cache.put(newParent, "key", "value");
202
203       Node n1 = cache.getRoot().getChild(fqn);
204       Node n2 = cache.getRoot().getChild(newParent);
205
206       eventLog.events.clear(); // clear events
207
assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
208
209       cache.move(n1.getFqn(), n2.getFqn());
210       //expected
211
Fqn newFqn = new Fqn(newParent, fqn.getLastElement());
212       List JavaDoc<CacheListenerEvent> expected = new ArrayList JavaDoc<CacheListenerEvent>();
213       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MOVED, fqn, newFqn, true, true));
214       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MOVED, fqn, newFqn, false, true));
215
216       assertEquals(expected, eventLog.events);
217    }
218
219    // -- now the transactional ones
220

221    /*
222
223    // TODO: Reinstate these once we have a proper plan for dealing with transactions and notifications.
224
225    public void testTxCreationCommit() throws Exception
226    {
227       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
228       tm.begin();
229       cache.put(fqn, "key", "value");
230       assertEquals("Events log should be empty until commit time", 0, eventLog.events.size());
231       tm.commit();
232
233       Map data = new HashMap();
234       data.put("key", "value");
235
236       //expected
237       List<CacheListenerEvent> expected = new ArrayList<CacheListenerEvent>();
238       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, true, true, null));
239       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, false, true, null));
240       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, Collections.emptyMap()));
241       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, data));
242
243       assertEquals(expected, eventLog.events);
244       assertEquals("value", cache.get(fqn, "key"));
245    }
246
247    public void testTxCreationRollback() throws Exception
248    {
249       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
250       tm.begin();
251       cache.put(fqn, "key", "value");
252       assertEquals("Events log should be empty until commit time", 0, eventLog.events.size());
253       tm.rollback();
254       assertEquals("Events log should be empty until commit time", 0, eventLog.events.size());
255    }
256
257
258    public void testTxOnlyModification() throws Exception
259    {
260       fail("implement me");
261       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
262       cache.put(fqn, "key", "value");
263       Map oldData = new HashMap();
264       oldData.put("key", "value");
265
266       // clear CacheListenerEvent log
267       eventLog.events.clear();
268       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
269
270       // modify existing node
271       cache.put(fqn, "key", "value2");
272       Map newData = new HashMap();
273       newData.put("key", "value2");
274
275       //expected
276       List<CacheListenerEvent> expected = new ArrayList<CacheListenerEvent>();
277       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
278       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, newData));
279
280       assertEquals(expected, eventLog.events);
281    }
282
283
284    public void testTxOnlyRemoval() throws Exception
285    {
286       fail("implement me");
287       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
288       cache.put(fqn, "key", "value");
289       Map oldData = new HashMap();
290       oldData.put("key", "value");
291
292       assertEquals("value", cache.get(fqn, "key"));
293
294       // clear CacheListenerEvent log
295       eventLog.events.clear();
296       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
297
298       // modify existing node
299       cache.removeNode(fqn);
300
301       //expected
302       List<CacheListenerEvent> expected = new ArrayList<CacheListenerEvent>();
303       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, true, true, oldData));
304       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, false, true, null));
305
306       assertEquals(expected, eventLog.events);
307
308       // test that the node has in fact been removed.
309       assertNull("Should be null", cache.getChild(fqn));
310    }
311
312
313    public void testTxRemoveData() throws Exception
314    {
315       fail("implement me");
316       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
317       cache.put(fqn, "key", "value");
318       cache.put(fqn, "key2", "value2");
319       Map oldData = new HashMap();
320       oldData.put("key", "value");
321       oldData.put("key2", "value2");
322
323       // clear CacheListenerEvent log
324       eventLog.events.clear();
325       assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
326
327       // modify existing node
328       cache.remove(fqn, "key2");
329       Map newData = new HashMap();
330       newData.put("key", "value");
331
332       //expected
333       List<CacheListenerEvent> expected = new ArrayList<CacheListenerEvent>();
334       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
335       expected.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, newData));
336
337       assertEquals(expected, eventLog.events);
338    }
339
340    public void testTxMove()
341    {
342       fail("implement me");
343    }
344    */

345
346    // ============= supporting classes and enums =======================
347

348
349    public static class EventLog extends AbstractCacheListener
350    {
351       public final List JavaDoc<CacheListenerEvent> events = new ArrayList JavaDoc<CacheListenerEvent>();
352
353       public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
354       {
355          CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, pre, isLocal, null);
356          events.add(e);
357       }
358
359       public void nodeRemoved(Fqn fqn, boolean pre, boolean isLocal, Map JavaDoc data)
360       {
361          CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, pre, isLocal, data);
362          events.add(e);
363       }
364
365       public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, ModificationType modType, Map JavaDoc data)
366       {
367          CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, pre, isLocal, data);
368          events.add(e);
369       }
370
371       public void nodeVisited(Fqn fqn, boolean pre)
372       {
373          CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_VISITED, fqn, pre, false, null);
374          events.add(e);
375       }
376
377       public void nodeMoved(Fqn from, Fqn to, boolean pre, boolean isLocal)
378       {
379          CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MOVED, from, to, pre, isLocal);
380          events.add(e);
381       }
382
383
384       public String JavaDoc toString()
385       {
386          return "EventLog{" +
387                  "events=" + events +
388                  '}';
389       }
390    }
391 }
392
Popular Tags