1 7 package org.jboss.cache.optimistic; 8 9 import junit.framework.Assert; 10 import org.jboss.cache.CacheImpl; 11 import org.jboss.cache.loader.CacheLoader; 12 import org.jboss.cache.transaction.DummyTransactionManager; 13 14 import javax.transaction.Transaction ; 15 16 21 public class OptimisticWithCacheLoaderTest extends AbstractOptimisticTestCase 22 { 23 24 public OptimisticWithCacheLoaderTest(String s) 25 { 26 super(s); 27 } 28 29 30 public void testLoaderIndependently() throws Exception 31 { 32 CacheImpl cache = createCacheWithLoader(); 33 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader(); 34 35 loader.remove(fqn); 37 Assert.assertNull(loader.get(fqn)); 38 loader.put(fqn, key, value); 39 Assert.assertEquals(value, loader.get(fqn).get(key)); 40 loader.remove(fqn); 42 Assert.assertNull(loader.get(fqn)); 43 } 44 45 public void testCacheLoadOnTree() throws Exception 46 { 47 CacheLoader loader = null; 48 try 49 { 50 CacheImpl cache = createCacheWithLoader(); 51 loader = cache.getCacheLoaderManager().getCacheLoader(); 52 53 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 54 Transaction tx; 55 56 assertNull(cache.get(fqn)); 58 59 loader.put(fqn, key, value); 61 62 mgr.begin(); 64 tx = mgr.getTransaction(); 65 assertEquals(value, cache.get(fqn, key)); 66 mgr.suspend(); 67 68 mgr.begin(); 70 assertEquals(value, cache.get(fqn, key)); 71 mgr.commit(); 72 73 mgr.resume(tx); 74 assertEquals(value, cache.get(fqn, key)); 75 mgr.commit(); 76 } 77 finally 78 { 79 if (loader != null) loader.remove(fqn); 81 } 82 } 83 84 public void testCacheStoring() throws Exception 85 { 86 Transaction tx; 87 CacheImpl cache = createCacheWithLoader(); 88 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader(); 89 90 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 92 assertNull(mgr.getTransaction()); 93 mgr.begin(); 94 cache.put(fqn, key, value); 95 mgr.commit(); 96 97 Assert.assertEquals(value, cache.get(fqn, key)); 98 99 Assert.assertEquals(value, loader.get(fqn).get(key)); 101 102 103 mgr.begin(); 104 cache.remove(fqn); 105 mgr.commit(); 106 107 Assert.assertNull(cache.get(fqn, key)); 108 Assert.assertNull(loader.get(fqn)); 110 111 mgr.begin(); 112 cache.put(fqn, key, value); 113 tx = mgr.getTransaction(); 114 mgr.suspend(); 115 116 Assert.assertNull(cache.get(fqn, key)); 118 Assert.assertNull(loader.get(fqn)); 119 120 mgr.resume(tx); 121 mgr.commit(); 122 123 Assert.assertEquals(value, cache.get(fqn, key)); 125 Assert.assertEquals(value, loader.get(fqn).get(key)); 126 127 loader.remove(fqn); 129 } 130 131 public void testCacheLoading() throws Exception 132 { 133 CacheImpl cache = createCacheWithLoader(); 134 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader(); 135 136 Assert.assertNull(cache.get(fqn, key)); 137 138 loader.put(fqn, key, value); 140 Assert.assertEquals(value, loader.get(fqn).get(key)); 141 142 Assert.assertEquals(value, cache.get(fqn, key)); 144 145 loader.remove(fqn); 147 148 Assert.assertNotNull(cache.get(fqn, key)); 150 } 151 152 public void testCacheLoadingWithReplication() throws Exception 153 { 154 CacheImpl cache1 = createReplicatedCacheWithLoader(false); 155 CacheLoader loader1 = cache1.getCacheLoader(); 156 157 CacheImpl cache2 = createReplicatedCacheWithLoader(false); 158 CacheLoader loader2 = cache2.getCacheLoader(); 159 160 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 162 assertNull(mgr.getTransaction()); 163 164 mgr.begin(); 165 166 cache1.put(fqn, key, value); 168 Assert.assertEquals(value, cache1.get(fqn, key)); 169 170 Assert.assertNull(loader1.get(fqn)); 172 Assert.assertNull(loader2.get(fqn)); 173 Assert.assertNull(cache2.get(fqn, key)); 174 175 mgr.commit(); 177 178 Assert.assertEquals(value, cache1.get(fqn, key)); 180 Assert.assertEquals(value, loader1.get(fqn).get(key)); 181 Assert.assertEquals(value, loader2.get(fqn).get(key)); 182 Assert.assertEquals(value, cache2.get(fqn, key)); 183 184 mgr.begin(); 186 cache2.remove(fqn); 187 Assert.assertNull(cache2.get(fqn, key)); 188 Assert.assertEquals(value, cache1.get(fqn, key)); 190 Assert.assertEquals(value, loader1.get(fqn).get(key)); 191 Assert.assertEquals(value, loader2.get(fqn).get(key)); 192 193 mgr.commit(); 195 196 Assert.assertNull(cache1.get(fqn)); 198 Assert.assertNull(loader1.get(fqn)); 199 Assert.assertNull(loader2.get(fqn)); 200 Assert.assertNull(cache2.get(fqn)); 201 202 } 203 204 public void testSharedCacheLoadingWithReplication() throws Exception 205 { 206 CacheImpl cache1 = createReplicatedCacheWithLoader(true); 207 CacheLoader loader1 = cache1.getCacheLoader(); 208 209 CacheImpl cache2 = createReplicatedCacheWithLoader(true); 210 CacheLoader loader2 = cache2.getCacheLoader(); 211 212 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 214 assertNull(mgr.getTransaction()); 215 216 mgr.begin(); 217 218 cache1.put(fqn, key, value); 220 Assert.assertEquals(value, cache1.get(fqn, key)); 221 222 System.out.println("*** " + loader1.get(fqn)); 224 Assert.assertNull(loader1.get(fqn)); 225 Assert.assertNull(loader2.get(fqn)); 226 Assert.assertNull(cache2.get(fqn, key)); 227 228 mgr.commit(); 230 231 Assert.assertEquals(value, cache1.get(fqn, key)); 233 Assert.assertEquals(value, loader1.get(fqn).get(key)); 234 Assert.assertEquals(value, loader2.get(fqn).get(key)); 235 Assert.assertEquals(value, cache2.get(fqn, key)); 236 237 mgr.begin(); 239 cache2.remove(fqn); 240 Assert.assertNull(cache2.get(fqn, key)); 241 Assert.assertEquals(value, cache1.get(fqn, key)); 243 Assert.assertEquals(value, loader1.get(fqn).get(key)); 244 Assert.assertEquals(value, loader2.get(fqn).get(key)); 245 246 mgr.commit(); 248 249 Assert.assertNull(cache1.get(fqn)); 251 Assert.assertNull(loader1.get(fqn)); 252 Assert.assertNull(loader2.get(fqn)); 253 Assert.assertNull(cache2.get(fqn)); 254 255 } 256 } 257 | Popular Tags |