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

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

/**
 * 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 (cache != null)
        {
            cache.stopService();
            cache = null;
        }
    }

    public void testPutKeyValue() throws Exception
    {
        try
        {
            manager.begin();
            cache.put(fqn, key, "value");
            tx = manager.suspend();
            cache.put(fqn, key, "value2", failSilently);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (tx != null)
            {
                manager.resume(tx);
                manager.rollback();
            }
        }
    }

    public void testPutData() throws Exception
    {
        Map data = new HashMap();
        data.put(key, "value");
        try
        {
            manager.begin();
            cache.put(fqn, data);
            tx = manager.suspend();
            cache.put(fqn, data, failSilently);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (tx != null)
            {
                manager.resume(tx);
                manager.rollback();
            }
        }
    }



    public void testRemoveNode() throws Exception
    {
        try
        {
            cache.put(fqn, key, "value");
            manager.begin();
            // get a read lock
            cache.get(fqn, key);
            tx = manager.suspend();
            cache.remove(fqn, failSilently);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (tx != null)
            {
                manager.resume(tx);
                manager.rollback();
            }
        }
    }

    public void testRemoveKey() throws Exception
    {
        try
        {
            cache.put(fqn, key, "value");
            manager.begin();
            // get a read lock
            cache.get(fqn, key);
            tx = manager.suspend();
            cache.remove(fqn, key, failSilently);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (tx != null)
            {
                manager.resume(tx);
                manager.rollback();
            }
        }
    }

    public void testGetNode() throws Exception
    {
        try
        {
            cache.put(fqn, key, "value");
            manager.begin();
            // get a WL
            cache.put(fqn, key, "value2");
            tx = manager.suspend();
            cache.get(fqn, failSilently);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (tx != null)
            {
                manager.resume(tx);
                manager.rollback();
            }
        }
    }

    public void testGetKey() throws Exception
    {
        try
        {
            cache.put(fqn, key, "value");
            manager.begin();
            // get a WL
            cache.put(fqn, key, "value2");
            tx = manager.suspend();
            cache.get(fqn, key, failSilently);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (tx != null)
            {
                manager.resume(tx);
                manager.rollback();
            }
        }
    }

    public void testGetChildrenNames() throws Exception
    {
        try
        {
            cache.put(fqn, key, "value");
            manager.begin();
            // get a WL
            cache.put(fqn, key, "value2");
            tx = manager.suspend();
            cache.getChildrenNames(fqn, failSilently);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (tx != null)
            {
                manager.resume(tx);
                manager.rollback();
            }
        }
    }
}
