/*
 *
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.cache.lock;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.jboss.cache.TreeCache;
import org.jboss.cache.transaction.DummyTransactionManager;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;
import java.util.Properties;

/**
 * Tests upgrade locks from read -> write
 *
 * @author Bela Ban
 * @version $Id: UpgradeLockTest.java 3090 2006-12-05 15:27:52Z msurtani $
 */
public class UpgradeLockTest extends TestCase {
   TreeCache cache=null;
   UserTransaction tx=null;
   Log log;
   Properties p=null;
   String old_factory=null;
   final String FACTORY="org.jboss.cache.transaction.DummyContextFactory";
   final String NODE1="/test";
   final String NODE2="/my/test";
   final String KEY="key";
   final String VAL1="val1";
   final String VAL2="val2";


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

   public void setUp() throws Exception {
      super.setUp();
      old_factory=System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
      System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
      DummyTransactionManager.getInstance();
      if(p == null) {
         p=new Properties();
         p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
      }
      tx=(UserTransaction)new InitialContext(p).lookup("UserTransaction");
   }

   public void tearDown() throws Exception {
      super.tearDown();
      if(cache != null)
         cache.stopService();

      // BW. kind of a hack to destroy jndi binding and thread local tx before next run.
      DummyTransactionManager.destroy();
      if(old_factory != null) {
         System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
         old_factory=null;
      }

      if(tx != null) {
         try {
            tx.rollback();
         }
         catch(Throwable t) {
         }
         tx=null;
      }
   }

   TreeCache createCache(IsolationLevel level) throws Exception {
      TreeCache c=new TreeCache("test", null, 10000);
      c.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
      c.setLockAcquisitionTimeout(500);
      c.setIsolationLevel(level);
      c.createService();
      c.startService();
      return c;
   }


   public void testUpgradeWithNone() throws Exception {
      runTestWithIsolationLevel(IsolationLevel.NONE);
   }


   public void testUpgradeWithReadUncommitted() throws Exception {
      runTestWithIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
   }

   public void testUpgradeWithReadCommitted() throws Exception {
      runTestWithIsolationLevel(IsolationLevel.READ_COMMITTED);
   }

   public void testUpgradeWithRepeatableRead() throws Exception {
      runTestWithIsolationLevel(IsolationLevel.REPEATABLE_READ);
   }

   public void testUpgradeWithSerializable() throws Exception {
      runTestWithIsolationLevel(IsolationLevel.SERIALIZABLE);
   }

   public void testIsolationLevelSerializable() throws Exception {
      _testIsolationLevel(IsolationLevel.SERIALIZABLE);
   }

   public void testIsolationLevelNone() throws Exception {
      _testIsolationLevel(IsolationLevel.NONE);
   }


   void _testIsolationLevel(IsolationLevel l) throws Exception {
      cache=createCache(l);
      tx.begin();

      int expected_num_locks=l == IsolationLevel.NONE? 0 : 2;

      cache.put(NODE1, null);
      assertEquals(expected_num_locks, cache.getNumberOfLocksHeld());

      cache.put(NODE1, null);
      assertEquals(expected_num_locks, cache.getNumberOfLocksHeld());

      tx.rollback();
      assertEquals(0, cache.getNumberOfLocksHeld());
   }



   void runTestWithIsolationLevel(IsolationLevel level) throws Exception {
      cache=createCache(level);
      // add initial values outside of TX
      cache.put(NODE1, KEY, VAL1);
      cache.put(NODE2, KEY, VAL1);

      tx.begin();
      try {
         assertEquals(VAL1, cache.get(NODE1, KEY));
         assertEquals(VAL1, cache.get(NODE2, KEY));

         cache.put(NODE1, KEY, VAL2);  // causes read lock to upgrade to r/w lock
         cache.put(NODE2, KEY, VAL2);  // causes read lock to upgrade to r/w lock
         assertEquals(VAL2, cache.get(NODE1, KEY));
         assertEquals(VAL2, cache.get(NODE2, KEY));
         tx.commit();
      }
      catch(Throwable t) {
         if(tx != null)
            tx.rollback();
      }
      assertEquals(VAL2, cache.get(NODE1, KEY));
      assertEquals(VAL2, cache.get(NODE2, KEY));
   }


   void log(String msg) {
      log.info("-- [" + Thread.currentThread() + "]: " + msg);
   }


   public static Test suite() throws Exception {
      // return getDeploySetup(TxUnitTestCase.class, "cachetest.jar");
      return new TestSuite(UpgradeLockTest.class);
   }

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


}
