/*
 * 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.Fqn;
import org.jboss.cache.TreeCache;
import org.jboss.cache.config.Option;

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

/**
 * Tests passing in the failSilently option in various scenarios.
 *
 * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 */
public class FailSilentlyTest extends TestCase
{
   private TreeCache cache;
   private TransactionManager manager;
   private Transaction tx;
   private Fqn fqn = Fqn.fromString("/a");
   private String key = "key";
   private Option failSilently;

   protected void setUp() throws Exception
   {
      if (cache != null) tearDown();
      cache = new TreeCache();
      // very short acquisition timeout
      cache.setLockAcquisitionTimeout(100);
      cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
      cache.setCacheMode(TreeCache.LOCAL);
      cache.startService();
      manager = cache.getTransactionManager();

      failSilently = new Option();
      failSilently.setFailSilently(true);
   }

   protected void tearDown()
   {
      if (tx != null)
      {
         try
         {
            manager.resume(tx);
            manager.rollback();
         }
         catch (Exception e)
         {

         }
      }
      if (cache != null)
      {
         cache.stopService();
         cache = null;
      }
   }

   public void testPutKeyValue() throws Exception
   {
      manager.begin();
      cache.put(fqn, key, "value");
      tx = manager.suspend();
      cache.put(fqn, key, "value2", failSilently);
   }

   public void testPutData() throws Exception
   {
      Map data = new HashMap();
      data.put(key, "value");
      manager.begin();
      cache.put(fqn, data);
      tx = manager.suspend();
      cache.put(fqn, data, failSilently);
   }


   public void testRemoveNode() throws Exception
   {
      cache.put(fqn, key, "value");
      manager.begin();
      // get a read lock
      cache.get(fqn, key);
      tx = manager.suspend();
      cache.remove(fqn, failSilently);
   }

   public void testRemoveKey() throws Exception
   {
      cache.put(fqn, key, "value");
      manager.begin();
      // get a read lock
      cache.get(fqn, key);
      tx = manager.suspend();
      cache.remove(fqn, key, failSilently);
   }

   public void testGetNode() throws Exception
   {
      cache.put(fqn, key, "value");
      manager.begin();
      // get a WL
      cache.put(fqn, key, "value2");
      tx = manager.suspend();
      cache.get(fqn, failSilently);
   }

   public void testGetKey() throws Exception
   {
      cache.put(fqn, key, "value");
      manager.begin();
      // get a WL
      cache.put(fqn, key, "value2");
      tx = manager.suspend();
      cache.get(fqn, key, failSilently);
   }

   public void testGetChildrenNames() throws Exception
   {
      cache.put(fqn, key, "value");
      manager.begin();
      // get a WL
      cache.put(fqn, key, "value2");
      tx = manager.suspend();
      cache.getChildrenNames(fqn, failSilently);
   }

   public void testPutThatWillFail() throws Exception
   {
      manager.begin();
      cache.put(fqn, "k", "v"); // this will get WLs on / and /a
      tx = manager.suspend();

      assertEquals(2, cache.getNumberOfLocksHeld());

      // now this call WILL fail, but should fail silently - i.e., not roll back.
      manager.begin();
      cache.put(fqn, "x", "y", failSilently);

      // should not roll back, despite the cache put call failing/timing out.
      manager.commit();
   }
}
