Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 438   Methods: 10
NCLOC: 276   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NodeInterceptorRemoveNodeTest.java 100% 100% 100% 100%
coverage
 1    /*
 2    * Created on 17-Feb-2005
 3    *
 4    *
 5    *
 6    */
 7    package org.jboss.cache.optimistic;
 8   
 9    import org.jboss.cache.CacheImpl;
 10    import org.jboss.cache.Fqn;
 11    import org.jboss.cache.GlobalTransaction;
 12    import org.jboss.cache.Node;
 13    import org.jboss.cache.OptimisticTransactionEntry;
 14    import org.jboss.cache.TransactionTable;
 15    import org.jboss.cache.factories.InterceptorChainFactory;
 16    import org.jboss.cache.interceptors.CallInterceptor;
 17    import org.jboss.cache.interceptors.Interceptor;
 18    import org.jboss.cache.loader.SamplePojo;
 19   
 20    import javax.transaction.Transaction;
 21    import javax.transaction.TransactionManager;
 22    import java.util.HashMap;
 23    import java.util.Map;
 24   
 25    /**
 26    * @author xenephon
 27    */
 28    public class NodeInterceptorRemoveNodeTest extends AbstractOptimisticTestCase
 29    {
 30    private CacheImpl cache;
 31    private TestListener listener;
 32    private MockInterceptor dummy;
 33    private TransactionManager mgr;
 34   
 35    /**
 36    * @param name
 37    */
 38  7 public NodeInterceptorRemoveNodeTest(String name)
 39    {
 40  7 super(name);
 41    }
 42   
 43  7 protected void setUp() throws Exception
 44    {
 45  7 super.setUp();
 46  7 listener = new TestListener();
 47  7 cache = createCacheWithListener(listener);
 48   
 49  7 dummy = new MockInterceptor();
 50  7 dummy.setCache(cache);
 51   
 52  7 Interceptor interceptor = cache.getInterceptorChain().get(0); // get the first interceptor
 53    // replace the last (call) interceptor with the mock interceptor.
 54  7 Interceptor next = interceptor, prev = interceptor;
 55  7 while (!(next instanceof CallInterceptor))
 56    {
 57  56 prev = next;
 58  56 next = next.getNext();
 59    }
 60   
 61  7 prev.setNext(dummy);
 62   
 63  7 InterceptorChainFactory.setLastInterceptorPointer(interceptor, dummy);
 64   
 65  7 cache.setInterceptorChain(interceptor);
 66  7 mgr = cache.getTransactionManager();
 67    }
 68   
 69  7 protected void tearDown()
 70    {
 71  7 super.tearDown();
 72  7 cache.stop();
 73    }
 74   
 75  1 public void testTransactionRemoveNotExistsNodeMethod() throws Exception
 76    {
 77  1 mgr.begin();
 78  1 Transaction tx = mgr.getTransaction();
 79   
 80    // inject InvocationContext
 81  1 cache.getInvocationContext().setTransaction(tx);
 82  1 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
 83   
 84   
 85  1 cache.remove("/one/two");
 86   
 87  1 TransactionTable table = cache.getTransactionTable();
 88  1 GlobalTransaction gtx = table.get(tx);
 89  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 90  1 TransactionWorkspace workspace = entry.getTransactionWorkSpace();
 91   
 92  1 mgr.commit();
 93  1 assertEquals(null, dummy.getCalled());
 94   
 95    //assert what should be the results of our call
 96  1 assertEquals(0, workspace.getNodes().size());
 97   
 98  1 assertTrue(entry.getLocks().isEmpty());
 99  1 assertEquals(1, entry.getModifications().size());
 100  1 assertTrue(!cache.exists("/one/two"));
 101  1 assertEquals(null, dummy.getCalled());
 102   
 103  1 assertEquals(0, listener.getNodesAdded());
 104    }
 105   
 106  1 public void testTransactionRemoveNodeMethod() throws Exception
 107    {
 108  1 mgr.begin();
 109  1 Transaction tx = mgr.getTransaction();
 110   
 111    // inject InvocationContext
 112  1 cache.getInvocationContext().setTransaction(tx);
 113  1 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
 114   
 115  1 SamplePojo pojo = new SamplePojo(21, "test");
 116  1 Map temp = new HashMap();
 117  1 temp.put("key1", pojo);
 118  1 cache.put("/one/two", temp);
 119   
 120  1 cache.remove("/one/two");
 121  1 assertEquals(null, dummy.getCalled());
 122  1 TransactionTable table = cache.getTransactionTable();
 123   
 124  1 GlobalTransaction gtx = table.get(tx);
 125   
 126  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 127   
 128  1 TransactionWorkspace workspace = entry.getTransactionWorkSpace();
 129   
 130  1 mgr.commit();
 131   
 132    //assert what should be the results of our call
 133  1 assertEquals(3, workspace.getNodes().size());
 134   
 135  1 assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
 136  1 assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
 137  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")));
 138  1 assertEquals(false, workspace.getNode(Fqn.fromString("/one")).isDeleted());
 139  1 assertEquals(0, workspace.getNode(Fqn.fromString("/one")).getMergedChildren().size());
 140  1 assertEquals(null, workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("/two")));
 141  1 assertEquals(2, entry.getModifications().size());
 142  1 assertTrue(!cache.exists("/one/two"));
 143  1 assertEquals(null, dummy.getCalled());
 144    }
 145   
 146  1 public void testTransactionRemoveIntermediateNodeMethod() throws Exception
 147    {
 148  1 mgr.begin();
 149  1 Transaction tx = mgr.getTransaction();
 150   
 151    // inject InvocationContext
 152  1 cache.getInvocationContext().setTransaction(tx);
 153  1 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
 154   
 155  1 SamplePojo pojo = new SamplePojo(21, "test");
 156  1 Map temp = new HashMap();
 157  1 temp.put("key1", pojo);
 158  1 cache.put("/one/two", temp);
 159   
 160  1 cache.remove("/one");
 161  1 assertEquals(null, dummy.getCalled());
 162  1 TransactionTable table = cache.getTransactionTable();
 163   
 164  1 GlobalTransaction gtx = table.get(tx);
 165   
 166  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 167   
 168  1 TransactionWorkspace workspace = entry.getTransactionWorkSpace();
 169   
 170  1 mgr.commit();
 171   
 172    //assert what should be the results of our call
 173  1 assertEquals(3, workspace.getNodes().size());
 174   
 175  1 assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
 176  1 assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
 177  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")));
 178  1 assertEquals(true, workspace.getNode(Fqn.fromString("/one")).isDeleted());
 179  1 assertEquals(1, workspace.getNode(Fqn.fromString("/one")).getMergedChildren().size());
 180  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
 181  1 assertEquals(null, workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("/one")));
 182  1 assertEquals(2, entry.getModifications().size());
 183  1 assertTrue(!cache.exists("/one/two"));
 184  1 assertEquals(null, dummy.getCalled());
 185    }
 186   
 187  1 public void testTransactionRemoveTwiceMethod() throws Exception
 188    {
 189  1 mgr.begin();
 190  1 Transaction tx = mgr.getTransaction();
 191   
 192    // inject InvocationContext
 193  1 cache.getInvocationContext().setTransaction(tx);
 194  1 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
 195   
 196  1 SamplePojo pojo = new SamplePojo(21, "test");
 197  1 Map temp = new HashMap();
 198  1 temp.put("key1", pojo);
 199  1 cache.put("/one/two", temp);
 200   
 201    // get the transaction stuff
 202  1 TransactionTable table = cache.getTransactionTable();
 203  1 GlobalTransaction gtx = table.get(tx);
 204   
 205  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 206   
 207  1 TransactionWorkspace workspace = entry.getTransactionWorkSpace();
 208   
 209  1 workspace.getNode(Fqn.fromString("/one"));
 210  1 workspace.getNode(Fqn.fromString("/one/two"));
 211   
 212   
 213  1 cache.remove("/one");
 214   
 215  1 assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
 216  1 assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
 217  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")));
 218  1 assertEquals(true, workspace.getNode(Fqn.fromString("/one")).isDeleted());
 219   
 220    //will still have alink from one to two
 221  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
 222  1 assertEquals(null, workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("one")));
 223   
 224    //now put /one/two back in
 225  1 cache.remove("/one");
 226   
 227   
 228  1 assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
 229  1 assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
 230  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")));
 231  1 assertEquals(true, workspace.getNode(Fqn.fromString("/one")).isDeleted());
 232   
 233    //will still have alink from one to two
 234  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
 235  1 assertEquals(null, workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("one")));
 236   
 237  1 assertEquals(null, dummy.getCalled());
 238   
 239   
 240  1 mgr.commit();
 241   
 242    //assert what should be the results of our call
 243  1 assertEquals(3, workspace.getNodes().size());
 244   
 245   
 246  1 assertEquals(3, entry.getModifications().size());
 247  1 assertTrue(!cache.exists("/one/two"));
 248  1 assertEquals(null, dummy.getCalled());
 249    }
 250   
 251   
 252  1 public void testTransactionRemovePutNodeMethod() throws Exception
 253    {
 254  1 mgr.begin();
 255  1 Transaction tx = mgr.getTransaction();
 256   
 257    // inject InvocationContext
 258  1 cache.getInvocationContext().setTransaction(tx);
 259  1 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
 260   
 261  1 SamplePojo pojo = new SamplePojo(21, "test");
 262  1 Map temp = new HashMap();
 263  1 temp.put("key1", pojo);
 264  1 cache.put("/one/two", temp);
 265   
 266    // get the transaction stuff
 267  1 TransactionTable table = cache.getTransactionTable();
 268  1 GlobalTransaction gtx = table.get(tx);
 269   
 270  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 271   
 272  1 TransactionWorkspace workspace = entry.getTransactionWorkSpace();
 273   
 274  1 Node one = workspace.getNode(Fqn.fromString("/one"));
 275  1 Node two = workspace.getNode(Fqn.fromString("/one/two"));
 276   
 277   
 278  1 cache.remove("/one");
 279   
 280  1 assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
 281  1 assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
 282  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")));
 283  1 assertEquals(true, workspace.getNode(Fqn.fromString("/one")).isDeleted());
 284   
 285    //will still have alink from one to two
 286  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
 287  1 assertEquals(null, workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("one")));
 288   
 289    //now put /one/two back in
 290  1 cache.put("/one/two", temp);
 291   
 292  1 WorkspaceNode oneAfter = workspace.getNode(Fqn.fromString("/one"));
 293  1 WorkspaceNode twoAfter = workspace.getNode(Fqn.fromString("/one/two"));
 294   
 295  1 assertNotSame(one, oneAfter);
 296  1 assertEquals(false, oneAfter.isDeleted());
 297  1 assertNotSame(two, twoAfter);
 298  1 assertEquals(false, twoAfter.isDeleted());
 299   
 300  1 assertEquals(twoAfter.getNode(), workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
 301  1 assertEquals(oneAfter.getNode(), workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("one")));
 302   
 303  1 assertEquals(null, dummy.getCalled());
 304   
 305   
 306  1 mgr.commit();
 307   
 308    //assert what should be the results of our call
 309  1 assertEquals(3, workspace.getNodes().size());
 310   
 311   
 312  1 assertEquals(3, entry.getModifications().size());
 313  1 assertEquals(null, dummy.getCalled());
 314  1 assertEquals(4, listener.getNodesAdded());
 315    }
 316   
 317   
 318  1 public void testTransactionRemovePutkeyValMethod() throws Exception
 319    {
 320  1 mgr.begin();
 321  1 Transaction tx = mgr.getTransaction();
 322   
 323    // inject InvocationContext
 324  1 cache.getInvocationContext().setTransaction(tx);
 325  1 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
 326   
 327  1 SamplePojo pojo = new SamplePojo(21, "test");
 328  1 Map temp = new HashMap();
 329  1 temp.put("key1", pojo);
 330  1 cache.put("/one/two", temp);
 331   
 332    // get the transaction stuff
 333  1 TransactionTable table = cache.getTransactionTable();
 334  1 GlobalTransaction gtx = table.get(tx);
 335   
 336  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 337   
 338  1 TransactionWorkspace workspace = entry.getTransactionWorkSpace();
 339   
 340  1 Node one = workspace.getNode(Fqn.fromString("/one"));
 341  1 Node two = workspace.getNode(Fqn.fromString("/one/two"));
 342   
 343   
 344  1 cache.remove("/one");
 345   
 346  1 assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
 347  1 assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
 348  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")));
 349  1 assertEquals(true, workspace.getNode(Fqn.fromString("/one")).isDeleted());
 350   
 351    //will still have alink from one to two
 352  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
 353  1 assertEquals(null, workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("one")));
 354   
 355    //now put /one back in
 356  1 SamplePojo pojo2 = new SamplePojo(21, "test");
 357  1 cache.put("/one", "key1", pojo2);
 358   
 359  1 WorkspaceNode oneAfter = workspace.getNode(Fqn.fromString("/one"));
 360  1 WorkspaceNode twoAfter = workspace.getNode(Fqn.fromString("/one/two"));
 361   
 362  1 assertNotSame(one, oneAfter);
 363  1 assertEquals(false, oneAfter.isDeleted());
 364  1 assertEquals(two, twoAfter);
 365  1 assertEquals(true, twoAfter.isDeleted());
 366   
 367  1 assertNull(workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
 368  1 assertEquals(oneAfter.getNode(), workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("one")));
 369   
 370  1 assertEquals(null, dummy.getCalled());
 371   
 372   
 373  1 mgr.commit();
 374   
 375  1 assertEquals(pojo2, workspace.getNode(Fqn.fromString("/one")).get("key1"));
 376    //assert what should be the results of our call
 377  1 assertEquals(3, workspace.getNodes().size());
 378   
 379   
 380  1 assertEquals(3, entry.getModifications().size());
 381  1 assertTrue(!cache.exists("/one/two"));
 382  1 assertEquals(null, dummy.getCalled());
 383  1 assertEquals(3, listener.getNodesAdded());
 384    }
 385   
 386  1 public void testTransactionRemoveSubNodeMethod() throws Exception
 387    {
 388  1 mgr.begin();
 389  1 Transaction tx = mgr.getTransaction();
 390   
 391    // inject InvocationContext
 392  1 cache.getInvocationContext().setTransaction(tx);
 393  1 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
 394   
 395  1 SamplePojo pojo = new SamplePojo(21, "test");
 396  1 Map temp = new HashMap();
 397  1 temp.put("key1", pojo);
 398  1 cache.put("/one/two", temp);
 399   
 400    // get the transaction stuff
 401  1 TransactionTable table = cache.getTransactionTable();
 402  1 GlobalTransaction gtx = table.get(tx);
 403   
 404  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 405   
 406  1 TransactionWorkspace workspace = entry.getTransactionWorkSpace();
 407   
 408  1 WorkspaceNode one = workspace.getNode(Fqn.fromString("/one"));
 409   
 410  1 assertEquals(1, one.getMergedChildren().size());
 411   
 412  1 cache.remove("/one/two");
 413   
 414  1 assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
 415  1 assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
 416  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")));
 417  1 assertEquals(false, workspace.getNode(Fqn.fromString("/one")).isDeleted());
 418   
 419    //will still have alink from one to two
 420  1 assertEquals(null, workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
 421   
 422   
 423  1 assertEquals(null, dummy.getCalled());
 424   
 425   
 426  1 mgr.commit();
 427   
 428  1 assertEquals(0, workspace.getNode(Fqn.fromString("/one")).getMergedChildren().size());
 429    //assert what should be the results of our call
 430  1 assertEquals(3, workspace.getNodes().size());
 431   
 432   
 433  1 assertEquals(2, entry.getModifications().size());
 434  1 assertTrue(!cache.exists("/one/two"));
 435  1 assertEquals(null, dummy.getCalled());
 436  1 assertEquals(2, listener.getNodesAdded());
 437    }
 438    }