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