Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 256   Methods: 7
NCLOC: 154   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
OptimisticWithCacheLoaderTest.java 50% 100% 100% 99.2%
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.optimistic;
 8   
 9    import junit.framework.Assert;
 10    import org.jboss.cache.CacheImpl;
 11    import org.jboss.cache.loader.CacheLoader;
 12    import org.jboss.cache.transaction.DummyTransactionManager;
 13   
 14    import javax.transaction.Transaction;
 15   
 16    /**
 17    * Tests optimistic locking with cache loaders
 18    *
 19    * @author Manik Surtani (<a href="mailto:manik@jboss.org">manik@jboss.org</a>)
 20    */
 21    public class OptimisticWithCacheLoaderTest extends AbstractOptimisticTestCase
 22    {
 23   
 24  6 public OptimisticWithCacheLoaderTest(String s)
 25    {
 26  6 super(s);
 27    }
 28   
 29   
 30  1 public void testLoaderIndependently() throws Exception
 31    {
 32  1 CacheImpl cache = createCacheWithLoader();
 33  1 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
 34   
 35    // test the cache loader independently first ...
 36  1 loader.remove(fqn);
 37  1 Assert.assertNull(loader.get(fqn));
 38  1 loader.put(fqn, key, value);
 39  1 Assert.assertEquals(value, loader.get(fqn).get(key));
 40    // clean up
 41  1 loader.remove(fqn);
 42  1 Assert.assertNull(loader.get(fqn));
 43    }
 44   
 45  1 public void testCacheLoadOnTree() throws Exception
 46    {
 47  1 CacheLoader loader = null;
 48  1 try
 49    {
 50  1 CacheImpl cache = createCacheWithLoader();
 51  1 loader = cache.getCacheLoaderManager().getCacheLoader();
 52   
 53  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 54  1 Transaction tx;
 55   
 56    // make sure the fqn is not in cache
 57  1 assertNull(cache.get(fqn));
 58   
 59    // put something in the loader and make sure all tx's can see it
 60  1 loader.put(fqn, key, value);
 61   
 62    // start the 1st tx
 63  1 mgr.begin();
 64  1 tx = mgr.getTransaction();
 65  1 assertEquals(value, cache.get(fqn, key));
 66  1 mgr.suspend();
 67   
 68    // start a new tx
 69  1 mgr.begin();
 70  1 assertEquals(value, cache.get(fqn, key));
 71  1 mgr.commit();
 72   
 73  1 mgr.resume(tx);
 74  1 assertEquals(value, cache.get(fqn, key));
 75  1 mgr.commit();
 76    }
 77    finally
 78    {
 79    // cleanup
 80  1 if (loader != null) loader.remove(fqn);
 81    }
 82    }
 83   
 84  1 public void testCacheStoring() throws Exception
 85    {
 86  1 Transaction tx;
 87  1 CacheImpl cache = createCacheWithLoader();
 88  1 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
 89   
 90    // test the cache ...
 91  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 92  1 assertNull(mgr.getTransaction());
 93  1 mgr.begin();
 94  1 cache.put(fqn, key, value);
 95  1 mgr.commit();
 96   
 97  1 Assert.assertEquals(value, cache.get(fqn, key));
 98   
 99    //now lets see if the state has been persisted in the cache loader
 100  1 Assert.assertEquals(value, loader.get(fqn).get(key));
 101   
 102   
 103  1 mgr.begin();
 104  1 cache.remove(fqn);
 105  1 mgr.commit();
 106   
 107  1 Assert.assertNull(cache.get(fqn, key));
 108    //now lets see if the state has been persisted in the cache loader
 109  1 Assert.assertNull(loader.get(fqn));
 110   
 111  1 mgr.begin();
 112  1 cache.put(fqn, key, value);
 113  1 tx = mgr.getTransaction();
 114  1 mgr.suspend();
 115   
 116    // lets see what we've got halfway within a tx
 117  1 Assert.assertNull(cache.get(fqn, key));
 118  1 Assert.assertNull(loader.get(fqn));
 119   
 120  1 mgr.resume(tx);
 121  1 mgr.commit();
 122   
 123    // and after committing...
 124  1 Assert.assertEquals(value, cache.get(fqn, key));
 125  1 Assert.assertEquals(value, loader.get(fqn).get(key));
 126   
 127    // clean up loader
 128  1 loader.remove(fqn);
 129    }
 130   
 131  1 public void testCacheLoading() throws Exception
 132    {
 133  1 CacheImpl cache = createCacheWithLoader();
 134  1 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
 135   
 136  1 Assert.assertNull(cache.get(fqn, key));
 137   
 138    // put something in the loader
 139  1 loader.put(fqn, key, value);
 140  1 Assert.assertEquals(value, loader.get(fqn).get(key));
 141   
 142    // test that this can now be accessed by the cache
 143  1 Assert.assertEquals(value, cache.get(fqn, key));
 144   
 145    // clean up loader
 146  1 loader.remove(fqn);
 147   
 148    // what's in the cache now? Should not ne null...
 149  1 Assert.assertNotNull(cache.get(fqn, key));
 150    }
 151   
 152  1 public void testCacheLoadingWithReplication() throws Exception
 153    {
 154  1 CacheImpl cache1 = createReplicatedCacheWithLoader(false);
 155  1 CacheLoader loader1 = cache1.getCacheLoader();
 156   
 157  1 CacheImpl cache2 = createReplicatedCacheWithLoader(false);
 158  1 CacheLoader loader2 = cache2.getCacheLoader();
 159   
 160    // test the cache ...
 161  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 162  1 assertNull(mgr.getTransaction());
 163   
 164  1 mgr.begin();
 165   
 166    // add something in cache1
 167  1 cache1.put(fqn, key, value);
 168  1 Assert.assertEquals(value, cache1.get(fqn, key));
 169   
 170    // test that loader1, loader2, cache2 doesnt have entry
 171  1 Assert.assertNull(loader1.get(fqn));
 172  1 Assert.assertNull(loader2.get(fqn));
 173  1 Assert.assertNull(cache2.get(fqn, key));
 174   
 175    // commit
 176  1 mgr.commit();
 177   
 178    // test that loader1, loader2, cache2 has entry
 179  1 Assert.assertEquals(value, cache1.get(fqn, key));
 180  1 Assert.assertEquals(value, loader1.get(fqn).get(key));
 181  1 Assert.assertEquals(value, loader2.get(fqn).get(key));
 182  1 Assert.assertEquals(value, cache2.get(fqn, key));
 183   
 184    // cache2 removes entry
 185  1 mgr.begin();
 186  1 cache2.remove(fqn);
 187  1 Assert.assertNull(cache2.get(fqn, key));
 188    // test that loader1, loader2 and cache2 have the entry
 189  1 Assert.assertEquals(value, cache1.get(fqn, key));
 190  1 Assert.assertEquals(value, loader1.get(fqn).get(key));
 191  1 Assert.assertEquals(value, loader2.get(fqn).get(key));
 192   
 193    // commit
 194  1 mgr.commit();
 195   
 196    // test that the entry has been removed everywhere.
 197  1 Assert.assertNull(cache1.get(fqn));
 198  1 Assert.assertNull(loader1.get(fqn));
 199  1 Assert.assertNull(loader2.get(fqn));
 200  1 Assert.assertNull(cache2.get(fqn));
 201   
 202    }
 203   
 204  1 public void testSharedCacheLoadingWithReplication() throws Exception
 205    {
 206  1 CacheImpl cache1 = createReplicatedCacheWithLoader(true);
 207  1 CacheLoader loader1 = cache1.getCacheLoader();
 208   
 209  1 CacheImpl cache2 = createReplicatedCacheWithLoader(true);
 210  1 CacheLoader loader2 = cache2.getCacheLoader();
 211   
 212    // test the cache ...
 213  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 214  1 assertNull(mgr.getTransaction());
 215   
 216  1 mgr.begin();
 217   
 218    // add something in cache1
 219  1 cache1.put(fqn, key, value);
 220  1 Assert.assertEquals(value, cache1.get(fqn, key));
 221   
 222    // test that loader1, loader2, cache2 doesnt have entry
 223  1 System.out.println("*** " + loader1.get(fqn));
 224  1 Assert.assertNull(loader1.get(fqn));
 225  1 Assert.assertNull(loader2.get(fqn));
 226  1 Assert.assertNull(cache2.get(fqn, key));
 227   
 228    // commit
 229  1 mgr.commit();
 230   
 231    // test that loader1, loader2, cache2 has entry
 232  1 Assert.assertEquals(value, cache1.get(fqn, key));
 233  1 Assert.assertEquals(value, loader1.get(fqn).get(key));
 234  1 Assert.assertEquals(value, loader2.get(fqn).get(key));
 235  1 Assert.assertEquals(value, cache2.get(fqn, key));
 236   
 237    // cache2 removes entry
 238  1 mgr.begin();
 239  1 cache2.remove(fqn);
 240  1 Assert.assertNull(cache2.get(fqn, key));
 241    // test that loader1, loader2 and cache2 have the entry
 242  1 Assert.assertEquals(value, cache1.get(fqn, key));
 243  1 Assert.assertEquals(value, loader1.get(fqn).get(key));
 244  1 Assert.assertEquals(value, loader2.get(fqn).get(key));
 245   
 246    // commit
 247  1 mgr.commit();
 248   
 249    // test that the entry has been removed everywhere.
 250  1 Assert.assertNull(cache1.get(fqn));
 251  1 Assert.assertNull(loader1.get(fqn));
 252  1 Assert.assertNull(loader2.get(fqn));
 253  1 Assert.assertNull(cache2.get(fqn));
 254   
 255    }
 256    }