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: * KeySyncCacheTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.cache;
025:
026: import junit.framework.*;
027: import java.util.Date;
028: import java.util.Map;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import org.apache.log4j.Logger;
032: import com.rift.coad.lib.configuration.ConfigurationFactory;
033: import com.rift.coad.lib.configuration.Configuration;
034: import com.rift.coad.lib.thread.ThreadStateMonitor;
035:
036: /**
037: *
038: * @author mincemeat
039: */
040: public class KeySyncCacheTest extends TestCase {
041:
042: public KeySyncCacheTest(String testName) {
043: super (testName);
044: }
045:
046: protected void setUp() throws Exception {
047: }
048:
049: protected void tearDown() throws Exception {
050: }
051:
052: public static Test suite() {
053: TestSuite suite = new TestSuite(KeySyncCacheTest.class);
054:
055: return suite;
056: }
057:
058: /**
059: * Test of getKeySync method, of class com.rift.coad.lib.cache.KeySyncCache.
060: */
061: public void testKeySyncCache() throws Exception {
062: System.out.println("KeySyncCache");
063:
064: KeySyncCache instance = new KeySyncCache();
065:
066: KeySyncCache.KeySync fred = instance.getKeySync("fred");
067: KeySyncCache.KeySync bob = instance.getKeySync("bob");
068: KeySyncCache.KeySync mary = instance.getKeySync("mary");
069: assertEquals(fred, instance.getKeySync("fred"));
070: assertEquals(bob, instance.getKeySync("bob"));
071: assertEquals(mary, instance.getKeySync("mary"));
072:
073: for (int count = 0; count < 4; count++) {
074: Thread.sleep(500);
075: fred.touch();
076: mary = instance.getKeySync("mary");
077: }
078:
079: instance.garbageCollect();
080:
081: assertEquals(fred, instance.getKeySync("fred"));
082: assertEquals(mary, instance.getKeySync("mary"));
083: if (bob == instance.getKeySync("bob")) {
084: fail("The entry should not have been found");
085: }
086:
087: instance.clear();
088:
089: if (instance.contains("fred")) {
090: fail("The instance still contains fred");
091: }
092: if (instance.contains("mary")) {
093: fail("The instance still contains mary");
094: }
095:
096: try {
097: instance.getKeySync("fred");
098: fail("Should not have been able to retrieve a sync instance");
099: } catch (CacheException ex) {
100: // ignore
101: }
102: }
103:
104: }
|