1 7 package org.jboss.cache.loader; 8 9 import junit.framework.Assert; 10 import junit.framework.Test; 11 import junit.framework.TestSuite; 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 import org.jboss.cache.CacheImpl; 15 import org.jboss.cache.Fqn; 16 import org.jboss.cache.config.Configuration; 17 import org.jboss.cache.misc.TestingUtil; 18 19 import javax.transaction.TransactionManager ; 20 import java.io.File ; 21 22 27 public class CacheLoaderWithReplicationTest extends AbstractCacheLoaderTestBase 28 { 29 private CacheImpl cache1, cache2; 30 private String tmpLocation1 = System.getProperty("java.io.tmpdir", ".") + File.separator + "JBossCache-CacheLoaderWithReplicationTest1"; 31 private String tmpLocation2 = System.getProperty("java.io.tmpdir", ".") + File.separator + "JBossCache-CacheLoaderWithReplicationTest2"; 32 private File dir1 = new File (tmpLocation1); 33 private File dir2 = new File (tmpLocation2); 34 private Fqn fqn = Fqn.fromString("/a"); 35 private String key = "key"; 36 37 private static final Log log = LogFactory.getLog(CacheLoaderWithReplicationTest.class); 38 39 40 public CacheLoaderWithReplicationTest() 41 { 42 recursivedelete(dir1); 43 recursivedelete(dir2); 44 45 if (!dir1.exists()) dir1.mkdirs(); 46 if (!dir2.exists()) dir2.mkdirs(); 47 48 log.debug(" System props dump: " + System.getProperties()); 49 log.debug("Using location for CL 1 : " + tmpLocation1); 50 log.debug("Using location for CL 2 : " + tmpLocation2); 51 } 52 53 public void setUp() throws Exception 54 { 55 cache1 = new CacheImpl(); 56 cache1.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.bdbje.BdbjeCacheLoader", "location=" + tmpLocation1, false, true, false)); 57 cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 58 59 cache2 = new CacheImpl(); 60 cache2.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.bdbje.BdbjeCacheLoader", "location=" + tmpLocation2, false, true, false)); 61 cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 62 } 63 64 public void tearDown() throws Exception 65 { 66 if (cache1 != null) 67 { 68 try 69 { 70 cache1.remove(fqn); 71 cache1.getCacheLoaderManager().getCacheLoader().remove(fqn); 72 cache1.stop(); 73 } 74 finally 75 { 76 cache1 = null; 77 } 78 } 79 80 if (cache2 != null) 81 { 82 try 83 { 84 cache2.remove(fqn); 85 cache2.getCacheLoaderManager().getCacheLoader().remove(fqn); 86 cache2.stop(); 87 } 88 finally 89 { 90 cache2 = null; 91 } 92 93 } 94 recursivedelete(dir1); 95 recursivedelete(dir2); 96 } 97 98 private void recursivedelete(File f) 99 { 100 if (f.isDirectory()) 101 { 102 File [] files = f.listFiles(); 103 for (int i = 0; i < files.length; i++) 104 { 105 recursivedelete(files[i]); 106 } 107 } 108 f.delete(); 110 } 111 112 public void testPessSyncRepl() throws Exception 113 { 114 cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 115 cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 116 117 cache1.start(); 118 cache2.start(); 119 120 Assert.assertNull(cache1.get(fqn, key)); 121 Assert.assertNull(cache2.get(fqn, key)); 122 123 124 CacheLoader loader1 = cache1.getCacheLoaderManager().getCacheLoader(); 125 CacheLoader loader2 = cache2.getCacheLoaderManager().getCacheLoader(); 126 127 TransactionManager mgr = cache1.getTransactionManager(); 128 mgr.begin(); 129 cache1.put(fqn, key, "value"); 130 131 Assert.assertEquals("value", cache1.get(fqn, key)); 132 Assert.assertNull(cache2.get(fqn, key)); 133 Assert.assertNull(loader1.get(fqn)); 134 Assert.assertNull(loader2.get(fqn)); 135 mgr.commit(); 136 137 Assert.assertEquals("value", cache1.get(fqn, key)); 138 Assert.assertEquals("value", cache2.get(fqn, key)); 139 Assert.assertEquals("value", loader1.get(fqn).get(key)); 140 Assert.assertEquals("value", loader2.get(fqn).get(key)); 141 142 mgr.begin(); 143 cache1.put(fqn, key, "value2"); 144 145 Assert.assertEquals("value2", cache1.get(fqn, key)); 146 Assert.assertEquals("value", cache2.get(fqn, key)); 147 Assert.assertEquals("value", loader1.get(fqn).get(key)); 148 Assert.assertEquals("value", loader2.get(fqn).get(key)); 149 150 mgr.rollback(); 151 152 Assert.assertEquals("value", cache1.get(fqn, key)); 153 Assert.assertEquals("value", cache2.get(fqn, key)); 154 Assert.assertEquals("value", loader1.get(fqn).get(key)); 155 Assert.assertEquals("value", loader2.get(fqn).get(key)); 156 157 tearDown(); 159 } 160 161 public void testPessAsyncRepl() throws Exception 162 { 163 cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_ASYNC); 164 cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_ASYNC); 165 166 cache1.start(); 167 cache2.start(); 168 169 CacheLoader loader1 = cache1.getCacheLoaderManager().getCacheLoader(); 170 CacheLoader loader2 = cache2.getCacheLoaderManager().getCacheLoader(); 171 172 Assert.assertNull(loader1.get(fqn)); 174 Assert.assertNull(loader2.get(fqn)); 175 Assert.assertNull(cache1.get(fqn)); 176 Assert.assertNull(cache2.get(fqn)); 177 178 TransactionManager mgr = cache1.getTransactionManager(); 179 mgr.begin(); 180 cache1.put(fqn, key, "value"); 181 182 Assert.assertEquals("value", cache1.get(fqn, key)); 183 Assert.assertNull(cache2.get(fqn, key)); 184 Assert.assertNull(loader1.get(fqn)); 185 Assert.assertNull(loader2.get(fqn)); 186 mgr.commit(); 187 188 TestingUtil.sleepThread(500); 189 190 Assert.assertEquals("value", cache1.get(fqn, key)); 191 Assert.assertEquals("value", cache2.get(fqn, key)); 192 Assert.assertEquals("value", loader1.get(fqn).get(key)); 193 Assert.assertEquals("value", loader2.get(fqn).get(key)); 194 195 mgr.begin(); 196 cache1.put(fqn, key, "value2"); 197 198 Assert.assertEquals("value2", cache1.get(fqn, key)); 199 Assert.assertEquals("value", cache2.get(fqn, key)); 200 Assert.assertEquals("value", loader1.get(fqn).get(key)); 201 Assert.assertEquals("value", loader2.get(fqn).get(key)); 202 203 mgr.rollback(); 204 205 TestingUtil.sleepThread(500); 206 207 Assert.assertEquals("value", cache1.get(fqn, key)); 208 Assert.assertEquals("value", cache2.get(fqn, key)); 209 Assert.assertEquals("value", loader1.get(fqn).get(key)); 210 Assert.assertEquals("value", loader2.get(fqn).get(key)); 211 212 tearDown(); 214 } 215 216 public void testOptSyncRepl() throws Exception 217 { 218 try 219 { 220 cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 221 cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 222 223 cache1.getConfiguration().setNodeLockingScheme("OPTIMISTIC"); 224 cache2.getConfiguration().setNodeLockingScheme("OPTIMISTIC"); 225 226 cache1.start(); 227 cache2.start(); 228 229 CacheLoader loader1 = cache1.getCacheLoaderManager().getCacheLoader(); 230 CacheLoader loader2 = cache2.getCacheLoaderManager().getCacheLoader(); 231 232 TransactionManager mgr = cache1.getTransactionManager(); 233 mgr.begin(); 234 cache1.put(fqn, key, "value"); 235 236 Assert.assertEquals("value", cache1.get(fqn, key)); 237 Assert.assertNull(cache2.get(fqn, key)); 238 Assert.assertNull(loader1.get(fqn)); 239 Assert.assertNull(loader2.get(fqn)); 240 mgr.commit(); 241 242 Assert.assertEquals("value", cache1.get(fqn, key)); 243 Assert.assertEquals("value", cache2.get(fqn, key)); 244 Assert.assertEquals("value", loader1.get(fqn).get(key)); 245 Assert.assertEquals("value", loader2.get(fqn).get(key)); 246 247 mgr.begin(); 248 cache1.put(fqn, key, "value2"); 249 250 Assert.assertEquals("value2", cache1.get(fqn, key)); 251 Assert.assertEquals("value", cache2.get(fqn, key)); 252 Assert.assertEquals("value", loader1.get(fqn).get(key)); 253 Assert.assertEquals("value", loader2.get(fqn).get(key)); 254 255 mgr.rollback(); 256 257 Assert.assertEquals("value", cache1.get(fqn, key)); 258 Assert.assertEquals("value", cache2.get(fqn, key)); 259 Assert.assertEquals("value", loader1.get(fqn).get(key)); 260 Assert.assertEquals("value", loader2.get(fqn).get(key)); 261 } 262 catch (Exception e) 263 { 264 Assert.assertTrue("Caught exception " + e.getMessage(), false); 265 e.printStackTrace(); 266 } 267 268 tearDown(); 270 } 271 272 public void testOptAsyncRepl() throws Exception 273 { 274 cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_ASYNC); 275 cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_ASYNC); 276 277 cache1.getConfiguration().setNodeLockingScheme("OPTIMISTIC"); 278 cache2.getConfiguration().setNodeLockingScheme("OPTIMISTIC"); 279 280 cache1.start(); 281 cache2.start(); 282 283 CacheLoader loader1 = cache1.getCacheLoaderManager().getCacheLoader(); 284 CacheLoader loader2 = cache2.getCacheLoaderManager().getCacheLoader(); 285 286 TransactionManager mgr = cache1.getTransactionManager(); 287 mgr.begin(); 288 cache1.put(fqn, key, "value"); 289 290 Assert.assertEquals("value", cache1.get(fqn, key)); 291 Assert.assertNull(cache2.get(fqn, key)); 292 Assert.assertNull(loader1.get(fqn)); 293 Assert.assertNull(loader2.get(fqn)); 294 mgr.commit(); 295 296 TestingUtil.sleepThread(500); 297 298 Assert.assertEquals("value", cache1.get(fqn, key)); 299 Assert.assertEquals("value", cache2.get(fqn, key)); 300 Assert.assertEquals("value", loader1.get(fqn).get(key)); 301 Assert.assertEquals("value", loader2.get(fqn).get(key)); 302 303 mgr.begin(); 304 cache1.put(fqn, key, "value2"); 305 306 Assert.assertEquals("value2", cache1.get(fqn, key)); 307 Assert.assertEquals("value", cache2.get(fqn, key)); 308 Assert.assertEquals("value", loader1.get(fqn).get(key)); 309 Assert.assertEquals("value", loader2.get(fqn).get(key)); 310 311 mgr.rollback(); 312 TestingUtil.sleepThread(500); 313 314 Assert.assertEquals("value", cache1.get(fqn, key)); 315 Assert.assertEquals("value", cache2.get(fqn, key)); 316 Assert.assertEquals("value", loader1.get(fqn).get(key)); 317 Assert.assertEquals("value", loader2.get(fqn).get(key)); 318 319 tearDown(); 321 } 322 323 public static Test suite() 324 { 325 return new TestSuite(CacheLoaderWithReplicationTest.class); 326 } 327 328 public static void main(String [] args) 329 { 330 junit.textui.TestRunner.run(suite()); 331 } 332 333 } 334 335 336 | Popular Tags |