Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 336   Methods: 15
NCLOC: 199   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CacheAPITest.java 83.3% 90.7% 93.3% 90.7%
coverage coverage
 1    package org.jboss.cache.api;
 2   
 3    import junit.framework.TestCase;
 4    import org.jboss.cache.AbstractCacheListener;
 5    import org.jboss.cache.Cache;
 6    import org.jboss.cache.CacheImpl;
 7    import org.jboss.cache.CacheListener;
 8    import org.jboss.cache.DefaultCacheFactory;
 9    import org.jboss.cache.DummyTransactionManagerLookup;
 10    import org.jboss.cache.Fqn;
 11    import org.jboss.cache.Node;
 12    import org.jboss.cache.Region;
 13    import org.jboss.cache.config.Configuration;
 14    import org.jboss.cache.config.ConfigurationException;
 15   
 16    import java.util.ArrayList;
 17    import java.util.HashMap;
 18    import java.util.List;
 19    import java.util.Map;
 20   
 21    /**
 22    * Tests the {@link org.jboss.cache.Cache} public API at a high level
 23    *
 24    * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
 25    */
 26    public class CacheAPITest extends TestCase
 27    {
 28    private Cache cache;
 29    protected boolean optimistic;
 30   
 31  22 protected void setUp() throws Exception
 32    {
 33    // start a single cache instance
 34  22 cache = DefaultCacheFactory.getInstance().createCache("META-INF/local-tx-service.xml", false);
 35  22 cache.getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC);
 36  22 cache.start();
 37    }
 38   
 39  22 protected void tearDown()
 40    {
 41  22 if (cache != null) cache.stop();
 42    }
 43   
 44   
 45    /**
 46    * Tests that the configuration contains the values expected, as well as immutability of certain elements
 47    */
 48  2 public void testConfiguration()
 49    {
 50  2 Configuration c = cache.getConfiguration();
 51  2 assertEquals(Configuration.CacheMode.LOCAL, c.getCacheMode());
 52  2 assertEquals(DummyTransactionManagerLookup.class.getName(), c.getTransactionManagerLookupClass());
 53   
 54    // note that certain values should be immutable. E.g., CacheMode cannot be changed on the fly.
 55  2 try
 56    {
 57  2 c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
 58  0 fail("Should have thrown an Exception");
 59    }
 60    catch (ConfigurationException e)
 61    {
 62    // expected
 63    }
 64   
 65    // others should be changeable though.
 66  2 c.setLockAcquisitionTimeout(100);
 67    }
 68   
 69    /**
 70    * Tests that getRoot() returns the same underlying object as the cache.
 71    */
 72  2 public void testGetRoot()
 73    {
 74    // This does not have to be true ...
 75    // assertSame(cache, cache.getRoot());
 76    }
 77   
 78   
 79    /**
 80    * Basic usage of cache listeners
 81    * <p/>
 82    * A more complete test that tests notifications is in org.jboss.cache.notifications
 83    */
 84  2 public void testCacheListeners()
 85    {
 86  2 assertEquals(0, cache.getCacheListeners().size());
 87   
 88   
 89  2 final List events = new ArrayList();
 90   
 91  2 CacheListener dummy = new AbstractCacheListener()
 92    {
 93  4 public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
 94    {
 95  2 if (pre) events.add("Created");
 96    }
 97    };
 98   
 99  2 cache.addCacheListener(dummy);
 100   
 101  2 assertEquals(1, cache.getCacheListeners().size());
 102   
 103  2 cache.getRoot().addChild(Fqn.fromString("/blah"));
 104   
 105    // test that the event was captured by the listener.
 106   
 107    // FOR A FULL TEST ON NOTIFICATIONS SEE TESTS IN org.jboss.cache.notifications
 108  2 assertEquals(1, events.size());
 109   
 110  2 cache.removeCacheListener(dummy);
 111   
 112  2 assertEquals(0, cache.getCacheListeners().size());
 113    }
 114   
 115    /**
 116    * Test that fqn-specific application of cache listeners has not been implemented and will not be implemented
 117    * in 2.0.0. It is a feature for 2.1.0 but the interface needed to be in place now.
 118    */
 119  2 public void testFqnBasedCacheListeners()
 120    {
 121  2 try
 122    {
 123  2 cache.getCacheListeners(Fqn.ROOT);
 124  0 fail("Fqn-based cache listener operation should throw an exception");
 125    }
 126    catch (Exception e)
 127    {
 128    // expected
 129    }
 130   
 131  2 try
 132    {
 133  2 cache.addCacheListener(Fqn.ROOT, new AbstractCacheListener()
 134    {
 135    });
 136  0 fail("Fqn-based cache listener operation should throw an exception");
 137    }
 138    catch (Exception e)
 139    {
 140    // expected
 141    }
 142   
 143  2 try
 144    {
 145  2 cache.removeCacheListener(Fqn.ROOT, new AbstractCacheListener()
 146    {
 147    });
 148  0 fail("Fqn-based cache listener operation should throw an exception");
 149    }
 150    catch (Exception e)
 151    {
 152    // expected
 153    }
 154    }
 155   
 156    /**
 157    * All cache operations should happen on a {@link Node} - I.e., you look up a {@link Node} and perform data operations
 158    * on this {@link Node}. For convenience and familiarity with JBoss Cache 1.x, we provide some helpers in {@link Cache}
 159    * which dives you direct data access to nodes.
 160    * <p/>
 161    * This test exercises these.
 162    */
 163  2 public void testConvenienceMethods()
 164    {
 165  2 Fqn fqn = Fqn.fromString("/test/fqn");
 166  2 Object key = "key", value = "value";
 167  2 Map data = new HashMap();
 168  2 data.put(key, value);
 169   
 170  2 assertNull(cache.get(fqn, key));
 171   
 172  2 cache.put(fqn, key, value);
 173   
 174  2 assertEquals(value, cache.get(fqn, key));
 175   
 176  2 cache.remove(fqn, key);
 177   
 178  2 assertNull(cache.get(fqn, key));
 179   
 180  2 cache.put(fqn, data);
 181   
 182  2 System.out.println(((CacheImpl) cache).printDetails());
 183   
 184  2 assertEquals(value, cache.get(fqn, key));
 185    }
 186   
 187   
 188    /**
 189    * Another convenience method that tests node removal
 190    */
 191  0 public void nodeConvenienceNodeRemoval()
 192    {
 193    // this fqn is relative, but since it is from the root it may as well be absolute
 194  0 Fqn fqn = Fqn.fromString("/test/fqn");
 195  0 cache.getRoot().addChild(fqn);
 196  0 assertTrue(cache.getRoot().hasChild(fqn));
 197   
 198  0 assertEquals(true, cache.removeNode(fqn));
 199  0 assertFalse(cache.getRoot().hasChild(fqn));
 200  0 assertEquals(false, cache.removeNode(fqn));
 201    }
 202   
 203    /**
 204    * Tests basic eviction
 205    */
 206  2 public void testEvict()
 207    {
 208  2 Fqn one = Fqn.fromString("/one");
 209  2 Fqn two = Fqn.fromString("/one/two");
 210  2 Object key = "key", value = "value";
 211   
 212  2 cache.getRoot().addChild(one).put(key, value);
 213  2 cache.getRoot().addChild(two).put(key, value);
 214   
 215  2 assertTrue(cache.getRoot().hasChild(one));
 216  2 assertFalse(cache.getRoot().getChild(one).getData().isEmpty());
 217  2 assertTrue(cache.getRoot().hasChild(two));
 218  2 assertFalse(cache.getRoot().getChild(two).getData().isEmpty());
 219   
 220    // evict two
 221  2 cache.evict(two, false);
 222   
 223  2 assertTrue(cache.getRoot().hasChild(one));
 224  2 assertTrue(cache.getRoot().getChild(one).getKeys().contains(key));
 225  2 assertFalse(cache.getRoot().hasChild(two));
 226   
 227    // now add 2 again...
 228  2 cache.getRoot().addChild(two).put(key, value);
 229   
 230    // now evict one, NOT recursive
 231  2 cache.evict(one, false);
 232   
 233    // one will NOT be removed, just emptied.
 234  2 assertTrue(cache.getRoot().hasChild(one));
 235  2 assertFalse(cache.getRoot().getChild(one).getKeys().contains(key));
 236   
 237    // two will be unaffected
 238  2 assertTrue(cache.getRoot().hasChild(two));
 239  2 assertTrue(cache.getRoot().getChild(two).getKeys().contains(key));
 240    }
 241   
 242   
 243    /**
 244    * Tests recursive eviction
 245    */
 246  2 public void testEvictRecursive()
 247    {
 248  2 Fqn one = Fqn.fromString("/one");
 249  2 Fqn two = Fqn.fromString("/one/two");
 250  2 Object key = "key", value = "value";
 251   
 252  2 cache.getRoot().addChild(one).put(key, value);
 253  2 cache.getRoot().addChild(two).put(key, value);
 254   
 255  2 assertTrue(cache.getRoot().hasChild(one));
 256  2 assertFalse(cache.getRoot().getChild(one).getData().isEmpty());
 257  2 assertTrue(cache.getRoot().hasChild(two));
 258  2 assertFalse(cache.getRoot().getChild(two).getData().isEmpty());
 259   
 260    // evict two
 261  2 cache.evict(two, true);
 262   
 263  2 assertTrue(cache.getRoot().hasChild(one));
 264  2 assertFalse(cache.getRoot().getChild(one).getData().isEmpty());
 265  2 assertFalse(cache.getRoot().hasChild(two));
 266   
 267    // now add 2 again...
 268  2 cache.getRoot().addChild(two).put(key, value);
 269   
 270    // now evict one, recursive
 271  2 cache.evict(one, true);
 272   
 273  2 assertFalse(cache.getRoot().hasChild(one));
 274  2 assertFalse(cache.getRoot().hasChild(two));
 275    }
 276   
 277   
 278    /**
 279    * Again, see org.jboss.cache for more extensive tests on Regions. This just tests the getRegion API on cache.
 280    */
 281  2 public void testRegion()
 282    {
 283  2 Region rootRegion = cache.getRegion(Fqn.ROOT, true);
 284  2 assertNotNull(rootRegion);// guaranteed never to return null if createIfAbsent is true.
 285  2 assertSame(rootRegion, cache.getRegion(Fqn.ROOT, true));
 286   
 287  2 Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"), true);
 288  2 assertNotNull(otherRegion);
 289  2 assertSame(otherRegion, cache.getRegion(Fqn.fromString("/other/region"), true));
 290    }
 291   
 292    /**
 293    * Again, see org.jboss.cache for more extensive tests on Regions. This just tests the getRegion API on cache.
 294    */
 295  2 public void testParentRegion1()
 296    {
 297  2 Region rootRegion = cache.getRegion(Fqn.ROOT, true);
 298  2 assertNotNull(rootRegion);// guaranteed never to return null if createIfAbsent is true.
 299  2 assertSame(rootRegion, cache.getRegion(Fqn.ROOT, false));
 300   
 301  2 Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"), false);
 302    // should return the same parent region as root.
 303   
 304  2 assertSame(otherRegion, rootRegion);
 305    }
 306   
 307    /**
 308    * Again, see org.jboss.cache for more extensive tests on Regions. This just tests the getRegion API on cache.
 309    */
 310  2 public void testParentRegion2()
 311    {
 312  2 Region rootRegion = cache.getRegion(Fqn.ROOT, true);
 313  2 Region parentRegion = cache.getRegion(Fqn.fromString("/parent"), true);
 314  2 assertNotSame("parentRegion should be a new region in its own right", rootRegion, parentRegion);
 315   
 316  2 Region childRegion = cache.getRegion(Fqn.fromString("/parent/region"), false);
 317  2 assertSame("Expecting the same region as parentRegion", childRegion, parentRegion);
 318    }
 319   
 320   
 321    /**
 322    * Again, see org.jboss.cache for more extensive tests on Regions. This just tests the getRegion API on cache.
 323    */
 324  2 public void testNullRegion()
 325    {
 326  2 Region myRegion = cache.getRegion(Fqn.fromString("/myregion"), true);
 327  2 assertNotNull(myRegion);// guaranteed never to return null if createIfAbsent is true.
 328  2 assertSame(myRegion, cache.getRegion(Fqn.fromString("/myregion"), false));
 329   
 330  2 Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"), false);
 331    // should return the default region
 332  2 assertNotNull(otherRegion);
 333  2 assertEquals(Fqn.ROOT, otherRegion.getFqn());
 334    }
 335   
 336    }