01: /*
02: * Copyright 2006, 2007 Odysseus Software GmbH
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package de.odysseus.el.tree.impl;
17:
18: import java.util.HashMap;
19: import java.util.Map;
20:
21: import de.odysseus.el.TestCase;
22: import de.odysseus.el.tree.Tree;
23:
24: public class CacheTest extends TestCase {
25: public void testPrimary() {
26: Cache cache = null;
27:
28: // check if non-caching mode works
29: cache = new Cache(0, null);
30: cache.put("1", parse("1"));
31: assertNull(cache.get("1"));
32:
33: // check if caching works, cache size 1
34: cache = new Cache(1, null);
35: cache.put("1", parse("1"));
36: assertNotNull(cache.get("1"));
37:
38: // check if removeEldest works, cache size 1
39: cache = new Cache(1, null);
40: cache.put("1", parse("1"));
41: cache.put("2", parse("2"));
42: assertNull(cache.get("1"));
43:
44: // check if removeEldest works, cache size 9
45: cache = new Cache(9, null);
46: for (int i = 1; i < 10; i++) {
47: cache.put("" + i, parse("" + i));
48: for (int j = 1; j <= i; j++) {
49: assertNotNull(cache.get("" + j));
50: }
51: }
52: cache.put("10", parse("10"));
53: assertNull(cache.get("1"));
54: for (int j = 2; j <= 10; j++) {
55: assertNotNull(cache.get("" + j));
56: }
57: }
58:
59: public void testSecondary() {
60: Map<String, Tree> map = new HashMap<String, Tree>();
61: Cache cache = new Cache(1, map);
62:
63: // check secondary cache
64: cache.put("1", parse("1"));
65: cache.put("2", parse("2"));
66: assertNotNull(cache.get("1"));
67: assertNotNull(cache.get("2"));
68: assertTrue(map.containsKey("1"));
69: assertFalse(map.containsKey("2"));
70: map.remove("1");
71: assertNull(cache.get("1"));
72: }
73: }
|