[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/notifications ...

Manik Surtani msurtani at jboss.com
Tue Aug 15 17:02:23 EDT 2006


  User: msurtani
  Date: 06/08/15 17:02:23

  Added:       tests/functional/org/jboss/cache/notifications    
                        CacheListenerOptimisticTest.java
                        CacheListenerTest.java
  Removed:     tests/functional/org/jboss/cache/notifications    
                        TreeCacheListenerOptimisticTest.java
                        TreeCacheListenerTest.java
  Log:
  Habanero stabilisation efforts
  
  Revision  Changes    Path
  1.1      date: 2006/08/15 21:02:23;  author: msurtani;  state: Exp;JBossCache/tests/functional/org/jboss/cache/notifications/CacheListenerOptimisticTest.java
  
  Index: CacheListenerOptimisticTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.notifications;
  
  public class CacheListenerOptimisticTest extends CacheListenerTest
  {
  
      public CacheListenerOptimisticTest(String s)
      {
          super(s);
          optLocking = true;
      }
  }
  
  
  
  1.1      date: 2006/08/15 21:02:23;  author: msurtani;  state: Exp;JBossCache/tests/functional/org/jboss/cache/notifications/CacheListenerTest.java
  
  Index: CacheListenerTest.java
  ===================================================================
  /*****************************************
   *                                       *
   *  JBoss Portal: The OpenSource Portal  *
   *                                       *
   *   Distributable under LGPL license.   *
   *   See terms of license at gnu.org.    *
   *                                       *
   *****************************************/
  package org.jboss.cache.notifications;
  
  import junit.framework.TestCase;
  import org.jboss.cache.AbstractCacheListener;
  import org.jboss.cache.Cache;
  import org.jboss.cache.CacheSPI;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.factories.DefaultCacheFactory;
  import org.jboss.cache.lock.IsolationLevel;
  
  import javax.transaction.TransactionManager;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  /**
   * Note that this is significantly different from the old <b>TreeCacheListenerTest</b> of the JBoss Cache 1.x series, and
   * exercises the new {@link org.jboss.cache.CacheListener} interface.
   *
   * @since 2.0.0
   */
  public class CacheListenerTest extends TestCase
  {
  
      protected boolean optLocking = false;
  
      public CacheListenerTest(String s)
      {
          super(s);
      }
  
      private Cache cache;
      private TransactionManager tm;
      private EventLog eventLog;
      private Fqn fqn = Fqn.fromString("/test");
  
      protected void setUp() throws Exception
      {
          super.setUp();
          Configuration c = new Configuration();
          c.setCacheMode(Configuration.CacheMode.LOCAL);
          c.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
          if (optLocking) c.setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
          c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
          cache = new DefaultCacheFactory().createCache(c);
          tm = ((CacheSPI) cache).getTransactionManager();
          eventLog = new EventLog();
          cache.addCacheListener(eventLog);
      }
  
      protected void tearDown() throws Exception
      {
          super.tearDown();
          cache.stop();
          cache.destroy();
      }
  
      // simple tests first
  
      public void testCreation() throws Exception
      {
          assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
          cache.put(fqn, "key", "value");
          Map data = new HashMap();
          data.put("key", "value");
  
          //expected
          List<Event> expected = new ArrayList<Event>();
          expected.add(new Event(ListenerMethod.NODE_CREATED, fqn, true, true, null));
          expected.add(new Event(ListenerMethod.NODE_CREATED, fqn, false, true, null));
          expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, true, true, Collections.emptyMap()));
          expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, false, true, data));
  
          assertEquals(expected, eventLog.events);
      }
  
      public void testOnlyModification() throws Exception
      {
          assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
          cache.put(fqn, "key", "value");
          Map oldData = new HashMap();
          oldData.put("key", "value");
  
          // clear event log
          eventLog.events.clear();
          assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
  
          // modify existing node
          cache.put(fqn, "key", "value2");
          Map newData = new HashMap();
          newData.put("key", "value2");
  
          //expected
          List<Event> expected = new ArrayList<Event>();
          expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
          expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, false, true, newData));
  
          assertEquals(expected, eventLog.events);
      }
  
  
      public void testOnlyRemoval() throws Exception
      {
          assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
          cache.put(fqn, "key", "value");
          Map oldData = new HashMap();
          oldData.put("key", "value");
  
          // clear event log
          eventLog.events.clear();
          assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
  
          // modify existing node
          cache.removeNode(fqn);
  
          //expected
          List<Event> expected = new ArrayList<Event>();
          expected.add(new Event(ListenerMethod.NODE_REMOVED, fqn, true, true, oldData));
          expected.add(new Event(ListenerMethod.NODE_REMOVED, fqn, false, true, null));
  
          assertEquals(expected, eventLog.events);
      }
  
  
      public void testRemoveData() throws Exception
      {
          assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
          cache.put(fqn, "key", "value");
          cache.put(fqn, "key2", "value2");
          Map oldData = new HashMap();
          oldData.put("key", "value");
          oldData.put("key2", "value2");
  
          // clear event log
          eventLog.events.clear();
          assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
  
          // modify existing node
          cache.remove(fqn, "key2");
          Map newData = new HashMap();
          newData.put("key", "value");
  
          //expected
          List<Event> expected = new ArrayList<Event>();
          expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
          expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, false, true, newData));
  
          assertEquals(expected, eventLog.events);
      }
  
      // TODO: Transactional stuff.  Test the above is only notified after a commit, and that rollbacks work accordingly as well.
  
      // ============= supporting classes and enums =======================
  
  
      public static class EventLog extends AbstractCacheListener
      {
          public final List<Event> events = new ArrayList<Event>();
  
          public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
          {
              Event e = new Event(ListenerMethod.NODE_CREATED, fqn, pre, isLocal, null);
              events.add(e);
          }
  
          public void nodeRemoved(Fqn fqn, boolean pre, boolean isLocal, Map data)
          {
              Event e = new Event(ListenerMethod.NODE_REMOVED, fqn, pre, isLocal, data);
              events.add(e);
          }
  
          public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, Map data)
          {
              Event e = new Event(ListenerMethod.NODE_MODIFIED, fqn, pre, isLocal, data);
              events.add(e);
          }
  
          public void nodeVisited(Fqn fqn, boolean pre)
          {
              Event e = new Event(ListenerMethod.NODE_VISITED, fqn, pre, false, null);
              events.add(e);
          }
  
  
          public String toString()
          {
              return "EventLog{" +
                      "events=" + events +
                      '}';
          }
      }
  
      public static class Event
      {
          ListenerMethod methodName;
          Fqn fqn;
          boolean pre;
          boolean local;
          Map data;
  
          public Event(ListenerMethod methodName, Fqn fqn, boolean pre, boolean local, Map data)
          {
              this.methodName = methodName;
              this.fqn = fqn;
              this.pre = pre;
              this.local = local;
              this.data = data;
          }
  
          public String toString()
          {
              return "Event{" +
                      "methodName=" + methodName +
                      ", fqn=" + fqn +
                      ", pre=" + pre +
                      ", local=" + local +
                      ", data=" + data +
                      '}';
          }
      }
  
      public enum ListenerMethod {NODE_CREATED, NODE_REMOVED, NODE_VISITED, NODE_MODIFIED};
  }
  
  
  



More information about the jboss-cvs-commits mailing list