1 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 ; 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 34 public class CacheListenerTest extends TestCase 35 { 36 37 protected boolean optLocking = false; 38 39 public CacheListenerTest(String s) 40 { 41 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 protected void setUp() throws Exception 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 64 { 65 super.tearDown(); 66 Transaction t = tm.getTransaction(); 67 if (t != null) t.rollback(); 68 cache.stop(); 69 cache.destroy(); 70 } 71 72 74 public void testCreation() throws Exception 75 { 76 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events); 77 cache.put(fqn, "key", "value"); 78 Map data = new HashMap (); 79 data.put("key", "value"); 80 81 List <CacheListenerEvent> expected = new ArrayList <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 93 { 94 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events); 95 cache.put(fqn, "key", "value"); 96 Map oldData = new HashMap (); 97 oldData.put("key", "value"); 98 99 eventLog.events.clear(); 101 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events); 102 103 cache.put(fqn, "key", "value2"); 105 Map newData = new HashMap (); 106 newData.put("key", "value2"); 107 108 List <CacheListenerEvent> expected = new ArrayList <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 119 { 120 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events); 121 cache.put(fqn, "key", "value"); 122 Map oldData = new HashMap (); 123 oldData.put("key", "value"); 124 125 assertEquals("value", cache.get(fqn, "key")); 126 127 eventLog.events.clear(); 129 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events); 130 131 cache.removeNode(fqn); 133 134 List <CacheListenerEvent> expected = new ArrayList <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 assertNull("Should be null", cache.getRoot().getChild(fqn)); 143 } 144 145 146 public void testRemoveData() throws Exception 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 oldData = new HashMap (); 152 oldData.put("key", "value"); 153 oldData.put("key2", "value2"); 154 155 eventLog.events.clear(); 157 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events); 158 159 cache.remove(fqn, "key2"); 161 Map removedData = new HashMap (); 162 removedData.put("key2", "value2"); 163 164 List <CacheListenerEvent> expected = new ArrayList <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 173 { 174 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events); 175 Map oldData = new HashMap (); 176 oldData.put("key", "value"); 177 oldData.put("key2", "value2"); 178 179 eventLog.events.clear(); 181 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events); 182 183 cache.put(fqn, oldData); 185 186 List <CacheListenerEvent> expected = new ArrayList <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(); assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events); 208 209 cache.move(n1.getFqn(), n2.getFqn()); 210 Fqn newFqn = new Fqn(newParent, fqn.getLastElement()); 212 List <CacheListenerEvent> expected = new ArrayList <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 221 345 346 348 349 public static class EventLog extends AbstractCacheListener 350 { 351 public final List <CacheListenerEvent> events = new ArrayList <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 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 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 toString() 385 { 386 return "EventLog{" + 387 "events=" + events + 388 '}'; 389 } 390 } 391 } 392 | Popular Tags |