KickJava   Java API By Example, From Geeks To Geeks.

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


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  * KeySyncCacheTest.java
20  *
21  * JUnit based test
22  */

23
24 package com.rift.coad.lib.cache;
25
26 import junit.framework.*;
27 import java.util.Date JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import org.apache.log4j.Logger;
32 import com.rift.coad.lib.configuration.ConfigurationFactory;
33 import com.rift.coad.lib.configuration.Configuration;
34 import com.rift.coad.lib.thread.ThreadStateMonitor;
35
36 /**
37  *
38  * @author mincemeat
39  */

40 public class KeySyncCacheTest extends TestCase {
41     
42     public KeySyncCacheTest(String JavaDoc testName) {
43         super(testName);
44     }
45
46     protected void setUp() throws Exception JavaDoc {
47     }
48
49     protected void tearDown() throws Exception JavaDoc {
50     }
51
52     public static Test suite() {
53         TestSuite suite = new TestSuite(KeySyncCacheTest.class);
54         
55         return suite;
56     }
57
58     /**
59      * Test of getKeySync method, of class com.rift.coad.lib.cache.KeySyncCache.
60      */

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