KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > cache > CacheTest


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

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 /**
75  * A test case to verify the functions of the Expresso CacheManager and related
76  * objects.
77  *
78  * @author Michael Nash
79  * @since Expresso 4.0
80  */

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     /**
86      * Cache name, may be modified
87      */

88     private String JavaDoc cacheName = "testcache";
89
90     /**
91      * Constructs a test case with the given name.
92      *
93      * @param name the testname
94      * @throws Exception upon instantiation error
95      */

96     public CacheTest(String JavaDoc name) throws Exception JavaDoc {
97         super(name);
98
99     }
100     /* CacheTest(String) */
101
102     /**
103      * Main Thread for running this test
104      *
105      * @param args command arguments
106      * @throws Exception upon error
107      */

108     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
109         //Set the system properties we need
110
junit.textui.TestRunner.run(suite());
111     }
112
113     /**
114      * Helper function called from CacheTestThread for multiple instances
115      * of caches
116      *
117      * @param newName The new cache name
118      */

119     public void setCacheName(String JavaDoc newName) {
120         cacheName = newName;
121     }
122
123     /**
124      * Sets up the fixture, for example, open a network connection. This method
125      * is called before a test is executed.
126      */

127     public void setUp() throws Exception JavaDoc {
128         try {
129             CacheManager.createCache(TestSystemInitializer.getTestContext(), cacheName,
130                     false);
131
132             /* Create an ordered cache as well */
133             CacheManager.createCache(TestSystemInitializer.getTestContext(),
134                     cacheName + "O", true);
135         } catch (java.util.ConcurrentModificationException JavaDoc cme) {
136             log.error("Error Setting Up", cme);
137             throw cme;
138         }
139     }
140     /* setUp() */
141
142     /**
143      * Counts the number of test cases executed by run(TestResult result).
144      *
145      * @return integer (1)
146      */

147     public int countTestCases() {
148         return 1;
149     }
150     /* countTestCases() */
151
152     /**
153      * Define the suite of tests that verify each function of the cache
154      *
155      * @return junit.framework.TestSuite
156      */

157     public static Test suite() throws Exception JavaDoc {
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     /* suite() */
167
168     /**
169      * Tears down the fixture, for example, close a network connection. This
170      * method is called after a test is executed.
171      */

172     public void tearDown() throws Exception JavaDoc {
173         try {
174             CacheManager.clear(TestSystemInitializer.getTestContext(), cacheName);
175             CacheManager.clear(TestSystemInitializer.getTestContext(), cacheName + "O");
176         } catch (java.util.ConcurrentModificationException JavaDoc cme) {
177             log.error("Error Tearing Down", cme);
178             throw cme;
179         }
180     }
181     /* tearDown() */
182
183     /**
184      * Test adding an item to a cache that does not exist to ensure it gets
185      * created
186      */

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 JavaDoc cme) {
219             cme.printStackTrace();
220             log.error(cme);
221             fail("ConcurrantModificationException - see log");
222         }
223     }
224     /* testCreateCache() */
225
226     /**
227      * Test adding an item to a cache that does not exist to ensure it gets
228      * created
229      */

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 JavaDoc cme) {
255             log.error(cme);
256             fail("ConcurrantModificationException - see log");
257         }
258     }
259     /* testCreateOrderedCache() */
260
261     /**
262      * Test multiple items to ensure the value and it's appropriate key are not
263      * being mixed up
264      */

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 JavaDoc cme) {
293             log.error(cme);
294             fail("ConcurrantModificationException - see log");
295         }
296     }
297     /* testMultipleItems() */
298
299     /**
300      * Test multiple items to ensure the value and it's appropriate key are not
301      * being mixed up
302      */

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 JavaDoc cme) {
333             log.error(cme);
334             fail("ConcurrantModificationException - see log");
335         }
336     }
337     /* testMultipleItems() */
338
339     /**
340      * All this class's goal is to try to test if we're going to get a
341      * <code>java.util.ConcurrentModificationException</code> or
342      * <code>java.lang.NullPointerException</code>. We write and trash
343      * various cache names. We don't care if the values exist, we're just
344      * randomly beating on the cache to try to get an exception
345      */

346     public void testRandomCaches() {
347         String JavaDoc[] 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 JavaDoc ce) {
397             ce.printStackTrace();
398             fail("Error during random tests: " + ce.getMessage());
399         } catch (java.lang.NullPointerException JavaDoc npe) {
400             npe.printStackTrace();
401             fail("Error during random tests." + npe.getMessage());
402         } catch (Throwable JavaDoc t) {
403             t.printStackTrace();
404             fail("Error during random tests." + t.getMessage());
405         }
406     }
407
408     /**
409      * Tests to make sure that when an item is removed from one cache, potentially
410      * because it is stale, that related caches should be cleared as well.
411      */

412     public void testRelatedCaches() {
413         try {
414             CacheSystem cs = CacheManager.getCacheSystem(TestSystemInitializer.getTestContext());
415
416             /* Now test "related" caches - we create 2 caches, one listening to the */
417             /* other, then modify an item */
418             /* in the first cache & make sure the second gets cleared */
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             /* this should have cleared secondCache */
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 JavaDoc cme) {
451             log.error(cme);
452             fail("ConcurrantModificationException - see log");
453         }
454     }
455
456
457     /**
458      * Checks listeners when dealing with put vs. add functionality.
459      */

460     public void testRelatedCachesWithAddingUnmodifiedItems() {
461         try {
462             CacheSystem cs = CacheManager.getCacheSystem(TestSystemInitializer.getTestContext());
463
464
465             /* Now test "related" caches - we create 2 caches, one listening to the */
466             /* other, then modify an item */
467             /* in the first cache & make sure the second gets cleared */
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             //This should NOT clear second cache
491
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             /* this should have cleared secondCache */
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 JavaDoc cme) {
515             log.error(cme);
516             fail("ConcurrantModificationException - see log");
517         }
518     }
519
520     /**
521      * Most basic possible test. If you put something in a cache, can you get
522      * it back out again?
523      */

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 JavaDoc cme) {
545             log.error("Error testing simple caching", cme);
546             throw cme;
547         }
548     }
549
550
551     /**
552      * Most basic possible test. If you put something in a cache, can you get
553      * it back out again?
554      */

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 JavaDoc cme) {
577             log.error(cme);
578             fail("ConcurrantModificationException - see log");
579         }
580     }
581
582 }
Popular Tags