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.HashMap ; 16 import java.util.Map ; 17 18 21 public class NodeInterceptorPutEraseTest extends AbstractOptimisticTestCase 22 { 23 24 25 28 public NodeInterceptorPutEraseTest(String name) 29 { 30 super(name); 31 } 32 33 public void testTransactionPutKeyMethod() throws Exception 34 { 35 36 TestListener listener = new TestListener(); 37 final CacheImpl cache = createCacheWithListener(listener); 38 39 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 40 interceptor.setCache(cache); 41 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 42 nodeInterceptor.setCache(cache); 43 MockInterceptor dummy = new MockInterceptor(); 44 dummy.setCache(cache); 45 46 interceptor.setNext(nodeInterceptor); 47 nodeInterceptor.setNext(dummy); 48 49 cache.setInterceptorChain(interceptor); 50 51 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 53 mgr.begin(); 54 Transaction tx = mgr.getTransaction(); 55 56 cache.getInvocationContext().setTransaction(tx); 58 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 59 60 SamplePojo pojo = new SamplePojo(21, "test"); 61 Map temp = new HashMap (); 62 temp.put("key1", pojo); 63 cache.put("/one/two", temp); 64 65 assertEquals(null, dummy.getCalled()); 66 TransactionTable table = cache.getTransactionTable(); 67 68 GlobalTransaction gtx = table.get(tx); 69 70 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 71 72 TransactionWorkspace workspace = entry.getTransactionWorkSpace(); 73 74 mgr.commit(); 75 76 assertEquals(3, workspace.getNodes().size()); 78 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 79 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 80 assertTrue(entry.getLocks().isEmpty()); 81 assertEquals(1, entry.getModifications().size()); 82 assertTrue(!cache.exists("/one/two")); 83 assertEquals(null, dummy.getCalled()); 84 cache.stop(); 85 } 86 87 public void testTransactionKeyValOverwriteMethod() throws Exception 88 { 89 90 TestListener listener = new TestListener(); 91 final CacheImpl cache = createCacheWithListener(listener); 92 93 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 94 interceptor.setCache(cache); 95 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 96 nodeInterceptor.setCache(cache); 97 MockInterceptor dummy = new MockInterceptor(); 98 dummy.setCache(cache); 99 100 interceptor.setNext(nodeInterceptor); 101 nodeInterceptor.setNext(dummy); 102 103 cache.setInterceptorChain(interceptor); 104 105 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 107 mgr.begin(); 108 Transaction tx = mgr.getTransaction(); 109 110 cache.getInvocationContext().setTransaction(tx); 112 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 113 114 SamplePojo pojo = new SamplePojo(21, "test"); 115 116 cache.put("/one/two", "key1", pojo); 117 118 SamplePojo pojo2 = new SamplePojo(21, "test"); 120 121 cache.put("/one/two", "key1", pojo2); 122 123 assertEquals(null, dummy.getCalled()); 124 TransactionTable table = cache.getTransactionTable(); 125 126 GlobalTransaction gtx = table.get(tx); 127 128 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 129 130 TransactionWorkspace workspace = entry.getTransactionWorkSpace(); 131 132 mgr.commit(); 133 assertEquals(3, workspace.getNodes().size()); 135 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 136 assertEquals(pojo2, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 137 assertTrue(entry.getLocks().isEmpty()); 138 assertEquals(2, entry.getModifications().size()); 139 assertTrue(!cache.exists("/one/two")); 140 assertEquals(null, dummy.getCalled()); 141 cache.stop(); 142 } 143 144 public void testTransactionKeyValOverwriteNullMethod() throws Exception 145 { 146 147 TestListener listener = new TestListener(); 148 final CacheImpl cache = createCacheWithListener(listener); 149 150 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 151 interceptor.setCache(cache); 152 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 153 nodeInterceptor.setCache(cache); 154 MockInterceptor dummy = new MockInterceptor(); 155 dummy.setCache(cache); 156 157 interceptor.setNext(nodeInterceptor); 158 nodeInterceptor.setNext(dummy); 159 160 cache.setInterceptorChain(interceptor); 161 162 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 164 mgr.begin(); 165 Transaction tx = mgr.getTransaction(); 166 167 cache.getInvocationContext().setTransaction(tx); 169 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 170 171 SamplePojo pojo = new SamplePojo(21, "test"); 172 173 cache.put("/one/two", "key1", pojo); 174 175 176 cache.put("/one/two", "key1", null); 177 178 assertEquals(null, dummy.getCalled()); 179 TransactionTable table = cache.getTransactionTable(); 180 181 GlobalTransaction gtx = table.get(tx); 182 183 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 184 185 TransactionWorkspace workspace = entry.getTransactionWorkSpace(); 186 187 mgr.commit(); 188 assertEquals(3, workspace.getNodes().size()); 190 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 191 assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 192 assertTrue(entry.getLocks().isEmpty()); 193 assertEquals(2, entry.getModifications().size()); 194 assertTrue(!cache.exists("/one/two")); 195 assertEquals(null, dummy.getCalled()); 196 cache.stop(); 197 } 198 199 200 } 201 | Popular Tags |