Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 178   Methods: 6
NCLOC: 132   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NodeSPITest.java 50% 91.7% 100% 91.3%
coverage coverage
 1    package org.jboss.cache.api;
 2   
 3    import junit.framework.TestCase;
 4    import org.jboss.cache.CacheSPI;
 5    import org.jboss.cache.DefaultCacheFactory;
 6    import org.jboss.cache.Fqn;
 7    import org.jboss.cache.NodeSPI;
 8   
 9    import java.util.Map;
 10    import java.util.Set;
 11   
 12    /**
 13    * Tests NodeSPI specific APIs.
 14    */
 15    public class NodeSPITest extends TestCase
 16    {
 17    private CacheSPI cache;
 18    private NodeSPI root;
 19   
 20  4 protected void setUp()
 21    {
 22  4 cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache();
 23  4 root = cache.getRoot();
 24    }
 25   
 26  4 protected void tearDown()
 27    {
 28  4 if (cache != null) cache.stop();
 29  4 root = null;
 30  4 cache = null;
 31    }
 32   
 33  1 public void testDeepOperations() throws Exception
 34    {
 35  1 Fqn A = Fqn.fromString("/a");
 36  1 Fqn B = Fqn.fromString("/b");
 37  1 Fqn A_B = Fqn.fromString("/a/b");
 38   
 39  1 NodeSPI nodeA, nodeB;
 40   
 41  1 cache.put(A, "k", "v");
 42  1 cache.put(A_B, "k", "v");
 43   
 44  1 nodeA = cache.getRoot().getChildDirect(A);// should work
 45  1 nodeB = cache.getRoot().getChildDirect(A_B);// should work
 46   
 47  1 assertEquals(A_B, nodeB.getFqn());
 48   
 49  1 nodeB = nodeA.getChildDirect(B);// should work
 50  1 assertEquals(A_B, nodeB.getFqn());
 51  1 assertEquals(true, cache.getRoot().removeChildDirect(A_B));// should work
 52  1 assertEquals(false, cache.getRoot().removeChildDirect(A_B));// should work
 53   
 54  1 cache.put(A_B, "k", "v");
 55  1 assertEquals(true, nodeA.removeChildDirect(B));// should work
 56  1 assertEquals(false, nodeA.removeChildDirect(B));// should work
 57  1 assertEquals(true, cache.getRoot().removeChildDirect(A.getLastElement()));
 58  1 assertEquals(false, cache.getRoot().removeChildDirect(A.getLastElement()));
 59   
 60  1 try
 61    {
 62  1 cache.getRoot().addChildDirect(A_B);// should fail
 63  0 fail("Should have failed");
 64    }
 65    catch (UnsupportedOperationException e)
 66    {
 67    // expected
 68    }
 69  1 nodeA = cache.getRoot().addChildDirect(A);// should work
 70  1 nodeA.addChildDirect(B);// should work
 71    }
 72   
 73  1 public void testDataImmutabilityAndDefensiveCopy()
 74    {
 75    // put some stuff in the root node
 76  1 root.put("k", "v");
 77  1 Map dataDirect = root.getDataDirect();
 78  1 Set keysDirect = root.getKeysDirect();
 79   
 80  1 try
 81    {
 82  1 dataDirect.remove("k");
 83  0 fail("getDataDirect() should return an unmodifiable collection object");
 84    }
 85    catch (UnsupportedOperationException uoe)
 86    {
 87    // good; should be immutable
 88    }
 89   
 90  1 try
 91    {
 92  1 keysDirect.clear();
 93  0 fail("getKeysDirect() should return an unmodifiable collection object");
 94    }
 95    catch (UnsupportedOperationException uoe)
 96    {
 97    // good; should be immutable
 98    }
 99   
 100    // now test defensive copy
 101  1 root.put("k2", "v2");
 102   
 103  1 assertTrue("root.put() should have succeeded", root.getDataDirect().containsKey("k2"));
 104  1 assertTrue("getDataDirect() should have made a defensive copy of the data collection object", !dataDirect.containsKey("k2"));
 105  1 assertTrue("getKeysDirect() should have made a defensive copy of the data collection object", !keysDirect.contains("k2"));
 106    }
 107   
 108  1 public void testChildrenImmutabilityAndDefensiveCopy()
 109    {
 110    // put some stuff in the root node
 111  1 Object childName = "childName";
 112  1 Object newChild = "newChild";
 113  1 root.addChild(new Fqn(childName));
 114  1 Map childrenMapDirect = root.getChildrenMapDirect();
 115  1 Set childrenDirect = root.getChildrenDirect();
 116  1 Set childrenNamesDirect = root.getChildrenNamesDirect();
 117   
 118  1 try
 119    {
 120  1 childrenMapDirect.clear();
 121  0 fail("getChildrenMapDirect() should return an unmodifiable collection object");
 122    }
 123    catch (UnsupportedOperationException uoe)
 124    {
 125    // good; should be immutable
 126    }
 127   
 128  1 try
 129    {
 130  1 childrenDirect.clear();
 131  0 fail("getChildrenDirect() should return an unmodifiable collection object");
 132    }
 133    catch (UnsupportedOperationException uoe)
 134    {
 135    // good; should be immutable
 136    }
 137   
 138  1 try
 139    {
 140  1 childrenNamesDirect.clear();
 141  0 fail("getChildrenNamesDirect() should return an unmodifiable collection object");
 142    }
 143    catch (UnsupportedOperationException uoe)
 144    {
 145    // good; should be immutable
 146    }
 147   
 148    // now test defensive copy
 149  1 root.addChild(new Fqn(newChild));
 150   
 151  1 assertTrue("root.addChild() should have succeeded", root.getChildrenNamesDirect().contains(newChild));
 152  1 assertTrue("getChildrenMapDirect() should have made a defensive copy of the data collection object", !childrenMapDirect.containsKey(newChild));
 153  1 assertTrue("getChildrenDirect() should have made a defensive copy of the data collection object", !childrenDirect.contains(newChild));
 154  1 assertTrue("getChildrenNamesDirect() should have made a defensive copy of the data collection object", !childrenNamesDirect.contains(newChild));
 155   
 156    }
 157   
 158  1 public void testNullCollections()
 159    {
 160    // nothing in root, make sure we see no nulls.
 161  1 assertNotNull("Should not be null", root.getDataDirect());
 162  1 assertTrue("Should be empty", root.getDataDirect().isEmpty());
 163   
 164  1 assertNotNull("Should not be null", root.getKeysDirect());
 165  1 assertTrue("Should be empty", root.getKeysDirect().isEmpty());
 166   
 167  1 assertNotNull("Should not be null", root.getChildrenMapDirect());
 168  1 assertTrue("Should be empty", root.getChildrenMapDirect().isEmpty());
 169   
 170  1 assertNotNull("Should not be null", root.getChildrenDirect());
 171  1 assertTrue("Should be empty", root.getChildrenDirect().isEmpty());
 172   
 173  1 assertNotNull("Should not be null", root.getChildrenNamesDirect());
 174  1 assertTrue("Should be empty", root.getChildrenNamesDirect().isEmpty());
 175    }
 176   
 177   
 178    }