KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > api > CacheAPITest


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.DummyTransactionManagerLookup;
9 import org.jboss.cache.Fqn;
10 import org.jboss.cache.Node;
11 import org.jboss.cache.Region;
12 import org.jboss.cache.config.Configuration;
13 import org.jboss.cache.config.ConfigurationException;
14 import org.jboss.cache.factories.DefaultCacheFactory;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  * Tests the {@link org.jboss.cache.Cache} public API at a high level
23  *
24  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani</a>
25  */

26 public class CacheAPITest extends TestCase
27 {
28    private Cache cache;
29    protected boolean optimistic;
30
31    protected void setUp() throws Exception JavaDoc
32    {
33       // start a single cache instance
34
cache = DefaultCacheFactory.getInstance().createCache("META-INF/local-tx-service.xml", false);
35       cache.getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC);
36       cache.start();
37    }
38
39    protected void tearDown()
40    {
41       if (cache != null) cache.stop();
42    }
43
44
45    /**
46     * Tests that the configuration contains the values expected, as well as immutability of certain elements
47     */

48    public void testConfiguration()
49    {
50       Configuration c = cache.getConfiguration();
51       assertEquals(Configuration.CacheMode.LOCAL, c.getCacheMode());
52       assertEquals(DummyTransactionManagerLookup.class.getName(), c.getTransactionManagerLookupClass());
53
54       // note that certain values should be immutable. E.g., CacheMode cannot be changed on the fly.
55
try
56       {
57          c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
58          fail("Should have thrown an Exception");
59       }
60       catch (ConfigurationException e)
61       {
62          // expected
63
}
64
65       // others should be changeable though.
66
c.setLockAcquisitionTimeout(100);
67    }
68
69    /**
70     * Tests that getRoot() returns the same underlying object as the cache.
71     */

72    public void testGetRoot()
73    {
74       // This does not have to be true ...
75
// assertSame(cache, cache.getRoot());
76
}
77
78
79    /**
80     * Basic usage of cache listeners
81     * <p/>
82     * A more complete test that tests notifications is in org.jboss.cache.notifications
83     */

84    public void testCacheListeners()
85    {
86       assertEquals(0, cache.getCacheListeners().size());
87
88
89       final List JavaDoc events = new ArrayList JavaDoc();
90
91       CacheListener dummy = new AbstractCacheListener()
92       {
93          public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
94          {
95             if (pre) events.add("Created");
96          }
97       };
98
99       cache.addCacheListener(dummy);
100
101       assertEquals(1, cache.getCacheListeners().size());
102
103       cache.getRoot().addChild(Fqn.fromString("/blah"));
104
105       // test that the event was captured by the listener.
106

107       // FOR A FULL TEST ON NOTIFICATIONS SEE TESTS IN org.jboss.cache.notifications
108
assertEquals(1, events.size());
109
110       cache.removeCacheListener(dummy);
111
112       assertEquals(0, cache.getCacheListeners().size());
113    }
114
115    /**
116     * Test that fqn-specific application of cache listeners has not been implemented and will not be implemented
117     * in 2.0.0. It is a feature for 2.1.0 but the interface needed to be in place now.
118     */

119    public void testFqnBasedCacheListeners()
120    {
121       try
122       {
123          cache.getCacheListeners(Fqn.ROOT);
124          fail("Fqn-based cache listener operation should throw an exception");
125       }
126       catch (Exception JavaDoc e)
127       {
128          // expected
129
}
130
131       try
132       {
133          cache.addCacheListener(Fqn.ROOT, new AbstractCacheListener()
134          {
135          });
136          fail("Fqn-based cache listener operation should throw an exception");
137       }
138       catch (Exception JavaDoc e)
139       {
140          // expected
141
}
142
143       try
144       {
145          cache.removeCacheListener(Fqn.ROOT, new AbstractCacheListener()
146          {
147          });
148          fail("Fqn-based cache listener operation should throw an exception");
149       }
150       catch (Exception JavaDoc e)
151       {
152          // expected
153
}
154    }
155
156    /**
157     * All cache operations should happen on a {@link Node} - I.e., you look up a {@link Node} and perform data operations
158     * on this {@link Node}. For convenience and familiarity with JBoss Cache 1.x, we provide some helpers in {@link Cache}
159     * which dives you direct data access to nodes.
160     * <p/>
161     * This test exercises these.
162     */

163    public void testConvenienceMethods()
164    {
165       Fqn fqn = Fqn.fromString("/test/fqn");
166       Object JavaDoc key = "key", value = "value";
167       Map JavaDoc data = new HashMap JavaDoc();
168       data.put(key, value);
169
170       assertNull(cache.get(fqn, key));
171
172       cache.put(fqn, key, value);
173
174       assertEquals(value, cache.get(fqn, key));
175
176       cache.remove(fqn, key);
177
178       assertNull(cache.get(fqn, key));
179
180       cache.put(fqn, data);
181
182       System.out.println(((CacheImpl) cache).printDetails());
183
184       assertEquals(value, cache.get(fqn, key));
185    }
186
187
188    /**
189     * Another convenience method that tests node removal
190     */

191    public void nodeConvenienceNodeRemoval()
192    {
193       // this fqn is relative, but since it is from the root it may as well be absolute
194
Fqn fqn = Fqn.fromString("/test/fqn");
195       cache.getRoot().addChild(fqn);
196       assertTrue(cache.getRoot().hasChild(fqn));
197
198       cache.removeNode(fqn);
199       assertFalse(cache.getRoot().hasChild(fqn));
200    }
201
202    /**
203     * Tests basic eviction
204     */

205    public void testEvict()
206    {
207       Fqn one = Fqn.fromString("/one");
208       Fqn two = Fqn.fromString("/one/two");
209       Object JavaDoc key = "key", value = "value";
210
211       cache.getRoot().addChild(one).put(key, value);
212       cache.getRoot().addChild(two).put(key, value);
213
214       assertTrue(cache.getRoot().hasChild(one));
215       assertFalse(cache.getRoot().getChild(one).getData().isEmpty());
216       assertTrue(cache.getRoot().hasChild(two));
217       assertFalse(cache.getRoot().getChild(two).getData().isEmpty());
218
219       // evict two
220
cache.evict(two, false);
221
222       assertTrue(cache.getRoot().hasChild(one));
223       assertTrue(cache.getRoot().getChild(one).getKeys().contains(key));
224       assertFalse(cache.getRoot().hasChild(two));
225
226       // now add 2 again...
227
cache.getRoot().addChild(two).put(key, value);
228
229       // now evict one, NOT recursive
230
cache.evict(one, false);
231
232       // one will NOT be removed, just emptied.
233
assertTrue(cache.getRoot().hasChild(one));
234       assertFalse(cache.getRoot().getChild(one).getKeys().contains(key));
235
236       // two will be unaffected
237
assertTrue(cache.getRoot().hasChild(two));
238       assertTrue(cache.getRoot().getChild(two).getKeys().contains(key));
239    }
240
241
242    /**
243     * Tests recursive eviction
244     */

245    public void testEvictRecursive()
246    {
247       Fqn one = Fqn.fromString("/one");
248       Fqn two = Fqn.fromString("/one/two");
249       Object JavaDoc key = "key", value = "value";
250
251       cache.getRoot().addChild(one).put(key, value);
252       cache.getRoot().addChild(two).put(key, value);
253
254       assertTrue(cache.getRoot().hasChild(one));
255       assertFalse(cache.getRoot().getChild(one).getData().isEmpty());
256       assertTrue(cache.getRoot().hasChild(two));
257       assertFalse(cache.getRoot().getChild(two).getData().isEmpty());
258
259       // evict two
260
cache.evict(two, true);
261
262       assertTrue(cache.getRoot().hasChild(one));
263       assertFalse(cache.getRoot().getChild(one).getData().isEmpty());
264       assertFalse(cache.getRoot().hasChild(two));
265
266       // now add 2 again...
267
cache.getRoot().addChild(two).put(key, value);
268
269       // now evict one, recursive
270
cache.evict(one, true);
271
272       assertFalse(cache.getRoot().hasChild(one));
273       assertFalse(cache.getRoot().hasChild(two));
274    }
275
276
277    /**
278     * Again, see org.jboss.cache for more extensive tests on Regions. This just tests the getRegion API on cache.
279     */

280    public void testRegion()
281    {
282       Region rootRegion = cache.getRegion(Fqn.ROOT, true);
283       assertNotNull(rootRegion);// guaranteed never to return null if createIfAbsent is true.
284
assertSame(rootRegion, cache.getRegion(Fqn.ROOT, true));
285
286       Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"), true);
287       assertNotNull(otherRegion);
288       assertSame(otherRegion, cache.getRegion(Fqn.fromString("/other/region"), true));
289    }
290
291    /**
292     * Again, see org.jboss.cache for more extensive tests on Regions. This just tests the getRegion API on cache.
293     */

294    public void testParentRegion1()
295    {
296       Region rootRegion = cache.getRegion(Fqn.ROOT, true);
297       assertNotNull(rootRegion);// guaranteed never to return null if createIfAbsent is true.
298
assertSame(rootRegion, cache.getRegion(Fqn.ROOT, false));
299
300       Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"), false);
301       // should return the same parent region as root.
302

303       assertSame(otherRegion, rootRegion);
304    }
305
306    /**
307     * Again, see org.jboss.cache for more extensive tests on Regions. This just tests the getRegion API on cache.
308     */

309    public void testParentRegion2()
310    {
311       Region rootRegion = cache.getRegion(Fqn.ROOT, true);
312       Region parentRegion = cache.getRegion(Fqn.fromString("/parent"), true);
313       assertNotSame("parentRegion should be a new region in its own right", rootRegion, parentRegion);
314
315       Region childRegion = cache.getRegion(Fqn.fromString("/parent/region"), false);
316       assertSame("Expecting the same region as parentRegion", childRegion, parentRegion);
317    }
318
319
320    /**
321     * Again, see org.jboss.cache for more extensive tests on Regions. This just tests the getRegion API on cache.
322     */

323    public void testNullRegion()
324    {
325       Region myRegion = cache.getRegion(Fqn.fromString("/myregion"), true);
326       assertNotNull(myRegion);// guaranteed never to return null if createIfAbsent is true.
327
assertSame(myRegion, cache.getRegion(Fqn.fromString("/myregion"), false));
328
329       Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"), false);
330       // should return null since createIfAbsent is null and none of the parents have defined regions
331
assertNull(otherRegion);
332    }
333
334 }
335
Popular Tags