Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 338   Methods: 23
NCLOC: 250   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BuddyReplicationTestsBase.java 94.7% 96.3% 95.7% 95.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.Cache;
 11    import org.jboss.cache.CacheImpl;
 12    import org.jboss.cache.DefaultCacheFactory;
 13    import org.jboss.cache.Fqn;
 14    import org.jboss.cache.config.BuddyReplicationConfig;
 15    import org.jboss.cache.config.CacheLoaderConfig;
 16    import org.jboss.cache.config.Configuration;
 17    import org.jboss.cache.config.Configuration.CacheMode;
 18    import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
 19    import org.jboss.cache.factories.XmlConfigurationParser;
 20    import org.jboss.cache.loader.CacheLoader;
 21    import org.jboss.cache.loader.CacheLoaderManager;
 22    import org.jboss.cache.misc.TestingUtil;
 23    import org.jboss.cache.xml.XmlHelper;
 24    import org.jgroups.Address;
 25    import org.w3c.dom.Element;
 26   
 27    import javax.transaction.TransactionManager;
 28    import java.io.File;
 29   
 30    /**
 31    * Base class for BR tests
 32    *
 33    * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 34    */
 35    public abstract class BuddyReplicationTestsBase extends TestCase
 36    {
 37    protected CacheImpl[] caches;
 38   
 39  50 protected void tearDown() throws Exception
 40    {
 41  50 System.out.println("***** TEARING DOWN *****");
 42  50 System.setProperty("org.jboss.cache.shutdown.force", "true");
 43  50 if (caches != null)
 44    {
 45    // an optimisation to aid the progress of unit tests, especially in the case of TCP connections. Note that this
 46    // is NOT necessary in live systems since each cache would typically be in a separate JVM.
 47  44 for (CacheImpl c : caches)
 48    {
 49  128 if (c != null && c.getBuddyManager() != null) c.getBuddyManager().stop();
 50    }
 51   
 52  44 for (CacheImpl c : caches)
 53    {
 54  136 if (c != null)
 55    {
 56  128 TransactionManager tm = c.getTransactionManager();
 57  128 if (tm != null)
 58    {
 59  126 try
 60    {
 61  0 if (tm.getTransaction() != null) tm.rollback();
 62    }
 63    catch (Exception e)
 64    {
 65    // error rolling back txs
 66  0 e.printStackTrace();
 67    }
 68    }
 69   
 70  128 CacheLoaderManager clm = c.getCacheLoaderManager();
 71  128 if (clm != null)
 72    {
 73  28 CacheLoader cl = c.getCacheLoaderManager().getCacheLoader();
 74  28 try
 75    {
 76  28 if (cl != null) cl.remove(Fqn.ROOT);
 77    }
 78    catch (Exception e)
 79    {
 80    // unable to clean cache loader
 81  0 e.printStackTrace();
 82    }
 83    }
 84   
 85  128 c.killChannel();
 86  128 c.stop();
 87  128 c = null;
 88    }
 89    }
 90    }
 91  50 caches = null;
 92  50 System.gc();
 93    }
 94   
 95    protected static int VIEW_BLOCK_TIMEOUT = 5000;
 96   
 97  2 protected CacheImpl createCache(int numBuddies, String buddyPoolName) throws Exception
 98    {
 99  2 return createCache(numBuddies, buddyPoolName, false, true);
 100    }
 101   
 102  0 protected CacheImpl createCache(int numBuddies, String buddyPoolName, boolean useDataGravitation) throws Exception
 103    {
 104  0 return createCache(numBuddies, buddyPoolName, useDataGravitation, true);
 105    }
 106   
 107  16 protected CacheImpl createCache(int numBuddies, String buddyPoolName, boolean useDataGravitation, boolean start) throws Exception
 108    {
 109  16 return createCache(false, numBuddies, buddyPoolName, useDataGravitation, true, start);
 110    }
 111   
 112  94 protected CacheImpl createCache(boolean optimisticLocks, int numBuddies, String buddyPoolName, boolean useDataGravitation, boolean start) throws Exception
 113    {
 114  94 return createCache(optimisticLocks, numBuddies, buddyPoolName, useDataGravitation, true, start);
 115    }
 116   
 117  28 protected CacheImpl createCache(int numBuddies, String buddyPoolName, boolean useDataGravitation, boolean removeOnFind, boolean start) throws Exception
 118    {
 119  28 return createCache(false, numBuddies, buddyPoolName, useDataGravitation, removeOnFind, start);
 120    }
 121   
 122  138 protected CacheImpl createCache(boolean optimisticLocks, int numBuddies, String buddyPoolName, boolean useDataGravitation, boolean removeOnFind, boolean start) throws Exception
 123    {
 124  138 CacheImpl c = (CacheImpl) DefaultCacheFactory.getInstance().createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), false);
 125  138 c.getConfiguration().setClusterName("BuddyReplicationTest");
 126    // basic config
 127  138 String xmlString = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled>\n" +
 128    "<buddyCommunicationTimeout>500</buddyCommunicationTimeout>\n" +
 129    " <buddyLocatorClass>org.jboss.cache.buddyreplication.NextMemberBuddyLocator</buddyLocatorClass>\n" +
 130    " <autoDataGravitation>" + useDataGravitation + "</autoDataGravitation>\n" +
 131    " <dataGravitationRemoveOnFind>" + removeOnFind + "</dataGravitationRemoveOnFind>\n" +
 132    " <buddyLocatorProperties>numBuddies = " + numBuddies + "</buddyLocatorProperties>\n";
 133   
 134  26 if (buddyPoolName != null) xmlString += "<buddyPoolName>" + buddyPoolName + "</buddyPoolName>";
 135  138 xmlString += "</config>";
 136  138 Element element = XmlHelper.stringToElement(xmlString);
 137  138 BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element);
 138  138 c.getConfiguration().setBuddyReplicationConfig(config);
 139   
 140  138 c.getConfiguration().setFetchInMemoryState(true);
 141  138 if (optimisticLocks)
 142    {
 143  12 c.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
 144    }
 145  138 c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
 146  138 c.getConfiguration().setSyncCommitPhase(true);// helps track down breakages
 147   
 148    // Call the hook that allows mux integration if that's what the test wants
 149  138 configureMultiplexer(c);
 150   
 151  138 if (start)
 152    {
 153  102 c.start();
 154  102 validateMultiplexer(c);
 155    }
 156  138 return c;
 157    }
 158   
 159    /**
 160    * Provides a hook for multiplexer integration. This default implementation
 161    * is a no-op; subclasses that test mux integration would override
 162    * to integrate the given cache with a multiplexer.
 163    * <p/>
 164    * param cache a cache that has been configured but not yet created.
 165    */
 166  108 protected void configureMultiplexer(Cache cache) throws Exception
 167    {
 168    // default does nothing
 169    }
 170   
 171    /**
 172    * Provides a hook to check that the cache's channel came from the
 173    * multiplexer, or not, as expected. This default impl asserts that
 174    * the channel did not come from the multiplexer.
 175    *
 176    * @param cache a cache that has already been started
 177    */
 178  74 protected void validateMultiplexer(Cache cache)
 179    {
 180  74 assertFalse("Cache is not using multiplexer", cache.getConfiguration().isUsingMultiplexer());
 181    }
 182   
 183  11 protected CacheImpl[] createCaches(int numCaches, boolean useBuddyPool) throws Exception
 184    {
 185  11 return createCaches(1, numCaches, useBuddyPool, false);
 186    }
 187   
 188  14 protected CacheImpl[] createCaches(int numCaches, boolean useBuddyPool, boolean useDataGravitation, boolean optimisticLocks) throws Exception
 189    {
 190  14 return createCaches(1, numCaches, useBuddyPool, useDataGravitation, optimisticLocks);
 191    }
 192   
 193  1 protected CacheImpl[] createCaches(int numCaches, boolean useBuddyPool, boolean useDataGravitation) throws Exception
 194    {
 195  1 return createCaches(1, numCaches, useBuddyPool, useDataGravitation);
 196    }
 197   
 198  8 protected CacheImpl[] createCachesWithCacheLoader(int numCaches, boolean useDataGravitation, boolean removeOnFind, boolean passivation) throws Exception
 199    {
 200  8 return this.createCachesWithCacheLoader(numCaches, useDataGravitation, removeOnFind, passivation, false);
 201    }
 202   
 203  8 protected CacheImpl[] createCachesWithCacheLoader(int numCaches, boolean useDataGravitation, boolean removeOnFind, boolean passivation, boolean fetchPersistent) throws Exception
 204    {
 205  8 String tmpLoc = System.getProperty("java.io.tmpdir", "/tmp");
 206  8 tmpLoc += File.separator + "BuddyReplicationTestsBase-";
 207   
 208  8 CacheImpl[] caches = new CacheImpl[numCaches];
 209  8 for (int i = 0; i < numCaches; i++)
 210    {
 211  24 caches[i] = createCacheWithCacheLoader(useDataGravitation, removeOnFind, passivation, fetchPersistent, true);
 212    }
 213   
 214    // allow some time for the caches to start up and discover each other
 215  8 TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
 216  8 TestingUtil.sleepThread(getSleepTimeout());
 217  8 return caches;
 218    }
 219   
 220  28 protected CacheImpl createCacheWithCacheLoader(boolean useDataGravitation, boolean removeOnFind, boolean passivation, boolean fetchPersistent, boolean start) throws Exception
 221    {
 222  28 CacheImpl cache = createCache(1, null, useDataGravitation, removeOnFind, false);
 223   
 224  28 String cloader = "<config>\n" +
 225    "<passivation>" + passivation + "</passivation>\n" +
 226    "<preload></preload>\n" +
 227   
 228    "<cacheloader>\n" +
 229    "<class>org.jboss.cache.loader.DummyInMemoryCacheLoader</class>\n" +
 230    // "<properties>location=" + (location) + "</properties>\n" +
 231    "<async>false</async>\n" +
 232    "<shared>false</shared>\n" +
 233    "<fetchPersistentState>" + fetchPersistent + "</fetchPersistentState>\n" +
 234    "</cacheloader>\n" +
 235    "</config>";
 236   
 237  28 Element element = XmlHelper.stringToElement(cloader);
 238  28 CacheLoaderConfig cfg = XmlConfigurationParser.parseCacheLoaderConfig(element);
 239  28 cache.getConfiguration().setCacheLoaderConfig(cfg);
 240  28 if (start)
 241    {
 242  24 cache.start();
 243    }
 244   
 245  28 return cache;
 246    }
 247   
 248  5 protected CacheImpl[] createCaches(int numBuddies, int numCaches, boolean useBuddyPool) throws Exception
 249    {
 250  5 return createCaches(numBuddies, numCaches, useBuddyPool, false);
 251    }
 252   
 253  17 protected CacheImpl[] createCaches(int numBuddies, int numCaches, boolean useBuddyPool, boolean useDataGravitation) throws Exception
 254    {
 255  17 return createCaches(numBuddies, numCaches, useBuddyPool, useDataGravitation, false);
 256    }
 257   
 258  31 protected CacheImpl[] createCaches(int numBuddies, int numCaches, boolean useBuddyPool, boolean useDataGravitation, boolean optimisticLocks) throws Exception
 259    {
 260  31 CacheImpl[] caches = new CacheImpl[numCaches];
 261  31 for (int i = 0; i < numCaches; i++)
 262    {
 263  94 caches[i] = createCache(optimisticLocks, numBuddies, useBuddyPool ? Character.toString((char) ('A' + i)) : null, useDataGravitation, true);
 264    }
 265   
 266    // allow some time for the caches to start up and discover each other
 267  31 TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
 268  31 TestingUtil.sleepThread(getSleepTimeout());
 269  31 return caches;
 270    }
 271   
 272  56 protected void printBuddyGroup(CacheImpl cache)
 273    {
 274  56 BuddyGroup bg = cache.getBuddyManager().buddyGroup;
 275  56 System.out.println("*** " + bg);
 276  56 System.out.println(" Groups I participate in: " + cache.getBuddyManager().buddyGroupsIParticipateIn.keySet());
 277    }
 278   
 279    /**
 280    * This is to allow for any state transfers involved (when assigning a buddy) to complete
 281    */
 282  50 protected int getSleepTimeout()
 283    {
 284  50 return 1000;
 285    }
 286   
 287  50 protected void assertIsBuddy(CacheImpl dataOwner, CacheImpl buddy, boolean onlyBuddy)
 288    {
 289  50 Address dataOwnerLocalAddress = dataOwner.getLocalAddress();
 290  50 Address buddyLocalAddress = buddy.getLocalAddress();
 291   
 292  50 System.out.println("*** assert with groups. Testing that " + buddyLocalAddress + " is a buddy for owner " + dataOwnerLocalAddress + " only buddy? " + onlyBuddy);
 293  50 printBuddyGroup(dataOwner);
 294   
 295  50 BuddyManager dataOwnerBuddyManager = dataOwner.getBuddyManager();
 296  50 BuddyManager buddyBuddyManager = buddy.getBuddyManager();
 297   
 298    // lets test things on the data owner's side of things
 299  19 if (onlyBuddy) assertEquals("Should only have one buddy", 1, dataOwnerBuddyManager.getBuddyAddresses().size());
 300   
 301  50 assertTrue(buddyLocalAddress + " should be a buddy to " + dataOwnerLocalAddress, dataOwnerBuddyManager.getBuddyAddresses().contains(buddyLocalAddress));
 302   
 303    // and now on the buddy end
 304  50 BuddyGroup group = buddyBuddyManager.buddyGroupsIParticipateIn.get(BuddyManager.getGroupNameFromAddress(dataOwnerLocalAddress));
 305  50 System.out.println("*** Buddy's version of dataOwner's group " + group);
 306   
 307  50 assertTrue("buddy's list of groups it participates in should contain data owner's group name", buddyBuddyManager.buddyGroupsIParticipateIn.containsKey(BuddyManager.getGroupNameFromAddress(dataOwnerLocalAddress)));
 308  19 if (onlyBuddy) assertEquals(1, group.getBuddies().size());
 309  49 assertTrue(buddyLocalAddress + " should be a buddy to " + group.getGroupName(), group.getBuddies().contains(buddyLocalAddress));
 310    }
 311   
 312  48 protected void dumpCacheContents(CacheImpl... caches)
 313    {
 314  48 System.out.println("**** START: Cache Contents ****");
 315  48 for (int i = 0; i < caches.length; i++)
 316    {
 317  144 if (caches[i] == null)
 318    {
 319  3 System.out.println(" ** Cache " + i + " is null!");
 320    }
 321    else
 322    {
 323  141 System.out.println(" ** Cache " + i + " is " + caches[i].getLocalAddress());
 324  141 System.out.println(" " + caches[i].printLockInfo());
 325    }
 326    }
 327  48 System.out.println("**** END: Cache Contents ****");
 328    }
 329   
 330  24 protected void assertNoLocks(CacheImpl[] caches)
 331    {
 332  24 for (CacheImpl cache : caches)
 333    {
 334  72 if (cache != null) assertEquals(0, cache.getNumberOfLocksHeld());
 335    }
 336    }
 337   
 338    }