package org.jboss.cache.optimistic;

import org.jboss.cache.Fqn;
import org.jboss.cache.TreeCache;

import javax.transaction.Transaction;
import javax.transaction.TransactionManager;

/**
 * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
 */
public class VersioningOnReadTest extends AbstractOptimisticTestCase
{
   TreeCache cache;
   Fqn fqn = Fqn.fromString("/a");
   TransactionManager tm;

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

   protected void setUp() throws Exception
   {
      super.setUp();
      cache = createCache();
      tm = cache.getTransactionManager();
   }

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

   public void testUpdateOnWrite() throws Exception
   {
      cache.put(fqn, "k", "v");

      assertEquals("v", cache.get(fqn, "k"));

      // now start a tx to mod the node
      tm.begin();
      cache.put(fqn, "k", "v2");

      // suspend the tx
      Transaction tx = tm.suspend();

      // now modify the node
      cache.put(fqn, "k", "v3");

      // resume the tx
      tm.resume(tx);

      try
      {
         tm.commit();
         fail("Should have failed with a data version mismatch");
      }
      catch (Exception e)
      {
         // do nothing
      }
   }

   public void testUpdateOnRemove() throws Exception
   {
      cache.put(fqn, "k", "v");

      assertEquals("v", cache.get(fqn, "k"));

      // now start a tx to mod the node
      tm.begin();
      cache.remove(fqn, "k");

      // suspend the tx
      Transaction tx = tm.suspend();

      // now modify the node
      cache.put(fqn, "k", "v3");

      // resume the tx
      tm.resume(tx);

      try
      {
         tm.commit();
         fail("Should have failed with a data version mismatch");
      }
      catch (Exception e)
      {
         // do nothing
      }
   }

   public void testUpdateOnRemoveNode() throws Exception
   {
      cache.put(fqn, "k", "v");

      assertEquals("v", cache.get(fqn, "k"));

      // now start a tx to mod the node
      tm.begin();
      cache.remove(fqn);

      // suspend the tx
      Transaction tx = tm.suspend();

      // now modify the node
      cache.put(fqn, "k", "v3");

      // resume the tx
      tm.resume(tx);

      try
      {
         tm.commit();
         fail("Should have failed with a data version mismatch");
      }
      catch (Exception e)
      {
         // do nothing
      }
   }


   public void testUpdateOnRead() throws Exception
   {
      cache.put(fqn, "k", "v");

      assertEquals("v", cache.get(fqn, "k"));

      // now start a tx to mod the node
      tm.begin();
      cache.get(fqn, "k");

      // suspend the tx
      Transaction tx = tm.suspend();

      // now modify the node
      cache.put(fqn, "k", "v3");

      // resume the tx
      tm.resume(tx);

      // now put some other stuff elsewhere
      cache.put(Fqn.fromString("/b"), "k", "v");

      // this should succeed since there is no contention on writing to /a
      tm.commit();
   }

}
