Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 391   Methods: 15
NCLOC: 182   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
CacheListenerTest.java 75% 98.2% 93.3% 96.9%
coverage coverage
 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.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    * 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  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    // simple tests first
 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    //expected
 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    // clear CacheListenerEvent log
 100  2 eventLog.events.clear();
 101  2 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
 102   
 103    // modify existing node
 104  2 cache.put(fqn, "key", "value2");
 105  2 Map newData = new HashMap();
 106  2 newData.put("key", "value2");
 107   
 108    //expected
 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    // clear CacheListenerEvent log
 128  2 eventLog.events.clear();
 129  2 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
 130   
 131    // modify existing node
 132  2 cache.removeNode(fqn);
 133   
 134    //expected
 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    // test that the node has in fact been removed.
 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    // clear CacheListenerEvent log
 156  2 eventLog.events.clear();
 157  2 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
 158   
 159    // modify existing node
 160  2 cache.remove(fqn, "key2");
 161  2 Map removedData = new HashMap();
 162  2 removedData.put("key2", "value2");
 163   
 164    //expected
 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    // clear CacheListenerEvent log
 180  2 eventLog.events.clear();
 181  2 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
 182   
 183    // modify existing node
 184  2 cache.put(fqn, oldData);
 185   
 186    //expected
 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();// clear events
 207  2 assertEquals("CacheListenerEvent log should be empty", Collections.emptyList(), eventLog.events);
 208   
 209  2 cache.move(n1.getFqn(), n2.getFqn());
 210    //expected
 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    // -- 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<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    }