1 7 package org.jboss.cache.loader; 8 9 import junit.framework.Assert; 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.jboss.cache.CacheImpl; 14 import org.jboss.cache.Fqn; 15 import org.jboss.cache.config.CacheLoaderConfig; 16 import org.jboss.cache.factories.XmlConfigurationParser; 17 import org.jboss.cache.xml.XmlHelper; 18 import org.w3c.dom.Element ; 19 20 import javax.transaction.TransactionManager ; 21 import java.io.File ; 22 23 28 public class ChainingCacheLoaderFullTest extends TestCase 29 { 30 private String loc1 = System.getProperty("java.io.tmpdir", ".") + File.separator + "JBossCache-ChainingCacheLoaderBasicTest-1"; 31 private String loc2 = System.getProperty("java.io.tmpdir", ".") + File.separator + "JBossCache-ChainingCacheLoaderBasicTest-2"; 32 33 private CacheImpl cache; 34 private ChainingCacheLoader chainingCacheLoader; 35 private CacheLoader loader1, loader2; 36 private Fqn fqn = Fqn.fromString("/a/b"); 37 private String key = "key"; 38 private String value = "value"; 39 40 41 public ChainingCacheLoaderFullTest() 42 { 43 File dir1 = new File (loc1); 44 File dir2 = new File (loc2); 45 46 if (!dir1.exists()) dir1.mkdirs(); 47 if (!dir2.exists()) dir2.mkdirs(); 48 } 49 50 protected void startCache(boolean ignoreMods1, boolean ignoreMods2) throws Exception 51 { 52 cache = new CacheImpl(); 53 cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig(loc1, loc2, ignoreMods1, ignoreMods2)); 54 cache.getConfiguration().setCacheMode("local"); 55 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 56 cache.start(); 57 58 chainingCacheLoader = (ChainingCacheLoader) cache.getCacheLoaderManager().getCacheLoader(); 59 loader1 = chainingCacheLoader.getCacheLoaders().get(0); 60 loader2 = chainingCacheLoader.getCacheLoaders().get(1); 61 } 62 63 protected void cleanup() throws Exception 64 { 65 cache.remove("/"); 66 cache.stop(); 67 cache = null; 68 } 69 70 protected CacheLoaderConfig getCacheLoaderConfig(String loc1, String loc2, boolean ignoreMods1, boolean ignoreMods2) throws Exception 71 { 72 String xml = "<config>\n" + 73 "<passivation>false</passivation>\n" + 74 "<preload></preload>\n" + 75 "<cacheloader>\n" + 76 "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" + 77 "<properties>" + 78 " location=" + loc1 + "\n" + 79 "</properties>\n" + 80 "<async>false</async>\n" + 81 "<fetchPersistentState>true</fetchPersistentState>\n" + 82 "<ignoreModifications>" + ignoreMods1 + "</ignoreModifications>\n" + 83 "</cacheloader>\n" + 84 "<cacheloader>\n" + 85 "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" + 86 "<properties>" + 87 " location=" + loc2 + "\n" + 88 "</properties>\n" + 89 "<async>false</async>\n" + 90 "<fetchPersistentState>false</fetchPersistentState>\n" + 91 "<ignoreModifications>" + ignoreMods2 + "</ignoreModifications>\n" + 92 "</cacheloader>\n" + 93 "</config>"; 94 Element element = XmlHelper.stringToElement(xml); 95 return XmlConfigurationParser.parseCacheLoaderConfig(element); 96 } 97 98 public void testCruds() throws Exception 99 { 100 startCache(false, false); 101 102 cache.put(fqn, key, value); 104 105 Assert.assertEquals(value, cache.get(fqn, key)); 106 Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key)); 107 Assert.assertEquals(value, loader1.get(fqn).get(key)); 108 Assert.assertEquals(value, loader2.get(fqn).get(key)); 109 110 cache.evict(fqn); 112 Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key)); 113 Assert.assertEquals(value, loader1.get(fqn).get(key)); 114 Assert.assertEquals(value, loader2.get(fqn).get(key)); 115 Assert.assertEquals(value, cache.get(fqn, key)); 116 117 cache.remove(fqn); 119 Assert.assertNull(value, cache.get(fqn, key)); 120 Assert.assertNull(value, chainingCacheLoader.get(fqn)); 121 Assert.assertNull(value, loader1.get(fqn)); 122 Assert.assertNull(value, loader2.get(fqn)); 123 124 cleanup(); 125 } 126 127 public void testGets() throws Exception 128 { 129 startCache(false, false); 130 131 cache.put(fqn, key, value); 132 133 cache.evict(fqn); 135 loader1.put(fqn, key, value + 2); 136 Assert.assertEquals(value, loader2.get(fqn).get(key)); 137 Assert.assertEquals(value + 2, loader1.get(fqn).get(key)); 138 Assert.assertEquals(value + 2, chainingCacheLoader.get(fqn).get(key)); 139 Assert.assertEquals(value + 2, cache.get(fqn, key)); 140 141 cache.remove("/"); 142 cache.put(fqn, key, value); 143 144 cache.evict(fqn); 146 loader2.put(fqn, key, value + 2); 147 Assert.assertEquals(value + 2, loader2.get(fqn).get(key)); 148 Assert.assertEquals(value, loader1.get(fqn).get(key)); 149 Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key)); 150 Assert.assertEquals(value, cache.get(fqn, key)); 151 152 cache.remove("/"); 153 cache.put(fqn, key, value); 154 155 cache.evict(fqn); 157 loader1.remove(fqn); 158 Assert.assertNull(loader1.get(fqn)); 159 Assert.assertEquals(value, loader2.get(fqn).get(key)); 160 Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key)); 161 Assert.assertEquals(value, cache.get(fqn, key)); 162 163 cleanup(); 164 } 165 166 public void testIgnoreMods() throws Exception 167 { 168 startCache(false, true); 169 170 loader1.put(fqn, key, value); 172 loader2.put(fqn, key, value); 173 174 Assert.assertEquals(value, cache.get(fqn, key)); 176 Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key)); 177 Assert.assertEquals(value, loader1.get(fqn).get(key)); 178 Assert.assertEquals(value, loader2.get(fqn).get(key)); 179 180 cache.put(fqn, key, value + 2); 182 Assert.assertEquals(value + 2, cache.get(fqn, key)); 183 Assert.assertEquals(value + 2, chainingCacheLoader.get(fqn).get(key)); 184 Assert.assertEquals(value + 2, loader1.get(fqn).get(key)); 185 Assert.assertEquals(value, loader2.get(fqn).get(key)); 186 187 cache.remove(fqn); 189 Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key)); 190 Assert.assertNull(loader1.get(fqn)); 191 Assert.assertEquals(value, loader2.get(fqn).get(key)); 192 Assert.assertEquals(value, cache.get(fqn, key)); 193 194 cleanup(); 195 } 196 197 public void testIgnoreModsTransactional() throws Exception 198 { 199 startCache(false, true); 200 TransactionManager mgr = cache.getTransactionManager(); 201 202 loader1.put(fqn, key, value); 204 loader2.put(fqn, key, value); 205 206 Assert.assertEquals(value, cache.get(fqn, key)); 208 Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key)); 209 Assert.assertEquals(value, loader1.get(fqn).get(key)); 210 Assert.assertEquals(value, loader2.get(fqn).get(key)); 211 212 mgr.begin(); 214 cache.put(fqn, key, value + 2); 215 Assert.assertEquals(value + 2, cache.get(fqn, key)); 216 Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key)); 217 Assert.assertEquals(value, loader1.get(fqn).get(key)); 218 Assert.assertEquals(value, loader2.get(fqn).get(key)); 219 mgr.commit(); 220 Assert.assertEquals(value + 2, cache.get(fqn, key)); 221 Assert.assertEquals(value + 2, chainingCacheLoader.get(fqn).get(key)); 222 Assert.assertEquals(value + 2, loader1.get(fqn).get(key)); 223 Assert.assertEquals(value, loader2.get(fqn).get(key)); 224 225 cache.remove(fqn); 228 229 Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key)); 235 Assert.assertNull(loader1.get(fqn)); 236 Assert.assertEquals(value, loader2.get(fqn).get(key)); 237 Assert.assertEquals(value, cache.get(fqn, key)); 238 239 cleanup(); 240 } 241 242 public void testCrudsTransactional() throws Exception 243 { 244 startCache(false, false); 245 TransactionManager mgr = cache.getTransactionManager(); 246 247 chainingCacheLoader.remove(cache.getRoot().getFqn()); 249 Assert.assertNull(loader1.get(fqn)); 250 Assert.assertNull(loader2.get(fqn)); 251 Assert.assertNull(chainingCacheLoader.get(fqn)); 252 253 mgr.begin(); 255 cache.put(fqn, key, value); 256 Assert.assertEquals(value, cache.get(fqn, key)); 257 Assert.assertNull(chainingCacheLoader.get(fqn)); 258 Assert.assertNull(loader1.get(fqn)); 259 Assert.assertNull(loader2.get(fqn)); 260 mgr.commit(); 261 Assert.assertEquals(value, cache.get(fqn, key)); 262 Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key)); 263 Assert.assertEquals(value, loader1.get(fqn).get(key)); 264 Assert.assertEquals(value, loader2.get(fqn).get(key)); 265 266 cache.evict(fqn); 268 Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key)); 269 Assert.assertEquals(value, loader1.get(fqn).get(key)); 270 Assert.assertEquals(value, loader2.get(fqn).get(key)); 271 Assert.assertEquals(value, cache.get(fqn, key)); 272 273 cache.remove(fqn); 276 Assert.assertNull(value, cache.get(fqn, key)); 282 Assert.assertNull(value, chainingCacheLoader.get(fqn)); 283 Assert.assertNull(value, loader1.get(fqn)); 284 Assert.assertNull(value, loader2.get(fqn)); 285 286 cleanup(); 287 } 288 289 public static Test suite() 290 { 291 return new TestSuite(ChainingCacheLoaderFullTest.class); 292 } 293 294 public static void main(String [] args) 295 { 296 junit.textui.TestRunner.run(suite()); 297 } 298 } 299 | Popular Tags |