package org.jboss.cache.transaction;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.cache.DummyTransactionManagerLookup;
import org.jboss.cache.Fqn;
import org.jboss.cache.TreeCache;
import org.jboss.cache.lock.IsolationLevel;
import org.jboss.cache.transaction.DummyTransactionManager;

import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;

/**
 * Tests whether modifications within callbacks (TreeCacheListener) are handled correctly
 * @author Bela Ban
 * @version $Id: IsolationLevelNoneTest.java,v 1.1 2005/07/08 05:58:10 msurtani Exp $
 */
public class IsolationLevelNoneTest extends TestCase {
   TreeCache cache=null;
   final Fqn FQN=Fqn.fromString("/a/b/c");
   final String KEY="key";
   final String VALUE="value";
   Transaction tx;



   protected void setUp() throws Exception {
      super.setUp();
   }

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



   public void testWithoutTransactions() throws Exception {
      cache=new TreeCache();
      cache.setCacheMode(TreeCache.LOCAL);
      cache.setIsolationLevel(IsolationLevel.NONE);
      cache.startService();
      cache.put(FQN, KEY, VALUE);
      cache.put(FQN + "/d", KEY, VALUE);
      assertTrue(cache.exists(FQN, KEY));
      assertEquals(VALUE, cache.get(FQN, KEY));
      System.out.println("cache: " + cache.toString(true) + ", locks: " + cache.printLockInfo());
      assertEquals(0, cache.getNumberOfLocksHeld());
   }

   public void testWithTransactions() throws Exception {
      cache=new TreeCache();
      cache.setCacheMode(TreeCache.LOCAL);
      cache.setIsolationLevel(IsolationLevel.NONE);
      cache.setTransactionManagerLookup(new DummyTransactionManagerLookup());
      cache.startService();
      tx=startTransaction();
      cache.put(FQN, KEY, VALUE);
      cache.put(FQN + "/d", KEY, VALUE);
      assertTrue(cache.exists(FQN, KEY));
      assertEquals(VALUE, cache.get(FQN, KEY));
      System.out.println("cache: " + cache.toString(true) + ", locks: " + cache.printLockInfo());
      assertEquals(0, cache.getNumberOfLocksHeld());
      tx.commit();
   }


   public void testWithTransactionsRepeatableRead() throws Exception {
      cache=new TreeCache();
      cache.setCacheMode(TreeCache.LOCAL);
      cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
      cache.setTransactionManagerLookup(new DummyTransactionManagerLookup());
      cache.startService();
      tx=startTransaction();
      cache.put(FQN, KEY, VALUE);
      cache.put(FQN + "/d", KEY, VALUE);
      assertTrue(cache.exists(FQN, KEY));
      assertEquals(VALUE, cache.get(FQN, KEY));
      System.out.println("cache: " + cache.toString(true) + ", locks: " + cache.printLockInfo());
      assertEquals(4, cache.getNumberOfLocksHeld());
      tx.commit();
   }

   private Transaction startTransaction() throws SystemException, NotSupportedException {
      DummyTransactionManager mgr=DummyTransactionManager.getInstance();
      mgr.begin();
      return mgr.getTransaction();
   }

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



}
