KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ObjectCacheUTest.java 0.9.0 17-APR-2001 - 14:40
3  *
4  * Copyright (C) 2001,,2003 2002 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/02/10 22:52:44 $
41  */

42 public class ObjectCacheUTest extends TestCase
43 {
44     private static final Class JavaDoc THIS_CLASS = ObjectCacheUTest.class;
45     
46     public ObjectCacheUTest( 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 OCreator implements ObjectCache.ObjectCreator
86     {
87         private int count = 0;
88         public synchronized Object JavaDoc createObject()
89         {
90             return "Object "+(++count);
91         }
92         
93         public int getCreationCount()
94         {
95             return count;
96         }
97     }
98     
99     
100     
101     public void testInstantiate()
102     {
103         ObjectCache oc = new ObjectCache();
104         assertEquals( "Size must be at default of UNLIMITED.",
105             oc.UNLIMITED_SIZE, oc.getMaxSize() );
106         assertEquals( "Underflows must be at zero.",
107             0, oc.getUnderflows() );
108         assertEquals( "Overflows must be at zero.",
109             0, oc.getOverflows() );
110         // this test only works because we didn't specify a creator
111
// needs to be at end to avoid underflows
112
assertTrue( "Cache should be empty after creation.", oc.get() == null );
113             
114         oc = new ObjectCache( 10 );
115         assertEquals( "Size must be at specified size.",
116             10, oc.getMaxSize() );
117         assertEquals( "Underflows must be at zero.",
118             0, oc.getUnderflows() );
119         assertEquals( "Overflows must be at zero.",
120             0, oc.getOverflows() );
121         // this test only works because we didn't specify a creator
122
// needs to be at end to avoid underflows
123
assertTrue( "Cache should be empty after creation.", oc.get() == null );
124
125         oc = new ObjectCache( Object JavaDoc.class );
126         assertEquals( "Size must be at default of UNLIMITED.",
127             oc.UNLIMITED_SIZE, oc.getMaxSize() );
128         assertEquals( "Underflows must be at zero.",
129             0, oc.getUnderflows() );
130         assertEquals( "Overflows must be at zero.",
131             0, oc.getOverflows() );
132         
133         oc = new ObjectCache( Object JavaDoc.class, 10 );
134         assertEquals( "Size must be at specified size.",
135             10, oc.getMaxSize() );
136         assertEquals( "Underflows must be at zero.",
137             0, oc.getUnderflows() );
138         assertEquals( "Overflows must be at zero.",
139             0, oc.getOverflows() );
140         
141         oc = new ObjectCache( new OCreator() );
142         assertEquals( "Size must be at default of UNLIMITED.",
143             oc.UNLIMITED_SIZE, oc.getMaxSize() );
144         assertEquals( "Underflows must be at zero.",
145             0, oc.getUnderflows() );
146         assertEquals( "Overflows must be at zero.",
147             0, oc.getOverflows() );
148         
149         oc = new ObjectCache( new OCreator(), 10 );
150         assertEquals( "Size must be at specified size.",
151             10, oc.getMaxSize() );
152         assertEquals( "Underflows must be at zero.",
153             0, oc.getUnderflows() );
154         assertEquals( "Overflows must be at zero.",
155             0, oc.getOverflows() );
156     }
157     
158     
159     public void testSimpleAdd() throws InterruptedException JavaDoc
160     {
161         ObjectCache oc = new ObjectCache();
162         Object JavaDoc o1 = new Object JavaDoc();
163         // this test only works because we didn't specify a creator
164
assertTrue( "Cache should be empty after creation.", oc.get() == null );
165         oc.putBack( o1 );
166         Object JavaDoc o2 = oc.get();
167         assertEquals( "What gets put in, needs to equal what gets put out.",
168             o1, o2 );
169         // this test only works because we didn't specify a creator
170
assertTrue( "Cache should be empty after removing.", oc.get() == null );
171     }
172     
173     public void testClassCreator()
174     {
175         ObjectCache oc = new ObjectCache( Object JavaDoc.class );
176         Object JavaDoc o1 = oc.get();
177         assertNotNull( "Cache should have created an entry.", o1 );
178         assertEquals( "Cache should have created an Object.",
179             Object JavaDoc.class, o1.getClass() );
180     }
181     
182     public void testObjectCreator()
183     {
184         ObjectCache oc = new ObjectCache( new OCreator() );
185         Object JavaDoc o1 = oc.get();
186         assertNotNull( "Cache should have created an entry.", o1 );
187         assertEquals( "Cache should have created a String.",
188             String JavaDoc.class, o1.getClass() );
189     }
190     
191     public void testFill()
192     {
193         OCreator creator = new OCreator();
194         ObjectCache oc = new ObjectCache( creator, 5, true );
195         assertEquals( "Cache did not fill itself.",
196             5, creator.getCreationCount() );
197         
198         // empty the cache
199
for (int i = 0; i < 5; i++)
200         {
201             Object JavaDoc o = oc.get();
202             assertNotNull( "Cache with creator should never return null.",
203                 o );
204         }
205         
206         // fill it up again
207
oc.fillCache();
208         assertEquals( "Cache did not fill itself.",
209             10, creator.getCreationCount() );
210     }
211     
212     public void testPutBack()
213     {
214         ObjectCache oc = new ObjectCache( 1 );
215         Object JavaDoc o1 = new Object JavaDoc();
216         Object JavaDoc o2 = new Object JavaDoc();
217         oc.putBack( o1 );
218         oc.putBack( o2 );
219         assertEquals( "ObjectCache should have an overflow.",
220             1, oc.getOverflows() );
221         Object JavaDoc o3 = oc.get();
222         assertEquals( "Cache should have returned 1st object.",
223             o1, o3 );
224         Object JavaDoc o4 = oc.get();
225         assertEquals( "Cache should have returned null.",
226             null, o4 );
227     }
228 }
229
Popular Tags