001: /**
002: * JDBM LICENSE v1.00
003: *
004: * Redistribution and use of this software and associated documentation
005: * ("Software"), with or without modification, are permitted provided
006: * that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain copyright
009: * statements and notices. Redistributions must also contain a
010: * copy of this document.
011: *
012: * 2. Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and the
014: * following disclaimer in the documentation and/or other
015: * materials provided with the distribution.
016: *
017: * 3. The name "JDBM" must not be used to endorse or promote
018: * products derived from this Software without prior written
019: * permission of Cees de Groot. For written permission,
020: * please contact cg@cdegroot.com.
021: *
022: * 4. Products derived from this Software may not be called "JDBM"
023: * nor may "JDBM" appear in their names without prior written
024: * permission of Cees de Groot.
025: *
026: * 5. Due credit should be given to the JDBM Project
027: * (http://jdbm.sourceforge.net/).
028: *
029: * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
030: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
031: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
032: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
033: * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
034: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
035: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
036: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
037: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
038: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
039: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
040: * OF THE POSSIBILITY OF SUCH DAMAGE.
041: *
042: * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
043: * Contributions are Copyright (C) 2000 by their associated contributors.
044: *
045: */package jdbm.helper;
046:
047: import junit.framework.TestSuite;
048:
049: /**
050: * Unit test for {@link MRU}.
051: *
052: * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
053: * @author <a href="mailto:dranatunga@users.sourceforge.net">Dilum Ranatunga</a>
054: * @version $Id: TestMRU.java,v 1.3 2003/11/01 13:27:18 dranatunga Exp $
055: */
056: public class TestMRU extends TestCachePolicy {
057:
058: public TestMRU(String name) {
059: super (name);
060: }
061:
062: protected CachePolicy createInstance(int capacity) {
063: return new MRU(capacity);
064: }
065:
066: /**
067: * Test constructor
068: */
069: public void testConstructor() {
070:
071: try {
072: // should not support 0-size cache
073: MRU m1 = new MRU(0);
074: fail("expected exception");
075: } catch (Exception e) {
076: }
077:
078: MRU m5 = new MRU(5);
079: }
080:
081: /**
082: * Test eviction
083: */
084: public void testEvict() throws CacheEvictionException {
085: Object o1 = new Object();
086: Object o2 = new Object();
087: Object o3 = new Object();
088: Object o4 = new Object();
089: Object o5 = new Object();
090:
091: MRU m1 = new MRU(3);
092:
093: m1.put("1", o1);
094: m1.put("2", o2);
095: m1.put("3", o3);
096: m1.put("4", o4);
097:
098: assertEquals(null, m1.get("1"));
099: assertEquals(o2, m1.get("2"));
100: assertEquals(o3, m1.get("3"));
101: assertEquals(o4, m1.get("4"));
102: }
103:
104: /**
105: * Test key replacement
106: */
107: public void testReplace() throws CacheEvictionException {
108: Object o1 = new Object();
109: Object o2 = new Object();
110: Object o3 = new Object();
111: Object o4 = new Object();
112:
113: MRU m1 = new MRU(3);
114:
115: m1.put("1", o1);
116: m1.put("2", o2);
117: m1.put("3", o3);
118: m1.put("1", o4);
119:
120: assertEquals(o4, m1.get("1"));
121: assertEquals(o2, m1.get("2"));
122: assertEquals(o3, m1.get("3"));
123: }
124:
125: /**
126: * Test multiple touch
127: */
128: public void testMultiple() throws CacheEvictionException {
129: Object o1 = new Object();
130: Object o2 = new Object();
131: Object o3 = new Object();
132: Object o4 = new Object();
133:
134: MRU m1 = new MRU(3);
135:
136: m1.put("1", o1);
137: m1.put("2", o2);
138: m1.put("3", o3);
139: m1.put("3", o3);
140: m1.put("3", o3);
141: m1.put("3", o3);
142:
143: assertEquals(o1, m1.get("1"));
144: assertEquals(o2, m1.get("2"));
145: assertEquals(o3, m1.get("3"));
146:
147: m1.put("1", o3); // replace with o3
148: m1.put("4", o4); // should evict 2
149:
150: assertEquals(o4, m1.get("4"));
151: assertEquals(o3, m1.get("3"));
152: assertEquals(o3, m1.get("1"));
153: assertEquals(null, m1.get("2"));
154: }
155:
156: public void testEvictionExceptionRecovery()
157: throws CacheEvictionException {
158: final CachePolicy cache = new MRU(1);
159: final Object oldKey = "to-be-evicted";
160: final Object newKey = "insert-attempt";
161:
162: { // null test
163: cache.removeAll();
164: cache.put(oldKey, new Object());
165: assertNotNull(cache.get(oldKey));
166: cache.put(newKey, new Object());
167: assertNull(cache.get(oldKey));
168: assertNotNull(cache.get(newKey));
169: }
170:
171: { // stability test.
172: cache.removeAll();
173: cache.addListener(new ThrowingListener());
174: cache.put(oldKey, new Object());
175: assertNotNull(cache.get(oldKey));
176: try {
177: cache.put(newKey, new Object());
178: fail("Did not propagate expected exception.");
179: } catch (CacheEvictionException cex) {
180: assertNotNull(
181: "old object missing after eviction exception!",
182: cache.get(oldKey));
183: assertNull(
184: "new key -> object mapping added even when eviction exception!",
185: cache.get(newKey));
186: }
187: }
188: }
189:
190: /**
191: * Runs all tests in this class
192: */
193: public static void main(String[] args) {
194: junit.textui.TestRunner.run(new TestSuite(TestMRU.class));
195: }
196: }
|