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 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 3100 2006-12-06 11:44:22Z msurtani $
 */
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(5, 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);
   }



}
