001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * KeySyncCacheManagerTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.cache;
025:
026: import junit.framework.*;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.HashMap;
030: import com.rift.coad.lib.thread.ThreadStateMonitor;
031:
032: /**
033: *
034: * @author mincemeat
035: */
036: public class KeySyncCacheManagerTest extends TestCase {
037:
038: public KeySyncCacheManagerTest(String testName) {
039: super (testName);
040: }
041:
042: protected void setUp() throws Exception {
043: }
044:
045: protected void tearDown() throws Exception {
046: }
047:
048: public static Test suite() {
049: TestSuite suite = new TestSuite(KeySyncCacheManagerTest.class);
050:
051: return suite;
052: }
053:
054: /**
055: * Test of KeySyncCacheManager of class com.rift.coad.lib.cache.KeySyncCacheManager.
056: */
057: public void testKeySyncCacheManager() throws Exception {
058: System.out.println("KeySyncCacheManager");
059:
060: Object identifier = null;
061: KeySyncCacheManager instance = new KeySyncCacheManager();
062:
063: KeySyncCache keySyncCache1 = instance.getKeySyncCache("key1");
064: KeySyncCache keySyncCache2 = instance.getKeySyncCache("key2");
065: assertEquals(keySyncCache1, instance.getKeySyncCache("key1"));
066: assertEquals(keySyncCache2, instance.getKeySyncCache("key2"));
067:
068: KeySyncCache.KeySync fred = keySyncCache1.getKeySync("fred");
069: KeySyncCache.KeySync bob = keySyncCache1.getKeySync("bob");
070: KeySyncCache.KeySync mary = keySyncCache2.getKeySync("mary");
071: KeySyncCache.KeySync jill = keySyncCache2.getKeySync("jill");
072: assertEquals(fred, keySyncCache1.getKeySync("fred"));
073: assertEquals(bob, keySyncCache1.getKeySync("bob"));
074: assertEquals(mary, keySyncCache2.getKeySync("mary"));
075: assertEquals(jill, keySyncCache2.getKeySync("jill"));
076:
077: for (int count = 0; count < 4; count++) {
078: Thread.sleep(500);
079: fred.touch();
080: mary = keySyncCache2.getKeySync("mary");
081: }
082:
083: instance.garbageCollect();
084:
085: assertEquals(fred, keySyncCache1.getKeySync("fred"));
086: assertEquals(mary, keySyncCache2.getKeySync("mary"));
087: if (bob == keySyncCache1.getKeySync("bob")) {
088: fail("The entry should not have been found");
089: }
090: if (jill == keySyncCache2.getKeySync("jill")) {
091: fail("The entry should not have been found");
092: }
093:
094: instance.clear();
095:
096: if (keySyncCache1.contains("fred")) {
097: fail("The instance still contains fred");
098: }
099: if (keySyncCache2.contains("mary")) {
100: fail("The instance still contains mary");
101: }
102:
103: try {
104: instance.getKeySyncCache("key1");
105: fail("Should not have been able to retrieve a sync instance");
106: } catch (CacheException ex) {
107: // ignore
108: }
109: }
110:
111: }
|