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