1 7 package org.jboss.cache.options.cachemodelocal; 8 9 import junit.framework.Assert; 10 import junit.framework.TestCase; 11 import org.jboss.cache.CacheImpl; 12 import org.jboss.cache.Fqn; 13 import org.jboss.cache.config.Configuration; 14 15 import javax.transaction.SystemException ; 16 import javax.transaction.TransactionManager ; 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 27 public abstract class CacheModeLocalTestBase extends TestCase 28 { 29 protected Configuration.CacheMode cacheMode; 31 protected String nodeLockingScheme; 32 35 protected boolean isInvalidation; 36 37 private CacheImpl cache1; 38 private CacheImpl cache2; 39 40 private Fqn fqn = Fqn.fromString("/a"); 41 private String key = "key"; 42 43 protected void setUp() throws Exception 44 { 45 if (cache1 != null || cache2 != null) tearDown(); 47 48 cache1 = new CacheImpl(); 49 cache1.getConfiguration().setClusterName("test"); 50 cache1.getConfiguration().setInitialStateRetrievalTimeout(1000); 51 cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 52 cache1.getConfiguration().setNodeLockingScheme(nodeLockingScheme); 53 cache1.getConfiguration().setCacheMode(cacheMode); 54 cache1.start(); 55 56 cache2 = new CacheImpl(); 57 cache2.getConfiguration().setClusterName("test"); 58 cache2.getConfiguration().setInitialStateRetrievalTimeout(1000); 59 cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 60 cache2.getConfiguration().setNodeLockingScheme(nodeLockingScheme); 61 cache2.getConfiguration().setCacheMode(cacheMode); 62 cache2.start(); 63 } 64 65 protected void tearDown() 66 { 67 if (cache1 != null) 68 { 69 cache1.stop(); 70 flushTxs(cache1.getTransactionManager()); 71 cache1 = null; 72 } 73 74 if (cache2 != null) 75 { 76 cache2.stop(); 77 flushTxs(cache2.getTransactionManager()); 78 cache2 = null; 79 } 80 } 81 82 private void flushTxs(TransactionManager mgr) 83 { 84 if (mgr != null) 85 { 86 try 87 { 88 if (mgr.getTransaction() != null) 89 { 90 mgr.rollback(); 91 } 92 } 93 catch (SystemException e) 94 { 95 } 97 } 98 } 99 100 public void testPutKeyValue() throws Exception 101 { 102 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true); 103 cache1.put(fqn, key, "value"); 104 delay(); 105 Assert.assertEquals("value", cache1.get(fqn, key)); 107 108 Assert.assertNull("Should be null", cache2.get(fqn, key)); 110 111 cache1.getInvocationContext().getOptionOverrides().reset(); 113 cache1.put(fqn, key, "value"); 114 delay(); 115 Assert.assertEquals("value", cache1.get(fqn, key)); 117 118 if (!isInvalidation) 120 Assert.assertEquals("value", cache2.get(fqn, key)); 121 else 122 Assert.assertNull("should be invalidated", cache2.get(fqn, key)); 123 124 cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true); 126 cache2.put(fqn, key, "value2"); 127 delay(); 128 Assert.assertEquals("value2", cache2.get(fqn, key)); 129 Assert.assertEquals("value", cache1.get(fqn, key)); 130 131 cache2.getInvocationContext().getOptionOverrides().reset(); 132 cache2.put(fqn, key, "value2"); 133 delay(); 134 Assert.assertEquals("value2", cache2.get(fqn, key)); 135 if (!isInvalidation) 136 Assert.assertEquals("value2", cache1.get(fqn, key)); 137 else 138 Assert.assertNull("should be invalidated", cache1.get(fqn, key)); 139 } 140 141 public void testPutData() throws Exception 142 { 143 Map map = new HashMap (); 144 map.put(key, "value"); 145 146 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true); 147 cache1.put(fqn, map); 148 delay(); 149 Assert.assertEquals("value", cache1.get(fqn, key)); 151 Assert.assertNull("Should be null", cache2.get(fqn, key)); 153 154 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(false); 156 cache1.put(fqn, map); 157 delay(); 158 Assert.assertEquals("value", cache1.get(fqn, key)); 160 if (!isInvalidation) 162 Assert.assertEquals("value", cache2.get(fqn, key)); 163 else 164 Assert.assertNull("should be invalidated", cache2.get(fqn, key)); 165 166 map.put(key, "value2"); 168 cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true); 169 cache2.put(fqn, map); 170 delay(); 171 Assert.assertEquals("value2", cache2.get(fqn, key)); 172 Assert.assertEquals("value", cache1.get(fqn, key)); 173 174 cache2.getInvocationContext().getOptionOverrides().reset(); 175 cache2.put(fqn, key, "value2"); 176 delay(); 177 Assert.assertEquals("value2", cache2.get(fqn, key)); 178 if (!isInvalidation) 179 Assert.assertEquals("value2", cache1.get(fqn, key)); 180 else 181 Assert.assertNull("should be invalidated", cache1.get(fqn, key)); 182 } 183 184 public void testRemoveNode() throws Exception 185 { 186 cache1.getInvocationContext().setOptionOverrides(null); 189 cache1.put(fqn, key, "value"); 190 delay(); 191 Assert.assertEquals("value", cache1.get(fqn, key)); 192 if (isInvalidation) 193 Assert.assertNull("Should be null", cache2.get(fqn, key)); 194 else 195 Assert.assertEquals("value", cache2.get(fqn, key)); 196 197 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true); 198 cache1.removeNode(fqn); 199 delay(); 200 201 Assert.assertNull("should be null", cache1.get(fqn, key)); 203 if (isInvalidation) 205 Assert.assertNull("Should be null", cache2.get(fqn, key)); 206 else 207 Assert.assertEquals("value", cache2.get(fqn, key)); 208 209 cache1.put(fqn, key, "value"); 211 delay(); 212 Assert.assertEquals("value", cache1.get(fqn, key)); 213 if (isInvalidation) 214 Assert.assertNull("Should be null", cache2.get(fqn, key)); 215 else 216 Assert.assertEquals("value", cache2.get(fqn, key)); 217 218 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(false); 220 cache1.remove(fqn); 221 delay(); 222 223 Assert.assertNull("should be null", cache1.get(fqn, key)); 225 Assert.assertNull("should be null", cache2.get(fqn, key)); 226 } 227 228 public void testRemoveKey() throws Exception 229 { 230 cache1.getInvocationContext().setOptionOverrides(null); 232 cache1.put(fqn, key, "value"); 233 delay(); 234 Assert.assertEquals("value", cache1.get(fqn, key)); 235 if (isInvalidation) 236 Assert.assertNull("Should be null", cache2.get(fqn, key)); 237 else 238 Assert.assertEquals("value", cache2.get(fqn, key)); 239 240 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true); 241 cache1.remove(fqn, key); 242 delay(); 243 244 Assert.assertNull("should be null", cache1.get(fqn, key)); 246 if (isInvalidation) 248 Assert.assertNull("Should be null", cache2.get(fqn, key)); 249 else 250 Assert.assertEquals("value", cache2.get(fqn, key)); 251 252 cache1.put(fqn, key, "value"); 254 delay(); 255 Assert.assertEquals("value", cache1.get(fqn, key)); 256 if (isInvalidation) 257 Assert.assertNull("Should be null", cache2.get(fqn, key)); 258 else 259 Assert.assertEquals("value", cache2.get(fqn, key)); 260 261 cache1.getInvocationContext().getOptionOverrides().reset(); 263 cache1.remove(fqn, key); 264 delay(); 265 266 Assert.assertNull("should be null", cache1.get(fqn, key)); 268 Assert.assertNull("should be null", cache2.get(fqn, key)); 269 } 270 271 public void testTransactionalBehaviour() throws Exception 272 { 273 TransactionManager mgr = cache1.getTransactionManager(); 274 mgr.begin(); 275 cache1.getInvocationContext().getOptionOverrides().reset(); 276 cache1.put(fqn, key, "value1"); 277 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true); 278 cache1.put(fqn, key, "value2"); 279 mgr.commit(); 280 delay(); 281 Assert.assertEquals("value2", cache1.get(fqn, key)); 283 284 if (!isInvalidation) 285 Assert.assertEquals("value1", cache2.get(fqn, key)); 286 else 287 assertNull(cache2.get(fqn, key)); 288 289 mgr.begin(); 291 cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true); 292 cache1.put(fqn, key, "value3"); 293 cache1.getInvocationContext().getOptionOverrides().reset(); 294 cache1.put(fqn, key, "value"); 295 mgr.commit(); 296 delay(); 297 Assert.assertEquals("value", cache1.get(fqn, key)); 299 300 if (!isInvalidation) 302 Assert.assertEquals("value", cache2.get(fqn, key)); 303 else 304 Assert.assertNull("should be invalidated", cache2.get(fqn, key)); 305 306 mgr = cache2.getTransactionManager(); 308 mgr.begin(); 309 cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(false); 310 cache2.put(fqn, key, "value3"); 311 cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true); 312 cache2.put(fqn, key, "value2"); 313 mgr.commit(); 314 delay(); 315 316 Assert.assertEquals("value2", cache2.get(fqn, key)); 317 318 if (!isInvalidation) 319 assertEquals("value3", cache1.get(fqn, key)); 320 else 321 assertNull(cache1.get(fqn, key)); 322 323 mgr.begin(); 324 cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true); 325 cache2.put(fqn, key, "value2"); 326 cache2.getInvocationContext().getOptionOverrides().reset(); 327 cache2.put(fqn, key, "value2"); 328 mgr.commit(); 329 delay(); 330 Assert.assertEquals("value2", cache2.get(fqn, key)); 331 if (!isInvalidation) 332 Assert.assertEquals("value2", cache1.get(fqn, key)); 333 else 334 Assert.assertNull("should be invalidated", cache1.get(fqn, key)); 335 336 } 337 338 protected abstract void delay(); 339 340 } | Popular Tags |