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.NodeSPI; 13 import org.jboss.cache.OptimisticTransactionEntry; 14 import org.jboss.cache.TransactionTable; 15 import org.jboss.cache.interceptors.Interceptor; 16 import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor; 17 import org.jboss.cache.interceptors.OptimisticLockingInterceptor; 18 import org.jboss.cache.interceptors.OptimisticNodeInterceptor; 19 import org.jboss.cache.loader.SamplePojo; 20 import org.jboss.cache.lock.NodeLock; 21 import org.jboss.cache.marshall.MethodCall; 22 import org.jboss.cache.marshall.MethodCallFactory; 23 import org.jboss.cache.marshall.MethodDeclarations; 24 import org.jboss.cache.transaction.DummyTransactionManager; 25 26 import javax.transaction.Transaction ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 34 public class OpLockingInterceptorTest extends AbstractOptimisticTestCase 35 { 36 37 38 41 public OpLockingInterceptorTest(String name) 42 { 43 super(name); 44 45 } 46 47 public void testTransactionPrepareMethod() throws Exception 48 { 49 50 TestListener listener = new TestListener(); 51 final CacheImpl cache = createCacheWithListener(listener); 52 53 Interceptor lockingInterceptor = new OptimisticLockingInterceptor(); 54 lockingInterceptor.setCache(cache); 55 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 56 interceptor.setCache(cache); 57 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 58 nodeInterceptor.setCache(cache); 59 MockInterceptor dummy = new MockInterceptor(); 60 dummy.setCache(cache); 61 lockingInterceptor.setNext(interceptor); 62 interceptor.setNext(nodeInterceptor); 63 nodeInterceptor.setNext(dummy); 64 65 cache.setInterceptorChain(lockingInterceptor); 66 67 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 69 mgr.begin(); 70 Transaction tx = mgr.getTransaction(); 71 72 cache.getInvocationContext().setTransaction(tx); 74 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 75 76 SamplePojo pojo = new SamplePojo(21, "test"); 77 Map temp = new HashMap (); 78 temp.put("key1", pojo); 79 cache.put("/one/two", temp); 80 81 assertEquals(null, dummy.getCalled()); 82 TransactionTable table = cache.getTransactionTable(); 83 84 GlobalTransaction gtx = table.get(tx); 85 86 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 87 88 TransactionWorkspace workspace = entry.getTransactionWorkSpace(); 89 90 94 95 assertEquals(3, workspace.getNodes().size()); 96 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 97 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 98 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 99 assertTrue(entry.getLocks().isEmpty()); 100 assertEquals(1, entry.getModifications().size()); 101 assertTrue(!cache.exists("/one/two")); 102 assertEquals(null, dummy.getCalled()); 103 104 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object []{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE}); 106 try 107 { 108 cache._replicate(prepareMethod); 109 } 110 catch (Throwable t) 111 { 112 113 } 114 115 116 assertEquals(3, workspace.getNodes().size()); 117 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 118 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 119 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 120 assertEquals(3, entry.getLocks().size()); 121 for (Iterator it = entry.getLocks().iterator(); it.hasNext();) 122 { 123 NodeLock lock = (NodeLock) it.next(); 124 assertTrue(lock.isWriteLocked()); 125 assertEquals(gtx, lock.getWriterOwner()); 126 } 127 assertEquals(1, entry.getModifications().size()); 128 assertTrue(!cache.exists("/one/two")); 129 131 132 mgr.commit(); 133 134 cache.stop(); 135 136 } 137 138 public void testTransactionCommitMethod() throws Exception 139 { 140 141 TestListener listener = new TestListener(); 142 final CacheImpl cache = createCacheWithListener(listener); 143 144 Interceptor lockingInterceptor = new OptimisticLockingInterceptor(); 145 lockingInterceptor.setCache(cache); 146 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 147 interceptor.setCache(cache); 148 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 149 nodeInterceptor.setCache(cache); 150 MockInterceptor dummy = new MockInterceptor(); 151 dummy.setCache(cache); 152 lockingInterceptor.setNext(interceptor); 153 interceptor.setNext(nodeInterceptor); 154 nodeInterceptor.setNext(dummy); 155 156 cache.setInterceptorChain(lockingInterceptor); 157 158 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 160 mgr.begin(); 161 Transaction tx = mgr.getTransaction(); 162 163 cache.getInvocationContext().setTransaction(tx); 165 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 166 167 SamplePojo pojo = new SamplePojo(21, "test"); 168 Map temp = new HashMap (); 169 temp.put("key1", pojo); 170 cache.put("/one/two", temp); 171 172 assertEquals(null, dummy.getCalled()); 173 TransactionTable table = cache.getTransactionTable(); 174 175 GlobalTransaction gtx = table.get(tx); 176 177 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 178 179 TransactionWorkspace workspace = entry.getTransactionWorkSpace(); 180 181 185 186 assertEquals(3, workspace.getNodes().size()); 187 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 188 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 189 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 190 assertTrue(entry.getLocks().isEmpty()); 191 assertEquals(1, entry.getModifications().size()); 192 assertTrue(!cache.exists("/one/two")); 193 assertEquals(null, dummy.getCalled()); 194 195 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object []{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE}); 197 try 198 { 199 cache._replicate(prepareMethod); 200 } 201 catch (Throwable t) 202 { 203 204 } 205 206 207 assertEquals(3, workspace.getNodes().size()); 208 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 209 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 210 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 211 assertEquals(3, entry.getLocks().size()); 212 for (Iterator it = entry.getLocks().iterator(); it.hasNext();) 213 { 214 NodeLock lock = (NodeLock) it.next(); 215 assertTrue(lock.isWriteLocked()); 216 assertEquals(gtx, lock.getWriterOwner()); 217 } 218 assertEquals(1, entry.getModifications().size()); 219 assertTrue(!cache.exists("/one/two")); 220 assertEquals(MethodDeclarations.optimisticPrepareMethod, dummy.getCalled()); 222 223 224 MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, new Object []{gtx}); 225 try 226 { 227 cache._replicate(commitMethod); 228 } 229 catch (Throwable t) 230 { 231 fail(); 232 } 233 assertEquals(3, entry.getLocks().size()); 234 for (Iterator it = entry.getLocks().iterator(); it.hasNext();) 235 { 236 NodeLock lock = (NodeLock) it.next(); 237 assertEquals(false, lock.isLocked()); 238 239 } 240 int i = 0; 242 for (Iterator it = workspace.getNodes().values().iterator(); it.hasNext();) 243 { 244 NodeSPI node = ((WorkspaceNode) it.next()).getNode(); 245 assertEquals(node.getLock(), entry.getLocks().get(i)); 246 i++; 247 } 248 assertEquals(MethodDeclarations.commitMethod, dummy.getCalled()); 249 mgr.commit(); 250 251 cache.stop(); 252 253 } 254 255 public void testTransactionRollbackMethod() throws Exception 256 { 257 258 TestListener listener = new TestListener(); 259 final CacheImpl cache = createCacheWithListener(listener); 260 261 Interceptor lockingInterceptor = new OptimisticLockingInterceptor(); 262 lockingInterceptor.setCache(cache); 263 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 264 interceptor.setCache(cache); 265 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 266 nodeInterceptor.setCache(cache); 267 MockInterceptor dummy = new MockInterceptor(); 268 dummy.setCache(cache); 269 lockingInterceptor.setNext(interceptor); 270 interceptor.setNext(nodeInterceptor); 271 nodeInterceptor.setNext(dummy); 272 273 cache.setInterceptorChain(lockingInterceptor); 274 275 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 277 mgr.begin(); 278 Transaction tx = mgr.getTransaction(); 279 280 cache.getInvocationContext().setTransaction(tx); 282 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 283 284 SamplePojo pojo = new SamplePojo(21, "test"); 285 Map temp = new HashMap (); 286 temp.put("key1", pojo); 287 cache.put("/one/two", temp); 288 289 assertEquals(null, dummy.getCalled()); 290 TransactionTable table = cache.getTransactionTable(); 291 292 GlobalTransaction gtx = table.get(tx); 293 294 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 295 296 TransactionWorkspace workspace = entry.getTransactionWorkSpace(); 297 298 302 303 assertEquals(3, workspace.getNodes().size()); 304 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 305 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 306 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 307 assertTrue(entry.getLocks().isEmpty()); 308 assertEquals(1, entry.getModifications().size()); 309 assertTrue(!cache.exists("/one/two")); 310 assertEquals(null, dummy.getCalled()); 311 312 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object []{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE}); 314 try 315 { 316 cache._replicate(prepareMethod); 317 } 318 catch (Throwable t) 319 { 320 321 } 322 323 324 assertEquals(3, workspace.getNodes().size()); 325 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 326 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 327 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 328 assertEquals(3, entry.getLocks().size()); 329 for (Iterator it = entry.getLocks().iterator(); it.hasNext();) 330 { 331 NodeLock lock = (NodeLock) it.next(); 332 assertTrue(lock.isWriteLocked()); 333 assertEquals(gtx, lock.getWriterOwner()); 334 } 335 assertEquals(1, entry.getModifications().size()); 336 assertTrue(!cache.exists("/one/two")); 337 assertEquals(MethodDeclarations.optimisticPrepareMethod, dummy.getCalled()); 338 339 340 MethodCall rollbackMethod = MethodCallFactory.create(MethodDeclarations.rollbackMethod, new Object []{gtx}); 341 try 342 { 343 cache._replicate(rollbackMethod); 344 } 345 catch (Throwable t) 346 { 347 fail(); 348 } 349 assertEquals(3, entry.getLocks().size()); 350 for (Iterator it = entry.getLocks().iterator(); it.hasNext();) 351 { 352 NodeLock lock = (NodeLock) it.next(); 353 assertEquals(false, lock.isLocked()); 354 355 } 356 int i = 0; 358 for (Iterator it = workspace.getNodes().values().iterator(); it.hasNext();) 359 { 360 NodeSPI node = ((WorkspaceNode) it.next()).getNode(); 361 assertEquals(node.getLock(), entry.getLocks().get(i)); 362 i++; 363 } 364 assertEquals(MethodDeclarations.rollbackMethod, dummy.getCalled()); 365 mgr.commit(); 366 367 cache.stop(); 368 369 } 370 371 } 372 | Popular Tags |