KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > cache > KeySyncCacheManagerTest


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * KeySyncCacheManagerTest.java
20  *
21  * JUnit based test
22  */

23
24 package com.rift.coad.lib.cache;
25
26 import junit.framework.*;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import com.rift.coad.lib.thread.ThreadStateMonitor;
31
32 /**
33  *
34  * @author mincemeat
35  */

36 public class KeySyncCacheManagerTest extends TestCase {
37     
38     public KeySyncCacheManagerTest(String JavaDoc testName) {
39         super(testName);
40     }
41
42     protected void setUp() throws Exception JavaDoc {
43     }
44
45     protected void tearDown() throws Exception JavaDoc {
46     }
47
48     public static Test suite() {
49         TestSuite suite = new TestSuite(KeySyncCacheManagerTest.class);
50         
51         return suite;
52     }
53     
54     
55     /**
56      * Test of KeySyncCacheManager of class com.rift.coad.lib.cache.KeySyncCacheManager.
57      */

58     public void testKeySyncCacheManager() throws Exception JavaDoc {
59         System.out.println("KeySyncCacheManager");
60         
61         Object JavaDoc identifier = null;
62         KeySyncCacheManager instance = new KeySyncCacheManager();
63         
64         KeySyncCache keySyncCache1 = instance.getKeySyncCache("key1");
65         KeySyncCache keySyncCache2 = instance.getKeySyncCache("key2");
66         assertEquals(keySyncCache1, instance.getKeySyncCache("key1"));
67         assertEquals(keySyncCache2, instance.getKeySyncCache("key2"));
68         
69         KeySyncCache.KeySync fred = keySyncCache1.getKeySync("fred");
70         KeySyncCache.KeySync bob = keySyncCache1.getKeySync("bob");
71         KeySyncCache.KeySync mary = keySyncCache2.getKeySync("mary");
72         KeySyncCache.KeySync jill = keySyncCache2.getKeySync("jill");
73         assertEquals(fred, keySyncCache1.getKeySync("fred"));
74         assertEquals(bob, keySyncCache1.getKeySync("bob"));
75         assertEquals(mary, keySyncCache2.getKeySync("mary"));
76         assertEquals(jill, keySyncCache2.getKeySync("jill"));
77         
78         for (int count = 0; count < 4; count++) {
79             Thread.sleep(500);
80             fred.touch();
81             mary = keySyncCache2.getKeySync("mary");
82         }
83         
84         instance.garbageCollect();
85         
86         assertEquals(fred, keySyncCache1.getKeySync("fred"));
87         assertEquals(mary, keySyncCache2.getKeySync("mary"));
88         if (bob == keySyncCache1.getKeySync("bob")) {
89             fail("The entry should not have been found");
90         }
91         if (jill == keySyncCache2.getKeySync("jill")) {
92             fail("The entry should not have been found");
93         }
94         
95         instance.clear();
96         
97         if (keySyncCache1.contains("fred")) {
98             fail("The instance still contains fred");
99         }
100         if (keySyncCache2.contains("mary")) {
101             fail("The instance still contains mary");
102         }
103         
104         try {
105             instance.getKeySyncCache("key1");
106             fail("Should not have been able to retrieve a sync instance");
107         } catch (CacheException ex) {
108             // ignore
109
}
110     }
111
112 }
113
Popular Tags