KickJava   Java API By Example, From Geeks To Geeks.

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


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.Cache;
11 import org.jboss.cache.CacheImpl;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.config.BuddyReplicationConfig;
14 import org.jboss.cache.config.CacheLoaderConfig;
15 import org.jboss.cache.config.Configuration;
16 import org.jboss.cache.factories.XmlConfigurationParser;
17 import org.jboss.cache.loader.CacheLoader;
18 import org.jboss.cache.loader.CacheLoaderManager;
19 import org.jboss.cache.misc.TestingUtil;
20 import org.jboss.cache.xml.XmlHelper;
21 import org.jgroups.Address;
22 import org.w3c.dom.Element JavaDoc;
23
24 import javax.transaction.TransactionManager JavaDoc;
25 import java.io.File JavaDoc;
26
27 /**
28  * Base class for BR tests
29  *
30  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
31  */

32 public abstract class BuddyReplicationTestsBase extends TestCase
33 {
34    protected static int VIEW_BLOCK_TIMEOUT = 5000;
35
36    protected CacheImpl createCache(int numBuddies, String JavaDoc buddyPoolName) throws Exception JavaDoc
37    {
38       return createCache(numBuddies, buddyPoolName, false, true);
39    }
40
41    protected CacheImpl createCache(int numBuddies, String JavaDoc buddyPoolName, boolean useDataGravitation) throws Exception JavaDoc
42    {
43       return createCache(numBuddies, buddyPoolName, useDataGravitation, true);
44    }
45
46    protected CacheImpl createCache(int numBuddies, String JavaDoc buddyPoolName, boolean useDataGravitation, boolean start) throws Exception JavaDoc
47    {
48       return createCache(false, numBuddies, buddyPoolName, useDataGravitation, true, start);
49    }
50
51    protected CacheImpl createCache(boolean optimisticLocks, int numBuddies, String JavaDoc buddyPoolName, boolean useDataGravitation, boolean start) throws Exception JavaDoc
52    {
53       return createCache(optimisticLocks, numBuddies, buddyPoolName, useDataGravitation, true, start);
54    }
55
56    protected CacheImpl createCache(int numBuddies, String JavaDoc buddyPoolName, boolean useDataGravitation, boolean removeOnFind, boolean start) throws Exception JavaDoc
57    {
58       return createCache(false, numBuddies, buddyPoolName, useDataGravitation, removeOnFind, start);
59    }
60
61    protected CacheImpl createCache(boolean optimisticLocks, int numBuddies, String JavaDoc buddyPoolName, boolean useDataGravitation, boolean removeOnFind, boolean start) throws Exception JavaDoc
62    {
63       CacheImpl c = new CacheImpl();
64       c.getConfiguration().setCacheMode("REPL_SYNC");
65       c.getConfiguration().setClusterName("BuddyReplicationTest");
66       // basic config
67
String JavaDoc xmlString = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled>\n" +
68               "<buddyCommunicationTimeout>600000</buddyCommunicationTimeout>\n" +
69               " <buddyLocatorClass>org.jboss.cache.buddyreplication.NextMemberBuddyLocator</buddyLocatorClass>\n" +
70               " <autoDataGravitation>" + useDataGravitation + "</autoDataGravitation>\n" +
71               " <dataGravitationRemoveOnFind>" + removeOnFind + "</dataGravitationRemoveOnFind>\n" +
72               " <buddyLocatorProperties>numBuddies = " + numBuddies + "</buddyLocatorProperties>\n";
73
74       if (buddyPoolName != null) xmlString += "<buddyPoolName>" + buddyPoolName + "</buddyPoolName>";
75       xmlString += "</config>";
76       Element JavaDoc element = XmlHelper.stringToElement(xmlString);
77       BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element);
78       c.getConfiguration().setBuddyReplicationConfig(config);
79
80       c.getConfiguration().setFetchInMemoryState(true);
81       if (optimisticLocks)
82       {
83          c.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
84       }
85       c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
86       c.getConfiguration().setSyncCommitPhase(true);// helps track down breakages
87

88       // Call the hook that allows mux integration if that's what the test wants
89
configureMultiplexer(c);
90
91       if (start)
92       {
93          c.start();
94          validateMultiplexer(c);
95       }
96       return c;
97    }
98
99    /**
100     * Provides a hook for multiplexer integration. This default implementation
101     * is a no-op; subclasses that test mux integration would override
102     * to integrate the given cache with a multiplexer.
103     * <p/>
104     * param cache a cache that has been configured but not yet created.
105     */

106    protected void configureMultiplexer(Cache cache) throws Exception JavaDoc
107    {
108       // default does nothing
109
}
110
111    /**
112     * Provides a hook to check that the cache's channel came from the
113     * multiplexer, or not, as expected. This default impl asserts that
114     * the channel did not come from the multiplexer.
115     *
116     * @param cache a cache that has already been started
117     */

118    protected void validateMultiplexer(Cache cache)
119    {
120       assertFalse("Cache is not using multiplexer", cache.getConfiguration().isUsingMultiplexer());
121    }
122
123    protected CacheImpl[] createCaches(int numCaches, boolean useBuddyPool) throws Exception JavaDoc
124    {
125       return createCaches(1, numCaches, useBuddyPool, false);
126    }
127
128    protected CacheImpl[] createCaches(int numCaches, boolean useBuddyPool, boolean useDataGravitation, boolean optimisticLocks) throws Exception JavaDoc
129    {
130       return createCaches(1, numCaches, useBuddyPool, useDataGravitation, optimisticLocks);
131    }
132
133    protected CacheImpl[] createCaches(int numCaches, boolean useBuddyPool, boolean useDataGravitation) throws Exception JavaDoc
134    {
135       return createCaches(1, numCaches, useBuddyPool, useDataGravitation);
136    }
137
138    protected CacheImpl[] createCachesWithCacheLoader(int numCaches, boolean useDataGravitation, boolean removeOnFind, boolean passivation) throws Exception JavaDoc
139    {
140       return this.createCachesWithCacheLoader(numCaches, useDataGravitation, removeOnFind, passivation, false);
141    }
142
143    protected CacheImpl[] createCachesWithCacheLoader(int numCaches, boolean useDataGravitation, boolean removeOnFind, boolean passivation, boolean fetchPersistent) throws Exception JavaDoc
144    {
145       String JavaDoc tmpLoc = System.getProperty("java.io.tmpdir", "/tmp");
146       tmpLoc += File.separator + "BuddyReplicationTestsBase-";
147
148       CacheImpl[] caches = new CacheImpl[numCaches];
149       for (int i = 0; i < numCaches; i++)
150       {
151          caches[i] = createCacheWithCacheLoader((tmpLoc + i), useDataGravitation, removeOnFind, passivation, fetchPersistent, true);
152       }
153
154       // allow some time for the caches to start up and discover each other
155
TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
156       TestingUtil.sleepThread(getSleepTimeout());
157       return caches;
158    }
159
160    protected CacheImpl createCacheWithCacheLoader(String JavaDoc location, boolean useDataGravitation, boolean removeOnFind, boolean passivation, boolean fetchPersistent, boolean start) throws Exception JavaDoc
161    {
162       CacheImpl cache = createCache(1, null, useDataGravitation, removeOnFind, false);
163
164       String JavaDoc cloader = "<config>\n" +
165               "<passivation>" + passivation + "</passivation>\n" +
166               "<preload></preload>\n" +
167
168               "<cacheloader>\n" +
169               "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
170               "<properties>location=" + (location) + "</properties>\n" +
171               "<async>false</async>\n" +
172               "<shared>false</shared>\n" +
173               "<fetchPersistentState>" + fetchPersistent + "</fetchPersistentState>\n" +
174               "</cacheloader>\n" +
175               "</config>";
176
177       Element JavaDoc element = XmlHelper.stringToElement(cloader);
178       CacheLoaderConfig cfg = XmlConfigurationParser.parseCacheLoaderConfig(element);
179       cache.getConfiguration().setCacheLoaderConfig(cfg);
180       if (start)
181       {
182          cache.start();
183       }
184
185       return cache;
186    }
187
188    protected CacheImpl[] createCaches(int numBuddies, int numCaches, boolean useBuddyPool) throws Exception JavaDoc
189    {
190       return createCaches(numBuddies, numCaches, useBuddyPool, false);
191    }
192
193    protected CacheImpl[] createCaches(int numBuddies, int numCaches, boolean useBuddyPool, boolean useDataGravitation) throws Exception JavaDoc
194    {
195       return createCaches(numBuddies, numCaches, useBuddyPool, useDataGravitation, false);
196    }
197
198    protected CacheImpl[] createCaches(int numBuddies, int numCaches, boolean useBuddyPool, boolean useDataGravitation, boolean optimisticLocks) throws Exception JavaDoc
199    {
200       CacheImpl[] caches = new CacheImpl[numCaches];
201       for (int i = 0; i < numCaches; i++)
202       {
203          caches[i] = createCache(optimisticLocks, numBuddies, useBuddyPool ? Character.toString((char) ('A' + i)) : null, useDataGravitation, true);
204       }
205
206       // allow some time for the caches to start up and discover each other
207
TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
208       TestingUtil.sleepThread(getSleepTimeout());
209       return caches;
210    }
211
212    protected void cleanup(CacheImpl[] caches) throws Exception JavaDoc
213    {
214       System.out.println("*** cleaning up");
215
216       if (caches != null)
217       {
218          for (int i = 0; i < caches.length; i++)
219          {
220             if (caches[i] != null)
221             {
222                destroyCache(caches[i]);
223                caches[i] = null;
224             }
225          }
226       }
227    }
228
229    private void destroyCache(CacheImpl c) throws Exception JavaDoc
230    {
231       TransactionManager JavaDoc tm = c.getTransactionManager();
232       if (tm != null && tm.getTransaction() != null)
233       {
234          try
235          {
236             tm.rollback();
237          }
238          catch (Exception JavaDoc e)
239          {
240          }
241       }
242       CacheLoaderManager clm = c.getCacheLoaderManager();
243       if (clm != null)
244       {
245          CacheLoader cl = c.getCacheLoaderManager().getCacheLoader();
246          if (cl != null) cl.remove(Fqn.ROOT);
247       }
248       c.stop();
249    }
250
251    protected void printBuddyGroup(CacheImpl cache)
252    {
253       BuddyGroup bg = cache.getBuddyManager().buddyGroup;
254       System.out.println("*** " + bg);
255       System.out.println(" Groups I participate in: " + cache.getBuddyManager().buddyGroupsIParticipateIn.keySet());
256    }
257
258    /**
259     * This is to allow for any state transfers involved (when assigning a buddy) to complete
260     */

261    protected int getSleepTimeout()
262    {
263       return 1000;
264    }
265
266    protected void assertIsBuddy(CacheImpl dataOwner, CacheImpl buddy, boolean onlyBuddy)
267    {
268       Address dataOwnerLocalAddress = dataOwner.getLocalAddress();
269       Address buddyLocalAddress = buddy.getLocalAddress();
270
271       System.out.println("*** assert with groups. Testing that " + buddyLocalAddress + " is a buddy for owner " + dataOwnerLocalAddress + " only buddy? " + onlyBuddy);
272       printBuddyGroup(dataOwner);
273
274       BuddyManager dataOwnerBuddyManager = dataOwner.getBuddyManager();
275       BuddyManager buddyBuddyManager = buddy.getBuddyManager();
276
277       // lets test things on the data owner's side of things
278
if (onlyBuddy) assertEquals("Should only have one buddy", 1, dataOwnerBuddyManager.getBuddyAddresses().size());
279
280       assertTrue(buddyLocalAddress + " should be a buddy to " + dataOwnerLocalAddress, dataOwnerBuddyManager.getBuddyAddresses().contains(buddyLocalAddress));
281
282       // and now on the buddy end
283
BuddyGroup group = buddyBuddyManager.buddyGroupsIParticipateIn.get(BuddyManager.getGroupNameFromAddress(dataOwnerLocalAddress));
284       System.out.println("*** Buddy's version of dataOwner's group " + group);
285
286       assertTrue("buddy's list of groups it participates in should contain data owner's group name", buddyBuddyManager.buddyGroupsIParticipateIn.containsKey(BuddyManager.getGroupNameFromAddress(dataOwnerLocalAddress)));
287       if (onlyBuddy) assertEquals(1, group.getBuddies().size());
288       assertTrue(buddyLocalAddress + " should be a buddy to " + group.getGroupName(), group.getBuddies().contains(buddyLocalAddress));
289    }
290
291    protected void dumpCacheContents(CacheImpl[] caches)
292    {
293       System.out.println("**** START: Cache Contents ****");
294       for (int i = 0; i < caches.length; i++)
295       {
296          if (caches[i] == null)
297          {
298             System.out.println(" ** Cache " + i + " is null!");
299          }
300          else
301          {
302             System.out.println(" ** Cache " + i + " is " + caches[i].getLocalAddress());
303             System.out.println(" " + caches[i].printLockInfo());
304          }
305       }
306       System.out.println("**** END: Cache Contents ****");
307    }
308
309    protected void assertNoLocks(CacheImpl[] caches)
310    {
311       for (int i = 0; i < caches.length; i++)
312       {
313          if (caches[i] != null) assertEquals(0, caches[i].getNumberOfLocksHeld());
314       }
315    }
316
317 }
318
Popular Tags