1 |
| package org.jboss.cache.api; |
2 |
| |
3 |
| import junit.framework.TestCase; |
4 |
| import org.jboss.cache.AbstractCacheListener; |
5 |
| import org.jboss.cache.Cache; |
6 |
| import org.jboss.cache.CacheImpl; |
7 |
| import org.jboss.cache.CacheListener; |
8 |
| import org.jboss.cache.DefaultCacheFactory; |
9 |
| import org.jboss.cache.DummyTransactionManagerLookup; |
10 |
| import org.jboss.cache.Fqn; |
11 |
| import org.jboss.cache.Node; |
12 |
| import org.jboss.cache.Region; |
13 |
| import org.jboss.cache.config.Configuration; |
14 |
| import org.jboss.cache.config.ConfigurationException; |
15 |
| |
16 |
| import java.util.ArrayList; |
17 |
| import java.util.HashMap; |
18 |
| import java.util.List; |
19 |
| import java.util.Map; |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class CacheAPITest extends TestCase |
27 |
| { |
28 |
| private Cache cache; |
29 |
| protected boolean optimistic; |
30 |
| |
31 |
22
| protected void setUp() throws Exception
|
32 |
| { |
33 |
| |
34 |
22
| cache = DefaultCacheFactory.getInstance().createCache("META-INF/local-tx-service.xml", false);
|
35 |
22
| cache.getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC);
|
36 |
22
| cache.start();
|
37 |
| } |
38 |
| |
39 |
22
| protected void tearDown()
|
40 |
| { |
41 |
22
| if (cache != null) cache.stop();
|
42 |
| } |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
2
| public void testConfiguration()
|
49 |
| { |
50 |
2
| Configuration c = cache.getConfiguration();
|
51 |
2
| assertEquals(Configuration.CacheMode.LOCAL, c.getCacheMode());
|
52 |
2
| assertEquals(DummyTransactionManagerLookup.class.getName(), c.getTransactionManagerLookupClass());
|
53 |
| |
54 |
| |
55 |
2
| try
|
56 |
| { |
57 |
2
| c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
58 |
0
| fail("Should have thrown an Exception");
|
59 |
| } |
60 |
| catch (ConfigurationException e) |
61 |
| { |
62 |
| |
63 |
| } |
64 |
| |
65 |
| |
66 |
2
| c.setLockAcquisitionTimeout(100);
|
67 |
| } |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
2
| public void testGetRoot()
|
73 |
| { |
74 |
| |
75 |
| |
76 |
| } |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
2
| public void testCacheListeners()
|
85 |
| { |
86 |
2
| assertEquals(0, cache.getCacheListeners().size());
|
87 |
| |
88 |
| |
89 |
2
| final List events = new ArrayList();
|
90 |
| |
91 |
2
| CacheListener dummy = new AbstractCacheListener()
|
92 |
| { |
93 |
4
| public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
|
94 |
| { |
95 |
2
| if (pre) events.add("Created");
|
96 |
| } |
97 |
| }; |
98 |
| |
99 |
2
| cache.addCacheListener(dummy);
|
100 |
| |
101 |
2
| assertEquals(1, cache.getCacheListeners().size());
|
102 |
| |
103 |
2
| cache.getRoot().addChild(Fqn.fromString("/blah"));
|
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
2
| assertEquals(1, events.size());
|
109 |
| |
110 |
2
| cache.removeCacheListener(dummy);
|
111 |
| |
112 |
2
| assertEquals(0, cache.getCacheListeners().size());
|
113 |
| } |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
2
| public void testFqnBasedCacheListeners()
|
120 |
| { |
121 |
2
| try
|
122 |
| { |
123 |
2
| cache.getCacheListeners(Fqn.ROOT);
|
124 |
0
| fail("Fqn-based cache listener operation should throw an exception");
|
125 |
| } |
126 |
| catch (Exception e) |
127 |
| { |
128 |
| |
129 |
| } |
130 |
| |
131 |
2
| try
|
132 |
| { |
133 |
2
| cache.addCacheListener(Fqn.ROOT, new AbstractCacheListener()
|
134 |
| { |
135 |
| }); |
136 |
0
| fail("Fqn-based cache listener operation should throw an exception");
|
137 |
| } |
138 |
| catch (Exception e) |
139 |
| { |
140 |
| |
141 |
| } |
142 |
| |
143 |
2
| try
|
144 |
| { |
145 |
2
| cache.removeCacheListener(Fqn.ROOT, new AbstractCacheListener()
|
146 |
| { |
147 |
| }); |
148 |
0
| fail("Fqn-based cache listener operation should throw an exception");
|
149 |
| } |
150 |
| catch (Exception e) |
151 |
| { |
152 |
| |
153 |
| } |
154 |
| } |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
2
| public void testConvenienceMethods()
|
164 |
| { |
165 |
2
| Fqn fqn = Fqn.fromString("/test/fqn");
|
166 |
2
| Object key = "key", value = "value";
|
167 |
2
| Map data = new HashMap();
|
168 |
2
| data.put(key, value);
|
169 |
| |
170 |
2
| assertNull(cache.get(fqn, key));
|
171 |
| |
172 |
2
| cache.put(fqn, key, value);
|
173 |
| |
174 |
2
| assertEquals(value, cache.get(fqn, key));
|
175 |
| |
176 |
2
| cache.remove(fqn, key);
|
177 |
| |
178 |
2
| assertNull(cache.get(fqn, key));
|
179 |
| |
180 |
2
| cache.put(fqn, data);
|
181 |
| |
182 |
2
| System.out.println(((CacheImpl) cache).printDetails());
|
183 |
| |
184 |
2
| assertEquals(value, cache.get(fqn, key));
|
185 |
| } |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
0
| public void nodeConvenienceNodeRemoval()
|
192 |
| { |
193 |
| |
194 |
0
| Fqn fqn = Fqn.fromString("/test/fqn");
|
195 |
0
| cache.getRoot().addChild(fqn);
|
196 |
0
| assertTrue(cache.getRoot().hasChild(fqn));
|
197 |
| |
198 |
0
| assertEquals(true, cache.removeNode(fqn));
|
199 |
0
| assertFalse(cache.getRoot().hasChild(fqn));
|
200 |
0
| assertEquals(false, cache.removeNode(fqn));
|
201 |
| } |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
2
| public void testEvict()
|
207 |
| { |
208 |
2
| Fqn one = Fqn.fromString("/one");
|
209 |
2
| Fqn two = Fqn.fromString("/one/two");
|
210 |
2
| Object key = "key", value = "value";
|
211 |
| |
212 |
2
| cache.getRoot().addChild(one).put(key, value);
|
213 |
2
| cache.getRoot().addChild(two).put(key, value);
|
214 |
| |
215 |
2
| assertTrue(cache.getRoot().hasChild(one));
|
216 |
2
| assertFalse(cache.getRoot().getChild(one).getData().isEmpty());
|
217 |
2
| assertTrue(cache.getRoot().hasChild(two));
|
218 |
2
| assertFalse(cache.getRoot().getChild(two).getData().isEmpty());
|
219 |
| |
220 |
| |
221 |
2
| cache.evict(two, false);
|
222 |
| |
223 |
2
| assertTrue(cache.getRoot().hasChild(one));
|
224 |
2
| assertTrue(cache.getRoot().getChild(one).getKeys().contains(key));
|
225 |
2
| assertFalse(cache.getRoot().hasChild(two));
|
226 |
| |
227 |
| |
228 |
2
| cache.getRoot().addChild(two).put(key, value);
|
229 |
| |
230 |
| |
231 |
2
| cache.evict(one, false);
|
232 |
| |
233 |
| |
234 |
2
| assertTrue(cache.getRoot().hasChild(one));
|
235 |
2
| assertFalse(cache.getRoot().getChild(one).getKeys().contains(key));
|
236 |
| |
237 |
| |
238 |
2
| assertTrue(cache.getRoot().hasChild(two));
|
239 |
2
| assertTrue(cache.getRoot().getChild(two).getKeys().contains(key));
|
240 |
| } |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
2
| public void testEvictRecursive()
|
247 |
| { |
248 |
2
| Fqn one = Fqn.fromString("/one");
|
249 |
2
| Fqn two = Fqn.fromString("/one/two");
|
250 |
2
| Object key = "key", value = "value";
|
251 |
| |
252 |
2
| cache.getRoot().addChild(one).put(key, value);
|
253 |
2
| cache.getRoot().addChild(two).put(key, value);
|
254 |
| |
255 |
2
| assertTrue(cache.getRoot().hasChild(one));
|
256 |
2
| assertFalse(cache.getRoot().getChild(one).getData().isEmpty());
|
257 |
2
| assertTrue(cache.getRoot().hasChild(two));
|
258 |
2
| assertFalse(cache.getRoot().getChild(two).getData().isEmpty());
|
259 |
| |
260 |
| |
261 |
2
| cache.evict(two, true);
|
262 |
| |
263 |
2
| assertTrue(cache.getRoot().hasChild(one));
|
264 |
2
| assertFalse(cache.getRoot().getChild(one).getData().isEmpty());
|
265 |
2
| assertFalse(cache.getRoot().hasChild(two));
|
266 |
| |
267 |
| |
268 |
2
| cache.getRoot().addChild(two).put(key, value);
|
269 |
| |
270 |
| |
271 |
2
| cache.evict(one, true);
|
272 |
| |
273 |
2
| assertFalse(cache.getRoot().hasChild(one));
|
274 |
2
| assertFalse(cache.getRoot().hasChild(two));
|
275 |
| } |
276 |
| |
277 |
| |
278 |
| |
279 |
| |
280 |
| |
281 |
2
| public void testRegion()
|
282 |
| { |
283 |
2
| Region rootRegion = cache.getRegion(Fqn.ROOT, true);
|
284 |
2
| assertNotNull(rootRegion);
|
285 |
2
| assertSame(rootRegion, cache.getRegion(Fqn.ROOT, true));
|
286 |
| |
287 |
2
| Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"), true);
|
288 |
2
| assertNotNull(otherRegion);
|
289 |
2
| assertSame(otherRegion, cache.getRegion(Fqn.fromString("/other/region"), true));
|
290 |
| } |
291 |
| |
292 |
| |
293 |
| |
294 |
| |
295 |
2
| public void testParentRegion1()
|
296 |
| { |
297 |
2
| Region rootRegion = cache.getRegion(Fqn.ROOT, true);
|
298 |
2
| assertNotNull(rootRegion);
|
299 |
2
| assertSame(rootRegion, cache.getRegion(Fqn.ROOT, false));
|
300 |
| |
301 |
2
| Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"), false);
|
302 |
| |
303 |
| |
304 |
2
| assertSame(otherRegion, rootRegion);
|
305 |
| } |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
2
| public void testParentRegion2()
|
311 |
| { |
312 |
2
| Region rootRegion = cache.getRegion(Fqn.ROOT, true);
|
313 |
2
| Region parentRegion = cache.getRegion(Fqn.fromString("/parent"), true);
|
314 |
2
| assertNotSame("parentRegion should be a new region in its own right", rootRegion, parentRegion);
|
315 |
| |
316 |
2
| Region childRegion = cache.getRegion(Fqn.fromString("/parent/region"), false);
|
317 |
2
| assertSame("Expecting the same region as parentRegion", childRegion, parentRegion);
|
318 |
| } |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
2
| public void testNullRegion()
|
325 |
| { |
326 |
2
| Region myRegion = cache.getRegion(Fqn.fromString("/myregion"), true);
|
327 |
2
| assertNotNull(myRegion);
|
328 |
2
| assertSame(myRegion, cache.getRegion(Fqn.fromString("/myregion"), false));
|
329 |
| |
330 |
2
| Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"), false);
|
331 |
| |
332 |
2
| assertNotNull(otherRegion);
|
333 |
2
| assertEquals(Fqn.ROOT, otherRegion.getFqn());
|
334 |
| } |
335 |
| |
336 |
| } |