/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache.optimistic;

import junit.framework.Assert;
import org.jboss.cache.TreeCache;
import org.jboss.cache.loader.CacheLoader;
import org.jboss.cache.transaction.DummyTransactionManager;
import org.jboss.cache.xml.XmlHelper;
import org.w3c.dom.Element;

/**
 * Tests optimistic locking with pasivation
 *
 * @author Manik Surtani (<a href="mailto:manik@jboss.org">manik@jboss.org</a>)
 */
public class OptimisticWithPassivationTest extends AbstractOptimisticTestCase
{

    public OptimisticWithPassivationTest(String s)
    {
        super(s);
    }

    protected Element getCacheLoaderConfig(String loc) throws Exception
    {
        String xml = "            <config>\n" +
                "                \n" +
                "                <passivation>true</passivation>\n" +
                "                <preload></preload>\n" +
                "\n" +
                "                <cacheloader>\n" +
                "                    <class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
                "                    <properties>\n" +
                "                    </properties>\n" +
                "                    <async>false</async>\n" +
                "                    <fetchPersistentState>false</fetchPersistentState>\n" +
                "                    <ignoreModifications>false</ignoreModifications>\n" +
                "                </cacheloader>\n" +
                "                \n" +
                "            </config>";
        return XmlHelper.stringToElement(xml);
    }

    private TreeCache createLocalCache() throws Exception
    {
        TreeCache cache = createCacheUnstarted(false);
        cache.setCacheLoaderConfiguration(getCacheLoaderConfig(getTempDir()));

        cache.createService();
        cache.startService();
        return cache;
    }

    public void testPassivationLocal() throws Exception
    {
        TreeCache cache = createLocalCache();
        CacheLoader loader = cache.getCacheLoader();

        // clean up
        cache.remove(fqn);
        loader.remove(fqn);

        Assert.assertNull(loader.get(fqn));

        DummyTransactionManager mgr = DummyTransactionManager.getInstance();

        // put something in the cache
        mgr.begin();
        cache.put(fqn, key, value);
        mgr.commit();

        // should be nothing in the loader
        Assert.assertEquals(value, cache.get(fqn, key));
        Assert.assertNull(loader.get(fqn));

        // evict from cache
        mgr.begin();
        cache.evict(fqn);
        mgr.commit();

        mgr.begin();
        // should now be passivated in the loader
        // don't do a cache.get() first as this will activate this node from the loader again!
        // Assert.assertNull( cache.get( fqn ) );
        Assert.assertEquals(value, loader.get(fqn).get(key));

        // now do a cache.get()...
        Assert.assertEquals(value, cache.get(fqn, key));

        // and the object should now be removed from the loader
        Assert.assertNull(loader.get(fqn));
        mgr.commit();

        // clean up
        mgr.begin();
        cache.remove(fqn);
        loader.remove(fqn);
        mgr.commit();

    }
}
