1 7 package org.jboss.cache.loader; 8 9 import junit.framework.Assert; 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.jboss.cache.CacheImpl; 13 import org.jboss.cache.Fqn; 14 import org.jboss.cache.config.Configuration; 15 16 import java.util.Map ; 17 import java.util.Set ; 18 19 24 public class ClusteredCacheLoaderTest extends AbstractCacheLoaderTestBase 25 { 26 private static Log log = LogFactory.getLog(ClusteredCacheLoaderTest.class); 27 private CacheImpl cache1, cache2; 28 private CacheLoader loader1, loader2; 29 private Fqn fqn = Fqn.fromString("/a"); 30 private String key = "key"; 31 32 protected void setUp() throws Exception 33 { 34 if (cache1 != null || cache2 != null) tearDown(); 35 cache1 = new CacheImpl(); 36 cache2 = new CacheImpl(); 37 38 cache1.getConfiguration().setClusterName("CCL-Test"); 39 cache1.getConfiguration().setInitialStateRetrievalTimeout(2000); 40 cache2.getConfiguration().setClusterName("CCL-Test"); 41 cache2.getConfiguration().setInitialStateRetrievalTimeout(2000); 42 cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 43 cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 44 45 cache1.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.ClusteredCacheLoader", "timeout=500", false, false, false)); 46 cache2.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.ClusteredCacheLoader", "timeout=500", false, false, false)); 47 48 cache1.start(); 49 cache2.start(); 50 51 loader1 = cache1.getCacheLoaderManager().getCacheLoader(); 52 loader2 = cache2.getCacheLoaderManager().getCacheLoader(); 53 } 54 55 protected void tearDown() 56 { 57 if (cache1 != null) 58 { 59 cache1.stop(); 60 cache1 = null; 61 loader1 = null; 62 } 63 64 if (cache2 != null) 65 { 66 cache2.stop(); 67 cache2 = null; 68 loader2 = null; 69 } 70 } 71 72 public void testGetKeyValue() throws Exception 73 { 74 cache1.put(fqn, key, "value"); 75 76 log.info("Finished put"); 77 Assert.assertEquals("value", loader1.get(fqn).get(key)); 79 Assert.assertEquals("value", loader2.get(fqn).get(key)); 80 81 cache1.evict(fqn); 82 83 Assert.assertEquals("value", loader1.get(fqn).get(key)); 86 Assert.assertNull("Expecting null", loader2.get(fqn)); 87 Assert.assertEquals("value", cache1.get(fqn, key)); 89 } 92 93 public void testGet() throws Exception 94 { 95 cache1.put(fqn, key, "value"); 96 97 Map map = loader1.get(fqn); 99 Assert.assertTrue("Should contain key", map.containsKey(key)); 100 Assert.assertEquals("value", map.get(key)); 101 Assert.assertEquals(1, map.size()); 102 103 map = loader2.get(fqn); 104 Assert.assertTrue("Should contain key", map.containsKey(key)); 105 Assert.assertEquals("value", map.get(key)); 106 Assert.assertEquals(1, map.size()); 107 108 cache1.evict(fqn); 109 110 map = loader1.get(fqn); 113 Assert.assertTrue(map.containsKey(key)); 114 Assert.assertEquals("value", map.get(key)); 115 Assert.assertEquals(1, map.size()); 116 117 Assert.assertNull("Expecting null", loader2.get(fqn)); 118 map = loader2.get(fqn); 119 Assert.assertNull("Should be null", map); 120 121 Assert.assertEquals("value", cache1.get(fqn, key)); 123 map = loader2.get(fqn); 125 Assert.assertTrue(map.containsKey(key)); 126 Assert.assertEquals("value", map.get(key)); 127 Assert.assertEquals(1, map.size()); 128 } 129 130 public void testGetChildrenNames() throws Exception 131 { 132 cache1.put(fqn, key, "value"); 133 Fqn child1 = new Fqn(fqn, "child1"); 134 Fqn child2 = new Fqn(fqn, "child2"); 135 Fqn child3 = new Fqn(fqn, "child3"); 136 cache1.put(child1, key, "value"); 137 cache1.put(child2, key, "value"); 138 cache1.put(child3, key, "value"); 139 140 Set childNames = loader1.getChildrenNames(fqn); 142 Assert.assertEquals(3, childNames.size()); 143 childNames = loader2.getChildrenNames(fqn); 144 Assert.assertEquals(3, childNames.size()); 145 146 cache1.evict(child1); 147 cache1.evict(child2); 148 cache1.evict(child3); 149 cache1.evict(fqn); 150 151 childNames = loader1.getChildrenNames(fqn); 154 Assert.assertEquals(3, childNames.size()); 155 156 childNames = loader2.getChildrenNames(fqn); 157 Assert.assertNull("should be null", childNames); 158 Assert.assertEquals("value", cache1.get(fqn, key)); 160 Assert.assertEquals("value", cache1.get(child1, key)); 162 Assert.assertEquals("value", cache1.get(child2, key)); 163 Assert.assertEquals("value", cache1.get(child3, key)); 164 childNames = loader2.getChildrenNames(fqn); 166 Assert.assertEquals(3, childNames.size()); 167 } 168 169 public void testExists() throws Exception 170 { 171 cache1.put(fqn, key, "value"); 172 173 Assert.assertTrue("should exist", loader1.exists(fqn)); 175 Assert.assertTrue("should exist", loader2.exists(fqn)); 176 177 cache1.evict(fqn); 178 179 Assert.assertTrue("should exist", loader1.exists(fqn)); 182 Assert.assertTrue("should not exist", !loader2.exists(fqn)); 183 Assert.assertEquals("value", cache1.get(fqn, key)); 185 Assert.assertTrue("should exist", loader2.exists(fqn)); 187 } 188 } 189 | Popular Tags |