package org.jboss.cache;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.cache.TreeCache;

/**
 * Tests restart (stop-destroy-create-start) of TreeCache
 * @author Bela Ban
 * @version $Id: TreeNodeTest.java,v 1.1 2005/09/02 00:33:06 bwang Exp $
 */
public class TreeNodeTest extends TestCase {
   TreeCache cache ;

   protected void setUp() throws Exception
   {
      super.setUp();
      cache = new TreeCache();
      startCache(cache);
   }

   protected void tearDown() throws Exception
   {
      super.tearDown();
      stopCache(cache);
   }

   public void testChildExist() throws Exception
   {
      Object key = new Integer(1);
      cache.put("/a/b/c", key, "test");
      DataNode node;
      node = cache.get("/a/b");
      assertTrue(node.hasChildren());
      assertTrue(node.childExists("c"));

      Fqn fqn = Fqn.fromString("/e/f");
      cache.put(fqn, "1", "1");
      node = cache.get("/e");
      assertTrue(node.hasChildren());
      assertTrue(node.childExists("f"));

   }

   /**
   public void testRelocate() throws Exception
   {
      Object key = new Integer(1);
      cache.put("/a/b/c", key, "test");
      cache.put("/a/b/c/d", key, "test1");
      cache.put("/relocate", "1", "1");

      DataNode node = cache.get("/a/b/c");
      DataNode newParentNode = cache.get("/relocate");
      assertFalse(newParentNode.hasChildren());

      Fqn newFqn = Fqn.fromString("/relocate/c");

      cache.put("/relocate/c", "1", "1");

      node.relocate(newParentNode, newFqn);
      assertNotNull("/relocat/c should not be null", cache.get(newFqn));

      node = cache.get("/a/b");
      assertTrue(!node.hasChildren());
      assertTrue(!node.childExists("c"));

      node = cache.get("/relocate");
      assertTrue(node.hasChildren());
      assertTrue(node.childExists(newFqn.get(1)));

      node = cache.get(newFqn);
      assertEquals(newFqn, node.getFqn());
      assertTrue(node.hasChildren());
      assertTrue(node.childExists("d"));

      node = cache.get("/relocate/c/d");
      assertNotNull(node);
      assertEquals("test1", cache.get("/relocate/c/d", key));

      assertNull(cache.get("/relocate/c/","1"));
   }
    */

   void startCache(TreeCache c) throws Exception {
      c.createService();
      c.startService();
   }

   void stopCache(TreeCache c) {
      c.stopService();
      c.destroyService();
   }

   public static Test suite() {
      return new TestSuite(TreeNodeTest.class);
   }

   public static void main(String[] args) {
      junit.textui.TestRunner.run(suite());
   }

}
