1 package org.jboss.cache.factories; 2 3 import junit.framework.Assert; 4 import junit.framework.Test; 5 import junit.framework.TestSuite; 6 import org.jboss.cache.CacheImpl; 7 import org.jboss.cache.config.BuddyReplicationConfig; 8 import org.jboss.cache.config.CacheLoaderConfig; 9 import org.jboss.cache.config.EvictionConfig; 10 import org.jboss.cache.interceptors.*; 11 import org.jboss.cache.xml.XmlHelper; 12 import org.w3c.dom.Element ; 13 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 public class InterceptorChainFactoryTest extends InterceptorChainTestBase 18 { 19 CacheImpl cache = null; 20 21 protected void setUp() throws Exception 22 { 23 super.setUp(); 24 cache = new CacheImpl(); 25 cache.getConfiguration().setCacheMode("LOCAL"); 26 } 27 28 protected void tearDown() throws Exception 29 { 30 super.tearDown(); 31 if (cache != null) 32 { 33 cache.stop(); 34 cache.destroy(); 35 } 36 } 37 38 39 public void testBareConfig() throws Exception 40 { 41 cache.getConfiguration().setExposeManagementStatistics(false); 42 Interceptor chain = new InterceptorChainFactory().buildInterceptorChain(cache); 43 List <Interceptor> list = InterceptorChainFactory.asList(chain); 44 Iterator <Interceptor> interceptors = list.iterator(); 45 46 System.out.println("testBareConfig interceptors are:\n" + list); 47 assertNotNull(list); 48 assertEquals(6, list.size()); 49 50 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 51 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 52 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 53 assertEquals(UnlockInterceptor.class, interceptors.next().getClass()); 54 assertEquals(PessimisticLockInterceptor.class, interceptors.next().getClass()); 55 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 56 57 assertInterceptorLinkage(list); 58 } 59 60 61 public void testTxConfig() throws Exception 62 { 63 cache.getConfiguration().setExposeManagementStatistics(false); 64 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 65 66 Interceptor chain = new InterceptorChainFactory().buildInterceptorChain(cache); 67 List <Interceptor> list = InterceptorChainFactory.asList(chain); 68 Iterator <Interceptor> interceptors = list.iterator(); 69 70 System.out.println("testTxConfig interceptors are:\n" + list); 71 assertNotNull(list); 72 assertEquals(6, list.size()); 73 74 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 75 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 76 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 77 assertEquals(UnlockInterceptor.class, interceptors.next().getClass()); 78 assertEquals(PessimisticLockInterceptor.class, interceptors.next().getClass()); 79 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 80 81 assertInterceptorLinkage(list); 82 } 83 84 protected CacheLoaderConfig getCacheLoaderConfig(boolean pasv, boolean fetchPersistentState) throws Exception 85 { 86 String xml = " <config>\n" + 87 " \n" + 88 " <passivation>" + pasv + "</passivation>\n" + 89 " <preload></preload>\n" + 90 "\n" + 91 " <cacheloader>\n" + 92 " <class>org.jboss.cache.loader.FileCacheLoader</class>\n" + 93 " <properties>\n" + 94 " location=/tmp\n" + 95 " </properties>\n" + 96 " <async>false</async>\n" + 97 " <fetchPersistentState>" + fetchPersistentState + "</fetchPersistentState>\n" + 98 " <ignoreModifications>false</ignoreModifications>\n" + 99 " </cacheloader>\n" + 100 " \n" + 101 " </config>"; 102 Element element = XmlHelper.stringToElement(xml); 103 return XmlConfigurationParser.parseCacheLoaderConfig(element); 104 } 105 106 public void testSharedCacheLoaderConfig() throws Exception 107 { 108 cache.getConfiguration().setExposeManagementStatistics(false); 109 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 110 cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig(false, false)); 111 cache.getConfiguration().setCacheMode("REPL_ASYNC"); 112 cache.getConfiguration().setFetchInMemoryState(false); 113 cache.create(); 114 Interceptor chain = new InterceptorChainFactory().buildInterceptorChain(cache); 115 List <Interceptor> list = InterceptorChainFactory.asList(chain); 116 Iterator <Interceptor> interceptors = list.iterator(); 117 118 System.out.println("testSharedCacheLoaderConfig interceptors are:\n" + list); 119 assertNotNull(list); 120 121 assertEquals(9, list.size()); 122 123 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 124 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 125 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 126 assertEquals(CacheStoreInterceptor.class, interceptors.next().getClass()); 127 assertEquals(ReplicationInterceptor.class, interceptors.next().getClass()); 128 assertEquals(UnlockInterceptor.class, interceptors.next().getClass()); 129 assertEquals(CacheLoaderInterceptor.class, interceptors.next().getClass()); 130 assertEquals(PessimisticLockInterceptor.class, interceptors.next().getClass()); 131 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 132 133 assertInterceptorLinkage(list); 134 } 135 136 public void testUnsharedCacheLoaderConfig() throws Exception 137 { 138 cache.getConfiguration().setExposeManagementStatistics(false); 139 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 140 cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig(false, true)); 141 cache.getConfiguration().setCacheMode("REPL_ASYNC"); 142 cache.getConfiguration().setFetchInMemoryState(false); 143 cache.create(); 144 Interceptor chain = new InterceptorChainFactory().buildInterceptorChain(cache); 145 List <Interceptor> list = InterceptorChainFactory.asList(chain); 146 Iterator <Interceptor> interceptors = list.iterator(); 147 148 System.out.println("testUnsharedCacheLoaderConfig interceptors are:\n" + list); 149 assertNotNull(list); 150 151 assertEquals(9, list.size()); 152 153 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 154 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 155 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 156 assertEquals(ReplicationInterceptor.class, interceptors.next().getClass()); 157 assertEquals(UnlockInterceptor.class, interceptors.next().getClass()); 158 assertEquals(CacheLoaderInterceptor.class, interceptors.next().getClass()); 159 assertEquals(CacheStoreInterceptor.class, interceptors.next().getClass()); 160 assertEquals(PessimisticLockInterceptor.class, interceptors.next().getClass()); 161 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 162 163 assertInterceptorLinkage(list); 164 } 165 166 public void testTxAndRepl() throws Exception 167 { 168 cache.getConfiguration().setExposeManagementStatistics(false); 169 cache.getConfiguration().setCacheMode("repl_sync"); 170 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 171 Interceptor chain = new InterceptorChainFactory().buildInterceptorChain(cache); 172 List <Interceptor> list = InterceptorChainFactory.asList(chain); 173 Iterator <Interceptor> interceptors = list.iterator(); 174 175 System.out.println("testTxAndRepl interceptors are:\n" + list); 176 assertNotNull(list); 177 178 assertEquals(7, list.size()); 179 180 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 181 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 182 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 183 assertEquals(ReplicationInterceptor.class, interceptors.next().getClass()); 184 assertEquals(UnlockInterceptor.class, interceptors.next().getClass()); 185 assertEquals(PessimisticLockInterceptor.class, interceptors.next().getClass()); 186 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 187 188 assertInterceptorLinkage(list); 189 } 190 191 192 public void testOptimisticChain() throws Exception 193 { 194 CacheImpl cache = new CacheImpl(); 195 cache.getConfiguration().setExposeManagementStatistics(false); 196 cache.getConfiguration().setNodeLockingOptimistic(true); 197 198 Interceptor next = new InterceptorChainFactory().buildInterceptorChain(cache); 199 200 List <Interceptor> list = InterceptorChainFactory.asList(next); 202 Iterator <Interceptor> interceptors = list.iterator(); 203 204 Assert.assertEquals(8, list.size()); 205 206 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 207 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 208 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 209 assertEquals(OptimisticLockingInterceptor.class, interceptors.next().getClass()); 210 assertEquals(OptimisticValidatorInterceptor.class, interceptors.next().getClass()); 211 assertEquals(OptimisticCreateIfNotExistsInterceptor.class, interceptors.next().getClass()); 212 assertEquals(OptimisticNodeInterceptor.class, interceptors.next().getClass()); 213 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 214 215 assertInterceptorLinkage(list); 216 } 217 218 public void testOptimisticReplicatedChain() throws Exception 219 { 220 CacheImpl cache = new CacheImpl(); 221 cache.getConfiguration().setExposeManagementStatistics(false); 222 cache.getConfiguration().setNodeLockingOptimistic(true); 223 cache.getConfiguration().setCacheMode("REPL_SYNC"); 224 225 Interceptor next = new InterceptorChainFactory().buildInterceptorChain(cache); 226 227 List <Interceptor> list = InterceptorChainFactory.asList(next); 229 Iterator <Interceptor> interceptors = list.iterator(); 230 231 Assert.assertEquals(9, list.size()); 232 233 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 234 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 235 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 236 assertEquals(OptimisticReplicationInterceptor.class, interceptors.next().getClass()); 237 assertEquals(OptimisticLockingInterceptor.class, interceptors.next().getClass()); 238 assertEquals(OptimisticValidatorInterceptor.class, interceptors.next().getClass()); 239 assertEquals(OptimisticCreateIfNotExistsInterceptor.class, interceptors.next().getClass()); 240 assertEquals(OptimisticNodeInterceptor.class, interceptors.next().getClass()); 241 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 242 243 assertInterceptorLinkage(list); 244 } 245 246 public void testOptimisticCacheLoaderChain() throws Exception 247 { 248 CacheImpl cache = new CacheImpl(); 249 cache.getConfiguration().setExposeManagementStatistics(false); 250 cache.getConfiguration().setNodeLockingOptimistic(true); 251 cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig(false, false)); 252 cache.create(); 253 Interceptor next = new InterceptorChainFactory().buildInterceptorChain(cache); 254 255 List <Interceptor> list = InterceptorChainFactory.asList(next); 257 Iterator <Interceptor> interceptors = list.iterator(); 258 259 Assert.assertEquals(10, list.size()); 260 261 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 262 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 263 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 264 assertEquals(CacheStoreInterceptor.class, interceptors.next().getClass()); 265 assertEquals(CacheLoaderInterceptor.class, interceptors.next().getClass()); 266 assertEquals(OptimisticLockingInterceptor.class, interceptors.next().getClass()); 267 assertEquals(OptimisticValidatorInterceptor.class, interceptors.next().getClass()); 268 assertEquals(OptimisticCreateIfNotExistsInterceptor.class, interceptors.next().getClass()); 269 assertEquals(OptimisticNodeInterceptor.class, interceptors.next().getClass()); 270 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 271 272 assertInterceptorLinkage(list); 273 } 274 275 public void testOptimisticPassivationCacheLoaderChain() throws Exception 276 { 277 CacheImpl cache = new CacheImpl(); 278 cache.getConfiguration().setExposeManagementStatistics(false); 279 cache.getConfiguration().setNodeLockingOptimistic(true); 280 cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig(true, false)); 281 cache.create(); 282 Interceptor next = new InterceptorChainFactory().buildInterceptorChain(cache); 283 284 List <Interceptor> list = InterceptorChainFactory.asList(next); 286 Iterator <Interceptor> interceptors = list.iterator(); 287 288 Assert.assertEquals(10, list.size()); 289 290 291 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 292 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 293 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 294 assertEquals(PassivationInterceptor.class, interceptors.next().getClass()); 295 assertEquals(ActivationInterceptor.class, interceptors.next().getClass()); 296 assertEquals(OptimisticLockingInterceptor.class, interceptors.next().getClass()); 297 assertEquals(OptimisticValidatorInterceptor.class, interceptors.next().getClass()); 298 assertEquals(OptimisticCreateIfNotExistsInterceptor.class, interceptors.next().getClass()); 299 assertEquals(OptimisticNodeInterceptor.class, interceptors.next().getClass()); 300 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 301 302 assertInterceptorLinkage(list); 303 } 304 305 public void testInvalidationInterceptorChain() throws Exception 306 { 307 CacheImpl cache = new CacheImpl(); 308 cache.getConfiguration().setExposeManagementStatistics(false); 309 cache.getConfiguration().setCacheMode("REPL_ASYNC"); 310 311 Interceptor chain = new InterceptorChainFactory().buildInterceptorChain(cache); 312 313 List <Interceptor> list = InterceptorChainFactory.asList(chain); 315 Iterator <Interceptor> interceptors = list.iterator(); 316 317 Assert.assertEquals(7, list.size()); 318 319 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 320 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 321 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 322 assertEquals(ReplicationInterceptor.class, interceptors.next().getClass()); 323 assertEquals(UnlockInterceptor.class, interceptors.next().getClass()); 324 assertEquals(PessimisticLockInterceptor.class, interceptors.next().getClass()); 325 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 326 327 329 cache = new CacheImpl(); 331 cache.getConfiguration().setExposeManagementStatistics(false); 332 cache.getConfiguration().setCacheMode("INVALIDATION_ASYNC"); 333 chain = new InterceptorChainFactory().buildInterceptorChain(cache); 334 335 list = InterceptorChainFactory.asList(chain); 337 interceptors = list.iterator(); 338 339 Assert.assertEquals(7, list.size()); 340 341 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 342 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 343 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 344 assertEquals(InvalidationInterceptor.class, interceptors.next().getClass()); 345 assertEquals(UnlockInterceptor.class, interceptors.next().getClass()); 346 assertEquals(PessimisticLockInterceptor.class, interceptors.next().getClass()); 347 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 348 349 assertInterceptorLinkage(list); 350 } 351 352 public void testCacheMgmtConfig() throws Exception 353 { 354 cache.getConfiguration().setExposeManagementStatistics(true); 355 Interceptor chain = new InterceptorChainFactory().buildInterceptorChain(cache); 356 List <Interceptor> list = InterceptorChainFactory.asList(chain); 357 Iterator <Interceptor> interceptors = list.iterator(); 358 359 System.out.println("testCacheMgmtConfig interceptors are:\n" + list); 360 assertNotNull(list); 361 assertEquals(7, list.size()); 362 363 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 364 assertEquals(CacheMgmtInterceptor.class, interceptors.next().getClass()); 365 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 366 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 367 assertEquals(UnlockInterceptor.class, interceptors.next().getClass()); 368 assertEquals(PessimisticLockInterceptor.class, interceptors.next().getClass()); 369 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 370 371 assertInterceptorLinkage(list); 372 373 } 374 375 public void testEvictionInterceptorConfig() throws Exception 376 { 377 cache.getConfiguration().setEvictionConfig(new EvictionConfig() 378 { 379 public boolean isValidConfig() 380 { 381 return true; 382 } 383 } 384 ); 385 Interceptor chain = new InterceptorChainFactory().buildInterceptorChain(cache); 386 List <Interceptor> list = InterceptorChainFactory.asList(chain); 387 Iterator <Interceptor> interceptors = list.iterator(); 388 389 System.out.println("testEvictionInterceptorConfig interceptors are:\n" + list); 390 assertNotNull(list); 391 assertEquals(8, list.size()); 392 393 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 394 assertEquals(CacheMgmtInterceptor.class, interceptors.next().getClass()); 395 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 396 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 397 assertEquals(UnlockInterceptor.class, interceptors.next().getClass()); 398 assertEquals(PessimisticLockInterceptor.class, interceptors.next().getClass()); 399 assertEquals(EvictionInterceptor.class, interceptors.next().getClass()); 400 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 401 402 assertInterceptorLinkage(list); 403 } 404 405 public void testBuddyReplicationOptLocking() throws Exception 406 { 407 String xmlString = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled>\n" + 408 "<buddyCommunicationTimeout>600000</buddyCommunicationTimeout>\n" + 409 " <buddyLocatorClass>org.jboss.cache.buddyreplication.NextMemberBuddyLocator</buddyLocatorClass>\n" + 410 " <buddyLocatorProperties>numBuddies = 1</buddyLocatorProperties>\n"; 411 412 xmlString += "<buddyPoolName>buddyPoolName</buddyPoolName>"; 413 xmlString += "</config>"; 414 Element element = XmlHelper.stringToElement(xmlString); 415 BuddyReplicationConfig brc = XmlConfigurationParser.parseBuddyReplicationConfig(element); 416 cache.getConfiguration().setBuddyReplicationConfig(brc); 417 418 cache.getConfiguration().setCacheMode("REPL_SYNC"); 419 cache.getConfiguration().setNodeLockingScheme("OPTIMISTIC"); 420 cache.create(); Interceptor chain = new InterceptorChainFactory().buildInterceptorChain(cache); 422 423 List <Interceptor> list = InterceptorChainFactory.asList(chain); 424 Iterator <Interceptor> interceptors = list.iterator(); 425 426 System.out.println("testEvictionInterceptorConfig interceptors are:\n" + list); 427 assertNotNull(list); 428 assertEquals(11, list.size()); 429 430 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 431 assertEquals(CacheMgmtInterceptor.class, interceptors.next().getClass()); 432 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 433 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 434 assertEquals(OptimisticReplicationInterceptor.class, interceptors.next().getClass()); 435 assertEquals(DataGravitatorInterceptor.class, interceptors.next().getClass()); 436 assertEquals(OptimisticLockingInterceptor.class, interceptors.next().getClass()); 437 assertEquals(OptimisticValidatorInterceptor.class, interceptors.next().getClass()); 438 assertEquals(OptimisticCreateIfNotExistsInterceptor.class, interceptors.next().getClass()); 439 assertEquals(OptimisticNodeInterceptor.class, interceptors.next().getClass()); 440 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 441 442 assertInterceptorLinkage(list); 443 } 444 445 public void testBuddyReplicationPessLocking() throws Exception 446 { 447 String xmlString = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled>\n" + 448 "<buddyCommunicationTimeout>600000</buddyCommunicationTimeout>\n" + 449 " <buddyLocatorClass>org.jboss.cache.buddyreplication.NextMemberBuddyLocator</buddyLocatorClass>\n" + 450 " <buddyLocatorProperties>numBuddies = 1</buddyLocatorProperties>\n"; 451 452 xmlString += "<buddyPoolName>buddyPoolName</buddyPoolName>"; 453 xmlString += "</config>"; 454 Element element = XmlHelper.stringToElement(xmlString); 455 BuddyReplicationConfig brc = XmlConfigurationParser.parseBuddyReplicationConfig(element); 456 cache.getConfiguration().setBuddyReplicationConfig(brc); 457 458 cache.getConfiguration().setCacheMode("REPL_SYNC"); 459 cache.create(); Interceptor chain = new InterceptorChainFactory().buildInterceptorChain(cache); 461 462 List <Interceptor> list = InterceptorChainFactory.asList(chain); 463 Iterator <Interceptor> interceptors = list.iterator(); 464 465 System.out.println("testEvictionInterceptorConfig interceptors are:\n" + list); 466 assertNotNull(list); 467 assertEquals(9, list.size()); 468 469 assertEquals(InvocationContextInterceptor.class, interceptors.next().getClass()); 470 assertEquals(CacheMgmtInterceptor.class, interceptors.next().getClass()); 471 assertEquals(TxInterceptor.class, interceptors.next().getClass()); 472 assertEquals(NotificationInterceptor.class, interceptors.next().getClass()); 473 assertEquals(ReplicationInterceptor.class, interceptors.next().getClass()); 474 assertEquals(UnlockInterceptor.class, interceptors.next().getClass()); 475 assertEquals(DataGravitatorInterceptor.class, interceptors.next().getClass()); 476 assertEquals(PessimisticLockInterceptor.class, interceptors.next().getClass()); 477 assertEquals(CallInterceptor.class, interceptors.next().getClass()); 478 479 assertInterceptorLinkage(list); 480 } 481 482 483 public static Test suite() 484 { 485 return new TestSuite(InterceptorChainFactoryTest.class); 486 } 487 488 public static void main(String [] args) 489 { 490 junit.textui.TestRunner.run(suite()); 491 } 492 493 } 494 | Popular Tags |