/*
 * 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;

/**
 *
 * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 */
public class CacheModeLocalSimpleTest extends TestCase
{
    private TreeCache cache1, cache2;
    private Option cacheModeLocal;

    public void setUp() throws Exception
    {
        cache1 = new TreeCache();
        cache1.setCacheMode("REPL_SYNC");
        cache1.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");

        cache2 = new TreeCache();
        cache2.setCacheMode("REPL_SYNC");
        cache2.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");

        cache1.startService();
        cache2.startService();

        cacheModeLocal = new Option();
        cacheModeLocal.setCacheModeLocal(true);
    }

    public void tearDown()
    {
        if (cache1 != null)
        {
            cache1.stopService();
            cache1=null;
        }

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

    public void testCacheModeLocalWithTx() throws Exception
    {
        TransactionManager mgr = cache1.getTransactionManager();
        mgr.begin();

        cache1.put(Fqn.fromString("/replicate"), "k", "v");
        cache1.put(Fqn.fromString("/not-replicate"), "k", "v", cacheModeLocal);

        mgr.commit();

        assertEquals("v", cache1.get("/replicate", "k"));
        assertEquals("v", cache1.get("/not-replicate", "k"));

        assertEquals("v", cache2.get("/replicate", "k"));
        assertNull(cache2.get("/not-replicate", "k"));        
    }
}
