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