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