1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.client.bindings.cache;
20
21 import junit.framework.TestCase;
22
23 import org.apache.chemistry.opencmis.client.bindings.cache.impl.CacheImpl;
24 import org.apache.chemistry.opencmis.client.bindings.cache.impl.ContentTypeCacheLevelImpl;
25 import org.apache.chemistry.opencmis.client.bindings.cache.impl.LruCacheLevelImpl;
26 import org.apache.chemistry.opencmis.client.bindings.cache.impl.MapCacheLevelImpl;
27
28
29
30
31 public class CacheTest extends TestCase {
32
33 public static final String MAP_CACHE_LEVEL = "org.apache.chemistry.opencmis.client.bindings.cache.impl.MapCacheLevelImpl";
34 public static final String LRU_CACHE_LEVEL = "org.apache.chemistry.opencmis.client.bindings.cache.impl.LruCacheLevelImpl";
35
36 public void testCache() {
37 Cache cache;
38
39 cache = new CacheImpl();
40 cache.initialize(new String[] { MAP_CACHE_LEVEL, LRU_CACHE_LEVEL, MAP_CACHE_LEVEL, MAP_CACHE_LEVEL });
41
42 String value1 = "value1";
43 String value2 = "value2";
44 String value3 = "value3";
45 Object valueObj;
46
47
48 cache.put(value1, "l1", "l2a", "l3", "l4");
49 cache.put(value2, "l1", "l2b", "l3", "l4");
50 cache.put(value3, "l1", "l2c", "l3", "l4");
51
52 valueObj = cache.get("l1", "l2a", "l3", "l4");
53 assertTrue(valueObj instanceof String);
54 assertSame(value1, valueObj);
55
56 valueObj = cache.get("l1", "l2b", "l3", "l4");
57 assertTrue(valueObj instanceof String);
58 assertSame(value2, valueObj);
59
60 valueObj = cache.get("l1", "l2c", "l3", "l4");
61 assertTrue(valueObj instanceof String);
62 assertSame(value3, valueObj);
63
64
65 cache.remove("l1", "l2", "l3", "l4");
66 valueObj = cache.get("l1", "l2", "l3", "l4");
67 assertNull(valueObj);
68
69
70 cache.put(value1, "l1", "l2", "l3", "l4");
71 valueObj = cache.get("l1", "l2", "l3", "l4");
72 assertTrue(valueObj instanceof String);
73 assertSame(value1, valueObj);
74
75
76 cache.remove("l1", "l2");
77 valueObj = cache.get("l1", "l2", "l3", "l4");
78 assertNull(valueObj);
79 }
80
81 public void testCacheBadUsage() {
82 Cache cache;
83
84 cache = new CacheImpl();
85 cache.initialize(new String[] { MAP_CACHE_LEVEL, LRU_CACHE_LEVEL, MAP_CACHE_LEVEL, MAP_CACHE_LEVEL });
86
87
88 try {
89 cache.put("value", "l1", "l2", "l3");
90 } catch (IllegalArgumentException e) {
91 }
92
93
94 try {
95 cache.put("value", "l1", "l2", "l3", "l4", "l5");
96 } catch (IllegalArgumentException e) {
97 }
98
99
100 assertNull(cache.get((String[]) null));
101 }
102
103 public void testCacheConfig() {
104 Cache cache;
105
106
107 try {
108 cache = new CacheImpl();
109 cache.initialize(new String[] {});
110 } catch (IllegalArgumentException e) {
111 }
112
113
114 try {
115 cache = new CacheImpl();
116 cache.initialize(null);
117 } catch (IllegalArgumentException e) {
118 }
119
120
121 try {
122 cache = new CacheImpl();
123 cache.initialize(new String[] { "this.is.not.a.valid.class" });
124 } catch (IllegalArgumentException e) {
125 }
126
127
128 try {
129 cache = new CacheImpl();
130 cache.initialize(new String[] { "org.apache.chemistry.opencmis.client.provider.cache.CacheTest" });
131 } catch (IllegalArgumentException e) {
132 }
133 }
134
135 public void testMapCache() {
136 Cache cache;
137
138 cache = new CacheImpl();
139 cache.initialize(new String[] { MAP_CACHE_LEVEL + " " + MapCacheLevelImpl.CAPACITY + "=10,"
140 + MapCacheLevelImpl.LOAD_FACTOR + "=0.5" });
141
142 for (int i = 0; i < 100; i++) {
143 cache.put("value" + i, "key" + i);
144 }
145
146 for (int i = 0; i < 100; i++) {
147 Object valueObj = cache.get("key" + i);
148 assertTrue(valueObj instanceof String);
149 assertEquals("value" + i, valueObj);
150 }
151 }
152
153 public void testURLCache() {
154 Cache cache;
155
156 cache = new CacheImpl();
157 cache.initialize(new String[] { LRU_CACHE_LEVEL + " " + LruCacheLevelImpl.MAX_ENTRIES + "=10" });
158
159 for (int i = 0; i < 100; i++) {
160 cache.put("value" + i, "key" + i);
161 }
162
163 for (int i = 0; i < 90; i++) {
164 Object valueObj = cache.get("key" + i);
165 assertNull(valueObj);
166 }
167
168 for (int i = 90; i < 100; i++) {
169 Object valueObj = cache.get("key" + i);
170 assertTrue(valueObj instanceof String);
171 assertEquals("value" + i, valueObj);
172 }
173 }
174
175 public void XtestFallback() {
176 Cache cache;
177
178 cache = new CacheImpl();
179 cache.initialize(new String[] { MAP_CACHE_LEVEL + " " + MapCacheLevelImpl.CAPACITY + "=10,"
180 + MapCacheLevelImpl.LOAD_FACTOR + "=0.5" });
181
182 cache.put("value1", new String[] { null });
183 cache.put("value2", "key2");
184
185 assertEquals("value1", cache.get(new String[] { null }));
186 assertEquals("value2", cache.get("key2"));
187 assertEquals("value1", cache.get("key3"));
188 }
189
190 public void testContentTypeCache() {
191 ContentTypeCacheLevelImpl cl = new ContentTypeCacheLevelImpl();
192 cl.initialize(null);
193
194 String type1 = "type1";
195
196 cl.put(type1, "text/plain; param1=test; charset=UTF-8");
197
198 assertEquals(type1, cl.get("text/plain; param1=test; charset=UTF-8"));
199 assertEquals(type1, cl.get("text/plain; param1=test; charset=utf-8"));
200 assertEquals(type1, cl.get("text/plain; charset=utf-8; param1=test"));
201 assertEquals(type1, cl.get("text/plain; charset=utf-8; param1=test;"));
202 assertEquals(type1, cl.get("text/plain;charset=utf-8;param1=test"));
203 assertEquals(type1, cl.get("text/plain;\tcharset=utf-8; param1=test"));
204 assertEquals(type1, cl.get("text/plain; charset=\"utf-8\"; param1=test;"));
205
206 assertNull(cl.get("text/plain; param1=blah; charset=UTF-8"));
207 assertNull(cl.get("text/plain; param1=test; charset=us-ascii"));
208 }
209 }