1 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.OptimisticTransactionEntry; 13 import org.jboss.cache.TransactionTable; 14 import org.jboss.cache.interceptors.Interceptor; 15 import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor; 16 import org.jboss.cache.loader.SamplePojo; 17 import org.jboss.cache.marshall.MethodDeclarations; 18 import org.jboss.cache.transaction.DummyTransactionManager; 19 20 import javax.transaction.Transaction ; 21 import javax.transaction.TransactionManager ; 22 23 26 public class OptimisticCreateIfNotExistsInterceptorTest extends AbstractOptimisticTestCase 27 { 28 29 protected TransactionManager txManager; 30 protected Transaction tx; 31 protected GlobalTransaction gtx; 32 protected TransactionTable table; 33 protected OptimisticTransactionEntry entry; 34 protected TransactionWorkspace workspace; 35 36 39 public OptimisticCreateIfNotExistsInterceptorTest(String name) 40 { 41 super(name); 42 } 43 44 protected void setupTransactionsInInvocationCtx(CacheImpl cache) throws Exception 45 { 46 txManager = DummyTransactionManager.getInstance(); 47 txManager.begin(); 49 50 table = cache.getTransactionTable(); 52 53 gtx = cache.getCurrentTransaction(); 55 tx = txManager.getTransaction(); 56 entry = (OptimisticTransactionEntry) table.get(gtx); 57 workspace = entry.getTransactionWorkSpace(); 58 59 cache.getInvocationContext().setGlobalTransaction(gtx); 61 cache.getInvocationContext().setTransaction(tx); 62 } 63 64 public void testNodeCreation() throws Exception 65 { 66 67 TestListener listener = new TestListener(); 68 final CacheImpl cache = createCacheWithListener(listener); 69 70 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 71 interceptor.setCache(cache); 72 MockInterceptor dummy = new MockInterceptor(); 73 dummy.setCache(cache); 74 75 interceptor.setNext(dummy); 76 77 cache.setInterceptorChain(interceptor); 78 79 setupTransactionsInInvocationCtx(cache); 80 81 final SamplePojo pojo = new SamplePojo(21, "test"); 82 cache.put("/one/two", "key1", pojo); 83 84 assertEquals(3, workspace.getNodes().size()); 85 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 86 assertNotNull(workspace.getNode(Fqn.fromString("/one/"))); 87 assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 88 assertTrue(entry.getLocks().isEmpty()); 89 90 assertTrue(!cache.exists("/one/two")); 91 assertEquals(MethodDeclarations.putKeyValMethodLocal, dummy.getCalled()); 92 txManager.commit(); 93 94 cache.stop(); 95 96 } 97 98 public void testInvalidTransaction() throws Exception 99 { 100 101 TestListener listener = new TestListener(); 102 final CacheImpl cache = createCacheWithListener(listener); 103 104 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 105 interceptor.setCache(cache); 106 MockInterceptor dummy = new MockInterceptor(); 107 dummy.setCache(cache); 108 109 interceptor.setNext(dummy); 110 111 112 cache.setInterceptorChain(interceptor); 113 114 setupTransactionsInInvocationCtx(cache); 115 final SamplePojo pojo = new SamplePojo(21, "test"); 116 cache.put("/one/two", "key1", pojo); 117 118 assertEquals(3, workspace.getNodes().size()); 119 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 120 assertNotNull(workspace.getNode(Fqn.fromString("/one/"))); 121 assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 122 assertTrue(entry.getLocks().isEmpty()); 123 124 assertTrue(!cache.exists("/one/two")); 125 assertEquals(MethodDeclarations.putKeyValMethodLocal, dummy.getCalled()); 126 127 txManager.commit(); 128 cache.getInvocationContext().setGlobalTransaction(null); 130 cache.getInvocationContext().setTransaction(null); 131 132 try 133 { 134 cache.put("/one/two/three", "key1", pojo); 135 assertTrue("Should never be reched", false); 136 } 137 catch (Throwable t) 138 { 139 assertTrue(true); 140 } 141 cache.stop(); 142 143 } 144 145 public void testMultiplePut() throws Exception 146 { 147 148 TestListener listener = new TestListener(); 149 final CacheImpl cache = createCacheWithListener(listener); 150 151 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 152 interceptor.setCache(cache); 153 MockInterceptor dummy = new MockInterceptor(); 154 dummy.setCache(cache); 155 interceptor.setNext(dummy); 156 157 cache.setInterceptorChain(interceptor); 158 SamplePojo pojo = new SamplePojo(21, "test"); 159 160 setupTransactionsInInvocationCtx(cache); 161 162 cache.put("/one/two", "key1", pojo); 163 cache.put("/one/two", "key2", pojo); 164 165 assertEquals(3, workspace.getNodes().size()); 166 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 167 assertNotNull(workspace.getNode(Fqn.fromString("/one/"))); 168 assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 169 assertTrue(entry.getLocks().isEmpty()); 170 171 assertTrue(!cache.exists("/one/two")); 172 assertEquals(MethodDeclarations.putKeyValMethodLocal, dummy.getCalled()); 173 174 txManager.commit(); 175 176 cache.stop(); 177 178 } 179 180 181 } 182 | Popular Tags |