/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache.options;

import junit.framework.TestCase;
import org.jboss.cache.TreeCache;
import org.jboss.cache.DummyTransactionManagerLookup;
import org.jboss.cache.Fqn;
import org.jboss.cache.config.Option;

import javax.transaction.TransactionManager;
import java.util.Map;
import java.util.HashMap;

/**
 * Tests the suppression of locking nodes
 *
 * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 */
public class SuppressLockingTest extends TestCase
{
    private Option suppressLocks = new Option();
   private Fqn fqn = Fqn.fromString("/blah");
   private Fqn fqn1 = Fqn.fromString("/blah/1");

    {
        suppressLocks.setSuppressLocking(true);
    }

   public void testSuppressionOfWriteLocks() throws Exception
   {
       TreeCache cache = new TreeCache();
       try
       {
           cache.setCacheMode(TreeCache.LOCAL);
           cache.setTransactionManagerLookup(new DummyTransactionManagerLookup());
           cache.startService();

           TransactionManager m = cache.getTransactionManager();

           m.begin();
           cache.put(fqn, "x", "1");
           assertEquals(1, cache.getNumberOfLocksHeld());
           m.commit();
           assertEquals(0, cache.getNumberOfLocksHeld());

           cache.remove(fqn);

           m.begin();
           cache.put(fqn, "x", "2", suppressLocks);
           assertEquals(0, cache.getNumberOfLocksHeld());
           m.commit();
           assertEquals(0, cache.getNumberOfLocksHeld());

           // test normal operation again
           cache.remove(fqn);

           m.begin();
           cache.put(fqn, "x", "3");
           assertEquals(1, cache.getNumberOfLocksHeld());
           m.commit();
           assertEquals(0, cache.getNumberOfLocksHeld());

       }
       finally
       {
           cache.stopService();
       }
   }

   /**
    * This one fails now.
    * @throws Exception
    */
   public void testSuppressionOf2WriteLocks() throws Exception
   {
       TreeCache cache = new TreeCache();
       try
       {
           cache.setCacheMode(TreeCache.LOCAL);
           cache.setTransactionManagerLookup(new DummyTransactionManagerLookup());
           cache.startService();

           TransactionManager m = cache.getTransactionManager();

           m.begin();
           cache.put(fqn, "x", "1");
           assertEquals(1, cache.getNumberOfLocksHeld());
           m.commit();
           assertEquals(0, cache.getNumberOfLocksHeld());

           cache.remove(fqn);

          m.begin();
         cache.put(fqn, "x", "2", suppressLocks);
         cache.put(fqn1, "y", "3", suppressLocks);
          assertEquals(0, cache.getNumberOfLocksHeld());
          m.commit();
          assertEquals(0, cache.getNumberOfLocksHeld());

          Map map = new HashMap();
          map.put("x", "1");
          m.begin();
         cache.put(fqn, map, suppressLocks);
         cache.put(fqn1, map, suppressLocks);
          assertEquals(0, cache.getNumberOfLocksHeld());
          m.commit();
          assertEquals(0, cache.getNumberOfLocksHeld());

           // test normal operation again
           cache.remove(fqn);

           m.begin();
           cache.put(fqn, "x", "3");
           assertEquals(1, cache.getNumberOfLocksHeld());
           m.commit();
           assertEquals(0, cache.getNumberOfLocksHeld());

       }
       finally
       {
           cache.stopService();
       }
   }

    public void testSuppressionOfReadLocks() throws Exception
    {
        TreeCache cache = new TreeCache();
        try
        {
            cache.setCacheMode(TreeCache.LOCAL);
            cache.setTransactionManagerLookup(new DummyTransactionManagerLookup());
            cache.startService();

            TransactionManager m = cache.getTransactionManager();

            cache.put(fqn, "x", "y");

            m.begin();
            cache.get(fqn, "x");
            assertEquals(1, cache.getNumberOfLocksHeld());
            m.commit();
            assertEquals(0, cache.getNumberOfLocksHeld());

            m.begin();
            cache.get(fqn, "x", suppressLocks);
            assertEquals(0, cache.getNumberOfLocksHeld());
            m.commit();
            assertEquals(0, cache.getNumberOfLocksHeld());

            // test normal operation again

            m.begin();
            cache.get(fqn, "x");
            assertEquals(1, cache.getNumberOfLocksHeld());
            m.commit();
            assertEquals(0, cache.getNumberOfLocksHeld());

        }
        finally
        {
            cache.stopService();
        }
    }

}
