1 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 ; 35 import java.util.ArrayList ; 36 import java.util.Collections ; 37 import java.util.HashMap ; 38 import java.util.List ; 39 import java.util.Map ; 40 41 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 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 c.setSyncCommitPhase(true); 69 70 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 84 { 85 super.tearDown(); 86 TransactionManager 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 e) 96 { 97 } 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 e) 110 { 111 } 113 } 114 } 115 116 cache1.stop(); 117 cache1.destroy(); 118 cache2.stop(); 119 cache2.destroy(); 120 } 121 122 124 public void testCreation() throws Exception 125 { 126 assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events); 127 cache1.put(fqn, "key", "value"); 128 Map data = new HashMap (); 129 data.put("key", "value"); 130 131 List <CacheListenerEvent> expectedLocal = new ArrayList <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 List <CacheListenerEvent> expectedRemote = new ArrayList <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 151 { 152 assertNull(cache1.get(fqn, "key")); 153 assertNull(cache2.get(fqn, "key")); 154 155 cache1.put(fqn, "key", "value"); 156 Map oldData = new HashMap (); 157 oldData.put("key", "value"); 158 159 assertEquals("value", cache1.get(fqn, "key")); 160 assertEquals("value", cache2.get(fqn, "key")); 161 162 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 cache1.put(fqn, "key", "value2"); 170 Map newData = new HashMap (); 171 newData.put("key", "value2"); 172 173 List <CacheListenerEvent> expectedLocal = new ArrayList <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 List <CacheListenerEvent> expectedRemote = new ArrayList <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 189 { 190 assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events); 191 cache1.put(fqn, "key", "value"); 192 Map oldData = new HashMap (); 193 oldData.put("key", "value"); 194 195 assertEquals("value", cache1.get(fqn, "key")); 196 assertEquals("value", cache2.get(fqn, "key")); 197 198 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 cache1.removeNode(fqn); 206 207 List <CacheListenerEvent> expectedLocal = new ArrayList <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 List <CacheListenerEvent> expectedRemote = new ArrayList <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 assertNull("Should be null", cache1.getRoot().getChild(fqn)); 222 } 223 224 225 public void testRemoveData() throws Exception 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 oldData = new HashMap (); 231 oldData.put("key", "value"); 232 oldData.put("key2", "value2"); 233 234 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 cache1.remove(fqn, "key2"); 242 Map removed = new HashMap (); 243 removed.put("key2", "value2"); 244 245 List <CacheListenerEvent> expectedLocal = new ArrayList <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 List <CacheListenerEvent> expectedRemote = new ArrayList <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 260 { 261 assertEquals("Event log should be empty", Collections.emptyList(), eventLog2.events); 262 Map oldData = new HashMap (); 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 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 List <CacheListenerEvent> expectedLocal = new ArrayList <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 List <CacheListenerEvent> expectedRemote = new ArrayList <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(); 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 List <CacheListenerEvent> expectedLocal = new ArrayList <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 List <CacheListenerEvent> expectedRemote = new ArrayList <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 329 452 453 455 456 public static class EventLog extends AbstractCacheListener 457 { 458 public final List <CacheListenerEvent> events = new ArrayList <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 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 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 toString() 492 { 493 return "EventLog{" + 494 "events=" + events + 495 '}'; 496 } 497 } 498 } 499 | Popular Tags |