KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > cache > ObjectCacheTest


1 package org.apache.ojb.broker.cache;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.Properties JavaDoc;
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 /**
23  * Do some basic tests using ObjectCache implementations.
24  *
25  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
26  * @author <a HREF="mailto:thma@apache.org">Thomas Mahler</a>
27  * @version $Id: ObjectCacheTest.java,v 1.1.2.2 2005/07/24 23:57:42 arminw Exp $
28  */

29 public class ObjectCacheTest extends OJBTestCase
30 {
31     static final String JavaDoc EXCLUDE_PACKAGE = "org.apache.ojb.broker.sequence";
32     static final String JavaDoc EXCLUDE_PACKAGE_NOT_EXIST = "org.apache.ojb.broker.sequence.xyz";
33
34     Class JavaDoc[] objectCacheImpls = new Class JavaDoc[]{
35         // ObjectCacheEmptyImpl.class,
36
ObjectCacheDefaultImpl.class,
37         ObjectCacheTwoLevelImpl.class,
38         ObjectCachePerBrokerImpl.class,
39         ObjectCacheJCSImpl.class,
40         ObjectCacheJCSPerClassImpl.class,
41         ObjectCachePerClassImpl.class
42     };
43
44     Class JavaDoc old_ObjectCache;
45     String JavaDoc[] old_CacheFilter;
46
47     public ObjectCacheTest(String JavaDoc s)
48     {
49         super(s);
50     }
51
52     public static void main(String JavaDoc[] args)
53     {
54         String JavaDoc[] arr = {ObjectCacheTest.class.getName()};
55         junit.textui.TestRunner.main(arr);
56     }
57
58     protected void setUp() throws Exception JavaDoc
59     {
60         super.setUp();
61     }
62
63     protected void tearDown() throws Exception JavaDoc
64     {
65         super.tearDown();
66     }
67
68     /**
69      * Test the JCS cache implementation. In JCS config file the following
70      * properties are set:
71      * <br/>
72      * jcs.region.org.apache.ojb.broker.cache.ObjectCacheTest$CacheObject.cacheattributes.MaxObjects=3
73      * jcs.region.org.apache.ojb.broker.cache.ObjectCacheTest$CacheObject.cacheattributes.MaxMemoryIdleTimeSeconds=2
74      * jcs.region.org.apache.ojb.broker.cache.ObjectCacheTest$CacheObject.cacheattributes.UseMemoryShrinker=true
75      * jcs.region.org.apache.ojb.broker.cache.ObjectCacheTest$CacheObject.cacheattributes.ShrinkerIntervalSeconds=1
76      */

77     public void testJCSPerClassObjectCacheImplementation() throws Exception JavaDoc
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             // two objects should be found
95
assertNotNull(cache.lookup(oid_1));
96             assertNotNull(cache.lookup(oid_2));
97             cache.cache(oid_3, obj_3);
98             // we only allow two objects in cache region
99
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             // idle time is 2 sec
104
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 JavaDoc 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 JavaDoc
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 JavaDoc
146     {
147         String JavaDoc 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             // modify name
163
String JavaDoc 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     /**
183      * This test check the 'cacheExcludes' property and try to exclude a whole package from
184      * caching.
185      * @throws Exception
186      */

187     public void testCacheFilterFunctions() throws Exception JavaDoc
188     {
189         PersistenceBrokerFactory.releaseAllInstances();
190         String JavaDoc 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 JavaDoc result = null;
216                 cache.clear();
217                 result = cache.lookup(oid);
218                 assertNull(result);
219                 result = cache.lookup(filterOutPackageOid);
220                 assertNull(result);
221
222                 // cache it
223
cache.cache(oid, obj);
224                 cache.cache(filterOutPackageOid, filterOutPackageObject);
225
226                 // lookup things
227
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     /**
248      * Check base caching functions of some cache implementations.
249      *
250      * @throws Exception
251      */

252     public void testSimpleObjectCacheFunctions() throws Exception JavaDoc
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 JavaDoc[] {PersistenceBroker.class,Properties JavaDoc.class},
262                             new Object JavaDoc[] {broker, null});
263                 checkBaseFunctions(broker, cache);
264             }
265             finally
266             {
267                 if (broker != null) broker.close();
268             }
269         }
270     }
271
272
273     /**
274      * Checks the base functions of the current ObjectCache implementation.
275      *
276      * @throws Exception
277      */

278     private void checkBaseFunctions(PersistenceBroker broker, ObjectCache cache) throws Exception JavaDoc
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 JavaDoc 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 JavaDoc 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         // cache.clear();
307
}
308
309     /**
310      * Test per class ObjectCache declaration. 'TestObjectEmptyCache'
311      * class metadata declare an 'empty ObjectCache' implementation
312      * as cache, CacheObject use the default ObjectCache implementation.
313      * Thus we should found 'CacheObject' instance in cache, but NOT found
314      * 'TestObjectEmptyCache' instance.
315      */

316     public void testPerClassCache() throws Exception JavaDoc
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 JavaDoc 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 JavaDoc ret_obj = cache.lookup(obj_oid);
343             Object JavaDoc 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     /**
355      * Read a specific jdbc-connction-descriptor at runtime, merge it with current
356      * ConnectionRepository, lookup a specific PersistenceBroker instance, get ObjectCache.
357      * This should be ObjectCacheEmptyImpl, because this is declared at jdbc-connection-descriptor
358      * level.
359      */

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 JavaDoc 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     /**
384      * This test checks if the caches of two different brokers are properly isolated.
385      * changes made to an object in tx1 should not be visible in tx2 !
386      * TODO: once we work without global cache only (e.g. intern temporary cache), this test should pass!
387      */

388     public void YYYtestCacheIsolation() throws Exception JavaDoc
389     {
390         Object JavaDoc[] pk = new Object JavaDoc[]{new Long JavaDoc(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 JavaDoc originalName = a1.getArticleName();
400         a1.setArticleName(guid.toString());
401
402 // start a second transaction
403
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     // **********************************************************************
423
// inner class
424
// **********************************************************************
425
public static class CacheObject implements Serializable JavaDoc
426     {
427         private Integer JavaDoc objId;
428         private String JavaDoc name;
429
430         public CacheObject(Integer JavaDoc objId, String JavaDoc name)
431         {
432             this.objId = objId;
433             this.name = name;
434         }
435
436         public CacheObject()
437         {
438         }
439
440         public Integer JavaDoc getObjId()
441         {
442             return objId;
443         }
444
445         public void setObjId(Integer JavaDoc objId)
446         {
447             this.objId = objId;
448         }
449
450         public String JavaDoc getName()
451         {
452             return name;
453         }
454
455         public void setName(String JavaDoc name)
456         {
457             this.name = name;
458         }
459     }
460
461     /**
462      * in class-descriptor ObjectCacheEmptyImpl class is declared
463      * as cache implementation.
464      */

465     public static class TestObjectEmptyCache
466     {
467         private Integer JavaDoc id;
468         private String JavaDoc name;
469
470         public TestObjectEmptyCache()
471         {
472         }
473
474         public Integer JavaDoc getId()
475         {
476             return id;
477         }
478
479         public void setId(Integer JavaDoc id)
480         {
481             this.id = id;
482         }
483
484         public String JavaDoc getName()
485         {
486             return name;
487         }
488
489         public void setName(String JavaDoc name)
490         {
491             this.name = name;
492         }
493     }
494
495     public static class TestObjectDefaultCache extends TestObjectEmptyCache
496     {
497     }
498 }
499
Popular Tags