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

import junit.framework.TestCase;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.TreeCache;
import org.jboss.cache.interceptors.DataGravitatorInterceptor;
import org.jboss.cache.xml.XmlHelper;
import org.w3c.dom.Element;

import java.util.Iterator;

/**
 * Tests basic configuration options by passing stuff into the TreeCache.
 *
 * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 */
public class BuddyReplicationConfigTest extends TestCase
{
    public void testNullConfig() throws Exception
    {
        TreeCache cache = new TreeCache();
        cache.setBuddyReplicationConfig(null);
        assertNull(cache.getBuddyManager());
    }

    public void testDisabledConfig() throws Exception
    {
        String xmlConfig = "<config><buddyReplicationEnabled>false</buddyReplicationEnabled></config>";
        Element config = XmlHelper.stringToElement(xmlConfig);
        TreeCache cache = new TreeCache();
        cache.setBuddyReplicationConfig(config);
        assertNull(cache.getBuddyManager());
    }

    public void testBasicConfig() throws Exception
    {
        String xmlConfig = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled></config>";
        Element config = XmlHelper.stringToElement(xmlConfig);
        TreeCache cache = new TreeCache();
        cache.setBuddyReplicationConfig(config);
        assertNotNull(cache.getBuddyManager());
        BuddyManager mgr = cache.getBuddyManager();
        assertTrue(mgr.isEnabled());
        assertNull(mgr.getBuddyPoolName());
        assertEquals(NextMemberBuddyLocator.class, mgr.buddyLocator.getClass());
        assertEquals(1, ((NextMemberBuddyLocator)mgr.buddyLocator).numBuddies);
        assertTrue(((NextMemberBuddyLocator)mgr.buddyLocator).ignoreColocatedBuddies);
    }

    public void testXmlConfig() throws Exception
    {
        TreeCache cache = new TreeCache();
        PropertyConfigurator pc = new PropertyConfigurator();
        pc.configure(cache, "META-INF/buddyreplication-service.xml");
        cache.createService();

        BuddyManager bm = cache.getBuddyManager();
        assertNotNull(bm);
        assertTrue(bm.isEnabled());
        assertTrue(bm.buddyLocator instanceof NextMemberBuddyLocator);
        NextMemberBuddyLocator bl = (NextMemberBuddyLocator) bm.buddyLocator;
        assertTrue(bl.ignoreColocatedBuddies);
        assertEquals(1, bl.numBuddies);
        assertEquals("myBuddyPoolReplicationGroup", bm.buddyPoolName);
        assertEquals(2000, bm.buddyCommunicationTimeout);

        // test Data Gravitator
        Iterator i = cache.getInterceptors().iterator();

        boolean hasDG = false;
        while (i.hasNext())
        {
            Object o = i.next();
            hasDG = hasDG || (o instanceof DataGravitatorInterceptor);
        }

        assertTrue("Should have a data gravitator!!", hasDG);
    }
}
