KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * as indicated by the @author tags. See the copyright.txt file in the
5  * distribution for a full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22
23 package org.jboss.cache.notifications;
24
25 import junit.framework.TestCase;
26 import org.jboss.cache.AbstractCacheListener;
27 import org.jboss.cache.Cache;
28 import org.jboss.cache.Fqn;
29 import org.jboss.cache.Node;
30 import org.jboss.cache.config.Configuration;
31 import org.jboss.cache.factories.DefaultCacheFactory;
32 import org.jboss.cache.lock.IsolationLevel;
33
34 import javax.transaction.TransactionManager JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * Remote conterpart of CacheListenerTest. Main difference is event is originating as local.
43  *
44  * @author Ben Wang
45  * @since 2.0.0
46  */

47 public class RemoteCacheListenerTest extends TestCase
48 {
49
50    protected boolean optLocking = false;
51
52    private Cache cache1, cache2;
53    private EventLog eventLog1 = new EventLog(), eventLog2 = new EventLog();
54    private Fqn fqn = Fqn.fromString("/test");
55
56    protected void setUp() throws Exception JavaDoc
57    {
58       super.setUp();
59       Configuration c = new Configuration();
60       c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
61       c.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
62       if (optLocking) c.setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
63       c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
64       c.setFetchInMemoryState(false);
65
66       // we need this because notifications emitted by the notification interceptor are done during the commit call.
67
// If we want to check notifications on remote nodes we need to make sure the commit completes before we test anything.
68
c.setSyncCommitPhase(true);
69
70       // more time to help with debugging
71
c.setSyncReplTimeout(60000);
72
73       cache1 = DefaultCacheFactory.getInstance().createCache(c);
74       cache2 = DefaultCacheFactory.getInstance().createCache(c);
75
76       eventLog1.events.clear();
77       eventLog2.events.clear();
78
79       cache1.addCacheListener(eventLog1);
80       cache2.addCacheListener(eventLog2);
81    }
82
83    protected void tearDown() throws Exception JavaDoc
84    {
85       super.tearDown();
86       TransactionManager JavaDoc tm;
87       if ((tm = cache1.getConfiguration().getRuntimeConfig().getTransactionManager()) != null)
88       {
89          if (tm.getTransaction() != null)
90          {
91             try
92             {
93                tm.rollback();
94             }
95             catch (Exception JavaDoc e)
96             {
97                // do nothing
98
}
99          }
100       }
101       if ((tm = cache2.getConfiguration().getRuntimeConfig().getTransactionManager()) != null)
102       {
103          if (tm.getTransaction() != null)
104          {
105             try
106             {
107                tm.rollback();
108             }
109             catch (Exception JavaDoc e)
110             {
111                // do nothing
112
}
113          }
114       }
115
116       cache1.stop();
117       cache1.destroy();
118       cache2.stop();
119       cache2.destroy();
120    }
121
122    // simple tests first
123

124    public void testCreation() throws Exception JavaDoc
125    {
126       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
127       cache1.put(fqn, "key", "value");
128       Map JavaDoc data = new HashMap JavaDoc();
129       data.put("key", "value");
130
131       //expectedRemote
132
List JavaDoc<CacheListenerEvent> expectedLocal = new ArrayList JavaDoc<CacheListenerEvent>();
133       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, true, true, null));
134       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, false, true, null));
135       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, Collections.emptyMap()));
136       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, data));
137
138       //expectedRemote
139
List JavaDoc<CacheListenerEvent> expectedRemote = new ArrayList JavaDoc<CacheListenerEvent>();
140       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, true, false, null));
141       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, false, false, null));
142       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, false, Collections.emptyMap()));
143       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, false, data));
144
145       assertEquals(expectedLocal, eventLog1.events);
146       assertEquals(expectedRemote, eventLog2.events);
147       assertEquals("value", cache1.get(fqn, "key"));
148    }
149
150    public void testOnlyModification() throws Exception JavaDoc
151    {
152       assertNull(cache1.get(fqn, "key"));
153       assertNull(cache2.get(fqn, "key"));
154
155       cache1.put(fqn, "key", "value");
156       Map JavaDoc oldData = new HashMap JavaDoc();
157       oldData.put("key", "value");
158
159       assertEquals("value", cache1.get(fqn, "key"));
160       assertEquals("value", cache2.get(fqn, "key"));
161
162       // clear event log
163
eventLog1.events.clear();
164       eventLog2.events.clear();
165       assertEquals("Event log should be empty", Collections.emptyList(), eventLog1.events);
166       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
167
168       // modify existing node
169
cache1.put(fqn, "key", "value2");
170       Map JavaDoc newData = new HashMap JavaDoc();
171       newData.put("key", "value2");
172
173       //expectedLocal
174
List JavaDoc<CacheListenerEvent> expectedLocal = new ArrayList JavaDoc<CacheListenerEvent>();
175       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
176       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, newData));
177
178       //expectedRemote
179
List JavaDoc<CacheListenerEvent> expectedRemote = new ArrayList JavaDoc<CacheListenerEvent>();
180       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, false, oldData));
181       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, false, newData));
182
183       assertEquals(expectedLocal, eventLog1.events);
184       assertEquals(expectedRemote, eventLog2.events);
185    }
186
187
188    public void testOnlyRemoval() throws Exception JavaDoc
189    {
190       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
191       cache1.put(fqn, "key", "value");
192       Map JavaDoc oldData = new HashMap JavaDoc();
193       oldData.put("key", "value");
194
195       assertEquals("value", cache1.get(fqn, "key"));
196       assertEquals("value", cache2.get(fqn, "key"));
197
198       // clear event log
199
eventLog1.events.clear();
200       eventLog2.events.clear();
201       assertEquals("Event log should be empty", Collections.emptyList(), eventLog1.events);
202       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
203
204       // modify existing node
205
cache1.removeNode(fqn);
206
207       //expectedLocal
208
List JavaDoc<CacheListenerEvent> expectedLocal = new ArrayList JavaDoc<CacheListenerEvent>();
209       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, true, true, oldData));
210       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, false, true, null));
211
212       //expectedRemote
213
List JavaDoc<CacheListenerEvent> expectedRemote = new ArrayList JavaDoc<CacheListenerEvent>();
214       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, true, false, oldData));
215       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, false, false, null));
216
217       assertEquals(expectedLocal, eventLog1.events);
218       assertEquals(expectedRemote, eventLog2.events);
219
220       // test that the node has in fact been removed.
221
assertNull("Should be null", cache1.getRoot().getChild(fqn));
222    }
223
224
225    public void testRemoveData() throws Exception JavaDoc
226    {
227       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
228       cache1.put(fqn, "key", "value");
229       cache1.put(fqn, "key2", "value2");
230       Map JavaDoc oldData = new HashMap JavaDoc();
231       oldData.put("key", "value");
232       oldData.put("key2", "value2");
233
234       // clear event log
235
eventLog1.events.clear();
236       eventLog2.events.clear();
237       assertEquals("Event log should be empty", Collections.emptyList(), eventLog1.events);
238       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
239
240       // modify existing node
241
cache1.remove(fqn, "key2");
242       Map JavaDoc removed = new HashMap JavaDoc();
243       removed.put("key2", "value2");
244
245       //expectedLocal
246
List JavaDoc<CacheListenerEvent> expectedLocal = new ArrayList JavaDoc<CacheListenerEvent>();
247       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
248       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, removed));
249
250       //expectedRemote
251
List JavaDoc<CacheListenerEvent> expectedRemote = new ArrayList JavaDoc<CacheListenerEvent>();
252       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, false, oldData));
253       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, false, removed));
254
255       assertEquals(expectedLocal, eventLog1.events);
256       assertEquals(expectedRemote, eventLog2.events);
257    }
258
259    public void testPutMap() throws Exception JavaDoc
260    {
261       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
262       Map JavaDoc oldData = new HashMap JavaDoc();
263       oldData.put("key", "value");
264       oldData.put("key2", "value2");
265
266       assertNull(cache1.getRoot().getChild(fqn));
267       assertNull(cache2.getRoot().getChild(fqn));
268
269       // clear event log
270
eventLog1.events.clear();
271       eventLog2.events.clear();
272       assertEquals("Event log should be empty", Collections.emptyList(), eventLog1.events);
273       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
274
275       cache1.put(fqn, oldData);
276
277       //expectedLocal
278
List JavaDoc<CacheListenerEvent> expectedLocal = new ArrayList JavaDoc<CacheListenerEvent>();
279       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, true, true, null));
280       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, false, true, null));
281       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, true, Collections.emptyMap()));
282       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, true, oldData));
283
284       //expectedRemote
285
List JavaDoc<CacheListenerEvent> expectedRemote = new ArrayList JavaDoc<CacheListenerEvent>();
286       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, true, false, null));
287       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, false, false, null));
288       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, true, false, Collections.emptyMap()));
289       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, false, false, oldData));
290
291       assertEquals(expectedLocal, eventLog1.events);
292       assertEquals(expectedRemote, eventLog2.events);
293    }
294
295    public void testMove()
296    {
297       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
298       Fqn newParent = Fqn.fromString("/a");
299       cache1.put(fqn, "key", "value");
300       cache1.put(newParent, "key", "value");
301
302       Node n1 = cache1.getRoot().getChild(fqn);
303       Node n2 = cache1.getRoot().getChild(newParent);
304
305       eventLog1.events.clear();
306       eventLog2.events.clear(); // clear events
307
assertEquals("Event log should be empty", Collections.emptyList(), eventLog1.events);
308       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
309
310       cache1.move(n1.getFqn(), n2.getFqn());
311       Fqn newFqn = new Fqn(newParent, fqn.getLastElement());
312
313       //expectedLocal
314
List JavaDoc<CacheListenerEvent> expectedLocal = new ArrayList JavaDoc<CacheListenerEvent>();
315       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MOVED, fqn, newFqn, true, true));
316       expectedLocal.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MOVED, fqn, newFqn, false, true));
317
318       //expectedRemote
319
List JavaDoc<CacheListenerEvent> expectedRemote = new ArrayList JavaDoc<CacheListenerEvent>();
320       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MOVED, fqn, newFqn, true, false));
321       expectedRemote.add(new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MOVED, fqn, newFqn, false, false));
322
323       assertEquals(expectedLocal, eventLog1.events);
324       assertEquals(expectedRemote, eventLog2.events);
325    }
326
327    // -- now the transactional ones
328

329    /*
330    // TODO: Reinstate these once we have a proper plan for dealing with transactions and notifications.
331
332    public void testTxCreationCommit() throws Exception
333    {
334       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
335       tm.begin();
336       cache1.put(fqn, "key", "value");
337 // assertEquals("Events log should be empty until commit time", 0, eventLog2.events.size());
338       tm.commit();
339
340       Map data = new HashMap();
341       data.put("key", "value");
342
343       //expected
344       List<CacheListenerEvent> expected = new ArrayList<CacheListenerEvent>();
345       expected.add(new CacheListenerEvent(RemoteCacheListenerTest.ListenerMethod.NODE_CREATED, fqn, true, false, null));
346       expected.add(new CacheListenerEvent(RemoteCacheListenerTest.ListenerMethod.NODE_CREATED, fqn, false, false, null));
347       expected.add(new CacheListenerEvent(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, true, false, Collections.emptyMap()));
348       expected.add(new CacheListenerEvent(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, false, false, data));
349
350       assertEquals(expected, eventLog2.events);
351       assertEquals("value", cache1.get(fqn, "key"));
352    }
353
354    public void testTxCreationRollback() throws Exception
355    {
356       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
357       tm.begin();
358       cache1.put(fqn, "key", "value");
359       assertEquals("Events log should be empty until commit time", 0, eventLog2.events.size());
360       tm.rollback();
361       assertEquals("Events log should be empty until commit time", 0, eventLog2.events.size());
362    }
363
364
365    public void testTxOnlyModification() throws Exception
366    {
367       fail("implement me");
368       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
369       cache1.put(fqn, "key", "value");
370       Map oldData = new HashMap();
371       oldData.put("key", "value");
372
373       // clear event log
374       eventLog2.events.clear();
375       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
376
377       // modify existing node
378       cache1.put(fqn, "key", "value2");
379       Map newData = new HashMap();
380       newData.put("key", "value2");
381
382       //expected
383       List<Event> expected = new ArrayList<Event>();
384       expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
385       expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, false, true, newData));
386
387       assertEquals(expected, eventLog2.events);
388    }
389
390
391    public void testTxOnlyRemoval() throws Exception
392    {
393       fail("implement me");
394       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
395       cache1.put(fqn, "key", "value");
396       Map oldData = new HashMap();
397       oldData.put("key", "value");
398
399       assertEquals("value", cache1.get(fqn, "key"));
400
401       // clear event log
402       eventLog2.events.clear();
403       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
404
405       // modify existing node
406       cache1.removeNode(fqn);
407
408       //expected
409       List<Event> expected = new ArrayList<Event>();
410       expected.add(new Event(ListenerMethod.NODE_REMOVED, fqn, true, true, oldData));
411       expected.add(new Event(ListenerMethod.NODE_REMOVED, fqn, false, true, null));
412
413       assertEquals(expected, eventLog2.events);
414
415       // test that the node has in fact been removed.
416       assertNull("Should be null", cache1.getChild(fqn));
417    }
418
419
420    public void testTxRemoveData() throws Exception
421    {
422       fail("implement me");
423       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
424       cache1.put(fqn, "key", "value");
425       cache1.put(fqn, "key2", "value2");
426       Map oldData = new HashMap();
427       oldData.put("key", "value");
428       oldData.put("key2", "value2");
429
430       // clear event log
431       eventLog2.events.clear();
432       assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events);
433
434       // modify existing node
435       cache1.remove(fqn, "key2");
436       Map newData = new HashMap();
437       newData.put("key", "value");
438
439       //expected
440       List<Event> expected = new ArrayList<Event>();
441       expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
442       expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, false, true, newData));
443
444       assertEquals(expected, eventLog2.events);
445    }
446
447    public void testTxMove()
448    {
449       fail("implement me");
450    }
451    */

452
453    // ============= supporting classes and enums =======================
454

455
456    public static class EventLog extends AbstractCacheListener
457    {
458       public final List JavaDoc<CacheListenerEvent> events = new ArrayList JavaDoc<CacheListenerEvent>();
459
460       public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
461       {
462          CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_CREATED, fqn, pre, isLocal, null);
463          events.add(e);
464       }
465
466       public void nodeRemoved(Fqn fqn, boolean pre, boolean isLocal, Map JavaDoc data)
467       {
468          CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_REMOVED, fqn, pre, isLocal, data);
469          events.add(e);
470       }
471
472       public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, ModificationType modType, Map JavaDoc data)
473       {
474          CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MODIFIED, fqn, pre, isLocal, data);
475          events.add(e);
476       }
477
478       public void nodeVisited(Fqn fqn, boolean pre)
479       {
480          CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_VISITED, fqn, pre, false, null);
481          events.add(e);
482       }
483
484       public void nodeMoved(Fqn from, Fqn to, boolean pre, boolean isLocal)
485       {
486          CacheListenerEvent e = new CacheListenerEvent(CacheListenerEvent.ListenerMethod.NODE_MOVED, from, to, pre, isLocal);
487          events.add(e);
488       }
489
490
491       public String JavaDoc toString()
492       {
493          return "EventLog{" +
494                  "events=" + events +
495                  '}';
496       }
497    }
498 }
499
Popular Tags