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 20 public class NodeInterceptorGetKeysTest extends AbstractOptimisticTestCase 21 { 22 23 24 27 public NodeInterceptorGetKeysTest(String name) 28 { 29 super(name); 30 } 31 32 33 public void testTransactionGetKeysMethod() 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 62 cache.put("/one/two", "key1", pojo); 63 64 assertEquals(null, dummy.getCalled()); 65 TransactionTable table = cache.getTransactionTable(); 66 67 GlobalTransaction gtx = table.get(tx); 68 69 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 70 71 TransactionWorkspace workspace = entry.getTransactionWorkSpace(); 72 73 assertEquals(1, cache.getKeys("/one/two").size()); 75 mgr.commit(); 76 77 assertEquals(3, workspace.getNodes().size()); 79 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 80 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 81 assertTrue(entry.getLocks().isEmpty()); 82 assertEquals(1, entry.getModifications().size()); 83 assertTrue(!cache.exists("/one/two")); 84 assertEquals(null, dummy.getCalled()); 85 86 mgr.begin(); 89 90 Transaction tx2 = mgr.getTransaction(); 91 92 cache.getInvocationContext().setTransaction(tx2); 94 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx2)); 95 96 assertNull(cache.get("/one/two", "key1")); 97 mgr.commit(); 98 cache.stop(); 99 } 100 101 102 public void testTransactionGetNoKeysMethod() throws Exception 103 { 104 105 TestListener listener = new TestListener(); 106 final CacheImpl cache = createCacheWithListener(listener); 107 108 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 109 interceptor.setCache(cache); 110 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 111 nodeInterceptor.setCache(cache); 112 MockInterceptor dummy = new MockInterceptor(); 113 dummy.setCache(cache); 114 115 interceptor.setNext(nodeInterceptor); 116 nodeInterceptor.setNext(dummy); 117 118 cache.setInterceptorChain(interceptor); 119 120 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 122 mgr.begin(); 123 Transaction tx = mgr.getTransaction(); 124 125 cache.getInvocationContext().setTransaction(tx); 127 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 128 129 130 assertEquals(null, dummy.getCalled()); 131 TransactionTable table = cache.getTransactionTable(); 132 133 GlobalTransaction gtx = table.get(tx); 134 135 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 136 137 assertEquals(0, cache.getKeys("/").size()); 139 mgr.commit(); 140 141 142 assertTrue(entry.getLocks().isEmpty()); 143 assertEquals(0, entry.getModifications().size()); 144 assertTrue(!cache.exists("/one/two")); 145 assertEquals(null, dummy.getCalled()); 146 147 148 cache.stop(); 149 } 150 151 public void testTransactionGetKeysIteratorMethod() throws Exception 152 { 153 154 TestListener listener = new TestListener(); 155 final CacheImpl cache = createCacheWithListener(listener); 156 157 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 158 interceptor.setCache(cache); 159 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 160 nodeInterceptor.setCache(cache); 161 MockInterceptor dummy = new MockInterceptor(); 162 dummy.setCache(cache); 163 164 interceptor.setNext(nodeInterceptor); 165 nodeInterceptor.setNext(dummy); 166 167 cache.setInterceptorChain(interceptor); 168 169 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 171 mgr.begin(); 172 Transaction tx = mgr.getTransaction(); 173 174 cache.getInvocationContext().setTransaction(tx); 176 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 177 178 179 SamplePojo pojo = new SamplePojo(21, "test"); 180 181 cache.put("/one/two", "key1", pojo); 182 183 assertEquals(null, dummy.getCalled()); 184 TransactionTable table = cache.getTransactionTable(); 185 186 GlobalTransaction gtx = table.get(tx); 187 188 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 189 190 assertEquals(1, cache.getKeys("/one/two").size()); 192 193 for (Iterator it = cache.getKeys("/one/two").iterator(); it.hasNext();) 194 { 195 it.next(); 196 it.remove(); 197 } 198 assertEquals(0, cache.getKeys("/one/two").size()); 199 mgr.commit(); 200 201 202 assertTrue(entry.getLocks().isEmpty()); 203 assertEquals(1, entry.getModifications().size()); 204 assertTrue(!cache.exists("/one/two")); 205 assertEquals(null, dummy.getCalled()); 206 207 208 cache.stop(); 209 } 210 211 212 } 213 | Popular Tags |