/*
 * 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 javax.transaction.TransactionManager;


/**
 * Tests buddy replication when using transactions
 *
 * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 */
public class BuddyReplicationWithTransactionsTest extends BuddyReplicationTestsBase
{
    private Fqn fqn =Fqn.fromString("test");
    private String key = "key";
    private String value = "value";
    private TreeCache[] caches;

    protected void tearDown() throws Exception
    {
        if (caches != null)
        {
            cleanup(caches);
            caches = null;
        }
    }

    public void testTransactionsCommit() throws Exception
    {
        caches = createCaches(3, false, true, false);

        caches[0].put(fqn, key, value);
        Fqn oldBackupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + "/test");
        Fqn newBackupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress()) + "/test");

        dumpCacheContents(caches);

        TransactionManager txman = caches[2].getTransactionManager();


        assertTrue(caches[0].exists(fqn));
        assertTrue(!caches[1].exists(fqn));
        assertTrue(!caches[2].exists(fqn));
        assertTrue(!caches[0].exists(oldBackupFqn));
        assertTrue(caches[1].exists(oldBackupFqn));
        assertTrue(!caches[2].exists(oldBackupFqn));
        assertTrue(!caches[0].exists(newBackupFqn));
        assertTrue(!caches[1].exists(newBackupFqn));
        assertTrue(!caches[2].exists(newBackupFqn));


        txman.begin();

        dumpCacheContents(caches);
        caches[2].get(fqn, key);
        dumpCacheContents(caches);

        assertTrue(caches[0].exists(fqn));
        assertTrue(!caches[1].exists(fqn));
        assertTrue(caches[2].exists(fqn));
        assertTrue(!caches[0].exists(oldBackupFqn));
        assertTrue(caches[1].exists(oldBackupFqn));
        assertTrue(!caches[2].exists(oldBackupFqn));
        assertTrue(!caches[0].exists(newBackupFqn));
        assertTrue(!caches[1].exists(newBackupFqn));
        assertTrue(!caches[2].exists(newBackupFqn));

        txman.commit();

        dumpCacheContents(caches);

        assertTrue(!caches[0].exists(fqn));
        assertTrue(!caches[1].exists(fqn));
        assertTrue(caches[2].exists(fqn));
        assertTrue(!caches[0].exists(oldBackupFqn));
        assertTrue(!caches[1].exists(oldBackupFqn));
        assertTrue(!caches[2].exists(oldBackupFqn));
        assertTrue(caches[0].exists(newBackupFqn));
        assertTrue(!caches[1].exists(newBackupFqn));
        assertTrue(!caches[2].exists(newBackupFqn));

        dumpCacheContents(caches);

        assertNoLocks(caches);
    }

    public void testTransactionsRollback() throws Exception
    {
        caches = createCaches(3, false, true, false);

        caches[0].put(fqn, key, value);
        Fqn oldBackupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + "/test");
        Fqn newBackupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress()) + "/test");

        dumpCacheContents(caches);

        TransactionManager txman = caches[2].getTransactionManager();


        assertTrue(caches[0].exists(fqn));
        assertTrue(!caches[1].exists(fqn));
        assertTrue(!caches[2].exists(fqn));
        assertTrue(!caches[0].exists(oldBackupFqn));
        assertTrue(caches[1].exists(oldBackupFqn));
        assertTrue(!caches[2].exists(oldBackupFqn));
        assertTrue(!caches[0].exists(newBackupFqn));
        assertTrue(!caches[1].exists(newBackupFqn));
        assertTrue(!caches[2].exists(newBackupFqn));


        txman.begin();

        dumpCacheContents(caches);
        caches[2].get(fqn, key);
        dumpCacheContents(caches);

        assertTrue(caches[0].exists(fqn));
        assertTrue(!caches[1].exists(fqn));
        assertTrue(caches[2].exists(fqn));
        assertTrue(!caches[0].exists(oldBackupFqn));
        assertTrue(caches[1].exists(oldBackupFqn));
        assertTrue(!caches[2].exists(oldBackupFqn));
        assertTrue(!caches[0].exists(newBackupFqn));
        assertTrue(!caches[1].exists(newBackupFqn));
        assertTrue(!caches[2].exists(newBackupFqn));

        txman.rollback();

        dumpCacheContents(caches);

        assertTrue(caches[0].exists(fqn));
        assertTrue(!caches[1].exists(fqn));
        assertTrue(!caches[2].exists(fqn));
        assertTrue(!caches[0].exists(oldBackupFqn));
        assertTrue(caches[1].exists(oldBackupFqn));
        assertTrue(!caches[2].exists(oldBackupFqn));
        assertTrue(!caches[0].exists(newBackupFqn));
        assertTrue(!caches[1].exists(newBackupFqn));
        assertTrue(!caches[2].exists(newBackupFqn));

        assertNoLocks(caches);
    }

}
