1 64 package com.jcorporate.expresso.core.cache; 65 66 import com.jcorporate.expresso.core.dbobj.ValidValue; 67 import com.jcorporate.expresso.services.test.ExpressoTestCase; 68 import com.jcorporate.expresso.services.test.TestSystemInitializer; 69 import junit.framework.Test; 70 import junit.framework.TestSuite; 71 import org.apache.log4j.Logger; 72 73 74 81 public class CacheTest extends ExpressoTestCase { 82 private static final Logger log = Logger.getLogger(CacheTest.class); 83 private static int NUM_ITERATIONS = 40; 84 85 88 private String cacheName = "testcache"; 89 90 96 public CacheTest(String name) throws Exception { 97 super(name); 98 99 } 100 101 102 108 public static void main(String [] args) throws Exception { 109 junit.textui.TestRunner.run(suite()); 111 } 112 113 119 public void setCacheName(String newName) { 120 cacheName = newName; 121 } 122 123 127 public void setUp() throws Exception { 128 try { 129 CacheManager.createCache(TestSystemInitializer.getTestContext(), cacheName, 130 false); 131 132 133 CacheManager.createCache(TestSystemInitializer.getTestContext(), 134 cacheName + "O", true); 135 } catch (java.util.ConcurrentModificationException cme) { 136 log.error("Error Setting Up", cme); 137 throw cme; 138 } 139 } 140 141 142 147 public int countTestCases() { 148 return 1; 149 } 150 151 152 157 public static Test suite() throws Exception { 158 TestSuite suite = new TestSuite("Cache Tests"); 159 160 for (int i = 0; i < NUM_ITERATIONS; i++) { 161 suite.addTestSuite(CacheTest.class); 162 } 163 164 return suite; 165 } 166 167 168 172 public void tearDown() throws Exception { 173 try { 174 CacheManager.clear(TestSystemInitializer.getTestContext(), cacheName); 175 CacheManager.clear(TestSystemInitializer.getTestContext(), cacheName + "O"); 176 } catch (java.util.ConcurrentModificationException cme) { 177 log.error("Error Tearing Down", cme); 178 throw cme; 179 } 180 } 181 182 183 187 public void testCreateCache() { 188 try { 189 ValidValue b = new ValidValue("b", "b"); 190 ValidValue c = new ValidValue("c", "c"); 191 192 if (CacheManager.existsCache(TestSystemInitializer.getTestContext(), cacheName)) { 193 if (log.isInfoEnabled()) { 194 log.debug("Clearing cache: " + cacheName); 195 } 196 197 CacheManager.clear(TestSystemInitializer.getTestContext(), cacheName); 198 } 199 200 CacheManager.addItem(TestSystemInitializer.getTestContext(), cacheName, b); 201 202 ValidValue retrieved = (ValidValue) CacheManager.getItem(TestSystemInitializer.getTestContext(), 203 cacheName, "b"); 204 205 if (retrieved == null) { 206 log.warn("'b' didn't exist in the test cache. This may or may" + 207 " not be a problem depending if we've removed caches or not"); 208 } else { 209 if (!retrieved.getDescription().equals("b")) { 210 fail("Unable to retrieve item 'b' from test cache"); 211 } else { 212 log.debug("Got item 'b' back from cache"); 213 } 214 } 215 } catch (CacheException ce) { 216 log.error(ce); 217 fail("CacheException occurred - see log"); 218 } catch (java.util.ConcurrentModificationException cme) { 219 cme.printStackTrace(); 220 log.error(cme); 221 fail("ConcurrantModificationException - see log"); 222 } 223 } 224 225 226 230 public void testCreateOrderedCache() { 231 try { 232 ValidValue b = new ValidValue("bO", "bO"); 233 ValidValue c = new ValidValue("cO", "cO"); 234 235 if (CacheManager.existsCache(TestSystemInitializer.getTestContext(), 236 cacheName + "O")) { 237 CacheManager.clear(TestSystemInitializer.getTestContext(), cacheName + 238 "O"); 239 } 240 241 CacheManager.addItem(TestSystemInitializer.getTestContext(), 242 cacheName + "O", b); 243 244 ValidValue retrieved = (ValidValue) CacheManager.getItem(TestSystemInitializer.getTestContext(), 245 cacheName + "O", "bO"); 246 247 assertTrue("Got null back for item 'bO' from ordered test cache", 248 retrieved != null); 249 assertTrue("Unable to retrieve item 'bO' from test cache", 250 retrieved.getDescription().equals("bO")); 251 } catch (CacheException ce) { 252 log.error(ce); 253 fail("CacheException occurred - see log"); 254 } catch (java.util.ConcurrentModificationException cme) { 255 log.error(cme); 256 fail("ConcurrantModificationException - see log"); 257 } 258 } 259 260 261 265 public void testMultipleItems() { 266 try { 267 ValidValue b = new ValidValue("b", "b"); 268 ValidValue c = new ValidValue("c", "c"); 269 CacheManager.addItem(TestSystemInitializer.getTestContext(), cacheName, b); 270 CacheManager.addItem(TestSystemInitializer.getTestContext(), cacheName, c); 271 272 ValidValue retrieved = (ValidValue) CacheManager.getItem(TestSystemInitializer.getTestContext(), 273 cacheName, "b"); 274 275 assertTrue("Got null back for item 'b' from ordered test cache", 276 retrieved != null); 277 assertTrue("Unable to retrieve item 'b' from test cache", 278 retrieved.getDescription().equals("b")); 279 280 281 retrieved = (ValidValue) CacheManager.getItem(TestSystemInitializer.getTestContext(), 282 cacheName, "c"); 283 assertTrue("Got null back for item 'b' from ordered test cache", 284 retrieved != null); 285 286 assertTrue("Unable to retrieve item 'c' from test cache", 287 retrieved.getDescription().equals("c")); 288 289 } catch (CacheException ce) { 290 log.error(ce); 291 fail("CacheException occurred - see log"); 292 } catch (java.util.ConcurrentModificationException cme) { 293 log.error(cme); 294 fail("ConcurrantModificationException - see log"); 295 } 296 } 297 298 299 303 public void testOrderedMultipleItems() { 304 try { 305 ValidValue b = new ValidValue("bO", "bO"); 306 ValidValue c = new ValidValue("cO", "cO"); 307 CacheManager.addItem(TestSystemInitializer.getTestContext(), cacheName + "O", 308 b); 309 CacheManager.addItem(TestSystemInitializer.getTestContext(), cacheName + "O", 310 c); 311 312 ValidValue retrieved = (ValidValue) CacheManager.getItem(TestSystemInitializer.getTestContext(), 313 cacheName + "O", "bO"); 314 315 assertTrue("Got null back for item 'b0' from ordered test cache", 316 retrieved != null); 317 assertTrue("Unable to retrieve item 'bO' from test cache", 318 retrieved.getDescription().equals("bO")); 319 320 321 retrieved = (ValidValue) CacheManager.getItem(TestSystemInitializer.getTestContext(), 322 cacheName + "O", "cO"); 323 324 assertTrue("Got null back for item 'c0' from ordered test cache", 325 retrieved != null); 326 327 assertTrue("Unable to retrieve item 'cO' from test cache", 328 retrieved.getDescription().equals("cO")); 329 } catch (CacheException ce) { 330 log.error(ce); 331 fail("CacheException occurred - see log"); 332 } catch (java.util.ConcurrentModificationException cme) { 333 log.error(cme); 334 fail("ConcurrantModificationException - see log"); 335 } 336 } 337 338 339 346 public void testRandomCaches() { 347 String [] aCacheNames = { 348 "random-testCache-1", "random-testCache-2", 349 "random-testCache-3" 350 }; 351 ValidValue[] a = { 352 new ValidValue("a", "a"), new ValidValue("b", "b"), 353 new ValidValue("c", "c") 354 }; 355 356 try { 357 for (int i = 0; i < aCacheNames.length; i++) { 358 CacheManager.createCache(TestSystemInitializer.getTestContext(), 359 aCacheNames[i], true); 360 361 for (int j = 0; j < a.length; j++) { 362 CacheManager.addItem(TestSystemInitializer.getTestContext(), 363 aCacheNames[i], a[j], 10000); 364 CacheManager.getItemCount(TestSystemInitializer.getTestContext(), 365 aCacheNames[i]); 366 CacheManager.getItem(TestSystemInitializer.getTestContext(), 367 aCacheNames[i], a[j].getKey()); 368 CacheManager.removeItem(TestSystemInitializer.getTestContext(), 369 aCacheNames[i], a[j]); 370 } 371 } 372 373 for (int i = 0; i < aCacheNames.length; i++) { 374 CacheManager.clear(TestSystemInitializer.getTestContext(), aCacheNames[i]); 375 } 376 377 for (int i = 0; i < aCacheNames.length; i++) { 378 CacheManager.createCache(TestSystemInitializer.getTestContext(), 379 aCacheNames[i], true); 380 381 for (int j = 0; j < a.length; j++) { 382 CacheManager.addItem(TestSystemInitializer.getTestContext(), 383 aCacheNames[i], a[j], 10000); 384 CacheManager.getItemCount(TestSystemInitializer.getTestContext(), 385 aCacheNames[i]); 386 CacheManager.getItem(TestSystemInitializer.getTestContext(), 387 aCacheNames[i], a[j].getKey()); 388 CacheManager.removeItem(TestSystemInitializer.getTestContext(), 389 aCacheNames[i], a[j]); 390 } 391 } 392 393 for (int i = 0; i < aCacheNames.length; i++) { 394 CacheManager.clear(TestSystemInitializer.getTestContext(), aCacheNames[i]); 395 } 396 } catch (java.util.ConcurrentModificationException ce) { 397 ce.printStackTrace(); 398 fail("Error during random tests: " + ce.getMessage()); 399 } catch (java.lang.NullPointerException npe) { 400 npe.printStackTrace(); 401 fail("Error during random tests." + npe.getMessage()); 402 } catch (Throwable t) { 403 t.printStackTrace(); 404 fail("Error during random tests." + t.getMessage()); 405 } 406 } 407 408 412 public void testRelatedCaches() { 413 try { 414 CacheSystem cs = CacheManager.getCacheSystem(TestSystemInitializer.getTestContext()); 415 416 417 418 419 cs.createCache("firstCache" + cacheName, false); 420 421 ValidValue a = new ValidValue("a", "a"); 422 cs.addItem("firstCache" + cacheName, a); 423 cs.createCache("secondCache" + cacheName, false); 424 425 ValidValue b = new ValidValue("b", "b"); 426 cs.addItem("secondCache" + cacheName, b); 427 428 ValidValue returnValue1 = (ValidValue) cs.getItem("secondCache" + 429 cacheName, 430 b.getKey()); 431 432 assertTrue("Item 'b' did not cache correctly in secondCache", returnValue1 != null); 433 434 cs.addListener("secondCache" + cacheName, "firstCache" + cacheName); 435 cs.removeItem("firstCache" + cacheName, a); 436 437 438 ValidValue returnValue = (ValidValue) cs.getItem("secondCache" + 439 cacheName, 440 b.getKey()); 441 442 assertTrue("Cache secondCache" + cacheName + 443 " should have been cleared when" + 444 "item was deleted from firstCache" + cacheName + 445 " - cache listener error", returnValue == null); 446 447 } catch (CacheException ce) { 448 log.error(ce); 449 fail("CacheException ocurred - see log"); 450 } catch (java.util.ConcurrentModificationException cme) { 451 log.error(cme); 452 fail("ConcurrantModificationException - see log"); 453 } 454 } 455 456 457 460 public void testRelatedCachesWithAddingUnmodifiedItems() { 461 try { 462 CacheSystem cs = CacheManager.getCacheSystem(TestSystemInitializer.getTestContext()); 463 464 465 466 467 468 cs.createCache("firstCache" + cacheName, false); 469 470 ValidValue a = new ValidValue("a", "a"); 471 cs.addItem("firstCache" + cacheName, a); 472 cs.createCache("secondCache" + cacheName, false); 473 474 ValidValue b = new ValidValue("b", "b"); 475 cs.addItem("secondCache" + cacheName, b); 476 477 ValidValue returnValue1 = (ValidValue) CacheManager.getItem(TestSystemInitializer.getTestContext(), 478 "secondCache" + 479 cacheName, 480 b.getKey()); 481 482 assertTrue("Item 'b' did not cache correctly in secondCache", returnValue1 != null); 483 484 cs.addListener("secondCache" + cacheName, "firstCache" + cacheName); 485 486 ValidValue c = new ValidValue("c", "c"); 487 ValidValue d = new ValidValue("d", "d"); 488 489 490 cs.put("firstCache" + cacheName, c); 492 493 ValidValue returnValue = (ValidValue) CacheManager.getItem(TestSystemInitializer.getTestContext(), 494 "secondCache" + 495 cacheName, 496 b.getKey()); 497 498 assertTrue("returnValue should not be null", returnValue != null); 499 500 cs.addItem("firstCache" + cacheName, d); 501 502 503 504 returnValue = (ValidValue) CacheManager.getItem(TestSystemInitializer.getTestContext(), 505 "secondCache" + 506 cacheName, 507 b.getKey()); 508 509 assertTrue("returnValue should now be null", returnValue == null); 510 511 } catch (CacheException ce) { 512 log.error(ce); 513 fail("CacheException ocurred - see log"); 514 } catch (java.util.ConcurrentModificationException cme) { 515 log.error(cme); 516 fail("ConcurrantModificationException - see log"); 517 } 518 } 519 520 524 public void testSimpleCaching() { 525 try { 526 ValidValue a = new ValidValue("a", "a"); 527 CacheManager.addItem(TestSystemInitializer.getTestContext(), cacheName, a); 528 529 ValidValue retrieved = (ValidValue) CacheManager.getItem(TestSystemInitializer.getTestContext(), 530 cacheName, "a"); 531 532 if (retrieved == null) { 533 fail("Got null back for item 'a' from test cache"); 534 } else { 535 if (!retrieved.getDescription().equals("a")) { 536 fail("Unable to retrieve item 'a' from test cache"); 537 } else { 538 log.debug("Got item 'a' back from cache"); 539 } 540 } 541 } catch (CacheException ce) { 542 log.error(ce); 543 fail("Cache exception occurred - see log"); 544 } catch (java.util.ConcurrentModificationException cme) { 545 log.error("Error testing simple caching", cme); 546 throw cme; 547 } 548 } 549 550 551 555 public void testSimpleOrderedCaching() { 556 try { 557 ValidValue a = new ValidValue("aO", "aO"); 558 CacheManager.addItem(TestSystemInitializer.getTestContext(), cacheName + "O", 559 a); 560 561 ValidValue retrieved = (ValidValue) CacheManager.getItem(TestSystemInitializer.getTestContext(), 562 cacheName + "O", "aO"); 563 564 if (retrieved == null) { 565 fail("Got null back for item 'aO' from ordered test cache"); 566 } else { 567 if (!retrieved.getDescription().equals("aO")) { 568 fail("Unable to retrieve item 'aO' from ordered test cache"); 569 } else { 570 log.debug("Got item 'aO' back from cache"); 571 } 572 } 573 } catch (CacheException ce) { 574 log.error(ce); 575 fail("Cache exception occurred - see log"); 576 } catch (java.util.ConcurrentModificationException cme) { 577 log.error(cme); 578 fail("ConcurrantModificationException - see log"); 579 } 580 } 581 582 } | Popular Tags |