Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 228   Methods: 4
NCLOC: 140   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NodeInterceptorGetChildrenNamesTest.java 50% 99% 100% 98.2%
coverage coverage
 1    package org.jboss.cache.optimistic;
 2   
 3    import org.jboss.cache.CacheImpl;
 4    import org.jboss.cache.Fqn;
 5    import org.jboss.cache.GlobalTransaction;
 6    import org.jboss.cache.OptimisticTransactionEntry;
 7    import org.jboss.cache.TransactionTable;
 8    import org.jboss.cache.interceptors.Interceptor;
 9    import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
 10    import org.jboss.cache.interceptors.OptimisticNodeInterceptor;
 11    import org.jboss.cache.loader.SamplePojo;
 12    import org.jboss.cache.transaction.DummyTransactionManager;
 13   
 14    import javax.transaction.Transaction;
 15    import java.util.Iterator;
 16   
 17    /**
 18    * @author xenephon
 19    */
 20    public class NodeInterceptorGetChildrenNamesTest extends AbstractOptimisticTestCase
 21    {
 22   
 23   
 24    /**
 25    * @param name
 26    */
 27  3 public NodeInterceptorGetChildrenNamesTest(String name)
 28    {
 29  3 super(name);
 30    }
 31   
 32   
 33  1 public void testTransactionGetNamesMethod() throws Exception
 34    {
 35   
 36  1 TestListener listener = new TestListener();
 37  1 final CacheImpl cache = createCacheWithListener(listener);
 38   
 39  1 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
 40  1 interceptor.setCache(cache);
 41  1 Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
 42  1 nodeInterceptor.setCache(cache);
 43  1 MockInterceptor dummy = new MockInterceptor();
 44  1 dummy.setCache(cache);
 45   
 46  1 interceptor.setNext(nodeInterceptor);
 47  1 nodeInterceptor.setNext(dummy);
 48   
 49  1 cache.setInterceptorChain(interceptor);
 50   
 51    // first set up a node with a pojo
 52  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 53  1 mgr.begin();
 54  1 Transaction tx = mgr.getTransaction();
 55   
 56    // inject InvocationContext
 57  1 cache.getInvocationContext().setTransaction(tx);
 58  1 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
 59   
 60   
 61  1 SamplePojo pojo = new SamplePojo(21, "test");
 62   
 63  1 cache.put("/one/two", "key1", pojo);
 64   
 65  1 assertEquals(null, dummy.getCalled());
 66  1 TransactionTable table = cache.getTransactionTable();
 67   
 68  1 GlobalTransaction gtx = table.get(tx);
 69   
 70  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 71   
 72  1 TransactionWorkspace workspace = entry.getTransactionWorkSpace();
 73   
 74    //assert we can see this with a key value get in the transaction
 75  1 assertEquals(1, cache.getChildrenNames("/one").size());
 76  1 mgr.commit();
 77   
 78    //assert what should be the results of our call
 79  1 assertEquals(3, workspace.getNodes().size());
 80  1 assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
 81  1 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
 82  1 assertTrue(entry.getLocks().isEmpty());
 83  1 assertEquals(1, entry.getModifications().size());
 84  1 assertTrue(!cache.exists("/one/two"));
 85  1 assertEquals(null, dummy.getCalled());
 86   
 87    //assert that we cannot see the change if we have not put it into the cache
 88    // we need to do this as we do not have the tx interceptor in this stack
 89  1 mgr.begin();
 90   
 91  1 Transaction tx2 = mgr.getTransaction();
 92   
 93    // inject InvocationContext
 94  1 cache.getInvocationContext().setTransaction(tx2);
 95  1 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx2));
 96   
 97  1 assertEquals(0, cache.getChildrenNames("/").size());
 98  1 mgr.commit();
 99  1 cache.stop();
 100    }
 101   
 102   
 103  1 public void testTransactionGetNoNamesMethod() throws Exception
 104    {
 105   
 106  1 TestListener listener = new TestListener();
 107  1 final CacheImpl cache = createCacheWithListener(listener);
 108   
 109  1 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
 110  1 interceptor.setCache(cache);
 111  1 Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
 112  1 nodeInterceptor.setCache(cache);
 113  1 MockInterceptor dummy = new MockInterceptor();
 114  1 dummy.setCache(cache);
 115   
 116  1 interceptor.setNext(nodeInterceptor);
 117  1 nodeInterceptor.setNext(dummy);
 118   
 119  1 cache.setInterceptorChain(interceptor);
 120   
 121    // first set up a node with a pojo
 122  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 123  1 mgr.begin();
 124  1 Transaction tx = mgr.getTransaction();
 125   
 126    // inject InvocationContext
 127  1 cache.getInvocationContext().setTransaction(tx);
 128  1 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
 129   
 130   
 131  1 assertEquals(null, dummy.getCalled());
 132  1 TransactionTable table = cache.getTransactionTable();
 133   
 134  1 GlobalTransaction gtx = table.get(tx);
 135   
 136  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 137   
 138  1 TransactionWorkspace workspace = entry.getTransactionWorkSpace();
 139   
 140    //assert we can see this with a key value get in the transaction
 141  1 assertEquals(0, cache.getChildrenNames("/").size());
 142  1 mgr.commit();
 143   
 144   
 145  1 assertTrue(entry.getLocks().isEmpty());
 146  1 assertEquals(0, entry.getModifications().size());
 147  1 assertTrue(!cache.exists("/one/two"));
 148  1 assertEquals(null, dummy.getCalled());
 149   
 150   
 151  1 cache.stop();
 152    }
 153   
 154  1 public void testTransactionGetNamesIteratorMethod() throws Exception
 155    {
 156   
 157  1 TestListener listener = new TestListener();
 158  1 final CacheImpl cache = createCacheWithListener(listener);
 159   
 160  1 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
 161  1 interceptor.setCache(cache);
 162  1 Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
 163  1 nodeInterceptor.setCache(cache);
 164  1 MockInterceptor dummy = new MockInterceptor();
 165  1 dummy.setCache(cache);
 166   
 167  1 interceptor.setNext(nodeInterceptor);
 168  1 nodeInterceptor.setNext(dummy);
 169   
 170  1 cache.setInterceptorChain(interceptor);
 171   
 172    // first set up a node with a pojo
 173  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 174  1 mgr.begin();
 175  1 Transaction tx = mgr.getTransaction();
 176   
 177    // inject InvocationContext
 178  1 cache.getInvocationContext().setTransaction(tx);
 179  1 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
 180   
 181  1 SamplePojo pojo = new SamplePojo(21, "test");
 182   
 183  1 cache.put("/one/two", "key1", pojo);
 184   
 185  1 assertEquals(null, dummy.getCalled());
 186  1 TransactionTable table = cache.getTransactionTable();
 187   
 188  1 GlobalTransaction gtx = table.get(tx);
 189   
 190  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 191   
 192  1 TransactionWorkspace workspace = entry.getTransactionWorkSpace();
 193   
 194    //assert we can see this
 195  1 assertEquals(1, cache.getChildrenNames("/one").size());
 196   
 197  1 try
 198    {
 199  1 for (Iterator it = cache.getChildrenNames("/one").iterator(); it.hasNext();)
 200    {
 201  1 Object temp = it.next();
 202  1 it.remove();
 203    }
 204  0 fail("Should not be allowed to modify elements in the set returned by getChildrenNames()");
 205    }
 206    catch (UnsupportedOperationException uoe)
 207    {
 208    // the returned set should be unmodifiable and a remove on the iterator should fail.
 209    }
 210   
 211    //assert the removal has had no effect
 212  1 assertEquals(1, cache.getChildrenNames("/one").size());
 213  1 assertNotNull(workspace.getNode(Fqn.fromString("/one")).getChild(new Fqn("two")));
 214   
 215  1 mgr.commit();
 216   
 217   
 218  1 assertTrue(entry.getLocks().isEmpty());
 219  1 assertEquals(1, entry.getModifications().size());
 220  1 assertTrue(!cache.exists("/one/two"));
 221  1 assertEquals(null, dummy.getCalled());
 222   
 223   
 224  1 cache.stop();
 225    }
 226   
 227   
 228    }