Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 226   Methods: 8
NCLOC: 111   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BuddyBackupActivationInactivationTest.java 8.3% 20.5% 62.5% 23.4%
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 org.jboss.cache.CacheImpl;
 10    import org.jboss.cache.DefaultCacheFactory;
 11    import org.jboss.cache.Fqn;
 12    import org.jboss.cache.config.BuddyReplicationConfig;
 13    import org.jboss.cache.config.Configuration;
 14    import org.jboss.cache.config.Configuration.CacheMode;
 15    import org.jboss.cache.factories.UnitTestCacheFactory;
 16    import org.jboss.cache.factories.XmlConfigurationParser;
 17    import org.w3c.dom.Document;
 18    import org.w3c.dom.Element;
 19   
 20    import javax.xml.parsers.DocumentBuilder;
 21    import javax.xml.parsers.DocumentBuilderFactory;
 22    import java.util.HashMap;
 23    import java.util.Map;
 24    import java.util.Set;
 25   
 26    /**
 27    * Tests handling of the buddy backup region during region
 28    * activation and inactivation
 29    *
 30    * @author Brian Stansberry
 31    */
 32    public class BuddyBackupActivationInactivationTest extends BuddyReplicationTestsBase
 33    {
 34    public static final Fqn A = Fqn.fromString("/a");
 35    public static final Fqn A_B = Fqn.fromString("/a/b");
 36    public static final String JOE = "JOE";
 37   
 38    protected Map caches;
 39    private ClassLoader orig_TCL;
 40   
 41  2 public void testBuddyBackupActivation() throws Exception
 42    {
 43    /** TODO: Uncomment once we have FLUSH in place
 44    *
 45   
 46    CacheImpl[] caches = new CacheImpl[2];
 47    caches[0] = createCache("cache1", true, true, true);
 48    caches[1] = createCache("cache2", true, true, true);
 49   
 50    TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
 51   
 52    caches[0].activateRegion("/a");
 53   
 54    caches[0].put(A_B, "name", JOE);
 55   
 56    TestingUtil.sleepThread(getSleepTimeout());
 57   
 58    caches[1].activateRegion("/a");
 59   
 60    Fqn fqn = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN,
 61    BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()));
 62    fqn = new Fqn(fqn, A_B);
 63   
 64    assertEquals("State transferred with activation", JOE, caches[1].get(fqn, "name"));
 65    */
 66    }
 67   
 68  2 public void testReplToInactiveRegion() throws Exception
 69    {
 70    // TODO: Reinstate once we have proper FLUSH working.
 71    // This test relies on calls to inactive regions being queued and re-run
 72    // when the region is activated. After discussions with Brian, the queueing
 73    // was removed, presuming FLUSH will fix this. Need to test with FLUSH.
 74    // - Manik Surtani (16 Oct 2006)
 75    /*
 76    CacheImpl[] caches = new CacheImpl[2];
 77    caches[0] = createCache("cache1", true, true, true);
 78    caches[1] = createCache("cache2", true, true, true);
 79   
 80    TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
 81   
 82    caches[0].activateRegion("/a");
 83   
 84    // Activate the buddy backup subtree in the recipient so any
 85    // repl message doesn't get rejected due to that tree being inactive
 86    caches[1].activateRegion(BuddyManager.BUDDY_BACKUP_SUBTREE);
 87   
 88    caches[0].put(A_B, "name", JOE);
 89   
 90    TestingUtil.sleepThread(getSleepTimeout());
 91   
 92    assertNull("No replication to inactive region", caches[1].get(A_B, "name"));
 93   
 94    Fqn fqn = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN,
 95    BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()));
 96    fqn = new Fqn(fqn, A_B);
 97   
 98    assertNull("No replication to inactive backup region", caches[1].get(fqn, "name"));
 99    */
 100    }
 101   
 102  2 public void testBuddyBackupInactivation() throws Exception
 103    {
 104    // TODO: Reinstate once we have proper FLUSH working.
 105    // This test relies on calls to inactive regions being queued and re-run
 106    // when the region is activated. After discussions with Brian, the queueing
 107    // was removed, presuming FLUSH will fix this. Need to test with FLUSH.
 108    // - Manik Surtani (16 Oct 2006)
 109    /*
 110    CacheImpl cache1 = createCache("cache1", true, true, true);
 111   
 112    cache1.activateRegion("/a");
 113   
 114    Fqn fqn = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, "test");
 115    fqn = new Fqn(fqn, A_B);
 116    cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
 117    cache1.put(fqn, "name", JOE);
 118   
 119    assertEquals("Put OK", JOE, cache1.get(fqn, "name"));
 120   
 121    cache1.inactivateRegion("/a");
 122   
 123    assertNull("Inactivation cleared region", cache1.get(fqn, "name"));
 124    */
 125   
 126    }
 127   
 128  0 protected CacheImpl createCache(String cacheID,
 129    boolean sync,
 130    boolean useMarshalling,
 131    boolean startCache)
 132    throws Exception
 133    {
 134  0 if (caches.get(cacheID) != null)
 135    {
 136  0 throw new IllegalStateException(cacheID + " already created");
 137    }
 138   
 139  0 CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 140   
 141  0 CacheMode mode = sync ? CacheMode.REPL_SYNC : CacheMode.REPL_ASYNC;
 142  0 Configuration c = UnitTestCacheFactory.createConfiguration(mode);
 143   
 144  0 cache.setConfiguration(c);
 145  0 cache.getConfiguration().setClusterName("Test");
 146  0 if (useMarshalling)
 147    {
 148  0 cache.getConfiguration().setUseRegionBasedMarshalling(true);
 149  0 cache.getConfiguration().setInactiveOnStartup(true);
 150    }
 151  0 cache.getConfiguration().setBuddyReplicationConfig(getBuddyConfig());
 152    // Put the cache in the map before starting, so if it fails in
 153    // start it can still be destroyed later
 154  0 caches.put(cacheID, cache);
 155   
 156  0 if (startCache)
 157    {
 158  0 cache.create();
 159  0 cache.start();
 160    }
 161   
 162  0 return cache;
 163    }
 164   
 165  6 protected void setUp() throws Exception
 166    {
 167  6 super.setUp();
 168   
 169  6 caches = new HashMap();
 170   
 171    // Save the TCL in case a test changes it
 172  6 orig_TCL = Thread.currentThread().getContextClassLoader();
 173    }
 174   
 175  6 protected void tearDown() throws Exception
 176    {
 177  6 super.tearDown();
 178   
 179    // Restore the TCL in case a test changed it
 180  6 Thread.currentThread().setContextClassLoader(orig_TCL);
 181   
 182  6 Set keys = caches.keySet();
 183  6 String[] cacheIDs = new String[keys.size()];
 184  6 cacheIDs = (String[]) keys.toArray(cacheIDs);
 185  6 for (int i = 0; i < cacheIDs.length; i++)
 186    {
 187  0 stopCache((CacheImpl) caches.get(cacheIDs[i]));
 188    }
 189    }
 190   
 191  0 protected void stopCache(CacheImpl cache)
 192    {
 193  0 if (cache != null)
 194    {
 195  0 try
 196    {
 197  0 cache.stop();
 198  0 cache.destroy();
 199    }
 200    catch (Exception e)
 201    {
 202  0 System.out.println("Exception stopping cache " + e.getMessage());
 203  0 e.printStackTrace(System.out);
 204    }
 205    }
 206    }
 207   
 208  0 private BuddyReplicationConfig getBuddyConfig() throws Exception
 209    {
 210    // TODO just build the object; skip the legacy XML business
 211   
 212  0 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 213  0 DocumentBuilder db = dbf.newDocumentBuilder();
 214  0 Document doc = db.newDocument();
 215  0 Element config = doc.createElement("config");
 216  0 doc.appendChild(config);
 217  0 Element replEnabled = doc.createElement("buddyReplicationEnabled");
 218  0 replEnabled.appendChild(doc.createTextNode("true"));
 219  0 config.appendChild(replEnabled);
 220  0 Element gravDisabled = doc.createElement("autoDataGravitation");
 221  0 gravDisabled.appendChild(doc.createTextNode("false"));
 222  0 config.appendChild(gravDisabled);
 223   
 224  0 return XmlConfigurationParser.parseBuddyReplicationConfig(config);
 225    }
 226    }