KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shiftone > cache > util > TestCaseBase


1 package org.shiftone.cache.util;
2
3
4
5 import junit.framework.TestCase;
6 import org.shiftone.cache.Cache;
7 import org.shiftone.cache.CacheFactory;
8
9 import java.util.Random JavaDoc;
10
11
12 /**
13  * Class TestCaseBase
14  *
15  *
16  * @author <a HREF="mailto:jeff@shiftone.org">Jeff Drost</a>
17  * @version $Revision: 1.13 $
18  */

19 public abstract class TestCaseBase extends TestCase
20 {
21
22     /// private static final Logger LOG = Logger.getLogger(TestCaseBase.class);
23
static Random JavaDoc random = new Random JavaDoc(System.currentTimeMillis());
24     private CacheFactory cacheFactory = getCacheFactory();
25
26     /**
27      * Constructor TestCaseBase
28      *
29      *
30      * @param name
31      */

32     public TestCaseBase(String JavaDoc name)
33     {
34         super(name);
35     }
36
37
38     /**
39      * Method getCacheFactory
40      */

41     public abstract CacheFactory getCacheFactory();
42
43
44     /**
45      * Method newInstance
46      */

47     public Cache newCache(long timeoutMilliSeconds, int maxSize)
48     {
49         return cacheFactory.newInstance("testCaseBase", timeoutMilliSeconds, maxSize);
50     }
51
52
53     /**
54      * Method test10000Puts
55      */

56     public void test10000UniquePuts()
57     {
58
59         Cache cache = newCache(1000 * 60, 1000);
60
61         for (int i = 0; i < 10000; i++)
62         {
63             cache.addObject("k-" + i, "v-" + i);
64         }
65
66         assertEquals(1000, cache.size());
67         cache.clear();
68         assertEquals(0, cache.size());
69     }
70
71
72     /**
73      * Method test10000Puts
74      */

75     public void test10000Puts()
76     {
77
78         Cache cache = newCache(1000 * 60, 1000);
79
80         for (int i = 0; i < 10000; i++)
81         {
82             cache.addObject("k-" + (i % 500), "v-" + i);
83         }
84
85         assertEquals(500, cache.size());
86         cache.clear();
87         assertEquals(0, cache.size());
88     }
89
90
91     /**
92      * Method test10000Puts
93      */

94     public void test1000Puts10000GetsSameKey()
95     {
96
97         Cache cache = newCache(1000 * 60, 500);
98
99         for (int i = 0; i < 1000; i++)
100         {
101             cache.addObject("k-" + i, "v-" + i);
102         }
103
104         assertEquals(500, cache.size());
105
106         for (int i = 0; i < 10000; i++)
107         {
108             assertNotNull(cache.getObject("k-987"));
109         }
110     }
111
112
113     /**
114      * Method test10000Puts99999Gets
115      */

116     public void test10000Puts99999Gets()
117     {
118
119         Cache cache = newCache(1000 * 60, 1000);
120         String JavaDoc key = null;
121
122         cache.addObject("k-0", "v-0");
123
124         for (int i = 0; i < 10000; i++)
125         {
126             key = "k-" + (i % 200);
127
128             cache.addObject(key, "v-" + i);
129             assertNotNull(key, cache.getObject(key));
130         }
131
132         assertEquals(200, cache.size());
133     }
134
135
136     /**
137      * Method test1Put10000GetsSameKey10Puts
138      */

139     public void test1Put10000GetsSameKey2Puts()
140     {
141
142         Cache cache = newCache(1000 * 60, 2);
143
144         cache.addObject("x", "x");
145         cache.addObject("y", "y");
146
147         for (int i = 0; i < 10000; i++)
148         {
149             assertNotNull(cache.getObject("x"));
150             assertNotNull(cache.getObject("y"));
151         }
152
153         assertEquals(2, cache.size());
154         cache.addObject("a", "a");
155         cache.addObject("b", "b");
156         assertEquals(2, cache.size());
157         assertNotNull(cache.getObject("b"));
158     }
159
160
161     /**
162      * Method testCacheOfSize1
163      */

164     public void testCacheOfSize1()
165     {
166
167         Cache cache = newCache(1000 * 60, 1);
168
169         for (int i = 0; i < 20; i++)
170         {
171             cache.addObject("k-" + i, "v-" + i);
172         }
173
174         assertEquals(1, cache.size());
175     }
176
177
178     /*
179         public void testRoyLuNullPointerException1()
180         {
181
182             Cache cache = newCache(1000, 5);
183             AbstractPolicyCache base = (AbstractPolicyCache) cache;
184
185             base.addObject("a", "b");
186
187             // cache contains one element
188             base.removeLeastValuableNode();
189
190             // cache is empty
191             base.removeLeastValuableNode();
192         }
193     */

194
195     /**
196      * Method randKey
197      */

198     public static String JavaDoc randKey()
199     {
200         return String.valueOf(Math.abs(random.nextInt()));
201     }
202
203
204     /**
205      * Method randValue
206      */

207     public static String JavaDoc randValue()
208     {
209
210         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
211
212         for (int i = 0; i < 100; i++)
213         {
214             sb.append(randKey());
215         }
216
217         return sb.toString();
218     }
219 }
220
Popular Tags