001: /*
002: * @(#)HashCacheUTest.java
003: *
004: * Copyright (C) 2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.util.datastruct.v1;
028:
029: import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
030:
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034:
035: /**
036: *
037: *
038: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
039: * @since April 17, 2001 (Alpha 0.9.0)
040: * @version $Date: 2003/05/23 20:57:00 $
041: */
042: public class HashCacheUTest extends TestCase {
043: private static final Class THIS_CLASS = HashCacheUTest.class;
044:
045: public HashCacheUTest(String name) {
046: super (name);
047: }
048:
049: public static Test suite() {
050: TestSuite suite = new TestSuite(THIS_CLASS);
051:
052: return suite;
053: }
054:
055: public static void main(String[] args) {
056: String[] name = { THIS_CLASS.getName() };
057:
058: // junit.textui.TestRunner.main( name );
059: // junit.swingui.TestRunner.main( name );
060:
061: junit.textui.TestRunner.main(name);
062: }
063:
064: protected void setUp() throws Exception {
065: super .setUp();
066:
067: // set ourself up
068: }
069:
070: protected void tearDown() throws Exception {
071: // tear ourself down
072:
073: super .tearDown();
074: }
075:
076: public class OMgr implements HashCache.ObjectManager {
077: private int count = 0;
078: private int closecount = 0;
079:
080: public synchronized Object createObject(Object key) {
081: return "[" + key + "] " + (++count);
082: }
083:
084: public synchronized void cleanUpObject(Object key, Object value) {
085: ++this .closecount;
086: }
087:
088: public int getCreationCount() {
089: return this .count;
090: }
091:
092: public int getCloseCount() {
093: return this .closecount;
094: }
095: }
096:
097: public void testConstructor1() {
098: HashCache oc = new HashCache(new OMgr(), 10);
099: assertEquals("Size must be instantiation size.", 10, oc
100: .getMaxSize());
101: assertEquals("Overflows must be at zero.", 0, oc
102: .getOverflowCount());
103: }
104:
105: public void testConstructor2() {
106: try {
107: new HashCache(null, 10);
108: fail("Did not throw IllegalArgumentException.");
109: } catch (IllegalArgumentException iae) {
110: // test exception
111: }
112: }
113:
114: public void testConstructor3() {
115: try {
116: new HashCache(null, 0);
117: fail("Did not throw IllegalArgumentException.");
118: } catch (IllegalArgumentException iae) {
119: // test exception
120: }
121: }
122:
123: public void testConstructor4() {
124: try {
125: new HashCache(new OMgr(), 1);
126: fail("Did not throw IllegalArgumentException.");
127: } catch (IllegalArgumentException iae) {
128: // test exception
129: }
130: }
131:
132: public void testConstructor5() {
133: try {
134: new HashCache(new OMgr(), -1);
135: fail("Did not throw IllegalArgumentException.");
136: } catch (IllegalArgumentException iae) {
137: // test exception
138: }
139: }
140:
141: public void testGet1() {
142: OMgr om = new OMgr();
143: HashCache hc = new HashCache(om, 10);
144: try {
145: hc.get(null);
146: } catch (NullPointerException npe) {
147: // test exception?
148: }
149: }
150:
151: public void testGet2() {
152: OMgr om = new OMgr();
153: HashCache hc = new HashCache(om, 10);
154: Object o = hc.get("a");
155: assertNotNull("get returned null.", o);
156: assertTrue("incorrect type returned.", o instanceof String);
157: assertEquals("Incorrect value of returned object.", "[a] 1",
158: (String) o);
159: assertEquals("The mgr reported the creation.", 1, om
160: .getCreationCount());
161:
162: Object o2 = hc.get("a");
163: assertSame("Did not return the same object.", o, o2);
164: assertEquals("The mgr was not called for the creation.", 1, om
165: .getCreationCount());
166: }
167:
168: public void testGet3() {
169: OMgr om = new OMgr();
170: HashCache hc = new HashCache(om, 2);
171: Object o = hc.get("a");
172: Object o2 = hc.get("b");
173: Object o3 = hc.get("c");
174: assertEquals(
175: "The mgr was not called for the creation correctly.",
176: 3, om.getCreationCount());
177: assertEquals("The mgr was not called for the clean-up.", 1, om
178: .getCloseCount());
179: assertEquals("The overflow count is wrong.", 1, hc
180: .getOverflowCount());
181: assertEquals("The size is wrong.", 2, hc.getSize());
182: }
183:
184: public void testGet4() {
185: System.out.println("Get4:");
186: OMgr om = new OMgr();
187: HashCache hc = new HashCache(om, 10);
188: int requests[] = { 0, 1, 2, 3, 0, 2, // looks like 1,3,0,2
189: 4, 5, 6, 0, 3, 6, // looks like 1,2,4,5,0,3,6
190: 7, 8, 9, 10, 11, 1, 2, // recreated, looks like 0,3,6,7,8,9,10,11,1,2
191: 12, 13, 14, 15, 16, 17, 18, 19, 0 // recreated, looks like 2,12,13,14,15,16,17,18,19,0
192: };
193: for (int i = 0; i < requests.length; ++i) {
194: hc.get(new Integer(requests[i]));
195: //hc.printChain();
196: }
197: assertEquals(
198: "The mgr was not called for the creation correctly.",
199: 23, om.getCreationCount());
200: assertEquals("The mgr was not called for the clean-up.", 13, om
201: .getCloseCount());
202: assertEquals("The overflow count is wrong.", 13, hc
203: .getOverflowCount());
204: assertEquals("The size is wrong.", 10, hc.getSize());
205: }
206:
207: public void testResetOverflowCount1() {
208: System.out.println("Get4:");
209: OMgr om = new OMgr();
210: HashCache hc = new HashCache(om, 10);
211: hc.resetOverflowCount();
212: }
213: }
|