1 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 27 public class BuddyManagerTest extends TestCase 28 { 29 34 public void testConstruction1() throws Exception 35 { 36 String xmlConfig = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled>\n" + 37 " <buddyLocatorProperties>numBuddies = 3</buddyLocatorProperties>\n" + 38 " <buddyPoolName>groupOne</buddyPoolName></config>"; 39 Element element = XmlHelper.stringToElement(xmlConfig); 40 BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element); 41 BuddyManager mgr = new BuddyManager(config); 42 43 assertTrue(mgr.isEnabled()); 44 assertEquals("groupOne", mgr.getBuddyPoolName()); 45 assertEquals(NextMemberBuddyLocator.class, mgr.buddyLocator.getClass()); 46 NextMemberBuddyLocatorConfig blc = (NextMemberBuddyLocatorConfig) mgr.buddyLocator.getConfig(); 47 assertEquals(3, blc.getNumBuddies()); 48 assertTrue(blc.isIgnoreColocatedBuddies()); 49 } 50 51 56 public void testConstruction2() throws Exception 57 { 58 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 Element element = XmlHelper.stringToElement(xmlConfig); 63 BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element); 64 BuddyManager mgr = new BuddyManager(config); 65 66 assertTrue(mgr.isEnabled()); 67 assertEquals("groupOne", mgr.getBuddyPoolName()); 68 assertEquals(NextMemberBuddyLocator.class, mgr.buddyLocator.getClass()); 69 70 NextMemberBuddyLocatorConfig blc = (NextMemberBuddyLocatorConfig) mgr.buddyLocator.getConfig(); 72 assertEquals(1, blc.getNumBuddies()); 73 assertTrue(blc.isIgnoreColocatedBuddies()); 74 } 75 76 81 public void testConstruction3() throws Exception 82 { 83 String xmlConfig = "<config><buddyReplicationEnabled>false</buddyReplicationEnabled></config>"; 84 85 Element element = XmlHelper.stringToElement(xmlConfig); 86 BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element); 87 BuddyManager mgr = new BuddyManager(config); 88 89 assertTrue(!mgr.isEnabled()); 90 } 91 92 97 public void testConstruction4() throws Exception 98 { 99 String xmlConfig = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled></config>"; 100 101 Element element = XmlHelper.stringToElement(xmlConfig); 102 BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element); 103 BuddyManager mgr = new BuddyManager(config); 104 105 assertTrue(mgr.isEnabled()); 106 assertNull(mgr.getBuddyPoolName()); 107 assertEquals(NextMemberBuddyLocator.class, mgr.buddyLocator.getClass()); 108 NextMemberBuddyLocatorConfig blc = (NextMemberBuddyLocatorConfig) mgr.buddyLocator.getConfig(); 109 assertEquals(1, blc.getNumBuddies()); 110 assertTrue(blc.isIgnoreColocatedBuddies()); 111 } 112 113 private BuddyManager createBasicBuddyManager() 114 { 115 BuddyManager bm = null; 116 try 117 { 118 Element element = XmlHelper.stringToElement("<config><buddyReplicationEnabled>true</buddyReplicationEnabled></config>"); 119 BuddyReplicationConfig cfg = XmlConfigurationParser.parseBuddyReplicationConfig(element); 120 bm = new BuddyManager(cfg); 121 } 122 catch (Exception e) 123 { 124 e.printStackTrace(); 125 } 126 return bm; 127 } 128 129 public void testFqnManipulation() 130 { 131 Fqn fqn1 = Fqn.fromString("/hello/world"); 132 133 MethodCall call1 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn1, "key", "value"); 134 MethodCall call2 = MethodCallFactory.create(MethodDeclarations.replicateMethod, call1); 135 136 BuddyManager bm = createBasicBuddyManager(); 137 138 MethodCall newReplicatedCall = bm.transformFqns(call2); 139 MethodCall newPutCall = (MethodCall) newReplicatedCall.getArgs()[0]; 140 141 String expected = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + null + "/hello/world"; 143 assertEquals(expected, newPutCall.getArgs()[0].toString()); 144 145 } 146 147 public void testRootFqnManipulation() 148 { 149 Fqn fqn1 = Fqn.ROOT; 150 151 MethodCall call1 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn1, "key", "value"); 152 MethodCall call2 = MethodCallFactory.create(MethodDeclarations.replicateMethod, call1); 153 154 BuddyManager bm = createBasicBuddyManager(); 155 156 MethodCall newReplicatedCall = bm.transformFqns(call2); 157 MethodCall newPutCall = (MethodCall) newReplicatedCall.getArgs()[0]; 158 159 String expected = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + null; 161 assertEquals(expected, newPutCall.getArgs()[0].toString()); 162 } 163 164 public void testMultiFqnManipulation() 165 { 166 Fqn fqn1 = Fqn.ROOT; 167 Fqn fqn2 = Fqn.fromString("/hello/world"); 168 Fqn fqn3 = Fqn.fromString("/hello/again"); 169 Fqn fqn4 = Fqn.fromString("/buddy/replication"); 170 171 MethodCall call1 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn1, "key", "value"); 172 MethodCall call2 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn2, "key", "value"); 173 MethodCall call3 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn3, "key", "value"); 174 MethodCall call4 = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn4, "key", "value"); 175 List list = new ArrayList (); 176 list.add(call1); 177 list.add(call2); 178 list.add(call3); 179 list.add(call4); 180 181 MethodCall call5 = MethodCallFactory.create(MethodDeclarations.replicateAllMethod, list); 182 183 BuddyManager bm = createBasicBuddyManager(); 184 185 MethodCall newReplicatedCall = bm.transformFqns(call5); 186 List l = (List ) newReplicatedCall.getArgs()[0]; 187 188 String expected = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/null"; 190 191 int i = 0; 192 assertEquals(expected, ((MethodCall) l.get(i++)).getArgs()[0].toString()); 193 assertEquals(expected + "/hello/world", ((MethodCall) l.get(i++)).getArgs()[0].toString()); 194 assertEquals(expected + "/hello/again", ((MethodCall) l.get(i++)).getArgs()[0].toString()); 195 assertEquals(expected + "/buddy/replication", ((MethodCall) l.get(i)).getArgs()[0].toString()); 196 } 197 } 198 | Popular Tags |