package org.jboss.cache;

import junit.framework.TestCase;

import javax.transaction.TransactionManager;

/**
 * Tests removing deep, nonexistent nodes
 *
 * @author Manik Surtani (<a href="mailto:manik@jboss.org">manik@jboss.org</a>)
 */
public class RemoveNodeTest extends TestCase
{
   protected boolean optimistic = false;
   protected TreeCache cache;

   protected void setUp() throws Exception
   {
      cache = new TreeCache();
      if (optimistic) cache.setNodeLockingScheme("OPTIMISTIC");
      cache.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
      cache.start();
   }

   protected void tearDown()
   {
      cache.stop();
   }

   public void testPhantomStructuralNodesOnRemove() throws Exception
   {
      assert cache.peek(Fqn.fromString("/a/b/c")) == null;
      cache.remove("/a/b/c");
      assert cache.peek(Fqn.fromString("/a/b/c")) == null;
      assert cache.peek(Fqn.fromString("/a/b")) == null;
      assert cache.peek(Fqn.fromString("/a")) == null;

      System.out.println("Cache: " + cache.printDetails());
   }

   public void testPhantomStructuralNodesOnRemoveTransactional() throws Exception
   {
      TransactionManager tm = cache.getTransactionManager();
      assert cache.peek(Fqn.fromString("/a/b/c")) == null;
      tm.begin();
      cache.remove("/a/b/c");
      tm.commit();
      assert cache.peek(Fqn.fromString("/a/b/c")) == null;
      assert cache.peek(Fqn.fromString("/a/b")) == null;
      assert cache.peek(Fqn.fromString("/a")) == null;

      System.out.println("Cache: " + cache.printDetails());
   }
}
