1 package org.jboss.cache.api; 2 3 import junit.framework.TestCase; 4 import org.jboss.cache.AbstractCacheListener; 5 import org.jboss.cache.Cache; 6 import org.jboss.cache.CacheImpl; 7 import org.jboss.cache.CacheListener; 8 import org.jboss.cache.DummyTransactionManagerLookup; 9 import org.jboss.cache.Fqn; 10 import org.jboss.cache.Node; 11 import org.jboss.cache.Region; 12 import org.jboss.cache.config.Configuration; 13 import org.jboss.cache.config.ConfigurationException; 14 import org.jboss.cache.factories.DefaultCacheFactory; 15 16 import java.util.ArrayList ; 17 import java.util.HashMap ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 26 public class CacheAPITest extends TestCase 27 { 28 private Cache cache; 29 protected boolean optimistic; 30 31 protected void setUp() throws Exception 32 { 33 cache = DefaultCacheFactory.getInstance().createCache("META-INF/local-tx-service.xml", false); 35 cache.getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC); 36 cache.start(); 37 } 38 39 protected void tearDown() 40 { 41 if (cache != null) cache.stop(); 42 } 43 44 45 48 public void testConfiguration() 49 { 50 Configuration c = cache.getConfiguration(); 51 assertEquals(Configuration.CacheMode.LOCAL, c.getCacheMode()); 52 assertEquals(DummyTransactionManagerLookup.class.getName(), c.getTransactionManagerLookupClass()); 53 54 try 56 { 57 c.setCacheMode(Configuration.CacheMode.REPL_SYNC); 58 fail("Should have thrown an Exception"); 59 } 60 catch (ConfigurationException e) 61 { 62 } 64 65 c.setLockAcquisitionTimeout(100); 67 } 68 69 72 public void testGetRoot() 73 { 74 } 77 78 79 84 public void testCacheListeners() 85 { 86 assertEquals(0, cache.getCacheListeners().size()); 87 88 89 final List events = new ArrayList (); 90 91 CacheListener dummy = new AbstractCacheListener() 92 { 93 public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal) 94 { 95 if (pre) events.add("Created"); 96 } 97 }; 98 99 cache.addCacheListener(dummy); 100 101 assertEquals(1, cache.getCacheListeners().size()); 102 103 cache.getRoot().addChild(Fqn.fromString("/blah")); 104 105 107 assertEquals(1, events.size()); 109 110 cache.removeCacheListener(dummy); 111 112 assertEquals(0, cache.getCacheListeners().size()); 113 } 114 115 119 public void testFqnBasedCacheListeners() 120 { 121 try 122 { 123 cache.getCacheListeners(Fqn.ROOT); 124 fail("Fqn-based cache listener operation should throw an exception"); 125 } 126 catch (Exception e) 127 { 128 } 130 131 try 132 { 133 cache.addCacheListener(Fqn.ROOT, new AbstractCacheListener() 134 { 135 }); 136 fail("Fqn-based cache listener operation should throw an exception"); 137 } 138 catch (Exception e) 139 { 140 } 142 143 try 144 { 145 cache.removeCacheListener(Fqn.ROOT, new AbstractCacheListener() 146 { 147 }); 148 fail("Fqn-based cache listener operation should throw an exception"); 149 } 150 catch (Exception e) 151 { 152 } 154 } 155 156 163 public void testConvenienceMethods() 164 { 165 Fqn fqn = Fqn.fromString("/test/fqn"); 166 Object key = "key", value = "value"; 167 Map data = new HashMap (); 168 data.put(key, value); 169 170 assertNull(cache.get(fqn, key)); 171 172 cache.put(fqn, key, value); 173 174 assertEquals(value, cache.get(fqn, key)); 175 176 cache.remove(fqn, key); 177 178 assertNull(cache.get(fqn, key)); 179 180 cache.put(fqn, data); 181 182 System.out.println(((CacheImpl) cache).printDetails()); 183 184 assertEquals(value, cache.get(fqn, key)); 185 } 186 187 188 191 public void nodeConvenienceNodeRemoval() 192 { 193 Fqn fqn = Fqn.fromString("/test/fqn"); 195 cache.getRoot().addChild(fqn); 196 assertTrue(cache.getRoot().hasChild(fqn)); 197 198 cache.removeNode(fqn); 199 assertFalse(cache.getRoot().hasChild(fqn)); 200 } 201 202 205 public void testEvict() 206 { 207 Fqn one = Fqn.fromString("/one"); 208 Fqn two = Fqn.fromString("/one/two"); 209 Object key = "key", value = "value"; 210 211 cache.getRoot().addChild(one).put(key, value); 212 cache.getRoot().addChild(two).put(key, value); 213 214 assertTrue(cache.getRoot().hasChild(one)); 215 assertFalse(cache.getRoot().getChild(one).getData().isEmpty()); 216 assertTrue(cache.getRoot().hasChild(two)); 217 assertFalse(cache.getRoot().getChild(two).getData().isEmpty()); 218 219 cache.evict(two, false); 221 222 assertTrue(cache.getRoot().hasChild(one)); 223 assertTrue(cache.getRoot().getChild(one).getKeys().contains(key)); 224 assertFalse(cache.getRoot().hasChild(two)); 225 226 cache.getRoot().addChild(two).put(key, value); 228 229 cache.evict(one, false); 231 232 assertTrue(cache.getRoot().hasChild(one)); 234 assertFalse(cache.getRoot().getChild(one).getKeys().contains(key)); 235 236 assertTrue(cache.getRoot().hasChild(two)); 238 assertTrue(cache.getRoot().getChild(two).getKeys().contains(key)); 239 } 240 241 242 245 public void testEvictRecursive() 246 { 247 Fqn one = Fqn.fromString("/one"); 248 Fqn two = Fqn.fromString("/one/two"); 249 Object key = "key", value = "value"; 250 251 cache.getRoot().addChild(one).put(key, value); 252 cache.getRoot().addChild(two).put(key, value); 253 254 assertTrue(cache.getRoot().hasChild(one)); 255 assertFalse(cache.getRoot().getChild(one).getData().isEmpty()); 256 assertTrue(cache.getRoot().hasChild(two)); 257 assertFalse(cache.getRoot().getChild(two).getData().isEmpty()); 258 259 cache.evict(two, true); 261 262 assertTrue(cache.getRoot().hasChild(one)); 263 assertFalse(cache.getRoot().getChild(one).getData().isEmpty()); 264 assertFalse(cache.getRoot().hasChild(two)); 265 266 cache.getRoot().addChild(two).put(key, value); 268 269 cache.evict(one, true); 271 272 assertFalse(cache.getRoot().hasChild(one)); 273 assertFalse(cache.getRoot().hasChild(two)); 274 } 275 276 277 280 public void testRegion() 281 { 282 Region rootRegion = cache.getRegion(Fqn.ROOT, true); 283 assertNotNull(rootRegion); assertSame(rootRegion, cache.getRegion(Fqn.ROOT, true)); 285 286 Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"), true); 287 assertNotNull(otherRegion); 288 assertSame(otherRegion, cache.getRegion(Fqn.fromString("/other/region"), true)); 289 } 290 291 294 public void testParentRegion1() 295 { 296 Region rootRegion = cache.getRegion(Fqn.ROOT, true); 297 assertNotNull(rootRegion); assertSame(rootRegion, cache.getRegion(Fqn.ROOT, false)); 299 300 Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"), false); 301 303 assertSame(otherRegion, rootRegion); 304 } 305 306 309 public void testParentRegion2() 310 { 311 Region rootRegion = cache.getRegion(Fqn.ROOT, true); 312 Region parentRegion = cache.getRegion(Fqn.fromString("/parent"), true); 313 assertNotSame("parentRegion should be a new region in its own right", rootRegion, parentRegion); 314 315 Region childRegion = cache.getRegion(Fqn.fromString("/parent/region"), false); 316 assertSame("Expecting the same region as parentRegion", childRegion, parentRegion); 317 } 318 319 320 323 public void testNullRegion() 324 { 325 Region myRegion = cache.getRegion(Fqn.fromString("/myregion"), true); 326 assertNotNull(myRegion); assertSame(myRegion, cache.getRegion(Fqn.fromString("/myregion"), false)); 328 329 Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"), false); 330 assertNull(otherRegion); 332 } 333 334 } 335 | Popular Tags |