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

import org.jboss.cache.Fqn;
import org.jboss.cache.TreeCache;
import org.jboss.cache.loader.CacheLoader;
import org.jboss.cache.misc.TestingUtil;

/**
 * Tests use of the data gravitator alongside other cache loaders as well as data gravitator options such as removeOnFind.
 *
 * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 */
public class BuddyReplicationWithCacheLoaderTest extends BuddyReplicationTestsBase
{

    protected Fqn fqn = Fqn.fromString("/test");
    protected String key = "key";
    protected String value = "value";
    protected boolean passivation = false;

    private CacheLoader[] getLoaders(TreeCache[] caches)
    {
        CacheLoader[] retVal = new CacheLoader[caches.length];

        for (int i=0; i<retVal.length; i++)
        {
            retVal[i] = caches[i].getCacheLoader();
        }

        return retVal;
    }

    public void testWithDataGravitationDefault() throws Exception
    {

        TreeCache[] caches = null;
        try
        {
            // create 3 caches
            caches = createCachesWithCacheLoader(3, true, true, passivation);

            TestingUtil.sleepThread(1000);

            CacheLoader[] loaders = getLoaders(caches);

            // cleanup
            for (int i=0; i<3; i++) loaders[i].remove(Fqn.ROOT);

            for (int i=0;i<3; i++) System.out.println(i + ": " + loaders[i].get(fqn));

            // put stuff in cache0
            caches[0].put(fqn, key, value);

            // make sure there are no locks.
            assertNoLocks(caches);
            for (int i=0;i<3; i++) System.out.println(i + ": " + loaders[i].get(fqn));

            dumpCacheContents(caches);

            // request data from cache2
            assertEquals(value, caches[2].get(fqn, key));
            assertNoLocks(caches);

            for (int i=0;i<3; i++) System.out.println(i + ": " + loaders[i].get(fqn));

            dumpCacheContents(caches);


            // test that data does not exist in cache0
            assertTrue("should not exist in cache0", !caches[0].exists(fqn));

            // test that data does not exist in cache1
            assertTrue("should not exist in cache1", !caches[1].exists(fqn));

            // test that data does exist in cache2
            assertTrue("should exist in cache2", caches[2].exists(fqn));

            // test that data does not exist in loader0
            assertTrue("should not exist in loader0", !loaders[0].exists(fqn));

            // test that data does not exist in loader1
            assertTrue("should not exist in loader1", !loaders[1].exists(fqn));

            // test that data does exist in loader2
            assertTrue("should exist in loader2", passivation ? !loaders[2].exists(fqn) : loaders[2].exists(fqn));

            Fqn b1 = new Fqn( new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress())), fqn.peekElements() );
            Fqn b2 = new Fqn( new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress())), fqn.peekElements() );

            // test that bkup does exist in cache0
            assertTrue("should not exist in cache0", !caches[0].exists(b1));
            assertTrue("should exist in cache0", caches[0].exists(b2));

            // test that bkup does not exist in cache1
            assertTrue("should not exist in cache1", !caches[1].exists(b1));
            assertTrue("should not exist in cache1", !caches[1].exists(b2));

            // test that bkup does not exist in cache2
            assertTrue("should not exist in cache2", !caches[2].exists(b1));
            assertTrue("should not exist in cache2", !caches[2].exists(b2));

            // test that bkup does exist in loader0
            assertTrue("should not exist in loader0", !loaders[0].exists(b1));
            assertTrue("should exist in loader0", passivation ? !loaders[0].exists(b2) : loaders[0].exists(b2));

            // test that bkup does not exist in loader1
            assertTrue("should not exist in loaders1", !loaders[1].exists(b1));
            assertTrue("should not exist in loaders1", !loaders[1].exists(b2));

            // test that bkup does not exist in loader2
            assertTrue("should not exist in loaders2", !loaders[2].exists(b1));
            assertTrue("should not exist in loaders2", !loaders[2].exists(b2));


        }
        finally
        {
            cleanup(caches);
        }

    }

    public void testWithDataGravitationEviction() throws Exception
    {
        TreeCache[] caches = null;
        try
        {
            // create 3 caches
            caches = createCachesWithCacheLoader(3, true, false, passivation);
            CacheLoader[] loaders = getLoaders(caches);

            // put stuff in cache0
            caches[0].put(fqn, key, value);

            // request data from cache2
            assertEquals(value, caches[2].get(fqn, key));

            // test that data does not exist in cache0
            assertTrue("should not exist in cache0", !caches[0].exists(fqn));

            // test that data does not exist in cache1
            assertTrue("should not exist in cache1", !caches[1].exists(fqn));

            // test that data does exist in cache2
            assertTrue("should exist in cache2", caches[2].exists(fqn));

            // test that data does exist in loader0
            assertTrue("should exist in loader0", loaders[0].exists(fqn));

            // test that data does not exist in loader1
            assertTrue("should not exist in loader1", passivation ? loaders[1].exists(fqn) : !loaders[1].exists(fqn));

            // test that data does exist in loader2
            assertTrue("should exist in loader2", passivation ? !loaders[2].exists(fqn) : loaders[2].exists(fqn));

            Fqn b1 = new Fqn( new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress())), fqn.peekElements() );
            Fqn b2 = new Fqn( new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress())), fqn.peekElements() );

            // test that bkup does exist in cache0
            assertTrue("should not exist in cache0", !caches[0].exists(b1));
            assertTrue("should exist in cache0", caches[0].exists(b2));

            // test that bkup does not exist in cache1
            assertTrue("should not exist in cache1", !caches[1].exists(b1));
            assertTrue("should not exist in cache1", !caches[1].exists(b2));

            // test that bkup does not exist in cache2
            assertTrue("should not exist in cache2", !caches[2].exists(b1));
            assertTrue("should not exist in cache2", !caches[2].exists(b2));

            // test that bkup does exist in loader0
            assertTrue("should not exist in loader0", passivation ? loaders[0].exists(b1) : !loaders[0].exists(b1));
            assertTrue("should exist in loader0", passivation ? !loaders[0].exists(b2) : loaders[0].exists(b2));

            // test that bkup does not exist in loader1
            assertTrue("should exist in loaders1", loaders[1].exists(b1));
            assertTrue("should not exist in loaders1", !loaders[1].exists(b2));

            // test that bkup does not exist in loader2
            assertTrue("should not exist in loaders2", !loaders[2].exists(b1));
            assertTrue("should not exist in loaders2", !loaders[2].exists(b2));
        }
        finally
        {
            cleanup(caches);
        }
    }

}
