KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > datastruct > v1 > HashCacheUTest


1 /*
2  * @(#)HashCacheUTest.java
3  *
4  * Copyright (C) 2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.util.datastruct.v1;
28
29 import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35 /**
36  *
37  *
38  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
39  * @since April 17, 2001 (Alpha 0.9.0)
40  * @version $Date: 2003/05/23 20:57:00 $
41  */

42 public class HashCacheUTest extends TestCase
43 {
44     private static final Class JavaDoc THIS_CLASS = HashCacheUTest.class;
45     
46     public HashCacheUTest( String JavaDoc name )
47     {
48         super( name );
49     }
50     
51     public static Test suite()
52     {
53         TestSuite suite = new TestSuite( THIS_CLASS );
54         
55         return suite;
56     }
57     
58     public static void main( String JavaDoc[] args )
59     {
60         String JavaDoc[] name = { THIS_CLASS.getName() };
61         
62         // junit.textui.TestRunner.main( name );
63
// junit.swingui.TestRunner.main( name );
64

65         junit.textui.TestRunner.main( name );
66     }
67     
68     protected void setUp() throws Exception JavaDoc
69     {
70         super.setUp();
71         
72         // set ourself up
73
}
74     
75     
76     protected void tearDown() throws Exception JavaDoc
77     {
78         // tear ourself down
79

80         
81         super.tearDown();
82     }
83     
84     
85     public class OMgr implements HashCache.ObjectManager
86     {
87         private int count = 0;
88         private int closecount = 0;
89         public synchronized Object JavaDoc createObject( Object JavaDoc key )
90         {
91             return "["+key+"] "+(++count);
92         }
93         
94         public synchronized void cleanUpObject( Object JavaDoc key, Object JavaDoc value )
95         {
96             ++this.closecount;
97         }
98         
99         public int getCreationCount()
100         {
101             return this.count;
102         }
103         
104         public int getCloseCount()
105         {
106             return this.closecount;
107         }
108     }
109     
110     
111     
112     public void testConstructor1()
113     {
114         HashCache oc = new HashCache( new OMgr(), 10 );
115         assertEquals( "Size must be instantiation size.",
116             10, oc.getMaxSize() );
117         assertEquals( "Overflows must be at zero.",
118             0, oc.getOverflowCount() );
119     }
120     
121     
122     public void testConstructor2()
123     {
124         try
125         {
126             new HashCache( null, 10 );
127             fail("Did not throw IllegalArgumentException.");
128         }
129         catch (IllegalArgumentException JavaDoc iae)
130         {
131             // test exception
132
}
133     }
134     
135     
136     public void testConstructor3()
137     {
138         try
139         {
140             new HashCache( null, 0 );
141             fail("Did not throw IllegalArgumentException.");
142         }
143         catch (IllegalArgumentException JavaDoc iae)
144         {
145             // test exception
146
}
147     }
148     
149     
150     public void testConstructor4()
151     {
152         try
153         {
154             new HashCache( new OMgr(), 1 );
155             fail("Did not throw IllegalArgumentException.");
156         }
157         catch (IllegalArgumentException JavaDoc iae)
158         {
159             // test exception
160
}
161     }
162     
163     
164     public void testConstructor5()
165     {
166         try
167         {
168             new HashCache( new OMgr(), -1 );
169             fail("Did not throw IllegalArgumentException.");
170         }
171         catch (IllegalArgumentException JavaDoc iae)
172         {
173             // test exception
174
}
175     }
176     
177     
178     public void testGet1()
179     {
180         OMgr om = new OMgr();
181         HashCache hc = new HashCache( om, 10 );
182         try
183         {
184             hc.get( null );
185         }
186         catch (NullPointerException JavaDoc npe)
187         {
188             // test exception?
189
}
190     }
191     
192     
193     public void testGet2()
194     {
195         OMgr om = new OMgr();
196         HashCache hc = new HashCache( om, 10 );
197         Object JavaDoc o = hc.get( "a" );
198         assertNotNull(
199             "get returned null.",
200             o );
201         assertTrue(
202             "incorrect type returned.",
203             o instanceof String JavaDoc );
204         assertEquals(
205             "Incorrect value of returned object.",
206             "[a] 1",
207             (String JavaDoc)o );
208         assertEquals(
209             "The mgr reported the creation.",
210             1,
211             om.getCreationCount() );
212         
213         Object JavaDoc o2 = hc.get( "a" );
214         assertSame(
215             "Did not return the same object.",
216             o,
217             o2 );
218         assertEquals(
219             "The mgr was not called for the creation.",
220             1,
221             om.getCreationCount() );
222     }
223     
224     
225     public void testGet3()
226     {
227         OMgr om = new OMgr();
228         HashCache hc = new HashCache( om, 2 );
229         Object JavaDoc o = hc.get( "a" );
230         Object JavaDoc o2 = hc.get( "b" );
231         Object JavaDoc o3 = hc.get( "c" );
232         assertEquals(
233             "The mgr was not called for the creation correctly.",
234             3,
235             om.getCreationCount() );
236         assertEquals(
237             "The mgr was not called for the clean-up.",
238             1,
239             om.getCloseCount() );
240         assertEquals(
241             "The overflow count is wrong.",
242             1,
243             hc.getOverflowCount() );
244         assertEquals(
245             "The size is wrong.",
246             2,
247             hc.getSize() );
248     }
249     
250     
251     public void testGet4()
252     {
253         System.out.println("Get4:");
254         OMgr om = new OMgr();
255         HashCache hc = new HashCache( om, 10 );
256         int requests[] = {
257             0,1,2,3,
258                 0,2, // looks like 1,3,0,2
259
4,5,6,
260                 0,3,6, // looks like 1,2,4,5,0,3,6
261
7,8,9,10,11,
262                 1,2, // recreated, looks like 0,3,6,7,8,9,10,11,1,2
263
12,13,14,15,16,17,18,19,
264                 0 // recreated, looks like 2,12,13,14,15,16,17,18,19,0
265
};
266         for (int i = 0; i < requests.length; ++i)
267         {
268             hc.get( new Integer JavaDoc( requests[i] ) );
269             //hc.printChain();
270
}
271         assertEquals(
272             "The mgr was not called for the creation correctly.",
273             23,
274             om.getCreationCount() );
275         assertEquals(
276             "The mgr was not called for the clean-up.",
277             13,
278             om.getCloseCount() );
279         assertEquals(
280             "The overflow count is wrong.",
281             13,
282             hc.getOverflowCount() );
283         assertEquals(
284             "The size is wrong.",
285             10,
286             hc.getSize() );
287     }
288     
289     
290     public void testResetOverflowCount1()
291     {
292         System.out.println("Get4:");
293         OMgr om = new OMgr();
294         HashCache hc = new HashCache( om, 10 );
295         hc.resetOverflowCount();
296     }
297 }
298
Popular Tags