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

import org.jboss.cache.CacheException;
import org.jboss.cache.Fqn;
import org.jboss.cache.TreeCache;
import org.jboss.cache.xml.XmlHelper;

/**
 *
 * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 */
public class CacheLoaderPurgingTest extends AbstractCacheLoaderTestBase
{
    private TreeCache cache;
    private String key = "key", value = "value";
    private Fqn fqn = Fqn.fromString("/a/b/c");

    public void tearDown() throws CacheException
    {
        if (cache != null)
        {
            cache.remove(Fqn.ROOT);
            cache.stopService();
            cache = null;
        }
    }

    public void testSingleLoaderNoPurge() throws Exception
    {
        cache = new TreeCache();
        cache.setCacheLoaderConfiguration(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "location=" + System.getProperty("java.io.tmpdir", "/tmp")+ "/" + "CacheLoaderPurgingTest", false, false, false));
        cache.startService();

        cache.put(fqn, key, value);

        CacheLoader loader = cache.getCacheLoader();

        assertEquals(value, cache.get(fqn, key));
        assertEquals(value, loader.get(fqn).get(key));

        cache.evict(fqn);
        cache.stopService();
        assertEquals(value, loader.get(fqn).get(key));

        cache.startService();
        assertEquals(value, cache.get(fqn, key));
        assertEquals(value, loader.get(fqn).get(key));
    }

    public void testSingleLoaderPurge() throws Exception
    {
        cache = new TreeCache();
        cache.setCacheLoaderConfiguration(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "location=" + System.getProperty("java.io.tmpdir", "/tmp") +"/" + "CacheLoaderPurgingTest", false, false, false, true));
        cache.startService();

        cache.put(fqn, key, value);

        CacheLoader loader = cache.getCacheLoader();

        assertEquals(value, cache.get(fqn, key));
        assertEquals(value, loader.get(fqn).get(key));

        cache.evict(fqn);
        cache.stopService();
        assertEquals(value, loader.get(fqn).get(key));

        cache.startService();
        assertNull(cache.get(fqn));
        assertNull(loader.get(fqn));
    }

    public void testTwoLoadersPurge() throws Exception
    {
        cache = new TreeCache();

        String xml = "<config>\n" +
                "<passivation>false</passivation>\n" +
                "<preload></preload>\n" +
                "<cacheloader>\n" +
                "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
                "<properties>" +
                " location=" + System.getProperty("java.io.tmpdir", "/tmp") + "/" + "CacheLoaderPurgingTest_1" + "\n" +
                "</properties>\n" +
                "<async>false</async>\n" +
                "<fetchPersistentState>true</fetchPersistentState>\n" +
                "<purgeOnStartup>"+true+"</purgeOnStartup>\n" +
                "</cacheloader>\n" +
                "<cacheloader>\n" +
                "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
                "<properties>" +
                " location=" + System.getProperty("java.io.tmpdir", "/tmp") + "/" + "CacheLoaderPurgingTest_2" + "\n" +
                "</properties>\n" +
                "<async>false</async>\n" +
                "<fetchPersistentState>false</fetchPersistentState>\n" +
                "<purgeOnStartup>" + false + "</purgeOnStartup>\n" +
                "</cacheloader>\n" +
                "</config>";

        cache.setCacheLoaderConfiguration(XmlHelper.stringToElement(xml));
        cache.startService();

        cache.put(fqn, key, value);

        CacheLoader loader[] = (CacheLoader[]) ((ChainingCacheLoader)cache.getCacheLoader()).getCacheLoaders().toArray(new CacheLoader[]{});

        assertEquals(value, cache.get(fqn, key));
        assertEquals(value, loader[0].get(fqn).get(key));
        assertEquals(value, loader[1].get(fqn).get(key));

        cache.evict(fqn);
        cache.stopService();
        assertEquals(value, loader[0].get(fqn).get(key));
        assertEquals(value, loader[1].get(fqn).get(key));

        cache.startService();
        assertTrue(!cache.exists(fqn));
        assertNull(loader[0].get(fqn));
        assertNotNull(loader[1].get(fqn));
        assertEquals(value, cache.get(fqn, key));
    }

}
