package org.jboss.cache.loader;

import org.jboss.cache.*;
import org.jboss.cache.transaction.DummyTransactionManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.transaction.NotSupportedException;
import javax.transaction.Transaction;
import java.io.File;
import java.io.Serializable;
import java.util.*;

/**
 * Commons tests for all CacheLoaders
 * @author Bela Ban
 * @version $Id: CacheLoaderTestsBase.java,v 1.24 2006/05/04 23:11:07 msurtani Exp $
 */
abstract public class CacheLoaderTestsBase extends AbstractCacheLoaderTestBase {

   private static final Log log = LogFactory.getLog(CacheLoaderTestsBase.class); 
   TreeCache        cache;
   CacheLoader      loader=null;
   Transaction      tx=null;
   static final Fqn FQN = new Fqn("key");

   public CacheLoaderTestsBase(String name) {
      super(name);
   }

   public CacheLoaderTestsBase() {
      super();
   }

   protected void setUp() throws Exception {
      super.setUp();
      log.debug("\nTest " + getName() + "\n");
      cache=new TreeCache();
      cache.setCacheMode("local");
      cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
      configureCache();
      cache.startService();
      loader=cache.getCacheLoader();
   }

   abstract protected void configureCache() throws Exception;


   protected void tearDown() throws Exception {
      super.tearDown();
      if(tx != null) {
         try {
            tx.commit();
         }
         catch(Throwable e) {
            e.printStackTrace();
         }
      }
      cache.remove("/");
      loader.remove(Fqn.fromString("/"));
      cache.stopService();
      cache.destroyService();
   }

   protected void addDelay()
   {
      ; // returns immediately in this case.  Subclasses may override where a delay is needed.
   }

    protected void clean(File dir)
    {
        File[] files = dir.listFiles();
        if (files != null)
        {
            for (int i = 0; i < files.length; i++)
            {
                if (files[i].isDirectory())
                {
                    clean(files[i]);
                }
                else
                {
                    if (!files[i].delete()) files[i].deleteOnExit();
                }
            }
        }
    }


    public void testPrint() throws CacheException {
      final Fqn NODE=Fqn.fromString("/test");
      final String KEY="key";
      cache.put(NODE, KEY, new Integer(10));
      cache.evict(NODE);
      addDelay();
      String ret=cache.print(NODE);
      assertNotNull(ret);
   }

   public void testPut() throws CacheException {
      final String NODE="/test";
      final String KEY="key";
      Object retval=null;
      cache.remove(NODE);
      addDelay();
      retval=cache.put(NODE, KEY, new Integer(10));
      assertEquals(null, retval);
      retval=cache.put(NODE, KEY, new Integer(20));
      addDelay();
      assertEquals(new Integer(10), retval);
      cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
      addDelay();
      log.debug("put 30, expect 20 back");
      retval=cache.put(NODE, KEY, new Integer(30));
      assertEquals(new Integer(20), retval);
   }

   public void testPut2() throws CacheException {
      final String NODE="/a/b/c";
      final String KEY="key";
      Object retval=null;
      cache.remove(NODE);
      addDelay();
      retval=cache.put(NODE, KEY, new Integer(10));
      assertNull(retval);
      addDelay();
      retval=cache.put(NODE, KEY, new Integer(20));
      assertEquals(new Integer(10), retval);
      cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
      cache.evict(Fqn.fromString("/a/b"));
      cache.evict(Fqn.fromString("/a"));
      addDelay();
      log.debug("replace KEY with 30, expect 20");
      retval=cache.put(NODE, KEY, new Integer(30));
      assertEquals(new Integer(20), retval);
   }

   /**
    * Tests various Map puts.
    */
   public void testPut3() throws CacheException {
      final String NODE="/a/b/c";
      final String KEY="key";
      Object retval=null;
      cache.remove(NODE);
      addDelay();
      Map m = new HashMap();
      m.put("a", "b");
      m.put("c", "d");
      Map m2 = new HashMap();
      m2.put("e", "f");
      m2.put("g", "h");
      cache.put(NODE, m);
      addDelay();
      cache.get(NODE, "X");
      assertEquals(m, cache.get(NODE).getData());
      cache.evict(Fqn.fromString(NODE));
      addDelay();
      cache.get(NODE, "X");
      assertEquals(m, cache.get(NODE).getData());
      cache.evict(Fqn.fromString(NODE));
      cache.get(NODE, "X");
      cache.put(NODE, m2);
      assertEquals("combined", 4, cache.get(NODE).getData().size());
   }

   /**
    * Tests various put combos which should exercise the CacheLoaderInterceptor.
    */
   public void testPutRemoveCombos() throws Exception {
      final String NODE="/a/b/c";
      final String KEY="key";
      Object retval=null;
      cache.remove(NODE);
      Fqn fqn = Fqn.fromString(NODE);
      addDelay();
      Map m = new HashMap();
      m.put("a", "b");
      m.put("c", "d");
      loader.put(fqn, m);
      cache.put(NODE, "e", "f");
      addDelay();
      cache.get(NODE, "X");
      assertEquals(3, cache.get(NODE).getData().size());
      cache.evict(fqn);
      cache.get(NODE, "X");
      cache.remove(NODE, "e");
      assertEquals(2, cache.get(NODE).getData().size());
   }

   public void testGet() throws CacheException {
      final String NODE="/a/b/c";
      Object retval=null;
      cache.remove(NODE);
      addDelay();
      retval=cache.put(NODE, "1", new Integer(10));
      assertNull(retval);
      addDelay();
      retval=cache.put(NODE, "2", new Integer(20));
      cache.evict(Fqn.fromString("/a/b/c"));
      assertTrue("DataNode should not exisit ", !cache.exists("/a/b/c"));
      addDelay();
      retval=cache.get(NODE, "1");
      assertEquals(new Integer(10), retval);
      retval=cache.get(NODE, "2");
      assertEquals(new Integer(20), retval);
   }

   public void testGetNode() throws CacheException
   {
      final String NODE="/a/b/c";
      Object retval=null;
      cache.remove(NODE);
      addDelay();
      cache.put(NODE, "1", new Integer(10));
      cache.evict(Fqn.fromString(NODE));
      assertTrue("DataNode should not exisit ", !cache.exists("/a/b/c"));
      addDelay();
      retval=cache.get(NODE);

      assertNotNull("Should not be null", retval);
      assertTrue("Expecting instance of data node", retval instanceof DataNode);

      DataNode node = (DataNode) retval;
      assertEquals(new Integer(10), node.get("1"));
   }


   public void testSerialization() throws CacheException {
      SamplePojo pojo=new SamplePojo(39, "Bela");
      pojo.getHobbies().add("Running");
      pojo.getHobbies().add("Beerathlon");
      pojo.getHobbies().add("Triathlon");
      cache.put("/mypojo", new Integer(322649), pojo);
      addDelay();
      assertNotNull(cache.get("/mypojo", new Integer(322649)));
      cache.evict(Fqn.fromString("/mypojo"));
      assertFalse(cache.exists("/mypojo"));
      SamplePojo pojo2=(SamplePojo)cache.get("/mypojo", new Integer(322649)); // should fetch from CacheLoader      
      assertNotNull(pojo2);
      assertEquals(39, pojo2.getAge());
      assertEquals("Bela", pojo2.getName());
      assertEquals(3, pojo2.getHobbies().size());
   }

   /** Just adds some data that wil be later retrieved. This test has to be run first */
   public void testPopulate() {
      try {
         Map m=new HashMap();
         for(int i=0; i < 10; i++)
           m.put("key" + i, "val" + i);
         cache.put("/a/b/c", m);
         cache.load("/1/2/3/4/5");
         cache.put("/1/2/3/4/5", null);
         cache.put("/1/2/3/4/5/a", null);
         cache.put("/1/2/3/4/5/b", null);
         cache.put("/1/2/3/4/5/c", null);
         cache.put("/1/2/3/4/5/d", null);
         cache.put("/1/2/3/4/5/e", null);
         cache.put("/1/2/3/4/5/d/one", null);
         cache.put("/1/2/3/4/5/d/two", null);
         cache.put("/1/2/3/4/5/d/three", null);
         // cache.put("/a/b/c", "newKey", "newValue");
         System.out.println("cache: " + cache);

         assertTrue(cache.exists("/1/2/3/4"));
         assertTrue(cache.exists("/a/b/c"));
         assertFalse(cache.exists("/a/b/c/d"));
      }
      catch(Exception e) {
         fail(e.toString());
      }
   }


   public void testPreloading() throws CacheException {
      cache.remove("/");
      cache.put("1/2/3/4/5/d", "key", "val");
      cache.evict(Fqn.fromString("1/2/3/4/5/d"));
      System.out.println("-- checking for 1/2/3/4/5/d");
      addDelay();
      assertFalse(cache.exists("1/2/3/4/5/d")); // exists() doesn't load
      cache.get("1/2/3/4/5/d"); // get *does* load
      assertTrue(cache.exists("1/2/3/4/5/d"));
      System.out.println("-- 1/2/3/4/5/d exists");
   }



   public void testCacheLoading2() throws CacheException {
      Set keys=null;
      cache.put("/a/b/c", "key", "val");
      try {
         keys=cache.getKeys("/a/b/c");
         assertNotNull(keys);
         assertEquals(1, keys.size());
      }
      catch(Exception e) {
         fail(e.toString());
      }

      try {
         keys.add("myKey");
      }
      catch(UnsupportedOperationException ex) {
         fail("unsupported operation: " + ex);
      }
   }


   public void testExists() throws Exception {
      cache.put("/eins/zwei/drei", "key1", "val1");
      assertTrue(cache.exists("/eins/zwei/drei"));
      assertTrue(cache.exists("/eins/zwei/drei", "key1"));
      assertFalse(cache.exists("/eins/zwei/drei", "key2"));
      assertFalse(cache.exists("/uno/due/tre"));
      assertFalse(cache.exists("/une/due/tre", "key1"));
   }

   public void testGetChildren() {
      try {
         cache.put("/1/2/3/4/5/d/one", null);
         cache.put("/1/2/3/4/5/d/two", null);
         cache.put("/1/2/3/4/5/d/three", null);
         Set children=cache.getChildrenNames("/1/2/3/4/5/d");
         assertNotNull(children);
         assertEquals(3, children.size());
         assertTrue(children.contains("one"));
         assertTrue(children.contains("two"));
         assertTrue(children.contains("three"));
      }
      catch(Exception e) {
         fail(e.toString());
      }
   }


   public void testGetChildrenWithEviction() throws CacheException {
      cache.put("/a/b/c/1", null);
      cache.put("/a/b/c/2", null);
      cache.put("/a/b/c/3", null);
      cache.evict(Fqn.fromString("/a/b/c/1"));
      cache.evict(Fqn.fromString("/a/b/c/2"));
      cache.evict(Fqn.fromString("/a/b/c/3"));
      cache.evict(Fqn.fromString("/a/b/c"));
      cache.evict(Fqn.fromString("/a/b"));
      cache.evict(Fqn.fromString("/a"));
      cache.evict(Fqn.fromString("/"));
      addDelay();
      Set children=cache.getChildrenNames("/a/b/c");
      assertNotNull(children);
      assertEquals(3, children.size());
      assertTrue(children.contains("1"));
      assertTrue(children.contains("2"));
      assertTrue(children.contains("3"));
   }

   public void testGetChildren2() {
      try {
         cache.put("/1", null);
         cache.put("a", null);
         Set children=cache.getChildrenNames("/");
         assertNotNull(children);
         assertEquals(2, children.size());
         assertTrue(children.contains("1"));
         assertTrue(children.contains("a"));
      }
      catch(Exception e) {
         fail(e.toString());
      }
   }

   public void testGetChildren3() {
      try {
         cache.put("/1", null);
         cache.put("a", null);
         Set children=cache.getChildrenNames("");
         assertNotNull(children);
         assertEquals(2, children.size());
         assertTrue(children.contains("1"));
         assertTrue(children.contains("a"));
      }
      catch(Exception e) {
         fail(e.toString());
      }
   }

   public void testGetChildren4() {
      try {
         if(!cache.exists("/a/b/c"))
            cache.put("/a/b/c", null);
         Set children=cache.getChildrenNames((Fqn)null);
         assertNull(children);
      }
      catch(Exception e) {
         fail(e.toString());
      }
   }


   public void testGetChildren5() {
      try {
         cache.put("/a/1", null);
         cache.put("/a/2", null);
         cache.put("/a/3", null);
         System.out.println("cache is " + cache.printLockInfo());

         DataNode n=cache.get("/a");
         assertNotNull(n);

         Set children=cache.getChildrenNames("/a");
         assertNotNull(children);
         assertEquals(3, children.size());
      }
      catch(Exception e) {
         fail(e.toString());
      }
   }


    public void testGetChildren6() {
       try {
          cache.put("/a/1", null);
          cache.put("/a/2", null);
          cache.put("/a/3", null);
          System.out.println("cache is " + cache.printLockInfo());
          cache.evict(Fqn.fromString("/a/1"));
          cache.evict(Fqn.fromString("/a/2"));
          cache.evict(Fqn.fromString("/a/3"));
          cache.evict(Fqn.fromString("/a"));
          System.out.println("cache is " + cache.printLockInfo());
          addDelay();
          assertNotNull(cache.get("/a"));

          Set children=cache.getChildrenNames("/a");
          assertNotNull("No children were loaded", children);
          System.out.println("children: " + children);
          assertEquals("3 children weren't loaded", 3, children.size());
       }
       catch(Exception e) {
          fail(e.toString());
       }
    }

   public void testGetChildren7() {
      try {
         cache.put("/a/1", null);
         cache.put("/a/2", null);
         cache.put("/a/3", null);
         cache.put("/a", "test", "test");
         System.out.println("cache is " + cache.printLockInfo());
         cache.evict(Fqn.fromString("/a/1"));
         cache.evict(Fqn.fromString("/a/2"));
         cache.evict(Fqn.fromString("/a/3"));
         cache.evict(Fqn.fromString("/a"));
         System.out.println("cache is " + cache.printLockInfo());
         addDelay();
         Object val=cache.get("/a", "test");
         assertEquals("attributes weren't loaded", "test", val);

         Set children=cache.getChildrenNames("/a");
         assertNotNull("No children were loaded", children);
         System.out.println("children: " + children);
         assertEquals("3 children weren't loaded", 3, children.size());
      }
      catch(Exception e) {
         fail(e.toString());
      }
   }

   public void testGetChildren8() {
      try {
         cache.put("/a/1", null);
         cache.put("/a/2", null);
         cache.put("/a/3", null);
         System.out.println("cache is " + cache.printLockInfo());
         cache.evict(Fqn.fromString("/a/1"));
         cache.evict(Fqn.fromString("/a/2"));
         cache.evict(Fqn.fromString("/a/3"));
         cache.evict(Fqn.fromString("/a"));
         System.out.println("cache is " + cache.printLockInfo());
         addDelay();
         assertNull(cache.get("/a", "test"));

         cache.get("/a/1");
         Set children=cache.getChildrenNames("/a");
         assertNotNull("No children were loaded", children);
         System.out.println("children: " + children);
         assertEquals("3 children weren't loaded", 3, children.size());
      }
      catch(Exception e) {
         fail(e.toString());
      }
   }

   public void testGetChildren9() {
      try {
         cache.put("/a/1", null);
         cache.put("/a/2", null);
         cache.put("/a/3", null);
         System.out.println("cache is " + cache.printLockInfo());
         cache.evict(Fqn.fromString("/a/1"));
         cache.evict(Fqn.fromString("/a/2"));
         cache.evict(Fqn.fromString("/a/3"));
         cache.evict(Fqn.fromString("/a"));
         System.out.println("cache is " + cache.printLockInfo());
         addDelay();
         assertNull(cache.get("/a", "test"));

         cache.get("/a/1");
         Set children=cache.getChildrenNames("/a");
         assertNotNull("No children were loaded", children);
         System.out.println("children: " + children);
         assertEquals("3 children weren't loaded", 3, children.size());

         cache.evict(Fqn.fromString("/a/1"));
         cache.evict(Fqn.fromString("/a/2"));
         cache.evict(Fqn.fromString("/a/3"));
         cache.evict(Fqn.fromString("/a"));
         System.out.println("cache is " + cache.printLockInfo());

         assertNull(cache.get("/a", "test"));

         cache.get("/a/1");
         children=cache.getChildrenNames("/a");
         assertNotNull("No children were loaded", children);
         System.out.println("children: " + children);
         assertEquals("3 children weren't loaded", 3, children.size());
      }
      catch(Exception e) {
         fail(e.toString());
      }
   }


    public void testGetChildren10() {
      try {
         cache.put("/a/1", null);
         cache.put("/a/2", null);
         cache.put("/a/3", null);
         System.out.println("cache is " + cache.printLockInfo());
         cache.evict(Fqn.fromString("/a/1"));
         cache.evict(Fqn.fromString("/a/2"));
         cache.evict(Fqn.fromString("/a/3"));
         cache.evict(Fqn.fromString("/a"));
         System.out.println("cache is " + cache.printLockInfo());
         addDelay();
         assertNull(cache.get("/a", "test"));

         cache.get("/a/1");
         Set children=cache.getChildrenNames("/a");
         assertNotNull("No children were loaded", children);
         System.out.println("children: " + children);
         assertEquals("3 children weren't loaded", 3, children.size());

         children=cache.getChildrenNames("/a");
         assertNotNull("No children were loaded", children);
         System.out.println("children: " + children);
         assertEquals("3 children weren't loaded", 3, children.size());
      }
      catch(Exception e) {
         fail(e.toString());
      }
   }


   public void testGetChildren11() {
      Set children;
      try {
         cache.put("/a/b", "key", "val");
         cache.put("/a/b/1", "key", "val");
         cache.put("/a/b/2", "key", "val");
         cache.put("/a/b/3", "key", "val");
         cache.put("/a/b/1/tmp", "key", "val");
         cache.put("/a/b/2/tmp", "key", "val");
         cache.put("/a/b/3/tmp", "key", "val");

         cache.evict(Fqn.fromString("/a"));
         cache.evict(Fqn.fromString("/a/b"));
         cache.evict(Fqn.fromString("/a/b/1"));
         cache.evict(Fqn.fromString("/a/b/2"));
         cache.evict(Fqn.fromString("/a/b/3"));

         // now load the children - this set childrenLoaded in /a/b to true
         children=cache.getChildrenNames("/a/b");
         assertEquals(3, children.size());
         
         cache.evict(Fqn.fromString("/a/b"));
         cache.evict(Fqn.fromString(("/a/b/1/tmp")));
         cache.evict(Fqn.fromString(("/a/b/2/tmp")));
         cache.evict(Fqn.fromString(("/a/b/3/tmp")));
         cache.evict(Fqn.fromString(("/a/b/1")));
         cache.evict(Fqn.fromString(("/a/b/2")));
         cache.evict(Fqn.fromString(("/a/b/3")));
         cache.evict(Fqn.fromString("/a"));

         children=cache.getChildrenNames("/a/b");
         assertEquals(3, children.size());
      }
      catch(Exception e) {
         fail(e.toString());
      }
   }


     public void testGetChildren12() {
      Set children;
      try {
         cache.put("/a/b", "key", "val");
         cache.put("/a/b/1", "key", "val");
         cache.put("/a/b/2", "key", "val");
         cache.put("/a/b/3", "key", "val");
         children=cache.getChildrenNames("/a/b");
         assertEquals(3, children.size());

         cache.evict(Fqn.fromString("/a/b/3"));
         cache.evict(Fqn.fromString("/a/b/2"));
         // cache.evict(Fqn.fromString("/a/b/1"));
         cache.evict(Fqn.fromString("/a/b"));
         cache.evict(Fqn.fromString("/a"));

         // now load the children - this set childrenLoaded in /a/b to true
         cache.getChildrenNames("/a/b");
         children=cache.getChildrenNames("/a/b");
         assertEquals(3, children.size());

         cache.evict(Fqn.fromString("/a/b/3"));
         cache.evict(Fqn.fromString("/a/b/2"));
         // cache.evict(Fqn.fromString("/a/b/1"));
         cache.evict(Fqn.fromString("/a/b"));
         cache.evict(Fqn.fromString("/a"));
         children=cache.getChildrenNames("/a/b");
         assertEquals(3, children.size());
      }
      catch(Exception e) {
         fail(e.toString());
      }
   }


   public void testRemoveData() throws Exception {
      String key="/x/y/z/";
      cache.put(key, "keyA", "valA");
      cache.put(key, "keyB", "valB");
      cache.put(key, "keyC", "valC");
      assertEquals(3, cache.getKeys(key).size());
      cache.removeData(key);
      Set keys=cache.getKeys(key);
      assertEquals(0, keys.size());
      cache.remove("/x");
      Object val=cache.get(key, "keyA");
      assertNull(val);
   }


   public void testRemoveData2() throws Exception {
      Set keys;
      Fqn key=Fqn.fromString("/x/y/z/");
      cache.put(key, "keyA", "valA");
      cache.put(key, "keyB", "valB");
      cache.put(key, "keyC", "valC");
      addDelay();
      keys=cache.getKeys(key);
      assertEquals(3, keys.size());
      cache.removeData(key);
      cache.evict(key);
      addDelay();
      keys=cache.getKeys(key);
      assertNotNull(keys);
      assertEquals(0, keys.size());
   }

   public void testRemoveData3() throws Exception {
      Set keys;
      Fqn key=Fqn.fromString("/x/y/z/");
      cache.put(key, "keyA", "valA");
      cache.put(key, "keyB", "valB");
      cache.put(key, "keyC", "valC");
      keys=cache.getKeys(key);
      assertEquals(3, keys.size());
      cache.evict(key);
      cache.removeData(key);
      keys=cache.getKeys(key);
      assertEquals("no more keys", 0, keys.size());
   }

   public void testRemoveKey() throws Exception {
      String key="/x/y/z/";
      cache.put(key, "keyA", "valA");
      cache.put(key, "keyB", "valB");
      cache.put(key, "keyC", "valC");
      cache.remove(key, "keyA");
      assertEquals(2, cache.getKeys(key).size());
      cache.remove("/x");
   }


   public void testRemoveKey2() throws CacheException {
      final String NODE="/test";
      final String KEY="key";
      Object retval=null;
      cache.remove(NODE);
      retval=cache.put(NODE, KEY, new Integer(10));
      assertNull(retval);
      addDelay();
      retval=cache.remove(NODE, KEY);
      assertEquals(new Integer(10), retval);
      addDelay();
      retval=cache.remove(NODE, KEY);
      assertNull(retval);
   }

   public void testRemoveKey3() throws CacheException {
      final String NODE="/test";
      final String KEY="key";
      Object retval=null;
      cache.remove(NODE);
      retval=cache.put(NODE, KEY, new Integer(10));
      assertNull(retval);

      cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
      addDelay();
      retval=cache.remove(NODE, KEY);
      assertEquals(new Integer(10), retval);

      cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
      addDelay();
      retval=cache.remove(NODE, KEY);
      assertNull(retval);
   }


   public void testRemove() throws Exception {
      String key="/x/y/z/";
      cache.put(key, "keyA", "valA");
      cache.put(key, "keyB", "valB");
      cache.put(key, "keyC", "valC");
      cache.remove("/x");
      assertNull(cache.get(key, "keyA"));
      addDelay();
      Set keys=cache.getKeys(key);
      assertNull("got keys " + keys, keys);
      cache.remove("/x");
   }


   public void testRemoveRoot() throws Exception {
      assertEquals(0, cache.getKeys("/").size());
      cache.put("/1/2/3/4/5", null);
      cache.put("uno/due/tre", null);
      cache.put("1/2/3/a", null);
      cache.put("/eins/zwei/drei", null);
      cache.put("/one/two/three", null);
      cache.remove("/");
      assertEquals(0, cache.getKeys("/").size());
   }


   public void testEvictionWithCacheLoader() throws Exception {
      cache.put("/first/second", "key1", "val1");        // stored in cache loader
      cache.put("/first/second/third", "key2", "val2"); // stored in cache loader
      cache.evict(Fqn.fromString("/first/second"));      // doesn't remove node, just data !
      addDelay();
      assertTrue(cache.exists("/first/second/third"));
      assertTrue(cache.exists("/first/second"));
      assertTrue(cache.exists("/first"));
      String val=(String)cache.get("/first/second", "key1"); // should be loaded from cache loader
      assertEquals("val1", val);
      assertTrue(cache.exists("/first/second/third"));
      assertTrue(cache.exists("/first/second"));
      assertTrue(cache.exists("/first"));
   }


   public void testEvictionWithCacheLoader2() throws Exception {
       cache.put("/first/second/third", "key1", "val1");   // stored in cache loader
       cache.evict(Fqn.fromString("/first/second/third"));  // removes node, because there are no children
       addDelay();
       assertFalse(cache.exists("/first/second/third"));
       assertTrue(cache.exists("/first/second"));
       assertTrue(cache.exists("/first"));
       String val=(String)cache.get("/first/second/third", "key1"); // should be loaded from cache loader
       assertEquals("val1", val);
       assertTrue(cache.exists("/first/second/third"));
       assertTrue(cache.exists("/first/second"));
       assertTrue(cache.exists("/first"));
    }



   public void testEvictionWithGetChildrenNames() throws Exception {
      cache.put("/a/1", null);
      cache.put("/a/2", null);
      cache.put("/a/3", null);
      // cache.put("/a/1/tmp", null);
      cache.evict(Fqn.fromString("/a/1"));
      cache.evict(Fqn.fromString("/a/2"));
      cache.evict(Fqn.fromString("/a/3"));
      cache.evict(Fqn.fromString("/a"));
      addDelay();

      DummyTransactionManager mgr=DummyTransactionManager.getInstance();

       mgr.begin();
      tx=mgr.getTransaction();
      Set children=cache.getChildrenNames("/a");

       System.out.println("**** " + cache.getTransactionManager());

      assertEquals(3, children.size());
      assertTrue(children.contains("1"));
      assertTrue(children.contains("2"));
      assertTrue(children.contains("3"));

       System.out.println("lock info " + cache.printLockInfo());

      assertEquals(4, cache.getNumberOfLocksHeld());
      tx.commit();

   }


   public void testTxPutCommit() throws Exception, NotSupportedException {
      DummyTransactionManager mgr=DummyTransactionManager.getInstance();
      mgr.begin();
      tx=mgr.getTransaction();

      cache.put("/one/two/three", "key1", "val1");
      cache.put("/one/two/three/four", "key2", "val2");
      tx.commit();
      assertNotNull("Cache has node /one/two/three", cache.getKeys("/one/two/three"));
      assertNotNull("Loader has node /one/two/three", loader.get(Fqn.fromString("/one/two/three")));
      Set children=cache.getChildrenNames("/one");
      assertEquals("Cache has correct number of children", 1, children.size());
      children = loader.getChildrenNames(Fqn.fromString("/one"));
      assertEquals("Loader has correct number of children", 1, children.size());
      cache.remove("/");
   }

   public void testTxPutRollback() throws Exception, NotSupportedException {
      DummyTransactionManager mgr=DummyTransactionManager.getInstance();

      cache.remove("/one");
      addDelay();
      mgr.begin();
      tx=mgr.getTransaction();

      cache.put("/one/two/three", "key1", "val1");
      cache.put("/one/two/three/four", "key2", "val2");
      log.debug("NODE1 " + cache.get("/one/two/three").getData());
      tx.rollback();
      log.debug("NODE2 " + cache.get("/one/two/three"));
      assertEquals(null, cache.get("/one/two/three", "key1"));
      assertEquals(null, cache.get("/one/two/three/four", "key2"));
      addDelay();
      assertNull("Loader does not have node /one/two/three", loader.get(Fqn.fromString("/one/two/three")));
      assertEquals("Cache does not have node /one/two/three", null, cache.getKeys("/one/two/three"));
      Set children=cache.getChildrenNames("/one");
      assertEquals("Cache has no children under /one", null, children);
      children = loader.getChildrenNames(Fqn.fromString("/one"));
      assertEquals("Loader has no children under /one", null, children);
   }


   /**
    * Tests basic operations without a transaction.
    */
   public void testBasicOperations()
      throws Exception {

      doTestBasicOperations();
   }

   /**
    * Tests basic operations with a transaction.
    */
   public void testBasicOperationsTransactional()
      throws Exception {

      DummyTransactionManager mgr=DummyTransactionManager.getInstance();
      mgr.begin();
      tx=mgr.getTransaction();
      doTestBasicOperations();
      tx.commit();
   }

   /**
    * Tests basic operations.
    */
   private void doTestBasicOperations() throws Exception {

      /* One FQN only. */
      doPutTests(new Fqn("key"));
      doRemoveTests(new Fqn("key"));
      // assertEquals(0, loader.loadEntireState().length);

      /* Add three FQNs, middle FQN last. */
      doPutTests(new Fqn("key1"));
      doPutTests(new Fqn("key3"));
      doPutTests(new Fqn("key2"));
      assertEquals(4, loader.get(new Fqn("key1")).size());
      assertEquals(4, loader.get(new Fqn("key2")).size());
      assertEquals(4, loader.get(new Fqn("key3")).size());

      /* Remove middle FQN first, then the others. */
      doRemoveTests(new Fqn("key2"));
      doRemoveTests(new Fqn("key3"));
      doRemoveTests(new Fqn("key1"));
      assertNull(loader.get(new Fqn("key1")));
      assertNull(loader.get(new Fqn("key2")));
      assertNull(loader.get(new Fqn("key3")));
      // assertEquals(0, loader.loadEntireState().length);
   }

   /**
    * Do basic put tests for a given FQN.
    */
   private void doPutTests(Fqn fqn)
      throws Exception {

      assertTrue(!loader.exists(fqn));

      /* put(Fqn,Object,Object) and get(Fqn,Object) */
      Object oldVal;
      oldVal = loader.put(fqn, "one", "two");
      assertNull(oldVal);
      addDelay();
      oldVal = loader.put(fqn, "three", "four");
      assertNull(oldVal);
      addDelay();
      assertEquals("two", loader.get(fqn).get("one"));
      assertEquals("four", loader.get(fqn).get("three"));
      addDelay();
      oldVal = loader.put(fqn, "one", "xxx");
      assertEquals("two", oldVal);
      addDelay();
      oldVal = loader.put(fqn, "one", "two");
      assertEquals("xxx", oldVal);

      /* get(Fqn) */
      addDelay();
      Map map = loader.get(fqn);
      assertEquals(2, map.size());
      assertEquals("two", map.get("one"));
      assertEquals("four", map.get("three"));

      /* put(Fqn,Map) */
      map.put("five", "six");
      map.put("seven", "eight");
      loader.put(fqn, map);
      addDelay();
      assertEquals("six", loader.get(fqn).get("five"));
      assertEquals("eight", loader.get(fqn).get("seven"));
      assertEquals(map, loader.get(fqn));
      assertEquals(4, map.size());

      assertTrue(loader.exists(fqn));
   }

   /**
    * Do basic remove tests for a given FQN.
    */
   private void doRemoveTests(Fqn fqn)
      throws Exception {

      /* remove(Fqn,Object) */
      Object oldVal;
      oldVal = loader.remove(fqn, "one");
      assertEquals("two", oldVal);
      addDelay();
      oldVal = loader.remove(fqn, "five");
      assertEquals("six", oldVal);
      addDelay();
      assertNull(loader.get(fqn).get("one"));
      assertNull(loader.get(fqn).get("five"));
      assertEquals("four", loader.get(fqn).get("three"));
      assertEquals("eight", loader.get(fqn).get("seven"));
      Map map = loader.get(fqn);
      assertEquals(2, map.size());
      assertEquals("four", map.get("three"));
      assertEquals("eight", map.get("seven"));

      /* remove(Fqn) */
      assertTrue(loader.exists(fqn));
      loader.remove(fqn);
      addDelay();
      map = loader.get(fqn);
      assertNull("Should be null", map);
      assertTrue(!loader.exists(fqn));
   }

   /**
    * Tests creating implicit intermediate nodes when a leaf node is created,
    * and tests removing subtrees.
    */
   public void testMultiLevelTree()
      throws Exception {

      /* Create top level node implicitly. */
      assertTrue(!loader.exists(new Fqn("key0")));
      loader.put(Fqn.fromString("/key0/level1/level2"), null);
      addDelay();
      assertTrue(loader.exists(Fqn.fromString("/key0/level1/level2")));
      assertTrue(loader.exists(Fqn.fromString("/key0/level1")));
      assertTrue(loader.exists(new Fqn("key0")));

      /* Remove leaf, leaving implicitly created middle level. */
      loader.put(Fqn.fromString("/key0/x/y"), null);
      addDelay();
      assertTrue(loader.exists(Fqn.fromString("/key0/x/y")));
      assertTrue(loader.exists(Fqn.fromString("/key0/x")));
      loader.remove(Fqn.fromString("/key0/x/y"));
      addDelay();
      assertTrue(!loader.exists(Fqn.fromString("/key0/x/y")));
      assertTrue(loader.exists(Fqn.fromString("/key0/x")));

      /* Delete top level to delete everything. */
      loader.remove(new Fqn("key0"));
      addDelay();
      assertTrue(!loader.exists(new Fqn("key0")));
      assertTrue(!loader.exists(Fqn.fromString("/key0/level1/level2")));
      assertTrue(!loader.exists(Fqn.fromString("/key0/level1")));
      assertTrue(!loader.exists(Fqn.fromString("/key0/x")));

      /* Add three top level nodes as context. */
      loader.put(new Fqn("key1"), null);
      loader.put(new Fqn("key2"), null);
      loader.put(new Fqn("key3"), null);
      addDelay();
      assertTrue(loader.exists(new Fqn("key1")));
      assertTrue(loader.exists(new Fqn("key2")));
      assertTrue(loader.exists(new Fqn("key3")));

      /* Put /key3/level1/level2.  level1 should be implicitly created. */
      assertTrue(!loader.exists(Fqn.fromString("/key3/level1")));
      assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2")));
      loader.put(Fqn.fromString("/key3/level1/level2"), null);
      addDelay();
      assertTrue(loader.exists(Fqn.fromString("/key3/level1/level2")));
      assertTrue(loader.exists(Fqn.fromString("/key3/level1")));

      /* Context nodes should still be intact. */
      assertTrue(loader.exists(new Fqn("key1")));
      assertTrue(loader.exists(new Fqn("key2")));
      assertTrue(loader.exists(new Fqn("key3")));

      /* Remove middle level only. */
      loader.remove(Fqn.fromString("/key3/level1"));
      addDelay();
      assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2")));
      assertTrue(!loader.exists(Fqn.fromString("/key3/level1")));

      /* Context nodes should still be intact. */
      assertTrue(loader.exists(new Fqn("key1")));
      assertTrue(loader.exists(new Fqn("key2")));
      assertTrue(loader.exists(new Fqn("key3")));

      /* Delete first root, leaving other roots. */
      loader.remove(new Fqn("key1"));
      addDelay();
      assertTrue(!loader.exists(new Fqn("key1")));
      assertTrue(loader.exists(new Fqn("key2")));
      assertTrue(loader.exists(new Fqn("key3")));

      /* Delete last root, leaving other roots. */
      loader.remove(new Fqn("key3"));
      addDelay();
      assertTrue(loader.exists(new Fqn("key2")));
      assertTrue(!loader.exists(new Fqn("key3")));

      /* Delete final root, leaving none. */
      loader.remove(new Fqn("key2"));
      addDelay();
      assertTrue(!loader.exists(new Fqn("key0")));
      assertTrue(!loader.exists(new Fqn("key1")));
      assertTrue(!loader.exists(new Fqn("key2")));
      assertTrue(!loader.exists(new Fqn("key3")));

      /* Repeat all tests above using put(Fqn,Object,Object) and get(Fqn) */

      assertNull(loader.get(new Fqn("key0")));
      loader.put(Fqn.fromString("/key0/level1/level2"), "a", "b");
      addDelay();
      assertNotNull(loader.get(Fqn.fromString("/key0/level1/level2")));
      assertNotNull(loader.get(Fqn.fromString("/key0/level1")));
      assertEquals(0, loader.get(Fqn.fromString("/key0/level1")).size());
      assertEquals(0, loader.get(new Fqn("key0")).size());

      loader.put(Fqn.fromString("/key0/x/y"), "a", "b");
      addDelay();
      assertNotNull(loader.get(Fqn.fromString("/key0/x/y")));
      assertNotNull(loader.get(Fqn.fromString("/key0/x")));
      assertEquals(0, loader.get(Fqn.fromString("/key0/x")).size());
      loader.remove(Fqn.fromString("/key0/x/y"));
      addDelay();
      assertNull(loader.get(Fqn.fromString("/key0/x/y")));
      assertNotNull(loader.get(Fqn.fromString("/key0/x")));
      assertEquals(0, loader.get(Fqn.fromString("/key0/x")).size());

      loader.remove(new Fqn("key0"));
      addDelay();
      assertNull(loader.get(new Fqn("key0")));
      assertNull(loader.get(Fqn.fromString("/key0/level1/level2")));
      assertNull(loader.get(Fqn.fromString("/key0/level1")));
      assertNull(loader.get(Fqn.fromString("/key0/x")));

      loader.put(new Fqn("key1"), "a", "b");
      loader.put(new Fqn("key2"), "a", "b");
      loader.put(new Fqn("key3"), "a", "b");
      addDelay();
      assertNotNull(loader.get(new Fqn("key1")));
      assertNotNull(loader.get(new Fqn("key2")));
      assertNotNull(loader.get(new Fqn("key3")));

      assertNull(loader.get(Fqn.fromString("/key3/level1")));
      assertNull(loader.get(Fqn.fromString("/key3/level1/level2")));
      loader.put(Fqn.fromString("/key3/level1/level2"), "a", "b");
      addDelay();
      assertNotNull(loader.get(Fqn.fromString("/key3/level1/level2")));
      assertNotNull(loader.get(Fqn.fromString("/key3/level1")));
      assertEquals(0, loader.get(Fqn.fromString("/key3/level1")).size());

      assertNotNull(loader.get(new Fqn("key1")));
      assertNotNull(loader.get(new Fqn("key2")));
      assertNotNull(loader.get(new Fqn("key3")));

      loader.remove(Fqn.fromString("/key3/level1"));
      addDelay();
      assertNull(loader.get(Fqn.fromString("/key3/level1/level2")));
      assertNull(loader.get(Fqn.fromString("/key3/level1")));

      assertNotNull(loader.get(new Fqn("key1")));
      assertNotNull(loader.get(new Fqn("key2")));
      assertNotNull(loader.get(new Fqn("key3")));

      loader.remove(new Fqn("key1"));
      addDelay();
      assertNull(loader.get(new Fqn("key1")));
      assertNotNull(loader.get(new Fqn("key2")));
      assertNotNull(loader.get(new Fqn("key3")));

      loader.remove(new Fqn("key3"));
      addDelay();
      assertNotNull(loader.get(new Fqn("key2")));
      assertNull(loader.get(new Fqn("key3")));

      loader.remove(new Fqn("key2"));
      addDelay();
      assertNull(loader.get(new Fqn("key0")));
      assertNull(loader.get(new Fqn("key1")));
      assertNull(loader.get(new Fqn("key2")));
      assertNull(loader.get(new Fqn("key3")));
   }

   /**
    * Tests the getChildrenNames() method.
    */
   public void testGetChildrenNames()
      throws Exception {

      checkChildren(new Fqn(), null);
      checkChildren(Fqn.fromString("/key0"), null);

      loader.put(Fqn.fromString("/key0"), null);
      addDelay();
      checkChildren(new Fqn(), new String[] { "key0" });

      loader.put(Fqn.fromString("/key1/x"), null);
      addDelay();
      checkChildren(new Fqn(), new String[] { "key0", "key1" });
      checkChildren(Fqn.fromString("/key1"), new String[] { "x" });

      loader.remove(Fqn.fromString("/key1/x"));
      addDelay();
      checkChildren(new Fqn(), new String[] { "key0", "key1" });
      checkChildren(Fqn.fromString("/key0"), null);
      checkChildren(Fqn.fromString("/key1"), null);

      loader.put(Fqn.fromString("/key0/a"), null);
      loader.put(Fqn.fromString("/key0/ab"), null);
      loader.put(Fqn.fromString("/key0/abc"), null);
      addDelay();
      checkChildren(Fqn.fromString("/key0"),
                    new String[] { "a", "ab", "abc" });

      loader.put(Fqn.fromString("/key0/xxx"), null);
      loader.put(Fqn.fromString("/key0/xx"), null);
      loader.put(Fqn.fromString("/key0/x"), null);
      addDelay();
      checkChildren(Fqn.fromString("/key0"),
                    new String[] { "a", "ab", "abc", "x", "xx", "xxx" });

      loader.put(Fqn.fromString("/key0/a/1"), null);
      loader.put(Fqn.fromString("/key0/a/2"), null);
      loader.put(Fqn.fromString("/key0/a/2/1"), null);
      addDelay();
      checkChildren(Fqn.fromString("/key0/a/2"), new String[] { "1" });
      checkChildren(Fqn.fromString("/key0/a"), new String[] { "1", "2" });
      checkChildren(Fqn.fromString("/key0"),
                    new String[] { "a", "ab", "abc", "x", "xx", "xxx" });
//
//      loader.put(Fqn.fromString("/key0/\u0000"), null);
//      loader.put(Fqn.fromString("/key0/\u0001"), null);
//      checkChildren(Fqn.fromString("/key0"),
//                    new String[] { "a", "ab", "abc", "x", "xx", "xxx",
//                                   "\u0000", "\u0001"});
//
//      loader.put(Fqn.fromString("/\u0001"), null);
//      checkChildren(new Fqn(), new String[] { "key0", "key1", "\u0001" });
//
//      loader.put(Fqn.fromString("/\u0001/\u0001"), null);
//      checkChildren(Fqn.fromString("/\u0001"), new String[] { "\u0001" });
//
//      loader.put(Fqn.fromString("/\u0001/\uFFFF"), null);
//      checkChildren(Fqn.fromString("/\u0001"),
//                    new String[] { "\u0001", "\uFFFF" });
//
//      loader.put(Fqn.fromString("/\u0001/\uFFFF/\u0001"), null);
//      checkChildren(Fqn.fromString("/\u0001/\uFFFF"),
//                    new String[] { "\u0001" });
   }

   /**
    * Checks that the given list of children part names is returned.
    */
   private void checkChildren(Fqn fqn, String[] names)
      throws Exception {

      Set set = loader.getChildrenNames(fqn);
      if (names != null) {
         assertEquals(names.length, set.size());
         for (int i = 0; i < names.length; i += 1) {
            assertTrue(set.contains(names[i]));
         }
      } else {
         assertNull(set);
      }
   }

   /**
    * Tests basic operations without a transaction.
    */
   public void testModifications()
      throws Exception {

      doTestModifications();
   }

   /**
    * Tests basic operations with a transaction.
    */
   public void testModificationsTransactional()
      throws Exception {

      DummyTransactionManager mgr=DummyTransactionManager.getInstance();
      mgr.begin();
      tx=mgr.getTransaction();
      doTestModifications();
      tx.commit();
   }

   /**
    * Tests modifications.
    */
   private void doTestModifications()
      throws Exception {

      /* PUT_KEY_VALUE, PUT_DATA */
      List list = createUpdates();
      loader.put(list);
      addDelay();
      checkModifications(list);

      /* REMOVE_KEY_VALUE */
      list = new ArrayList();
      Modification mod = new Modification();
      mod.setType(Modification.REMOVE_KEY_VALUE);
      mod.setFqn(FQN);
      mod.setKey("one");
      list.add(mod);
      loader.put(list);
      addDelay();
      checkModifications(list);

      /* REMOVE_NODE */
      list = new ArrayList();
      mod = new Modification();
      mod.setType(Modification.REMOVE_NODE);
      mod.setFqn(FQN);
      list.add(mod);
      loader.put(list);
      addDelay();
      checkModifications(list);
      assertNull(loader.get(FQN));

      /* REMOVE_DATA */
      loader.put(FQN, "one", "two");
      list = new ArrayList();
      mod = new Modification();
      mod.setType(Modification.REMOVE_DATA);
      mod.setFqn(FQN);
      list.add(mod);
      loader.put(list);
      addDelay();
      checkModifications(list);
   }

   /**
    * Tests a one-phase transaction.
    */
   public void testOnePhaseTransaction()
      throws Exception {
      List mods = createUpdates();
      loader.prepare(null, mods, true);
      checkModifications(mods);
   }

   /**
    * Tests a two-phase transaction.
    */
   public void testTwoPhaseTransaction()
      throws Exception {

      Object txnKey = new Object();
      List mods = createUpdates();
      loader.prepare(txnKey, mods, false);
      loader.commit(txnKey);
      addDelay();
      checkModifications(mods);
   }

   /**
    * Tests rollback of a two-phase transaction.
    */
   public void testTransactionRollback() throws Exception {
      loader.remove(Fqn.fromString("/"));
      int num=0;
      try {
         num=loader.loadEntireState().length;
      }
      catch(UnsupportedOperationException ex) {
         System.out.println("caught unsupported operation exception that's okay: " + ex);
         return;
      }

      Object txnKey = new Object();
      List mods = createUpdates();
      loader.prepare(txnKey, mods, false);
      loader.rollback(txnKey);
      assertEquals(num, loader.loadEntireState().length);
   }

   /**
    * Tests rollback of a two-phase transaction that is mediated by the cache.
    */
   public void testIntegratedTransactionRollback() throws Exception {
      loader.remove(Fqn.fromString("/"));
      int num=0;
      try {
         num=loader.loadEntireState().length;
      }
      catch(UnsupportedOperationException ex) {
         System.out.println("caught unsupported operation exception that's okay: " + ex);
         return;
      }

      Object txnKey = new Object();
      List mods = createUpdates();
      loader.prepare(txnKey, mods, false);
      loader.rollback(txnKey);
      assertEquals(num, loader.loadEntireState().length);
   }

   /**
    * Creates a set of update (PUT_KEY_VALUE, PUT_DATA) modifications.
    */
   private List createUpdates() {

      List list = new ArrayList();

      Modification mod = new Modification();
      mod.setType(Modification.PUT_KEY_VALUE);
      mod.setFqn(FQN);
      mod.setKey("one");
      mod.setValue("two");
      list.add(mod);

      mod = new Modification();
      mod.setType(Modification.PUT_KEY_VALUE);
      mod.setFqn(FQN);
      mod.setKey("three");
      mod.setValue("four");
      list.add(mod);

      Map map = new HashMap();
      map.put("five", "six");
      map.put("seven", "eight");
      mod = new Modification();
      mod.setType(Modification.PUT_DATA);
      mod.setFqn(FQN);
      mod.setData(map);
      list.add(mod);

      return list;
   }

   /**
    * Checks that a list of modifications was applied.
    */
   private void checkModifications(List list)
      throws Exception {

      for (int i = 0; i < list.size(); i += 1) {
         Modification mod = (Modification) list.get(i);
         Fqn fqn = mod.getFqn();
         switch (mod.getType()) {
         case Modification.PUT_KEY_VALUE:
            assertEquals(mod.getValue(), loader.get(fqn).get(mod.getKey()));
            break;
         case Modification.PUT_DATA:
            Map map = mod.getData();
            for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
               Object key = iter.next();
               assertEquals(map.get(key), loader.get(fqn).get(key));
            }
            break;
         case Modification.REMOVE_KEY_VALUE:
            assertNull(loader.get(fqn).get(mod.getKey()));
            break;
         case Modification.REMOVE_DATA:
            map = loader.get(fqn);
            assertNotNull(map);
            assertEquals(0, map.size());
            break;
         case Modification.REMOVE_NODE:
            assertNull(loader.get(fqn));
            break;
         default:
            fail("unknown type: " + mod);
            break;
         }
      }
   }

   /**
    * Tests that null keys and values work as for a standard Java Map.
    */
   public void testNullKeysAndValues()
      throws Exception {

      loader.put(FQN, null, "x");
      addDelay();
      assertEquals("x", loader.get(FQN).get(null));
      Map map = loader.get(FQN);
      assertEquals(1, map.size());
      assertEquals("x", map.get(null));

      loader.put(FQN, "y", null);
      addDelay();
      assertNull(loader.get(FQN).get("y"));
      map = loader.get(FQN);
      assertEquals(2, map.size());
      assertEquals("x", map.get(null));
      assertNull(map.get("y"));

      loader.remove(FQN, null);
      addDelay();
      assertNull(loader.get(FQN).get(null));
      assertEquals(1, loader.get(FQN).size());

      loader.remove(FQN, "y");
      addDelay();
      assertNotNull(loader.get(FQN));
      assertEquals(0, loader.get(FQN).size());

      map = new HashMap();
      map.put(null, null);
      loader.put(FQN, map);
      addDelay();
      assertEquals(map, loader.get(FQN));

      loader.remove(FQN);
      addDelay();
      assertNull(loader.get(FQN));

      map = new HashMap();
      map.put("xyz", null);
      map.put(null, "abc");
      loader.put(FQN, map);
      addDelay();
      assertEquals(map, loader.get(FQN));

      loader.remove(FQN);
      addDelay();
      assertNull(loader.get(FQN));
   }

   /**
    * Test non-default database name.
    */
   public void testDatabaseName()
      throws Exception {

      loader.put(FQN, "one", "two");
      addDelay();
      assertEquals("two", loader.get(FQN).get("one"));
   }

   /**
    * Test load/store state.
    */
   public void testLoadAndStore()
      throws Exception {

      // Empty state
      loader.remove(Fqn.fromString("/"));

      // Use a complex object to ensure that the class catalog is used.
      Complex c1 = new Complex();
      Complex c2 = new Complex(c1);

      // Add objects
      loader.put(FQN, new Integer(1), c1);
      loader.put(FQN, new Integer(2), c2);
      addDelay();
      assertEquals(c1, loader.get(FQN).get(new Integer(1)));
      assertEquals(c2, loader.get(FQN).get(new Integer(2)));
      assertEquals(2, loader.get(FQN).size());

      // Save state
      byte[] state=null;
      try {
         state=loader.loadEntireState();
         assertTrue(state.length > 0);
      }
      catch(UnsupportedOperationException ex) {
         System.out.println("caught unsupported operation exception (this is expected): " + ex);
      }


      /* Restore state. */
      try {
         loader.storeEntireState(state);
      }
      catch(UnsupportedOperationException ex) {
         System.out.println("caught unsupported operation exception (this is expected): " + ex);
      }

      addDelay();
      assertEquals(c1, loader.get(FQN).get(new Integer(1)));
      assertEquals(c2, loader.get(FQN).get(new Integer(2)));
      assertEquals(2, loader.get(FQN).size());
   }

   /**
    * Complex object whose class description is stored in the class catalog.
    */
   public static class Complex implements Serializable {

      Complex nested;

      Complex() {
         this(null);
      }

      Complex(Complex nested) {
         this.nested = nested;
      }

      public boolean equals(Object o) {
         if (!(o instanceof Complex))
            return false;
         Complex x = (Complex) o;
         return (nested != null) ? nested.equals(x.nested)
                                 : (x.nested == null);
      }

      public int hashCode() {
         if(nested == null)
            return super.hashCode();
         else
            return 13 + nested.hashCode();
      }
   }

    public void testRemoveInTransactionCommit() throws Exception
    {
        Fqn fqn = Fqn.fromString("/a/b");
        loader.remove(fqn);
        String key = "key";
        String value = "value";
        cache.put(fqn, key, value);
        loader = cache.getCacheLoader();

        assertEquals(value, cache.get(fqn, key));
        assertEquals(value, loader.get(fqn).get(key));

        cache.getTransactionManager().begin();

        cache.remove(fqn);

        cache.get(fqn); // forces the node to be loaded from cache loader again
        cache.getTransactionManager().commit();

        log.debug("expect the cache and the loader to be null here.");
        assertEquals(null, cache.get(fqn, key));
        assertEquals(null, loader.get(fqn));
    }


    public void testRemoveInTransactionRollback() throws Exception
    {
        Fqn fqn = Fqn.fromString("/a/b");
        loader.remove(fqn);
        String key = "key";
        String value = "value";

        cache.put(fqn, key, value);
        loader = cache.getCacheLoader();

        assertEquals(value, cache.get(fqn, key));
        assertEquals(value, loader.get(fqn).get(key));

        cache.getTransactionManager().begin();

        cache.remove(fqn);
        cache.get(fqn); // forces a reload from cloader
        cache.getTransactionManager().rollback();

        assertEquals(value, cache.get(fqn, key));
        assertEquals(value, loader.get(fqn).get(key));
    }


   /** See http://jira.jboss.com/jira/browse/JBCACHE-352 */
   public void testRemoveAndGetInTransaction() throws Exception
   {
       Fqn fqn = new Fqn("/a/b");
       String key = "key";
       String value = "value";

       cache.put(fqn, key, value);
       CacheLoader loader = cache.getCacheLoader();

       assertEquals(value, cache.get(fqn, key));
       assertEquals(value, loader.get(fqn).get(key));

       cache.getTransactionManager().begin();

       cache.remove(fqn);
       assertNull("Expecting a null since I have already called a remove() - see JBCACHE-352", cache.get(fqn, key));
       cache.getTransactionManager().rollback();

       assertEquals(value, cache.get(fqn, key));
       assertEquals(value, loader.get(fqn).get(key));
   }




}
