Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 233   Methods: 12
NCLOC: 180   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NodeAPITest.java 83.3% 97.1% 100% 96.1%
coverage coverage
 1    package org.jboss.cache.api;
 2   
 3    import junit.framework.TestCase;
 4    import org.jboss.cache.CacheImpl;
 5    import org.jboss.cache.DefaultCacheFactory;
 6    import org.jboss.cache.Fqn;
 7    import org.jboss.cache.Node;
 8    import org.jboss.cache.config.Configuration;
 9   
 10    import javax.transaction.TransactionManager;
 11    import java.util.Map;
 12   
 13    /**
 14    * Tests {@link org.jboss.cache.Node}-centric operations
 15    *
 16    * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
 17    * @since 2.0.0
 18    */
 19    public class NodeAPITest extends TestCase
 20    {
 21    private Node rootNode;
 22   
 23    private CacheImpl cache;
 24   
 25    private TransactionManager tm;
 26   
 27    private static final Fqn A = Fqn.fromString("/a"), B = Fqn.fromString("/b"), C = Fqn.fromString("/c"), D = Fqn
 28    .fromString("/d");
 29   
 30    protected boolean optimistic = false;
 31   
 32  20 protected void setUp() throws Exception
 33    {
 34    // start a single cache instance
 35  20 cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache("META-INF/local-tx-service.xml", false);
 36  20 cache.getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC);
 37  20 cache.start();
 38  20 rootNode = cache.getRoot();
 39  20 tm = cache.getTransactionManager();
 40    }
 41   
 42  20 protected void tearDown()
 43    {
 44  20 if (cache != null)
 45    {
 46  20 cache.stop();
 47    }
 48  20 if (rootNode != null)
 49    {
 50  20 rootNode = null;
 51    }
 52    }
 53   
 54  2 public void testAddingData()
 55    {
 56  2 Node nodeA = rootNode.addChild(A);
 57  2 nodeA.put("key", "value");
 58   
 59  2 assertEquals("value", nodeA.get("key"));
 60    }
 61   
 62  2 public void testAddingDataTx() throws Exception
 63    {
 64  2 tm.begin();
 65  2 Node nodeA = rootNode.addChild(A);
 66  2 nodeA.put("key", "value");
 67   
 68  2 assertEquals("value", nodeA.get("key"));
 69  2 tm.commit();
 70    }
 71   
 72   
 73    /**
 74    * Remember, Fqns are relative!!
 75    */
 76  2 public void testParentsAndChildren()
 77    {
 78  2 Node nodeA = rootNode.addChild(A);
 79  2 Node nodeB = nodeA.addChild(B);
 80  2 Node nodeC = nodeA.addChild(C);
 81  2 Node nodeD = rootNode.addChild(D);
 82   
 83  2 assertEquals(rootNode, nodeA.getParent());
 84  2 assertEquals(nodeA, nodeB.getParent());
 85  2 assertEquals(nodeA, nodeC.getParent());
 86  2 assertEquals(rootNode, nodeD.getParent());
 87   
 88  2 assertTrue(rootNode.hasChild(A));
 89  2 assertFalse(rootNode.hasChild(B));
 90  2 assertFalse(rootNode.hasChild(C));
 91  2 assertTrue(rootNode.hasChild(D));
 92   
 93  2 assertTrue(nodeA.hasChild(B));
 94  2 assertTrue(nodeA.hasChild(C));
 95   
 96  2 assertEquals(nodeA, rootNode.getChild(A));
 97  2 assertEquals(nodeD, rootNode.getChild(D));
 98  2 assertEquals(nodeB, nodeA.getChild(B));
 99  2 assertEquals(nodeC, nodeA.getChild(C));
 100   
 101  2 assertTrue(nodeA.getChildren().contains(nodeB));
 102  2 assertTrue(nodeA.getChildren().contains(nodeC));
 103  2 assertEquals(2, nodeA.getChildren().size());
 104   
 105  2 assertTrue(rootNode.getChildren().contains(nodeA));
 106  2 assertTrue(rootNode.getChildren().contains(nodeD));
 107  2 assertEquals(2, rootNode.getChildren().size());
 108   
 109  2 assertEquals(true, rootNode.removeChild(A));
 110  2 assertFalse(rootNode.getChildren().contains(nodeA));
 111  2 assertTrue(rootNode.getChildren().contains(nodeD));
 112  2 assertEquals(1, rootNode.getChildren().size());
 113   
 114  2 assertEquals("double remove", false, rootNode.removeChild(A));
 115  2 assertEquals("double remove", false, rootNode.removeChild(A.getLastElement()));
 116    }
 117   
 118  2 public void testLocking() throws Exception
 119    {
 120  2 tm.begin();
 121  2 Node nodeA = rootNode.addChild(A);
 122  2 Node nodeB = nodeA.addChild(B);
 123  2 Node nodeC = nodeB.addChild(C);
 124   
 125  2 if (!optimistic)
 126    {
 127  1 assertEquals(3, cache.getNumberOfNodes());
 128  1 assertEquals(4, cache.getNumberOfLocksHeld());
 129    }
 130  2 tm.commit();
 131   
 132  2 tm.begin();
 133  2 assertEquals(0, cache.getNumberOfLocksHeld());
 134  2 nodeC.put("key", "value");
 135  1 if (!optimistic) assertEquals(4, cache.getNumberOfLocksHeld());
 136  2 tm.commit();
 137    }
 138   
 139  2 public void testImmutabilityOfData()
 140    {
 141  2 rootNode.put("key", "value");
 142  2 Map m = rootNode.getData();
 143  2 try
 144    {
 145  2 m.put("x", "y");
 146  0 fail("Map should be immutable!!");
 147    }
 148    catch (Exception e)
 149    {
 150    // expected
 151    }
 152   
 153  2 try
 154    {
 155  2 rootNode.getKeys().add(new Object());
 156  0 fail("Key set should be immutable");
 157    }
 158    catch (Exception e)
 159    {
 160    // expected
 161    }
 162    }
 163   
 164  2 public void testImmutabilityOfChildren()
 165    {
 166  2 rootNode.addChild(A);
 167   
 168  2 try
 169    {
 170  2 rootNode.getChildren().clear();
 171  0 fail("Collection of child nodes returned in getChildrenDirect() should be immutable");
 172    }
 173    catch (Exception e)
 174    {
 175    // expected
 176    }
 177    }
 178   
 179  2 public void testGetChildrenUnderTx() throws Exception
 180    {
 181  2 tm.begin();
 182  2 cache.put(new Fqn(A, B), "1", "1");
 183  2 cache.put(new Fqn(A, C), "2", "2");
 184   
 185  2 if (!optimistic)
 186    {
 187  1 assertEquals(3, cache.getNumberOfNodes());
 188  1 assertEquals(4, cache.getNumberOfLocksHeld());
 189    }
 190  2 assertEquals("Number of child", 2, cache.getRoot().getChild(A).getChildren().size());
 191  2 tm.commit();
 192    }
 193   
 194  2 public void testGetChildAPI()
 195    {
 196    // creates a node with fqn /a/b/c
 197  2 rootNode.addChild(A).addChild(B).addChild(C);
 198   
 199  2 rootNode.getChild(A).put("key", "value");
 200  2 rootNode.getChild(A).getChild(B).put("key", "value");
 201  2 rootNode.getChild(A).getChild(B).getChild(C).put("key", "value");
 202   
 203  2 assertEquals("value", rootNode.getChild(A).get("key"));
 204  2 assertEquals("value", rootNode.getChild(A).getChild(B).get("key"));
 205  2 assertEquals("value", rootNode.getChild(A).getChild(B).getChild(C).get("key"));
 206   
 207  2 assertNull(rootNode.getChild(new Fqn("nonexistent")));
 208    }
 209   
 210  2 public void testClearingData()
 211    {
 212  2 rootNode.put("k", "v");
 213  2 rootNode.put("k2", "v2");
 214  2 assertEquals(2, rootNode.getKeys().size());
 215  2 rootNode.clearData();
 216  2 assertEquals(0, rootNode.getKeys().size());
 217  2 assertTrue(rootNode.getData().isEmpty());
 218    }
 219   
 220  2 public void testClearingDataTx() throws Exception
 221    {
 222  2 tm.begin();
 223  2 rootNode.put("k", "v");
 224  2 rootNode.put("k2", "v2");
 225  2 assertEquals(2, rootNode.getKeys().size());
 226  2 rootNode.clearData();
 227  2 assertEquals(0, rootNode.getKeys().size());
 228  2 assertTrue(rootNode.getData().isEmpty());
 229  2 tm.commit();
 230  2 assertTrue(rootNode.getData().isEmpty());
 231    }
 232   
 233    }