Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 397   Methods: 8
NCLOC: 302   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CacheModeLocalTestBase.java 84.2% 97.7% 100% 95.5%
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.options.cachemodelocal;
 8   
 9    import junit.framework.Assert;
 10    import junit.framework.TestCase;
 11    import org.jboss.cache.CacheImpl;
 12    import org.jboss.cache.DefaultCacheFactory;
 13    import org.jboss.cache.Fqn;
 14    import org.jboss.cache.config.Configuration;
 15   
 16    import javax.transaction.SystemException;
 17    import javax.transaction.TransactionManager;
 18    import java.util.HashMap;
 19    import java.util.Map;
 20   
 21    /**
 22    * Tests the cache mode local override in various scenarios. To be subclassed to test REPL_SYNC, REPL_ASYNC, INVALIDATION_SYNC, INVALIDATION_ASYNC for Opt and Pess locking.
 23    * <p/>
 24    * Option.setCacheModeLocal() only applies to put() and remove() methods.
 25    *
 26    * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 27    */
 28    public abstract class CacheModeLocalTestBase extends TestCase
 29    {
 30    // to be subclassed.
 31    protected Configuration.CacheMode cacheMode;
 32    protected String nodeLockingScheme;
 33    /**
 34    * set this to true if the implementing class plans to use an invalidating cache mode *
 35    */
 36    protected boolean isInvalidation;
 37   
 38    private CacheImpl cache1;
 39    private CacheImpl cache2;
 40   
 41    private Fqn fqn = Fqn.fromString("/a");
 42    private String key = "key";
 43   
 44  40 protected void setUp() throws Exception
 45    {
 46    // force a tear down if the test runner didn't run one before (happens in IDEA)
 47  0 if (cache1 != null || cache2 != null) tearDown();
 48   
 49  40 cache1 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 50  40 cache1.getConfiguration().setClusterName("test");
 51  40 cache1.getConfiguration().setStateRetrievalTimeout(1000);
 52  40 cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
 53  40 cache1.getConfiguration().setNodeLockingScheme(nodeLockingScheme);
 54  40 cache1.getConfiguration().setCacheMode(cacheMode);
 55  40 cache1.start();
 56   
 57  40 cache2 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 58  40 cache2.getConfiguration().setClusterName("test");
 59  40 cache2.getConfiguration().setStateRetrievalTimeout(1000);
 60  40 cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
 61  40 cache2.getConfiguration().setNodeLockingScheme(nodeLockingScheme);
 62  40 cache2.getConfiguration().setCacheMode(cacheMode);
 63  40 cache2.start();
 64    }
 65   
 66  40 protected void tearDown()
 67    {
 68  40 if (cache1 != null)
 69    {
 70  40 cache1.stop();
 71  40 flushTxs(cache1.getTransactionManager());
 72  40 cache1 = null;
 73    }
 74   
 75  40 if (cache2 != null)
 76    {
 77  40 cache2.stop();
 78  40 flushTxs(cache2.getTransactionManager());
 79  40 cache2 = null;
 80    }
 81    }
 82   
 83  80 private void flushTxs(TransactionManager mgr)
 84    {
 85  80 if (mgr != null)
 86    {
 87  0 try
 88    {
 89  0 if (mgr.getTransaction() != null)
 90    {
 91  0 mgr.rollback();
 92    }
 93    }
 94    catch (SystemException e)
 95    {
 96    // do nothing
 97    }
 98    }
 99    }
 100   
 101  8 public void testPutKeyValue() throws Exception
 102    {
 103  8 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
 104  8 cache1.put(fqn, key, "value");
 105  8 delay();
 106    // cache1 should still have this
 107  8 Assert.assertEquals("value", cache1.get(fqn, key));
 108   
 109    // cache 2 should not
 110  8 Assert.assertNull("Should be null", cache2.get(fqn, key));
 111   
 112    // now try again with passing the default options
 113  8 cache1.getInvocationContext().getOptionOverrides().reset();
 114  8 cache1.put(fqn, key, "value");
 115  8 delay();
 116    // cache1 should still have this
 117  8 Assert.assertEquals("value", cache1.get(fqn, key));
 118   
 119    // cache 2 should as well
 120  8 if (!isInvalidation)
 121    {
 122  4 Assert.assertEquals("value", cache2.get(fqn, key));
 123    }
 124    else
 125    {
 126  4 Assert.assertNull("should be invalidated", cache2.get(fqn, key));
 127    }
 128   
 129    // now cache2
 130  8 cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
 131  8 cache2.put(fqn, key, "value2");
 132  8 delay();
 133  8 Assert.assertEquals("value2", cache2.get(fqn, key));
 134  8 Assert.assertEquals("value", cache1.get(fqn, key));
 135   
 136  8 cache2.getInvocationContext().getOptionOverrides().reset();
 137  8 cache2.put(fqn, key, "value2");
 138  8 delay();
 139  8 Assert.assertEquals("value2", cache2.get(fqn, key));
 140  8 if (!isInvalidation)
 141    {
 142  4 Assert.assertEquals("value2", cache1.get(fqn, key));
 143    }
 144    else
 145    {
 146  4 Assert.assertNull("should be invalidated", cache1.get(fqn, key));
 147    }
 148    }
 149   
 150  8 public void testPutData() throws Exception
 151    {
 152  8 Map map = new HashMap();
 153  8 map.put(key, "value");
 154   
 155  8 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
 156  8 cache1.put(fqn, map);
 157  8 delay();
 158    // cache1 should still have this
 159  8 Assert.assertEquals("value", cache1.get(fqn, key));
 160    // cache 2 should not
 161  8 Assert.assertNull("Should be null", cache2.get(fqn, key));
 162   
 163    // now try again with passing the default options
 164  8 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(false);
 165  8 cache1.put(fqn, map);
 166  8 delay();
 167    // cache1 should still have this
 168  8 Assert.assertEquals("value", cache1.get(fqn, key));
 169    // cache 2 should as well
 170  8 if (!isInvalidation)
 171    {
 172  4 Assert.assertEquals("value", cache2.get(fqn, key));
 173    }
 174    else
 175    {
 176  4 Assert.assertNull("should be invalidated", cache2.get(fqn, key));
 177    }
 178   
 179    // now cache2
 180  8 map.put(key, "value2");
 181  8 cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
 182  8 cache2.put(fqn, map);
 183  8 delay();
 184  8 Assert.assertEquals("value2", cache2.get(fqn, key));
 185  8 Assert.assertEquals("value", cache1.get(fqn, key));
 186   
 187  8 cache2.getInvocationContext().getOptionOverrides().reset();
 188  8 cache2.put(fqn, key, "value2");
 189  8 delay();
 190  8 Assert.assertEquals("value2", cache2.get(fqn, key));
 191  8 if (!isInvalidation)
 192    {
 193  4 Assert.assertEquals("value2", cache1.get(fqn, key));
 194    }
 195    else
 196    {
 197  4 Assert.assertNull("should be invalidated", cache1.get(fqn, key));
 198    }
 199    }
 200   
 201  8 public void testRemoveNode() throws Exception
 202    {
 203    // put some stuff in the cache first
 204    // make sure we cleanup thread local vars.
 205  8 cache1.getInvocationContext().setOptionOverrides(null);
 206  8 cache1.put(fqn, key, "value");
 207  8 delay();
 208  8 Assert.assertEquals("value", cache1.get(fqn, key));
 209  8 if (isInvalidation)
 210    {
 211  4 Assert.assertNull("Should be null", cache2.get(fqn, key));
 212    }
 213    else
 214    {
 215  4 Assert.assertEquals("value", cache2.get(fqn, key));
 216    }
 217   
 218  8 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
 219  8 cache1.removeNode(fqn);
 220  8 delay();
 221   
 222    // should be removed in cache1
 223  8 Assert.assertNull("should be null", cache1.get(fqn, key));
 224    // Not in cache2
 225  8 if (isInvalidation)
 226    {
 227  4 Assert.assertNull("Should be null", cache2.get(fqn, key));
 228    }
 229    else
 230    {
 231  4 Assert.assertEquals("value", cache2.get(fqn, key));
 232    }
 233   
 234    // replace cache entries
 235  8 cache1.put(fqn, key, "value");
 236  8 delay();
 237  8 Assert.assertEquals("value", cache1.get(fqn, key));
 238  8 if (isInvalidation)
 239    {
 240  4 Assert.assertNull("Should be null", cache2.get(fqn, key));
 241    }
 242    else
 243    {
 244  4 Assert.assertEquals("value", cache2.get(fqn, key));
 245    }
 246   
 247    // now try again with passing the default options
 248  8 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(false);
 249  8 cache1.remove(fqn);
 250  8 delay();
 251   
 252    // both should be null
 253  8 Assert.assertNull("should be null", cache1.get(fqn, key));
 254  8 Assert.assertNull("should be null", cache2.get(fqn, key));
 255    }
 256   
 257  8 public void testRemoveKey() throws Exception
 258    {
 259    // put some stuff in the cache first
 260  8 cache1.getInvocationContext().setOptionOverrides(null);
 261  8 cache1.put(fqn, key, "value");
 262  8 delay();
 263  8 Assert.assertEquals("value", cache1.get(fqn, key));
 264  8 if (isInvalidation)
 265    {
 266  4 Assert.assertNull("Should be null", cache2.get(fqn, key));
 267    }
 268    else
 269    {
 270  4 Assert.assertEquals("value", cache2.get(fqn, key));
 271    }
 272   
 273  8 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
 274  8 cache1.remove(fqn, key);
 275  8 delay();
 276   
 277    // should be removed in cache1
 278  8 Assert.assertNull("should be null", cache1.get(fqn, key));
 279    // Not in cache2
 280  8 if (isInvalidation)
 281    {
 282  4 Assert.assertNull("Should be null", cache2.get(fqn, key));
 283    }
 284    else
 285    {
 286  4 Assert.assertEquals("value", cache2.get(fqn, key));
 287    }
 288   
 289    // replace cache entries
 290  8 cache1.put(fqn, key, "value");
 291  8 delay();
 292  8 Assert.assertEquals("value", cache1.get(fqn, key));
 293  8 if (isInvalidation)
 294    {
 295  4 Assert.assertNull("Should be null", cache2.get(fqn, key));
 296    }
 297    else
 298    {
 299  4 Assert.assertEquals("value", cache2.get(fqn, key));
 300    }
 301   
 302    // now try again with passing the default options
 303  8 cache1.getInvocationContext().getOptionOverrides().reset();
 304  8 cache1.remove(fqn, key);
 305  8 delay();
 306   
 307    // both should be null
 308  8 Assert.assertNull("should be null", cache1.get(fqn, key));
 309  8 Assert.assertNull("should be null", cache2.get(fqn, key));
 310    }
 311   
 312  8 public void testTransactionalBehaviour() throws Exception
 313    {
 314  8 TransactionManager mgr = cache1.getTransactionManager();
 315  8 mgr.begin();
 316  8 cache1.getInvocationContext().getOptionOverrides().reset();
 317  8 cache1.put(fqn, key, "value1");
 318  8 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
 319  8 cache1.put(fqn, key, "value2");
 320  8 mgr.commit();
 321  8 delay();
 322    // cache1 should still have this
 323  8 Assert.assertEquals("value2", cache1.get(fqn, key));
 324   
 325  8 if (!isInvalidation)
 326    {
 327  4 Assert.assertEquals("value1", cache2.get(fqn, key));
 328    }
 329    else
 330    {
 331  4 assertNull(cache2.get(fqn, key));
 332    }
 333   
 334    // now try again with passing the default options
 335  8 mgr.begin();
 336  8 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
 337  8 cache1.put(fqn, key, "value3");
 338  8 cache1.getInvocationContext().getOptionOverrides().reset();
 339  8 cache1.put(fqn, key, "value");
 340  8 mgr.commit();
 341  8 delay();
 342    // cache1 should still have this
 343  8 Assert.assertEquals("value", cache1.get(fqn, key));
 344   
 345    // cache 2 should as well
 346  8 if (!isInvalidation)
 347    {
 348  4 Assert.assertEquals("value", cache2.get(fqn, key));
 349    }
 350    else
 351    {
 352  4 Assert.assertNull("should be invalidated", cache2.get(fqn, key));
 353    }
 354   
 355    // now cache2
 356  8 mgr = cache2.getTransactionManager();
 357  8 mgr.begin();
 358  8 cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(false);
 359  8 cache2.put(fqn, key, "value3");
 360  8 cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
 361  8 cache2.put(fqn, key, "value2");
 362  8 mgr.commit();
 363  8 delay();
 364   
 365  8 Assert.assertEquals("value2", cache2.get(fqn, key));
 366   
 367  8 if (!isInvalidation)
 368    {
 369  4 assertEquals("value3", cache1.get(fqn, key));
 370    }
 371    else
 372    {
 373  4 assertNull(cache1.get(fqn, key));
 374    }
 375   
 376  8 mgr.begin();
 377  8 cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
 378  8 cache2.put(fqn, key, "value2");
 379  8 cache2.getInvocationContext().getOptionOverrides().reset();
 380  8 cache2.put(fqn, key, "value2");
 381  8 mgr.commit();
 382  8 delay();
 383  8 Assert.assertEquals("value2", cache2.get(fqn, key));
 384  8 if (!isInvalidation)
 385    {
 386  4 Assert.assertEquals("value2", cache1.get(fqn, key));
 387    }
 388    else
 389    {
 390  4 Assert.assertNull("should be invalidated", cache1.get(fqn, key));
 391    }
 392   
 393    }
 394   
 395    protected abstract void delay();
 396   
 397    }