1
2
3
4
5
6
7
8 /
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class CachingTest extends LedgeTestCase
68 {
69 private CacheFactorySPI caching;
70
71 private Notification notification;
72
73 public void setUp()
74 throws Exception
75 {
76 Context context = new Context();
77 Valve cleanup = null;
78 Configuration config = new DefaultConfiguration("config", "", "/config");
79 Logger logger = new Log4JLogger(org.apache.log4j.Logger.getLogger(getClass()));
80 ThreadPool pool = new ThreadPool(cleanup, context, config, logger);
81 DataSource dataSource = getDataSource();
82 IdGenerator idGenerator = new IdGenerator(dataSource);
83 JotmTransaction transaction = new JotmTransaction(0, 120, new Context(), logger, null);
84 Database database = new DefaultDatabase(dataSource, idGenerator, transaction);
85 Persistence persistence = new DefaultPersistence(database, logger);
86 notification = new Notification();
87
88 FileSystemProvider lfs = new LocalFileSystemProvider("local", "src/test/resources");
89 FileSystemProvider cfs = new ClasspathFileSystemProvider("classpath",
90 getClass().getClassLoader());
91 FileSystem fs = new FileSystem(new FileSystemProvider[] { lfs, cfs }, 4096, 4096);
92 config = getConfig(fs, "config/org.objectledge.cache.CacheFactory.xml");
93 caching = new DefaultCacheFactory(config, logger, pool, notification, persistence);
94 }
95
96 public void testCaching()
97 {
98 assertNotNull(caching);
99 }
100
101 public void testGetMap()
102 {
103 Map map = caching.getMap("LRUMap");
104 assertNotNull(map);
105 assertNotNull(caching.getMap("foo"));
106 try
107 {
108 caching.getMap("bar");
109 fail("should throw the exception");
110 }
111 catch (IllegalArgumentException e)
112 {
113
114 }
115 }
116
117
118
119
120 public void testGetInstanceString()
121 {
122 Object cache = caching.getInstance("instance1");
123 assertNotNull(cache);
124 assertNotNull(((DelegateMap)cache).getDelegate());
125 assertNotNull(caching.getInstance("instance2"));
126 try
127 {
128 caching.getInstance("not_configured");
129 fail("should throw the exception");
130 }
131 catch(IllegalArgumentException e)
132 {
133
134 }
135 }
136
137
138
139
140 public void testGetInstanceStringString()
141 throws Exception
142 {
143 Object cache = caching.getInstance("not_configured","alias1");
144 assertNotNull(cache);
145 assertNotNull(((DelegateMap)cache).getDelegate());
146 Object cache2 = caching.getInstance("not_configured","aliasXXX");
147 assertNotNull(cache2);
148 assertEquals(cache, cache2);
149 assertEquals(caching.getInstance("not_configured"),cache);
150 try
151 {
152 caching.getInstance("not_configured_2","alias_not_configured");
153 fail("should throw the exception");
154 }
155 catch(IllegalArgumentException e)
156 {
157
158 }
159 }
160
161 public void testGetMaps()
162 {
163 assertNotNull(caching.getHashMap());
164 assertNotNull(caching.getLRUMap(4));
165 assertNotNull(caching.getSoftMap(4));
166 assertNotNull(caching.getStatisticsMap("xxx",new HashMap()));
167 assertNotNull(caching.getTimeoutMap(1000));
168 ValueFactory valueFactory = caching.getPersitenceValueFactory(TestValue.class);
169 assertNotNull(valueFactory);
170 assertNotNull(caching.getFactoryMap(valueFactory, new HashMap()));
171 assertNotNull(caching.getDistributedMap("yyy", new HashMap()));
172 }
173
174
175
176 public void testHash()
177 {
178 caching.getInstance("hash");
179 }
180
181 public void testTimeout()
182 {
183 try
184 {
185 Map map = caching.getInstance("timeout");
186 map.put("k1","v");
187 Thread.sleep(500);
188 assertNotNull("@500", map.get("k1"));
189 Thread.sleep(1500);
190 assertNull("@2000", map.get("k1"));
191 }
192 catch(InterruptedException e)
193 {
194 fail("thread was interrupted?!");
195 }
196 }
197
198 public void testLRU()
199 {
200 Map map = caching.getInstance("LRU");
201 map.put("k1","v");
202 map.put("k2","v");
203 map.put("k3","v");
204 map.put("k4","v");
205 map.put("k5","v");
206 map.get("k3");
207 map.get("k4");
208 map.get("k2");
209 map.get("k1");
210 map.get("k5");
211 map.put("k6", "v");
212 assertNotNull("k1 in", map.get("k1"));
213 assertNotNull("k2 in", map.get("k2"));
214 assertNull("k3 out", map.get("k3"));
215 assertNotNull("k4 in", map.get("k4"));
216 assertNotNull("k5 in", map.get("k5"));
217 assertNotNull("k6 in",map.get("k6"));
218 }
219
220 public void testSoft()
221 {
222 Map map = caching.getInstance("soft");
223
224
225 int count = 100;
226 for(int i=0; i<count; i++)
227 {
228 map.put(new Integer(i), new byte[1024*1024]);
229 }
230 for(int i=1; i<=5; i++)
231 {
232 assertNotNull("last - "+i, map.get(new Integer(count-i)));
233 }
234 for(int i=0; i<count; i++)
235 {
236 map.remove(new Integer(i));
237 }
238 }
239
240 private String stats = "statistics: 3 items, 3 requests, 2 hits, 1 misses, 67% hit ratio\n";
241
242 public void testStatistics()
243 {
244 Map map = caching.getInstance("statistics");
245 map.put("k1","v");
246 map.put("k2","v");
247 map.put("k3","v");
248 map.get("k1");
249 map.get("k2");
250 map.get("k4");
251 assertEquals(stats,((StatisticsMap)map).getStatistics());
252 }
253
254 public void testDistributed()
255 {
256 Map map = caching.getInstance("distributed");
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 }
275
276 public void testFactory()
277 {
278 Map map = caching.getInstance("factory");
279 /***
280 TestValue v = (TestValue)map.get(new Long(1));
281 assertNotNull("v@1",v);
282 assertEquals("apples",v.getName());
283 v = (TestValue)map.get(new Long(11));
284 assertNull("v@11",v);
285 */
286 }
287
288 public void testGlobalStatistics()
289 {
290 StringWriter sw = new StringWriter();
291 PrintWriter pw = new PrintWriter(sw, true);
292 caching.getStatus(pw);
293 String status = sw.getBuffer().toString();
294
295
296 }
297
298 public void testConfigAlias()
299 {
300 Map map = caching.getInstance("shared");
301 assertTrue("shared=LRUMap", LRUMap.class.isAssignableFrom(map.getClass()));
302 }
303
304 public void testCustom()
305 {
306 Map map = caching.getInstance("custom");
307 assertTrue("custom=LRUMap", LRUMap.class.isAssignableFrom(map.getClass()));
308 }
309
310 private int updateCounter = 0;
311
312 private class Delayed
313 implements DelayedUpdate
314 {
315 public long getUpdateLatency()
316 {
317 return 100;
318 }
319
320 public void update()
321 {
322 updateCounter++;
323 }
324 }
325
326 public void testDelayedUpdate()
327 {
328 try
329 {
330 Delayed d = new Delayed();
331 caching.register(d);
332 Thread.sleep(200);
333 caching.register(d);
334 Thread.sleep(200);
335 caching.register(d);
336 Thread.sleep(200);
337 assertEquals("delayed 3", 3, updateCounter);
338 caching.register(d);
339 Thread.sleep(20);
340 caching.register(d);
341 Thread.sleep(20);
342 caching.register(d);
343 Thread.sleep(200);
344 assertEquals("delayed 4", 4, updateCounter);
345 }
346 catch(InterruptedException e)
347 {
348
349 }
350 }
351
352 public void testGetNotification()
353 {
354 assertNotNull(caching.getNotification());
355 }
356
357
358 private DataSource getDataSource() throws Exception
359 {
360 DefaultConfiguration conf = new DefaultConfiguration("config", "", "/");
361 DefaultConfiguration url = new DefaultConfiguration("url", "", "/config");
362 url.setValue("jdbc:hsqldb:.");
363 conf.addChild(url);
364 DefaultConfiguration user = new DefaultConfiguration("user", "", "/config");
365 user.setValue("sa");
366 conf.addChild(user);
367 return new HsqldbDataSource(conf);
368 }
369 }