KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > buddyreplication > BuddyManagerTest


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 JavaDoc;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
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    public void testConstruction1() throws Exception JavaDoc
35    {
36       String JavaDoc xmlConfig = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled>\n" +
37               " <buddyLocatorProperties>numBuddies = 3</buddyLocatorProperties>\n" +
38               " <buddyPoolName>groupOne</buddyPoolName></config>";
39       Element JavaDoc 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    /**
52     * Constructs a buddy manager using a nonexistent buddy locator but with some specific properties.
53     *
54     * @throws Exception
55     */

56    public void testConstruction2() throws Exception JavaDoc
57    {
58       String JavaDoc 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 JavaDoc 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       // since the properties are not passed on to the next member buddy locator - they were obviously meant for a different impl.
71
NextMemberBuddyLocatorConfig blc = (NextMemberBuddyLocatorConfig) mgr.buddyLocator.getConfig();
72       assertEquals(1, blc.getNumBuddies());
73       assertTrue(blc.isIgnoreColocatedBuddies());
74    }
75
76    /**
77     * Constructs a disabled buddy manager
78     *
79     * @throws Exception
80     */

81    public void testConstruction3() throws Exception JavaDoc
82    {
83       String JavaDoc xmlConfig = "<config><buddyReplicationEnabled>false</buddyReplicationEnabled></config>";
84
85       Element JavaDoc element = XmlHelper.stringToElement(xmlConfig);
86       BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element);
87       BuddyManager mgr = new BuddyManager(config);
88
89       assertTrue(!mgr.isEnabled());
90    }
91
92    /**
93     * Constructs a buddy manager using a minimal config set
94     *
95     * @throws Exception
96     */

97    public void testConstruction4() throws Exception JavaDoc
98    {
99       String JavaDoc xmlConfig = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled></config>";
100
101       Element JavaDoc 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 JavaDoc element = XmlHelper.stringToElement("<config><buddyReplicationEnabled>true</buddyReplicationEnabled></config>");
119          BuddyReplicationConfig cfg = XmlConfigurationParser.parseBuddyReplicationConfig(element);
120          bm = new BuddyManager(cfg);
121       }
122       catch (Exception JavaDoc 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       // should use object refs to transform the original MethodCall.
142
String JavaDoc 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       // should use object refs to transform the original MethodCall.
160
String JavaDoc 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 JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc l = (List JavaDoc) newReplicatedCall.getArgs()[0];
187
188       // should use object refs to transform the original MethodCall.
189
String JavaDoc 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