1 package org.apache.ojb.broker.cache; 2 3 import java.io.Serializable ; 4 import java.util.Properties ; 5 6 import org.apache.ojb.broker.Article; 7 import org.apache.ojb.broker.Identity; 8 import org.apache.ojb.broker.InterfaceArticle; 9 import org.apache.ojb.broker.PBKey; 10 import org.apache.ojb.broker.PersistenceBroker; 11 import org.apache.ojb.broker.PersistenceBrokerFactory; 12 import org.apache.ojb.broker.metadata.ConnectionRepository; 13 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; 14 import org.apache.ojb.broker.metadata.MetadataManager; 15 import org.apache.ojb.broker.metadata.MetadataTest; 16 import org.apache.ojb.broker.query.QueryByIdentity; 17 import org.apache.ojb.broker.sequence.Repository; 18 import org.apache.ojb.broker.util.ClassHelper; 19 import org.apache.ojb.broker.util.GUID; 20 import org.apache.ojb.junit.OJBTestCase; 21 22 29 public class ObjectCacheTest extends OJBTestCase 30 { 31 static final String EXCLUDE_PACKAGE = "org.apache.ojb.broker.sequence"; 32 static final String EXCLUDE_PACKAGE_NOT_EXIST = "org.apache.ojb.broker.sequence.xyz"; 33 34 Class [] objectCacheImpls = new Class []{ 35 ObjectCacheDefaultImpl.class, 37 ObjectCacheTwoLevelImpl.class, 38 ObjectCachePerBrokerImpl.class, 39 ObjectCacheJCSImpl.class, 40 ObjectCacheJCSPerClassImpl.class, 41 ObjectCachePerClassImpl.class 42 }; 43 44 Class old_ObjectCache; 45 String [] old_CacheFilter; 46 47 public ObjectCacheTest(String s) 48 { 49 super(s); 50 } 51 52 public static void main(String [] args) 53 { 54 String [] arr = {ObjectCacheTest.class.getName()}; 55 junit.textui.TestRunner.main(arr); 56 } 57 58 protected void setUp() throws Exception 59 { 60 super.setUp(); 61 } 62 63 protected void tearDown() throws Exception 64 { 65 super.tearDown(); 66 } 67 68 77 public void testJCSPerClassObjectCacheImplementation() throws Exception 78 { 79 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 80 try 81 { 82 ObjectCache cache = new ObjectCacheJCSPerClassImpl(broker, null); 83 84 CacheObject obj_1 = new CacheObject(null, "testJCSPerClassObjectCacheImplementation_1"); 85 Identity oid_1 = new Identity(obj_1, broker); 86 CacheObject obj_2 = new CacheObject(null, "testJCSPerClassObjectCacheImplementation_2"); 87 Identity oid_2 = new Identity(obj_2, broker); 88 CacheObject obj_3 = new CacheObject(null, "testJCSPerClassObjectCacheImplementation_2"); 89 Identity oid_3 = new Identity(obj_3, broker); 90 91 cache.cache(oid_1, obj_1); 92 cache.cache(oid_2, obj_2); 93 94 assertNotNull(cache.lookup(oid_1)); 96 assertNotNull(cache.lookup(oid_2)); 97 cache.cache(oid_3, obj_3); 98 boolean bool = cache.lookup(oid_1) != null; 100 bool = bool && cache.lookup(oid_2) != null; 101 bool = bool && cache.lookup(oid_3) != null; 102 assertFalse("We should not found all cached objects", bool); 103 Thread.sleep(4000); 105 assertNull(cache.lookup(oid_1)); 106 assertNull(cache.lookup(oid_2)); 107 assertNull(cache.lookup(oid_3)); 108 109 } 110 catch (Exception e) 111 { 112 e.printStackTrace(); 113 throw e; 114 } 115 finally 116 { 117 if(broker != null) broker.close(); 118 } 119 } 120 121 public void testObjectCacheDefaultImplTimeout() throws Exception 122 { 123 TestObjectDefaultCache obj = new TestObjectDefaultCache(); 124 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 125 try 126 { 127 broker.beginTransaction(); 128 broker.store(obj); 129 broker.commitTransaction(); 130 131 Identity oid = new Identity(obj, broker); 132 obj = (TestObjectDefaultCache) broker.serviceObjectCache().lookup(oid); 133 assertNotNull(obj); 134 135 Thread.sleep(5000); 136 obj = (TestObjectDefaultCache) broker.serviceObjectCache().lookup(oid); 137 assertNull(obj); 138 } 139 finally 140 { 141 if(broker != null) broker.close(); 142 } 143 } 144 145 public void testObjectCacheDefaultImpl() throws Exception 146 { 147 String name = "testObjectCacheDefaultImpl_"+System.currentTimeMillis(); 148 TestObjectDefaultCache obj = new TestObjectDefaultCache(); 149 obj.setName(name); 150 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 151 try 152 { 153 broker.beginTransaction(); 154 broker.store(obj); 155 broker.commitTransaction(); 156 157 Identity oid = new Identity(obj, broker); 158 obj = (TestObjectDefaultCache) broker.serviceObjectCache().lookup(oid); 159 assertNotNull(obj); 160 assertEquals(name, obj.getName()); 161 162 String new_name = "modified_"+name; 164 obj.setName(new_name); 165 obj = (TestObjectDefaultCache) broker.getObjectByIdentity(oid); 166 assertNotNull(obj); 167 assertEquals("current version of cache should return the modified object", new_name, obj.getName()); 168 169 broker.removeFromCache(oid); 170 obj = (TestObjectDefaultCache) broker.serviceObjectCache().lookup(oid); 171 assertNull("Should be removed from cache", obj); 172 obj = (TestObjectDefaultCache) broker.getObjectByIdentity(oid); 173 assertNotNull(obj); 174 assertEquals("Should return the unmodified object", name, obj.getName()); 175 } 176 finally 177 { 178 if(broker != null) broker.close(); 179 } 180 } 181 182 187 public void testCacheFilterFunctions() throws Exception 188 { 189 PersistenceBrokerFactory.releaseAllInstances(); 190 String old = null; 191 try 192 { 193 MetadataManager mm = MetadataManager.getInstance(); 194 JdbcConnectionDescriptor jcd = mm.connectionRepository().getDescriptor(mm.getDefaultPBKey()); 195 if(jcd.getObjectCacheDescriptor().getObjectCache().equals(ObjectCacheEmptyImpl.class)) 196 { 197 ojbSkipTestMessage("Doesn't work with " + ObjectCacheEmptyImpl.class + " as default cache."); 198 return; 199 } 200 old = jcd.getAttribute(CacheDistributor.CACHE_EXCLUDES_STRING); 201 jcd.addAttribute(CacheDistributor.CACHE_EXCLUDES_STRING, "org.apache.ojb.broker.sequence"); 202 203 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 204 try 205 { 206 ObjectCache cache = broker.serviceObjectCache(); 207 CacheObject obj = new CacheObject(null, "CacheObject persistent obj"); 208 Identity oid = new Identity(obj, broker); 209 210 211 Repository.SMKey filterOutPackageObject = new Repository.SMKey(); 212 filterOutPackageObject.setName("ObjectCacheTest: package filter"); 213 Identity filterOutPackageOid = new Identity(filterOutPackageObject, broker); 214 215 Object result = null; 216 cache.clear(); 217 result = cache.lookup(oid); 218 assertNull(result); 219 result = cache.lookup(filterOutPackageOid); 220 assertNull(result); 221 222 cache.cache(oid, obj); 224 cache.cache(filterOutPackageOid, filterOutPackageObject); 225 226 result = cache.lookup(oid); 228 assertNotNull(result); 229 assertEquals(obj, result); 230 result = cache.lookup(filterOutPackageOid); 231 assertNull(result); 232 } 233 finally 234 { 235 jcd.addAttribute(CacheDistributor.CACHE_EXCLUDES_STRING, old); 236 if (broker != null) broker.close(); 237 } 238 } 239 finally 240 { 241 PersistenceBrokerFactory.releaseAllInstances(); 242 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 243 broker.close(); 244 } 245 } 246 247 252 public void testSimpleObjectCacheFunctions() throws Exception 253 { 254 for (int i = 0; i < objectCacheImpls.length; i++) 255 { 256 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 257 try 258 { 259 ObjectCache cache = (ObjectCache) ClassHelper.newInstance( 260 objectCacheImpls[i], 261 new Class [] {PersistenceBroker.class,Properties .class}, 262 new Object [] {broker, null}); 263 checkBaseFunctions(broker, cache); 264 } 265 finally 266 { 267 if (broker != null) broker.close(); 268 } 269 } 270 } 271 272 273 278 private void checkBaseFunctions(PersistenceBroker broker, ObjectCache cache) throws Exception 279 { 280 CacheObject obj = new CacheObject(null, "ObjectCache test"); 281 Identity oid = new Identity(obj, broker); 282 CacheObject obj2 = new CacheObject(null, "ObjectCache test 2"); 283 Identity oid2 = new Identity(obj2, broker); 284 cache.clear(); 285 Object result = cache.lookup(oid); 286 assertNull(result); 287 288 cache.cache(oid, obj); 289 cache.cache(oid2, obj2); 290 result = cache.lookup(oid); 291 assertNotNull(result); 292 assertEquals(obj, result); 293 assertNotSame(obj2, result); 294 295 cache.remove(oid); 296 result = cache.lookup(oid); 297 Object result2 = cache.lookup(oid2); 298 assertNull(result); 299 assertNotNull(result2); 300 301 cache.clear(); 302 result = cache.lookup(oid); 303 assertNull(result); 304 result = cache.lookup(oid2); 305 assertNull(result); 306 } 308 309 316 public void testPerClassCache() throws Exception 317 { 318 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 319 JdbcConnectionDescriptor jcd = broker.serviceConnectionManager().getConnectionDescriptor(); 320 if(jcd.getObjectCacheDescriptor().getObjectCache().equals(ObjectCacheEmptyImpl.class)) 321 { 322 ojbSkipTestMessage("Doesn't work with " + ObjectCacheEmptyImpl.class + " as default cache."); 323 return; 324 } 325 String name = "testPerClassCache_" + System.currentTimeMillis(); 326 327 TestObjectEmptyCache obj = new TestObjectEmptyCache(); 328 obj.setName(name); 329 CacheObject dummy = new CacheObject(); 330 dummy.setName(name); 331 332 try 333 { 334 broker.beginTransaction(); 335 broker.store(obj); 336 broker.store(dummy); 337 broker.commitTransaction(); 338 339 Identity obj_oid = new Identity(obj, broker); 340 Identity dummy_oid = new Identity(dummy, broker); 341 ObjectCache cache = broker.serviceObjectCache(); 342 Object ret_obj = cache.lookup(obj_oid); 343 Object ret_dummy = cache.lookup(dummy_oid); 344 assertNotNull(ret_dummy); 345 assertNull(ret_obj); 346 } 347 finally 348 { 349 if (broker != null && broker.isInTransaction()) broker.abortTransaction(); 350 if (broker != null) broker.close(); 351 } 352 } 353 354 360 public void testPerDatabaseCache() 361 { 362 ConnectionRepository cr = MetadataManager.getInstance() 363 .readConnectionRepository(MetadataTest.TEST_REPOSITORY); 364 MetadataManager.getInstance().mergeConnectionRepository(cr); 365 366 PersistenceBroker pb = PersistenceBrokerFactory.createPersistenceBroker(new PBKey("runtime_2")); 367 try 368 { 369 ObjectCache oc = pb.serviceObjectCache(); 370 CacheObject testObj = new CacheObject(null, "testPerDatabaseCache"); 371 Identity oid = new Identity(testObj, pb); 372 oc.cache(oid, testObj); 373 Object result = oc.lookup(oid); 374 assertNull("We should not found this object in cache", result); 375 } 376 finally 377 { 378 if (pb != null && !pb.isClosed()) pb.close(); 379 MetadataManager.getInstance().connectionRepository().removeDescriptor(cr.getAllDescriptor().get(0)); 380 } 381 } 382 383 388 public void YYYtestCacheIsolation() throws Exception 389 { 390 Object [] pk = new Object []{new Long (42)}; 391 Identity oid = new Identity(Article.class, InterfaceArticle.class, pk); 392 393 GUID guid = new GUID(); 394 395 PersistenceBroker broker1 = PersistenceBrokerFactory.defaultPersistenceBroker(); 396 broker1.beginTransaction(); 397 398 Article a1 = (Article) broker1.getObjectByQuery(new QueryByIdentity(oid)); 399 String originalName = a1.getArticleName(); 400 a1.setArticleName(guid.toString()); 401 402 PersistenceBroker broker2 = PersistenceBrokerFactory.defaultPersistenceBroker(); 404 broker2.beginTransaction(); 405 406 Article a2 = (Article) broker2.getObjectByQuery(new QueryByIdentity(oid)); 407 408 assertEquals(guid.toString(), a1.getArticleName()); 409 assertEquals(originalName, a2.getArticleName()); 410 assertNotSame(a1, a2); 411 412 413 broker1.commitTransaction(); 414 broker1.close(); 415 416 broker2.commitTransaction(); 417 broker2.close(); 418 } 419 420 421 422 public static class CacheObject implements Serializable 426 { 427 private Integer objId; 428 private String name; 429 430 public CacheObject(Integer objId, String name) 431 { 432 this.objId = objId; 433 this.name = name; 434 } 435 436 public CacheObject() 437 { 438 } 439 440 public Integer getObjId() 441 { 442 return objId; 443 } 444 445 public void setObjId(Integer objId) 446 { 447 this.objId = objId; 448 } 449 450 public String getName() 451 { 452 return name; 453 } 454 455 public void setName(String name) 456 { 457 this.name = name; 458 } 459 } 460 461 465 public static class TestObjectEmptyCache 466 { 467 private Integer id; 468 private String name; 469 470 public TestObjectEmptyCache() 471 { 472 } 473 474 public Integer getId() 475 { 476 return id; 477 } 478 479 public void setId(Integer id) 480 { 481 this.id = id; 482 } 483 484 public String getName() 485 { 486 return name; 487 } 488 489 public void setName(String name) 490 { 491 this.name = name; 492 } 493 } 494 495 public static class TestObjectDefaultCache extends TestObjectEmptyCache 496 { 497 } 498 } 499 | Popular Tags |