Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 197   Methods: 8
NCLOC: 130   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BuddyManagerTest.java - 98.8% 100% 98.9%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.buddyreplication;
 8   
 9    import junit.framework.TestCase;
 10    import org.jboss.cache.Fqn;
 11    import org.jboss.cache.config.BuddyReplicationConfig;
 12    import org.jboss.cache.factories.XmlConfigurationParser;
 13    import org.jboss.cache.marshall.MethodCall;
 14    import org.jboss.cache.marshall.MethodCallFactory;
 15    import org.jboss.cache.marshall.MethodDeclarations;
 16    import org.jboss.cache.xml.XmlHelper;
 17    import org.w3c.dom.Element;
 18   
 19    import java.util.ArrayList;
 20    import java.util.List;
 21   
 22    /**
 23    * Tests the BuddyManager class
 24    *
 25    * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 26    */
 27    public class BuddyManagerTest extends TestCase
 28    {
 29    /**
 30    * Constructs a buddy manager using the default buddy locator but with some specific properties.
 31    *
 32    * @throws Exception
 33    */
 34  1 public void testConstruction1() throws Exception
 35    {
 36  1 String xmlConfig = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled>\n" +
 37    " <buddyLocatorProperties>numBuddies = 3</buddyLocatorProperties>\n" +
 38    " <buddyPoolName>groupOne</buddyPoolName></config>";
 39  1 Element element = XmlHelper.stringToElement(xmlConfig);
 40  1 BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element);
 41  1 BuddyManager mgr = new BuddyManager(config);
 42   
 43  1 assertTrue(mgr.isEnabled());
 44  1 assertEquals("groupOne", mgr.getBuddyPoolName());
 45  1 assertEquals(NextMemberBuddyLocator.class, mgr.buddyLocator.getClass());
 46  1 NextMemberBuddyLocatorConfig blc = (NextMemberBuddyLocatorConfig) mgr.buddyLocator.getConfig();
 47  1 assertEquals(3, blc.getNumBuddies());
 48  1 assertTrue(blc.isIgnoreColocatedBuddies());
 49    }
 50   
 51    /**
 52    * Constructs a buddy manager using a nonexistent buddy locator but with some specific properties.
 53    *
 54    * @throws Exception
 55    */
 56  1 public void testConstruction2() throws Exception
 57    {
 58  1 String xmlConfig = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled>\n" +
 59    " <buddyLocatorClass>org.i.dont.exist.PhantomBuddyLocator</buddyLocatorClass>\n" +
 60    " <buddyLocatorProperties>numBuddies = 3</buddyLocatorProperties>\n" +
 61    " <buddyPoolName>groupOne</buddyPoolName></config>";
 62  1 Element element = XmlHelper.stringToElement(xmlConfig);
 63  1 BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element);
 64  1 BuddyManager mgr = new BuddyManager(config);
 65   
 66  1 assertTrue(mgr.isEnabled());
 67  1 assertEquals("groupOne", mgr.getBuddyPoolName());
 68  1 assertEquals(NextMemberBuddyLocator.class, mgr.buddyLocator.getClass());
 69   
 70    // since the properties are not passed on to the next member buddy locator - they were obviously meant for a different impl.
 71  1 NextMemberBuddyLocatorConfig blc = (NextMemberBuddyLocatorConfig) mgr.buddyLocator.getConfig();
 72  1 assertEquals(1, blc.getNumBuddies());
 73  1 assertTrue(blc.isIgnoreColocatedBuddies());
 74    }
 75   
 76    /**
 77    * Constructs a disabled buddy manager
 78    *
 79    * @throws Exception
 80    */
 81  1 public void testConstruction3() throws Exception
 82    {
 83  1 String xmlConfig = "<config><buddyReplicationEnabled>false</buddyReplicationEnabled></config>";
 84   
 85  1 Element element = XmlHelper.stringToElement(xmlConfig);
 86  1 BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element);
 87  1 BuddyManager mgr = new BuddyManager(config);
 88   
 89  1 assertTrue(!mgr.isEnabled());
 90    }
 91   
 92    /**
 93    * Constructs a buddy manager using a minimal config set
 94    *
 95    * @throws Exception
 96    */
 97  1 public void testConstruction4() throws Exception
 98    {
 99  1 String xmlConfig = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled></config>";
 100   
 101  1 Element element = XmlHelper.stringToElement(xmlConfig);
 102  1 BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element);
 103  1 BuddyManager mgr = new BuddyManager(config);
 104   
 105  1 assertTrue(mgr.isEnabled());
 106  1 assertNull(mgr.getBuddyPoolName());
 107  1 assertEquals(NextMemberBuddyLocator.class, mgr.buddyLocator.getClass());
 108  1 NextMemberBuddyLocatorConfig blc = (NextMemberBuddyLocatorConfig) mgr.buddyLocator.getConfig();
 109  1 assertEquals(1, blc.getNumBuddies());
 110  1 assertTrue(blc.isIgnoreColocatedBuddies());
 111    }
 112   
 113  3 private BuddyManager createBasicBuddyManager()
 114    {
 115  3 BuddyManager bm = null;
 116  3 try
 117    {
 118  3 Element element = XmlHelper.stringToElement("<config><buddyReplicationEnabled>true</buddyReplicationEnabled></config>");
 119  3 BuddyReplicationConfig cfg = XmlConfigurationParser.parseBuddyReplicationConfig(element);
 120  3 bm = new BuddyManager(cfg);
 121    }
 122    catch (Exception e)
 123    {
 124  0 e.printStackTrace();
 125    }
 126  3 return bm;
 127    }
 128   
 129  1 public void testFqnManipulation()
 130    {
 131  1 Fqn fqn1 = Fqn.fromString("/hello/world");
 132   
 133  1 MethodCall call1 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn1, "key", "value");
 134  1 MethodCall call2 = MethodCallFactory.create(MethodDeclarations.replicateMethod, call1);
 135   
 136  1 BuddyManager bm = createBasicBuddyManager();
 137   
 138  1 MethodCall newReplicatedCall = bm.transformFqns(call2);
 139  1 MethodCall newPutCall = (MethodCall) newReplicatedCall.getArgs()[0];
 140   
 141    // should use object refs to transform the original MethodCall.
 142  1 String expected = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + null + "/hello/world";
 143  1 assertEquals(expected, newPutCall.getArgs()[0].toString());
 144   
 145    }
 146   
 147  1 public void testRootFqnManipulation()
 148    {
 149  1 Fqn fqn1 = Fqn.ROOT;
 150   
 151  1 MethodCall call1 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn1, "key", "value");
 152  1 MethodCall call2 = MethodCallFactory.create(MethodDeclarations.replicateMethod, call1);
 153   
 154  1 BuddyManager bm = createBasicBuddyManager();
 155   
 156  1 MethodCall newReplicatedCall = bm.transformFqns(call2);
 157  1 MethodCall newPutCall = (MethodCall) newReplicatedCall.getArgs()[0];
 158   
 159    // should use object refs to transform the original MethodCall.
 160  1 String expected = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + null;
 161  1 assertEquals(expected, newPutCall.getArgs()[0].toString());
 162    }
 163   
 164  1 public void testMultiFqnManipulation()
 165    {
 166  1 Fqn fqn1 = Fqn.ROOT;
 167  1 Fqn fqn2 = Fqn.fromString("/hello/world");
 168  1 Fqn fqn3 = Fqn.fromString("/hello/again");
 169  1 Fqn fqn4 = Fqn.fromString("/buddy/replication");
 170   
 171  1 MethodCall call1 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn1, "key", "value");
 172  1 MethodCall call2 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn2, "key", "value");
 173  1 MethodCall call3 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn3, "key", "value");
 174  1 MethodCall call4 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn4, "key", "value");
 175  1 List list = new ArrayList();
 176  1 list.add(call1);
 177  1 list.add(call2);
 178  1 list.add(call3);
 179  1 list.add(call4);
 180   
 181  1 MethodCall call5 = MethodCallFactory.create(MethodDeclarations.replicateAllMethod, list);
 182   
 183  1 BuddyManager bm = createBasicBuddyManager();
 184   
 185  1 MethodCall newReplicatedCall = bm.transformFqns(call5);
 186  1 List l = (List) newReplicatedCall.getArgs()[0];
 187   
 188    // should use object refs to transform the original MethodCall.
 189  1 String expected = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/null";
 190   
 191  1 int i = 0;
 192  1 assertEquals(expected, ((MethodCall) l.get(i++)).getArgs()[0].toString());
 193  1 assertEquals(expected + "/hello/world", ((MethodCall) l.get(i++)).getArgs()[0].toString());
 194  1 assertEquals(expected + "/hello/again", ((MethodCall) l.get(i++)).getArgs()[0].toString());
 195  1 assertEquals(expected + "/buddy/replication", ((MethodCall) l.get(i)).getArgs()[0].toString());
 196    }
 197    }