Package org.jboss.cache.buddyreplication

Source Code of org.jboss.cache.buddyreplication.BuddyManagerTest

/*
* 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.config.BuddyReplicationConfig;
import org.jboss.cache.factories.XmlConfigurationParser;
import org.jboss.cache.marshall.MethodCall;
import org.jboss.cache.marshall.MethodCallFactory;
import org.jboss.cache.marshall.MethodDeclarations;
import org.jboss.cache.xml.XmlHelper;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.Test;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.List;

/**
* Tests the BuddyManager class
*
* @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
*/
@Test(groups = {"functional", "jgroups"})
public class BuddyManagerTest
{
   /**
    * Constructs a buddy manager using the default buddy locator but with some specific properties.
    *
    * @throws Exception
    */
   public void testConstruction1() throws Exception
   {
      String xmlConfig = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled>\n" +
            "          <buddyLocatorProperties>numBuddies = 3</buddyLocatorProperties>\n" +
            "          <buddyPoolName>groupOne</buddyPoolName></config>";
      Element element = XmlHelper.stringToElement(xmlConfig);
      BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element);
      BuddyManager mgr = new BuddyManager(config);

      assertTrue(mgr.isEnabled());
      assertEquals("groupOne", mgr.getBuddyPoolName());
      assertEquals(NextMemberBuddyLocator.class, mgr.buddyLocator.getClass());
      NextMemberBuddyLocatorConfig blc = (NextMemberBuddyLocatorConfig) mgr.buddyLocator.getConfig();
      assertEquals(3, blc.getNumBuddies());
      assertTrue(blc.isIgnoreColocatedBuddies());
   }

   /**
    * Constructs a buddy manager using a nonexistent buddy locator but with some specific properties.
    *
    * @throws Exception
    */
   public void testConstruction2() throws Exception
   {
      String xmlConfig = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled>\n" +
            "          <buddyLocatorClass>org.i.dont.exist.PhantomBuddyLocator</buddyLocatorClass>\n" +
            "          <buddyLocatorProperties>numBuddies = 3</buddyLocatorProperties>\n" +
            "          <buddyPoolName>groupOne</buddyPoolName></config>";
      Element element = XmlHelper.stringToElement(xmlConfig);
      BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element);
      BuddyManager mgr = new BuddyManager(config);

      assertTrue(mgr.isEnabled());
      assertEquals("groupOne", mgr.getBuddyPoolName());
      assertEquals(NextMemberBuddyLocator.class, mgr.buddyLocator.getClass());

      // since the properties are not passed on to the next member buddy locator - they were obviously meant for a different impl.
      NextMemberBuddyLocatorConfig blc = (NextMemberBuddyLocatorConfig) mgr.buddyLocator.getConfig();
      assertEquals(1, blc.getNumBuddies());
      assertTrue(blc.isIgnoreColocatedBuddies());
   }

   /**
    * Constructs a disabled buddy manager
    *
    * @throws Exception
    */
   public void testConstruction3() throws Exception
   {
      String xmlConfig = "<config><buddyReplicationEnabled>false</buddyReplicationEnabled></config>";

      Element element = XmlHelper.stringToElement(xmlConfig);
      BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element);
      BuddyManager mgr = new BuddyManager(config);

      assertTrue(!mgr.isEnabled());
   }

   /**
    * Constructs a buddy manager using a minimal config set
    *
    * @throws Exception
    */
   public void testConstruction4() throws Exception
   {
      String xmlConfig = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled></config>";

      Element element = XmlHelper.stringToElement(xmlConfig);
      BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element);
      BuddyManager mgr = new BuddyManager(config);

      assertTrue(mgr.isEnabled());
      assertNull(mgr.getBuddyPoolName());
      assertEquals(NextMemberBuddyLocator.class, mgr.buddyLocator.getClass());
      NextMemberBuddyLocatorConfig blc = (NextMemberBuddyLocatorConfig) mgr.buddyLocator.getConfig();
      assertEquals(1, blc.getNumBuddies());
      assertTrue(blc.isIgnoreColocatedBuddies());
   }

   private BuddyManager createBasicBuddyManager()
   {
      BuddyManager bm = null;
      try
      {
         Element element = XmlHelper.stringToElement("<config><buddyReplicationEnabled>true</buddyReplicationEnabled></config>");
         BuddyReplicationConfig cfg = XmlConfigurationParser.parseBuddyReplicationConfig(element);
         bm = new BuddyManager(cfg);
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      return bm;
   }

   public void testFqnManipulation()
   {
      Fqn fqn1 = Fqn.fromString("/hello/world");

      MethodCall call1 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal_id, fqn1, "key", "value");
      MethodCall call2 = MethodCallFactory.create(MethodDeclarations.replicateMethod_id, call1);

      BuddyManager bm = createBasicBuddyManager();

      MethodCall newReplicatedCall = bm.transformFqns(call2);
      MethodCall newPutCall = (MethodCall) newReplicatedCall.getArgs()[0];

      // should use object refs to transform the original MethodCall.
      String expected = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + null + "/hello/world";
      assertEquals(expected, newPutCall.getArgs()[0].toString());

   }

   public void testRootFqnManipulation()
   {
      Fqn fqn1 = Fqn.ROOT;

      MethodCall call1 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal_id, fqn1, "key", "value");
      MethodCall call2 = MethodCallFactory.create(MethodDeclarations.replicateMethod_id, call1);

      BuddyManager bm = createBasicBuddyManager();

      MethodCall newReplicatedCall = bm.transformFqns(call2);
      MethodCall newPutCall = (MethodCall) newReplicatedCall.getArgs()[0];

      // should use object refs to transform the original MethodCall.
      String expected = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + null;
      assertEquals(expected, newPutCall.getArgs()[0].toString());
   }

   public void testMultiFqnManipulation()
   {
      Fqn fqn1 = Fqn.ROOT;
      Fqn fqn2 = Fqn.fromString("/hello/world");
      Fqn fqn3 = Fqn.fromString("/hello/again");
      Fqn fqn4 = Fqn.fromString("/buddy/replication");

      MethodCall call1 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal_id, fqn1, "key", "value");
      MethodCall call2 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal_id, fqn2, "key", "value");
      MethodCall call3 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal_id, fqn3, "key", "value");
      MethodCall call4 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal_id, fqn4, "key", "value");
      List<MethodCall> list = new ArrayList<MethodCall>();
      list.add(call1);
      list.add(call2);
      list.add(call3);
      list.add(call4);

      MethodCall call5 = MethodCallFactory.create(MethodDeclarations.replicateAllMethod_id, list);

      BuddyManager bm = createBasicBuddyManager();

      MethodCall newReplicatedCall = bm.transformFqns(call5);
      List l = (List) newReplicatedCall.getArgs()[0];

      // should use object refs to transform the original MethodCall.
      String expected = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/null";

      int i = 0;
      assertEquals(expected, ((MethodCall) l.get(i++)).getArgs()[0].toString());
      assertEquals(expected + "/hello/world", ((MethodCall) l.get(i++)).getArgs()[0].toString());
      assertEquals(expected + "/hello/again", ((MethodCall) l.get(i++)).getArgs()[0].toString());
      assertEquals(expected + "/buddy/replication", ((MethodCall) l.get(i)).getArgs()[0].toString());
   }

   public void testGetActualFqn()
   {
      Fqn<String> x = new Fqn<String>("x");
      Fqn backup = BuddyManager.getBackupFqn("y", x);
      assertEquals(x, BuddyManager.getActualFqn(backup));
   }
}
TOP

Related Classes of org.jboss.cache.buddyreplication.BuddyManagerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.