[jbosscache-commits] JBoss Cache SVN: r5467 - core/trunk/src/test/java/org/jboss/cache/api.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Thu Mar 27 09:00:06 EDT 2008


Author: bstansberry at jboss.com
Date: 2008-03-27 09:00:06 -0400 (Thu, 27 Mar 2008)
New Revision: 5467

Added:
   core/trunk/src/test/java/org/jboss/cache/api/DestroyedCacheAPITest.java
Log:
[JBCACHE-1312] Test for NPE's from destroyed cache

Added: core/trunk/src/test/java/org/jboss/cache/api/DestroyedCacheAPITest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/api/DestroyedCacheAPITest.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/api/DestroyedCacheAPITest.java	2008-03-27 13:00:06 UTC (rev 5467)
@@ -0,0 +1,375 @@
+package org.jboss.cache.api;
+
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertFalse;
+import static org.testng.AssertJUnit.assertNotNull;
+import static org.testng.AssertJUnit.assertNull;
+import static org.testng.AssertJUnit.assertSame;
+import static org.testng.AssertJUnit.fail;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheFactory;
+import org.jboss.cache.CacheStatus;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.InvocationContext;
+import org.jboss.cache.Node;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.notifications.annotation.CacheListener;
+import org.jboss.cache.notifications.annotation.NodeCreated;
+import org.jboss.cache.notifications.event.Event;
+import org.jboss.cache.transaction.GenericTransactionManagerLookup;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * Tests aspects of the {@link org.jboss.cache.Cache} public API when
+ * destroy() has been called on the cache.
+ *
+ * @author Brian Stansberry
+ */
+
+ at Test(groups = "functional")
+public class DestroyedCacheAPITest
+{
+   private Cache<String, String> cache;
+   protected boolean optimistic;
+   private Fqn parent = Fqn.fromString("/test/fqn");
+   private Fqn child = Fqn.fromString("/test/fqn/child");
+   private String version;
+   
+   @BeforeMethod(alwaysRun = true)
+   public void setUp() throws Exception
+   {
+      // start a single cache instance
+      CacheFactory<String, String> cf = new DefaultCacheFactory();
+      cache = cf.createCache("META-INF/conf-test/local-tx-service.xml", false);
+      cache.getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC);
+      cache.start();
+      version = cache.getVersion();
+      cache.getRoot().addChild(parent);
+      cache.getRoot().addChild(child);
+      cache.stop();
+      cache.destroy();
+   }
+
+   /**
+    * Tests that the configuration contains the values expected, as well as immutability of certain elements
+    */
+   public void testConfiguration()
+   {
+      Configuration c = cache.getConfiguration();
+      assertEquals(Configuration.CacheMode.LOCAL, c.getCacheMode());
+      assertEquals(GenericTransactionManagerLookup.class.getName(), c.getTransactionManagerLookupClass());
+
+      // Certain values, e.g. CacheMode should be immutable once started, 
+      // but we aren't started, so should be able to change them
+      c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
+      assertEquals(Configuration.CacheMode.REPL_SYNC, c.getCacheMode());
+      
+      // others are always changeable.
+      c.setLockAcquisitionTimeout(100);
+      assertEquals(100, c.getLockAcquisitionTimeout());
+   }
+
+   /**
+    * Basic usage of cache listeners
+    * <p/>
+    * A more complete test that tests notifications is in org.jboss.cache.notifications
+    */
+   public void testCacheListeners()
+   {
+      assertEquals(0, cache.getCacheListeners().size());
+
+      Object dummy = new Listener();
+
+      cache.addCacheListener(dummy);
+
+      assertEquals(1, cache.getCacheListeners().size());
+
+      cache.removeCacheListener(dummy);
+
+      assertEquals(0, cache.getCacheListeners().size());
+   }
+
+   /**
+    * Test that fqn-specific application of cache listeners has not been implemented and will not be implemented
+    * in 2.0.0.  It is a feature for 2.1.0 but the interface needed to be in place now.
+    */
+   public void testFqnBasedCacheListeners()
+   {
+      try
+      {
+         cache.getCacheListeners(Fqn.ROOT);
+         fail("Fqn-based cache listener operation should throw an exception");
+      }
+      catch (Exception e)
+      {
+         // expected
+      }
+
+      try
+      {
+         cache.addCacheListener(Fqn.ROOT, new Listener());
+         fail("Fqn-based cache listener operation should throw an exception");
+      }
+      catch (Exception e)
+      {
+         // expected
+      }
+
+      try
+      {
+         cache.removeCacheListener(Fqn.ROOT, new Listener());
+         fail("Fqn-based cache listener operation should throw an exception");
+      }
+      catch (Exception e)
+      {
+         // expected
+      }
+   }
+
+   /**
+    * Tests the basic gets, puts.  Expectation is all will throw an
+    * ISE.  
+    * 
+    * BES 2008/03/22 -- This behavior is not actually documented.  Maintainers
+    * shouldn't feel constrained from updating this test to match
+    * agreed upon behavior changes; I'm just adding it so any changes to the 
+    * current behavior will trigger failures and ensure that people are aware of
+    * the change and agree that it's correct. 
+    */
+   public void testConvenienceMethods()
+   {
+      String key = "key", value = "value";
+      Map<String, String> data = new HashMap<String, String>();
+      data.put(key, value);
+
+      try
+      {
+         cache.get(parent, key);
+         fail("Get key on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+
+      try
+      {
+         cache.getNode(parent);
+         fail("Get node on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+
+      try
+      {
+         cache.put(parent, key, value);
+         fail("Put key/value on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+
+      try
+      {
+         cache.putForExternalRead(parent, key, value);
+         fail("Put for external read on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+
+      try
+      {
+         cache.put(parent, data);
+         fail("Put Map on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+      
+      try
+      {
+         cache.move(child, Fqn.ROOT);
+         fail("Remove move on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+      
+      try
+      {
+         cache.getData(parent);
+         fail("getData on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+      
+      try
+      {
+         cache.getKeys(parent);
+         fail("getKeys on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+      
+      try
+      {
+         cache.clearData(parent);
+         fail("clearData on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+
+      try
+      {
+         cache.remove(parent, key);
+         fail("Remove key on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+
+      try
+      {
+         cache.removeNode(parent);
+         fail("Remove node on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+
+   }
+   
+   /**
+    * Tests that Cache.getRoot() returns a node.  
+    * 
+    * BES 2008/03/22 -- This behavior is not actually documented.  Maintainers
+    * shouldn't feel constrained from updating this test to match
+    * agreed upon behavior changes; I'm just adding it so any changes to the 
+    * current behavior will trigger failures and ensure that people are aware of
+    * the change and agree that it's correct. 
+    */   
+   public void testGetRoot()
+   {
+      Node root = cache.getRoot();
+      assertNotNull("have root", root);
+      assertEquals(Fqn.ROOT, root.getFqn());
+   }
+
+
+   /**
+    * Tests the basic node addition, existence check, get, remove operations.  
+    * Expectation is all will throw an ISE.  
+    * 
+    * BES 2008/03/22 -- This behavior is not actually documented.  Maintainers
+    * shouldn't feel constrained from updating this test to match
+    * agreed upon behavior changes; I'm just adding it so any changes to the 
+    * current behavior will trigger failures and ensure that people are aware of
+    * the change and agree that it's correct. 
+    */
+   public void testNodeAPI()
+   {
+      Node root = cache.getRoot();
+      try
+      {
+         root.addChild(parent);
+         fail("addChild on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+      
+      try
+      {
+         root.hasChild(parent);
+         fail("hasChild on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+      
+      try
+      {
+         root.getChild(parent);
+         fail("getChild on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+      
+      try
+      {
+         root.removeChild(parent);
+         fail("removeChild on destroyed cache did not throw ISE");
+      }
+      catch (IllegalStateException good) {}
+   }
+
+   /**
+    * Tests the evict operation. Expectation is it will throw an ISE, but
+    * a success is OK as well.  
+    * 
+    * BES 2008/03/22 -- This behavior is not actually documented.  Maintainers
+    * shouldn't feel constrained from updating this test to match
+    * agreed upon behavior changes; I'm just adding it so any changes to the 
+    * current behavior will trigger failures and ensure that people are aware of
+    * the change and agree that it's correct. 
+    */
+   public void testEvict()
+   {
+      try
+      {
+         cache.evict(parent, false);
+      }
+      catch (IllegalStateException ok) {}
+      
+      try
+      {
+         cache.evict(child, false);
+      }
+      catch (IllegalStateException ok) {}
+
+      try
+      {
+         cache.evict(parent, true);
+      }
+      catch (IllegalStateException ok) {}
+   }
+
+   public void testGetRegion()
+   {      
+      assertNull(cache.getRegion(parent, false));
+      
+      // The javadoc mentions "UnsupportedOperationException" although it's
+      // not clear about why it would be thrown. 
+      assertNotNull(cache.getRegion(Fqn.ROOT, true));
+   }
+   
+   public void testRemoveRegion()
+   {
+      assertFalse(cache.removeRegion(parent));
+   }
+   
+   public void testGetLocalAddress()
+   {
+      assertEquals("CacheMode.LOCAL cache has no address", null, cache.getLocalAddress());
+   }
+   
+   public void testGetMembers()
+   {
+      assertNull(cache.getMembers());
+   }
+   
+   public void testGetCacheStatus()
+   {
+      assertEquals(CacheStatus.DESTROYED, cache.getCacheStatus());
+   }
+   
+   /**
+    * BES 2008/03/22.  I don't know what the correct behavior should be.
+    * The test checks the call succeeds, which is one possibility and is 
+    * what the current impl does. Can be something else, just not NPE.
+    */
+   public void testInvocationContext()
+   {
+      InvocationContext ctx = new InvocationContext();
+      cache.setInvocationContext(ctx);
+      assertSame(ctx, cache.getInvocationContext());
+   }
+   
+   public void testGetVersion()
+   {
+      assertEquals(version, cache.getVersion());
+   }
+   
+
+   @CacheListener
+   public class Listener
+   {
+      @NodeCreated
+      public void nodeCreated(Event e)
+      {         
+      }
+   }
+}




More information about the jbosscache-commits mailing list