KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > CacheLoaderTestsBase


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.cache.tests;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.jboss.cache.CacheException;
13 import org.jboss.cache.Fqn;
14 import org.jboss.cache.Modification;
15 import org.jboss.cache.TreeCache;
16 import org.jboss.cache.loader.CacheLoader;
17 import org.jboss.cache.transaction.DummyTransactionManager;
18
19 import javax.transaction.NotSupportedException JavaDoc;
20 import javax.transaction.Transaction JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.*;
23
24 /**
25  * Commons tests for all CacheLoaders
26  * @author Bela Ban
27  * @version $Id: CacheLoaderTestsBase.java,v 1.2.2.2 2005/04/06 21:07:02 starksm Exp $
28  */

29 abstract public class CacheLoaderTestsBase extends TestCase {
30    TreeCache cache;
31    CacheLoader loader=null;
32    Transaction JavaDoc tx=null;
33    static final Fqn FQN = new Fqn("key");
34
35
36    protected void setUp() throws Exception JavaDoc {
37       super.setUp();
38       cache=new TreeCache();
39       cache.setCacheMode("local");
40       configureCache();
41       // cache.setCacheLoaderPreload("/1/2/3/4/5/d");
42
cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
43       cache.createService();
44       cache.startService();
45       loader=cache.getCacheLoader();
46    }
47
48    abstract protected void configureCache() throws Exception JavaDoc;
49
50
51    protected void tearDown() throws Exception JavaDoc {
52       super.tearDown();
53       if(tx != null) {
54          try {
55             tx.commit();
56          }
57          catch(Throwable JavaDoc e) {
58             e.printStackTrace();
59          }
60       }
61       cache.remove("/");
62       cache.stopService();
63       cache.destroyService();
64    }
65
66
67
68    public void testPrint() throws CacheException {
69       final Fqn NODE=Fqn.fromString("/test");
70       final String JavaDoc KEY="key";
71       cache.put(NODE, KEY, new Integer JavaDoc(10));
72       cache.evict(NODE);
73       String JavaDoc ret=cache.print(NODE);
74       assertNotNull(ret);
75    }
76
77    public void testPut() throws CacheException {
78       final String JavaDoc NODE="/test";
79       final String JavaDoc KEY="key";
80       Object JavaDoc retval=null;
81       cache.remove(NODE);
82       retval=cache.put(NODE, KEY, new Integer JavaDoc(10));
83       assertNull(retval);
84       retval=cache.put(NODE, KEY, new Integer JavaDoc(20));
85       assertEquals(new Integer JavaDoc(10), retval);
86       cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
87

88       retval=cache.put(NODE, KEY, new Integer JavaDoc(30));
89       assertEquals(new Integer JavaDoc(20), retval);
90    }
91
92    public void testPut2() throws CacheException {
93       final String JavaDoc NODE="/a/b/c";
94       final String JavaDoc KEY="key";
95       Object JavaDoc retval=null;
96       cache.remove(NODE);
97       retval=cache.put(NODE, KEY, new Integer JavaDoc(10));
98       assertNull(retval);
99       retval=cache.put(NODE, KEY, new Integer JavaDoc(20));
100       assertEquals(new Integer JavaDoc(10), retval);
101       cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
102
cache.evict(Fqn.fromString("/a/b"));
103       cache.evict(Fqn.fromString("/a"));
104       retval=cache.put(NODE, KEY, new Integer JavaDoc(30));
105       assertEquals(new Integer JavaDoc(20), retval);
106    }
107
108
109    public void testSerialization() throws CacheException {
110       SamplePojo pojo=new SamplePojo(39, "Bela");
111       pojo.getHobbies().add("Running");
112       pojo.getHobbies().add("Beerathlon");
113       pojo.getHobbies().add("Triathlon");
114       cache.put("/mypojo", new Integer JavaDoc(322649), pojo);
115       assertNotNull(cache.get("/mypojo", new Integer JavaDoc(322649)));
116       cache.evict(Fqn.fromString("/mypojo"));
117       assertFalse(cache.exists("/mypojo"));
118       SamplePojo pojo2=(SamplePojo)cache.get("/mypojo", new Integer JavaDoc(322649)); // should fetch from CacheLoader
119
assertNotNull(pojo2);
120       assertEquals(39, pojo2.getAge());
121       assertEquals("Bela", pojo2.getName());
122       assertEquals(3, pojo2.getHobbies().size());
123    }
124
125    /** Just adds some data that wil be later retrieved. This test has to be run first */
126    public void testPopulate() {
127       try {
128          Map m=new HashMap();
129          for(int i=0; i < 10; i++)
130            m.put("key" + i, "val" + i);
131          cache.put("/a/b/c", m);
132          cache.load("/1/2/3/4/5");
133          cache.put("/1/2/3/4/5", null);
134          cache.put("/1/2/3/4/5/a", null);
135          cache.put("/1/2/3/4/5/b", null);
136          cache.put("/1/2/3/4/5/c", null);
137          cache.put("/1/2/3/4/5/d", null);
138          cache.put("/1/2/3/4/5/e", null);
139          cache.put("/1/2/3/4/5/d/one", null);
140          cache.put("/1/2/3/4/5/d/two", null);
141          cache.put("/1/2/3/4/5/d/three", null);
142          // cache.put("/a/b/c", "newKey", "newValue");
143
System.out.println("cache: " + cache);
144
145          assertTrue(cache.exists("/1/2/3/4"));
146          assertTrue(cache.exists("/a/b/c"));
147          assertFalse(cache.exists("/a/b/c/d"));
148       }
149       catch(Exception JavaDoc e) {
150          fail(e.toString());
151       }
152    }
153
154
155    public void testPreloading() throws CacheException {
156       cache.remove("/");
157       cache.put("1/2/3/4/5/d", "key", "val");
158       cache.evict(Fqn.fromString("1/2/3/4/5/d"));
159       System.out.println("-- checking for 1/2/3/4/5/d");
160       assertFalse(cache.exists("1/2/3/4/5/d")); // exists() doesn't load
161
cache.get("1/2/3/4/5/d"); // get *does* load
162
assertTrue(cache.exists("1/2/3/4/5/d"));
163       System.out.println("-- 1/2/3/4/5/d exists");
164    }
165
166
167
168    public void testCacheLoading2() throws CacheException {
169       Set keys=null;
170       cache.put("/a/b/c", "key", "val");
171       try {
172          keys=cache.getKeys("/a/b/c");
173          assertNotNull(keys);
174          assertEquals(1, keys.size());
175       }
176       catch(Exception JavaDoc e) {
177          fail(e.toString());
178       }
179
180       try {
181          keys.add("myKey");
182       }
183       catch(UnsupportedOperationException JavaDoc ex) {
184          fail("unsupported operation: " + ex);
185       }
186    }
187
188
189    public void testExists() throws Exception JavaDoc {
190       cache.put("/eins/zwei/drei", "key1", "val1");
191       assertTrue(cache.exists("/eins/zwei/drei"));
192       assertTrue(cache.exists("/eins/zwei/drei", "key1"));
193       assertFalse(cache.exists("/eins/zwei/drei", "key2"));
194       assertFalse(cache.exists("/uno/due/tre"));
195       assertFalse(cache.exists("/une/due/tre", "key1"));
196    }
197
198    public void testGetChildren() {
199       try {
200          cache.put("/1/2/3/4/5/d/one", null);
201          cache.put("/1/2/3/4/5/d/two", null);
202          cache.put("/1/2/3/4/5/d/three", null);
203          Set children=cache.getChildrenNames("/1/2/3/4/5/d");
204          assertNotNull(children);
205          assertEquals(3, children.size());
206          assertTrue(children.contains("one"));
207          assertTrue(children.contains("two"));
208          assertTrue(children.contains("three"));
209       }
210       catch(Exception JavaDoc e) {
211          fail(e.toString());
212       }
213    }
214
215    public void testGetChildrenWithEviction() throws CacheException {
216       cache.put("/a/b/c/1", null);
217       cache.put("/a/b/c/2", null);
218       cache.put("/a/b/c/3", null);
219       cache.evict(Fqn.fromString("/a/b/c/1"));
220       cache.evict(Fqn.fromString("/a/b/c/2"));
221       cache.evict(Fqn.fromString("/a/b/c/3"));
222       cache.evict(Fqn.fromString("/a/b/c"));
223       cache.evict(Fqn.fromString("/a/b"));
224       cache.evict(Fqn.fromString("/a"));
225       cache.evict(Fqn.fromString("/"));
226       Set children=cache.getChildrenNames("/a/b/c");
227       assertNotNull(children);
228       assertEquals(3, children.size());
229       assertTrue(children.contains("1"));
230       assertTrue(children.contains("2"));
231       assertTrue(children.contains("3"));
232    }
233
234    public void testGetChildren2() {
235       try {
236          cache.put("/1", null);
237          cache.put("a", null);
238          Set children=cache.getChildrenNames("/");
239          assertNotNull(children);
240          assertEquals(2, children.size());
241          assertTrue(children.contains("1"));
242          assertTrue(children.contains("a"));
243       }
244       catch(Exception JavaDoc e) {
245          fail(e.toString());
246       }
247    }
248
249    public void testGetChildren3() {
250       try {
251          cache.put("/1", null);
252          cache.put("a", null);
253          Set children=cache.getChildrenNames("");
254          assertNotNull(children);
255          assertEquals(2, children.size());
256          assertTrue(children.contains("1"));
257          assertTrue(children.contains("a"));
258       }
259       catch(Exception JavaDoc e) {
260          fail(e.toString());
261       }
262    }
263
264    public void testGetChildren4() {
265       try {
266          if(!cache.exists("/a/b/c"))
267             cache.put("/a/b/c", null);
268          Set children=cache.getChildrenNames((Fqn)null);
269          assertNull(children);
270       }
271       catch(Exception JavaDoc e) {
272          fail(e.toString());
273       }
274    }
275
276
277
278    public void testRemoveData() throws Exception JavaDoc {
279       String JavaDoc key="/x/y/z/";
280       cache.put(key, "keyA", "valA");
281       cache.put(key, "keyB", "valB");
282       cache.put(key, "keyC", "valC");
283       assertEquals(3, cache.getKeys(key).size());
284       cache.removeData(key);
285       Set keys=cache.getKeys(key);
286       assertNull(keys);
287       cache.remove("/x");
288       Object JavaDoc val=cache.get(key, "keyA");
289       assertNull(val);
290    }
291
292
293    public void testRemoveData2() throws Exception JavaDoc {
294       Set keys;
295       Fqn key=Fqn.fromString("/x/y/z/");
296       cache.put(key, "keyA", "valA");
297       cache.put(key, "keyB", "valB");
298       cache.put(key, "keyC", "valC");
299       keys=cache.getKeys(key);
300       assertEquals(3, keys.size());
301       cache.removeData(key);
302       cache.evict(key);
303       keys=cache.getKeys(key);
304       assertEquals(0, keys.size());
305    }
306
307    public void testRemoveData3() throws Exception JavaDoc {
308       Set keys;
309       Fqn key=Fqn.fromString("/x/y/z/");
310       cache.put(key, "keyA", "valA");
311       cache.put(key, "keyB", "valB");
312       cache.put(key, "keyC", "valC");
313       keys=cache.getKeys(key);
314       assertEquals(3, keys.size());
315       cache.evict(key);
316       cache.removeData(key);
317       keys=cache.getKeys(key);
318       assertNull(keys);
319    }
320
321    public void testRemoveKey() throws Exception JavaDoc {
322       String JavaDoc key="/x/y/z/";
323       cache.put(key, "keyA", "valA");
324       cache.put(key, "keyB", "valB");
325       cache.put(key, "keyC", "valC");
326       cache.remove(key, "keyA");
327       assertEquals(2, cache.getKeys(key).size());
328       cache.remove("/x");
329    }
330
331
332    public void testRemoveKey2() throws CacheException {
333       final String JavaDoc NODE="/test";
334       final String JavaDoc KEY="key";
335       Object JavaDoc retval=null;
336       cache.remove(NODE);
337       retval=cache.put(NODE, KEY, new Integer JavaDoc(10));
338       assertNull(retval);
339       retval=cache.remove(NODE, KEY);
340       assertEquals(new Integer JavaDoc(10), retval);
341       retval=cache.remove(NODE, KEY);
342       assertNull(retval);
343    }
344
345    public void testRemoveKey3() throws CacheException {
346       final String JavaDoc NODE="/test";
347       final String JavaDoc KEY="key";
348       Object JavaDoc retval=null;
349       cache.remove(NODE);
350       retval=cache.put(NODE, KEY, new Integer JavaDoc(10));
351       assertNull(retval);
352
353       cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
354
retval=cache.remove(NODE, KEY);
355       assertEquals(new Integer JavaDoc(10), retval);
356
357       cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
358
retval=cache.remove(NODE, KEY);
359       assertNull(retval);
360    }
361
362
363    public void testRemove() throws Exception JavaDoc {
364       String JavaDoc key="/x/y/z/";
365       cache.put(key, "keyA", "valA");
366       cache.put(key, "keyB", "valB");
367       cache.put(key, "keyC", "valC");
368       cache.remove("/x");
369       assertNull(cache.get(key, "keyA"));
370       Set keys=cache.getKeys(key);
371       assertNull(keys);
372       cache.remove("/x");
373    }
374
375
376    public void testRemoveRoot() throws Exception JavaDoc {
377       assertNull(cache.getKeys("/"));
378       cache.put("/1/2/3/4/5", null);
379       cache.put("uno/due/tre", null);
380       cache.put("1/2/3/a", null);
381       cache.put("/eins/zwei/drei", null);
382       cache.put("/one/two/three", null);
383       cache.remove("/");
384       assertEquals(null, cache.getKeys("/"));
385    }
386
387
388    public void testEvictionWithCacheLoader() throws Exception JavaDoc {
389       cache.put("/first/second", "key1", "val1"); // stored in cache loader
390
cache.put("/first/second/third", "key2", "val2"); // stored in cache loader
391
cache.evict(Fqn.fromString("/first/second")); // doesn't remove node, just data !
392
assertTrue(cache.exists("/first/second/third"));
393       assertTrue(cache.exists("/first/second"));
394       assertTrue(cache.exists("/first"));
395       String JavaDoc val=(String JavaDoc)cache.get("/first/second", "key1"); // should be loaded from cache loader
396
assertEquals("val1", val);
397       assertTrue(cache.exists("/first/second/third"));
398       assertTrue(cache.exists("/first/second"));
399       assertTrue(cache.exists("/first"));
400    }
401
402
403    public void testEvictionWithCacheLoader2() throws Exception JavaDoc {
404        cache.put("/first/second/third", "key1", "val1"); // stored in cache loader
405
cache.evict(Fqn.fromString("/first/second/third")); // removes node, because there are no children
406
assertFalse(cache.exists("/first/second/third"));
407        assertTrue(cache.exists("/first/second"));
408        assertTrue(cache.exists("/first"));
409        String JavaDoc val=(String JavaDoc)cache.get("/first/second/third", "key1"); // should be loaded from cache loader
410
assertEquals("val1", val);
411        assertTrue(cache.exists("/first/second/third"));
412        assertTrue(cache.exists("/first/second"));
413        assertTrue(cache.exists("/first"));
414     }
415
416
417
418    public void testEvictionWithGetChildrenNames() throws Exception JavaDoc {
419       cache.put("/a/1", null);
420       cache.put("/a/2", null);
421       cache.put("/a/3", null);
422       // cache.put("/a/1/tmp", null);
423
cache.evict(Fqn.fromString("/a/1"));
424       cache.evict(Fqn.fromString("/a/2"));
425       cache.evict(Fqn.fromString("/a/3"));
426       cache.evict(Fqn.fromString("/a"));
427
428       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
429       mgr.begin();
430       tx=mgr.getTransaction();
431       Set children=cache.getChildrenNames("/a");
432       assertEquals(3, children.size());
433       assertTrue(children.contains("1"));
434       assertTrue(children.contains("2"));
435       assertTrue(children.contains("3"));
436       assertEquals(4, cache.getNumberOfLocksHeld());
437       tx.commit();
438    }
439
440
441    public void testTxPutCommit() throws Exception JavaDoc, NotSupportedException JavaDoc {
442       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
443       mgr.begin();
444       tx=mgr.getTransaction();
445
446       cache.put("/one/two/three", "key1", "val1");
447       cache.put("/one/two/three/four", "key2", "val2");
448       tx.commit();
449       assertNotNull(cache.getKeys("/one/two/three"));
450       Set children=cache.getChildrenNames("/one");
451       assertEquals(1, children.size());
452       cache.remove("/");
453    }
454
455
456    public void testTxPutRollback() throws Exception JavaDoc, NotSupportedException JavaDoc {
457       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
458
459       cache.remove("/one");
460
461       mgr.begin();
462       tx=mgr.getTransaction();
463
464       cache.put("/one/two/three", "key1", "val1");
465       cache.put("/one/two/three/four", "key2", "val2");
466       tx.rollback();
467       assertNull(cache.getKeys("/one/two/three"));
468       Set children=cache.getChildrenNames("/one");
469       assertNull(children);
470    }
471
472
473
474
475
476
477
478
479    /**
480     * Tests basic operations without a transaction.
481     */

482    public void testBasicOperations()
483       throws Exception JavaDoc {
484
485       doTestBasicOperations();
486    }
487
488    /**
489     * Tests basic operations with a transaction.
490     */

491    public void testBasicOperationsTransactional()
492       throws Exception JavaDoc {
493
494       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
495       mgr.begin();
496       tx=mgr.getTransaction();
497       doTestBasicOperations();
498       tx.commit();
499    }
500
501    /**
502     * Tests basic operations.
503     */

504    private void doTestBasicOperations() throws Exception JavaDoc {
505
506       /* One FQN only. */
507       doPutTests(new Fqn("key"));
508       doRemoveTests(new Fqn("key"));
509       // assertEquals(0, loader.loadEntireState().length);
510

511       /* Add three FQNs, middle FQN last. */
512       doPutTests(new Fqn("key1"));
513       doPutTests(new Fqn("key3"));
514       doPutTests(new Fqn("key2"));
515       assertEquals(4, loader.get(new Fqn("key1")).size());
516       assertEquals(4, loader.get(new Fqn("key2")).size());
517       assertEquals(4, loader.get(new Fqn("key3")).size());
518
519       /* Remove middle FQN first, then the others. */
520       doRemoveTests(new Fqn("key2"));
521       doRemoveTests(new Fqn("key3"));
522       doRemoveTests(new Fqn("key1"));
523       assertEquals(null, loader.get(new Fqn("key1")));
524       assertEquals(null, loader.get(new Fqn("key2")));
525       assertEquals(null, loader.get(new Fqn("key3")));
526       // assertEquals(0, loader.loadEntireState().length);
527
}
528
529    /**
530     * Do basic put tests for a given FQN.
531     */

532    private void doPutTests(Fqn fqn)
533       throws Exception JavaDoc {
534
535       assertTrue(!loader.exists(fqn));
536
537       /* put(Fqn,Object,Object) and get(Fqn,Object) */
538       Object JavaDoc oldVal;
539       oldVal = loader.put(fqn, "one", "two");
540       assertNull(oldVal);
541       oldVal = loader.put(fqn, "three", "four");
542       assertNull(oldVal);
543       assertEquals("two", loader.get(fqn, "one"));
544       assertEquals("four", loader.get(fqn, "three"));
545       oldVal = loader.put(fqn, "one", "xxx");
546       assertEquals("two", oldVal);
547       oldVal = loader.put(fqn, "one", "two");
548       assertEquals("xxx", oldVal);
549
550       /* get(Fqn) */
551       Map map = loader.get(fqn);
552       assertEquals(2, map.size());
553       assertEquals("two", map.get("one"));
554       assertEquals("four", map.get("three"));
555
556       /* put(Fqn,Map) */
557       map.put("five", "six");
558       map.put("seven", "eight");
559       loader.put(fqn, map);
560       assertEquals("six", loader.get(fqn, "five"));
561       assertEquals("eight", loader.get(fqn, "seven"));
562       assertEquals(map, loader.get(fqn));
563       assertEquals(4, map.size());
564
565       assertTrue(loader.exists(fqn));
566    }
567
568    /**
569     * Do basic remove tests for a given FQN.
570     */

571    private void doRemoveTests(Fqn fqn)
572       throws Exception JavaDoc {
573
574       /* remove(Fqn,Object) */
575       Object JavaDoc oldVal;
576       oldVal = loader.remove(fqn, "one");
577       assertEquals("two", oldVal);
578       oldVal = loader.remove(fqn, "five");
579       assertEquals("six", oldVal);
580       assertEquals(null, loader.get(fqn, "one"));
581       assertEquals(null, loader.get(fqn, "five"));
582       assertEquals("four", loader.get(fqn, "three"));
583       assertEquals("eight", loader.get(fqn, "seven"));
584       Map map = loader.get(fqn);
585       assertEquals(2, map.size());
586       assertEquals("four", map.get("three"));
587       assertEquals("eight", map.get("seven"));
588
589       /* remove(Fqn) */
590       assertTrue(loader.exists(fqn));
591       loader.remove(fqn);
592       assertEquals(null, loader.get(fqn, "three"));
593       assertEquals(null, loader.get(fqn, "seven"));
594       map = loader.get(fqn);
595       assertEquals(null, map);
596       assertTrue(!loader.exists(fqn));
597    }
598
599    /**
600     * Tests creating implicit intermediate nodes when a leaf node is created,
601     * and tests removing subtrees.
602     */

603    public void testMultiLevelTree()
604       throws Exception JavaDoc {
605
606       /* Create top level node implicitly. */
607       assertTrue(!loader.exists(new Fqn("key0")));
608       loader.put(Fqn.fromString("/key0/level1/level2"), null);
609       assertTrue(loader.exists(Fqn.fromString("/key0/level1/level2")));
610       assertTrue(loader.exists(Fqn.fromString("/key0/level1")));
611       assertTrue(loader.exists(new Fqn("key0")));
612
613       /* Remove leaf, leaving implicitly created middle level. */
614       loader.put(Fqn.fromString("/key0/x/y"), null);
615       assertTrue(loader.exists(Fqn.fromString("/key0/x/y")));
616       assertTrue(loader.exists(Fqn.fromString("/key0/x")));
617       loader.remove(Fqn.fromString("/key0/x/y"));
618       assertTrue(!loader.exists(Fqn.fromString("/key0/x/y")));
619       assertTrue(loader.exists(Fqn.fromString("/key0/x")));
620
621       /* Delete top level to delete everything. */
622       loader.remove(new Fqn("key0"));
623       assertTrue(!loader.exists(new Fqn("key0")));
624       assertTrue(!loader.exists(Fqn.fromString("/key0/level1/level2")));
625       assertTrue(!loader.exists(Fqn.fromString("/key0/level1")));
626       assertTrue(!loader.exists(Fqn.fromString("/key0/x")));
627
628       /* Add three top level nodes as context. */
629       loader.put(new Fqn("key1"), null);
630       loader.put(new Fqn("key2"), null);
631       loader.put(new Fqn("key3"), null);
632       assertTrue(loader.exists(new Fqn("key1")));
633       assertTrue(loader.exists(new Fqn("key2")));
634       assertTrue(loader.exists(new Fqn("key3")));
635
636       /* Put /key3/level1/level2. level1 should be implicitly created. */
637       assertTrue(!loader.exists(Fqn.fromString("/key3/level1")));
638       assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2")));
639       loader.put(Fqn.fromString("/key3/level1/level2"), null);
640       assertTrue(loader.exists(Fqn.fromString("/key3/level1/level2")));
641       assertTrue(loader.exists(Fqn.fromString("/key3/level1")));
642
643       /* Context nodes should still be intact. */
644       assertTrue(loader.exists(new Fqn("key1")));
645       assertTrue(loader.exists(new Fqn("key2")));
646       assertTrue(loader.exists(new Fqn("key3")));
647
648       /* Remove middle level only. */
649       loader.remove(Fqn.fromString("/key3/level1"));
650       assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2")));
651       assertTrue(!loader.exists(Fqn.fromString("/key3/level1")));
652
653       /* Context nodes should still be intact. */
654       assertTrue(loader.exists(new Fqn("key1")));
655       assertTrue(loader.exists(new Fqn("key2")));
656       assertTrue(loader.exists(new Fqn("key3")));
657
658       /* Delete first root, leaving other roots. */
659       loader.remove(new Fqn("key1"));
660       assertTrue(!loader.exists(new Fqn("key1")));
661       assertTrue(loader.exists(new Fqn("key2")));
662       assertTrue(loader.exists(new Fqn("key3")));
663
664       /* Delete last root, leaving other roots. */
665       loader.remove(new Fqn("key3"));
666       assertTrue(loader.exists(new Fqn("key2")));
667       assertTrue(!loader.exists(new Fqn("key3")));
668
669       /* Delete final root, leaving none. */
670       loader.remove(new Fqn("key2"));
671       assertTrue(!loader.exists(new Fqn("key0")));
672       assertTrue(!loader.exists(new Fqn("key1")));
673       assertTrue(!loader.exists(new Fqn("key2")));
674       assertTrue(!loader.exists(new Fqn("key3")));
675
676       /* Repeat all tests above using put(Fqn,Object,Object) and get(Fqn) */
677
678       assertNull(loader.get(new Fqn("key0")));
679       loader.put(Fqn.fromString("/key0/level1/level2"), "a", "b");
680       assertNotNull(loader.get(Fqn.fromString("/key0/level1/level2")));
681       assertNull(loader.get(Fqn.fromString("/key0/level1")));
682       assertNull(loader.get(new Fqn("key0")));
683
684       loader.put(Fqn.fromString("/key0/x/y"), "a", "b");
685       assertNotNull(loader.get(Fqn.fromString("/key0/x/y")));
686       assertNull(loader.get(Fqn.fromString("/key0/x")));
687       loader.remove(Fqn.fromString("/key0/x/y"));
688       assertNull(loader.get(Fqn.fromString("/key0/x/y")));
689       assertNull(loader.get(Fqn.fromString("/key0/x")));
690
691       loader.remove(new Fqn("key0"));
692       assertNull(loader.get(new Fqn("key0")));
693       assertNull(loader.get(Fqn.fromString("/key0/level1/level2")));
694       assertNull(loader.get(Fqn.fromString("/key0/level1")));
695       assertNull(loader.get(Fqn.fromString("/key0/x")));
696
697       loader.put(new Fqn("key1"), "a", "b");
698       loader.put(new Fqn("key2"), "a", "b");
699       loader.put(new Fqn("key3"), "a", "b");
700       assertNotNull(loader.get(new Fqn("key1")));
701       assertNotNull(loader.get(new Fqn("key2")));
702       assertNotNull(loader.get(new Fqn("key3")));
703
704       assertNull(loader.get(Fqn.fromString("/key3/level1")));
705       assertNull(loader.get(Fqn.fromString("/key3/level1/level2")));
706       loader.put(Fqn.fromString("/key3/level1/level2"), "a", "b");
707       assertNotNull(loader.get(Fqn.fromString("/key3/level1/level2")));
708       assertNull(loader.get(Fqn.fromString("/key3/level1")));
709
710       assertNotNull(loader.get(new Fqn("key1")));
711       assertNotNull(loader.get(new Fqn("key2")));
712       assertNotNull(loader.get(new Fqn("key3")));
713
714       loader.remove(Fqn.fromString("/key3/level1"));
715       assertNull(loader.get(Fqn.fromString("/key3/level1/level2")));
716       assertNull(loader.get(Fqn.fromString("/key3/level1")));
717
718       assertNotNull(loader.get(new Fqn("key1")));
719       assertNotNull(loader.get(new Fqn("key2")));
720       assertNotNull(loader.get(new Fqn("key3")));
721
722       loader.remove(new Fqn("key1"));
723       assertNull(loader.get(new Fqn("key1")));
724       assertNotNull(loader.get(new Fqn("key2")));
725       assertNotNull(loader.get(new Fqn("key3")));
726
727       loader.remove(new Fqn("key3"));
728       assertNotNull(loader.get(new Fqn("key2")));
729       assertNull(loader.get(new Fqn("key3")));
730
731       loader.remove(new Fqn("key2"));
732       assertNull(loader.get(new Fqn("key0")));
733       assertNull(loader.get(new Fqn("key1")));
734       assertNull(loader.get(new Fqn("key2")));
735       assertNull(loader.get(new Fqn("key3")));
736    }
737
738    /**
739     * Tests the getChildrenNames() method.
740     */

741    public void testGetChildrenNames()
742       throws Exception JavaDoc {
743
744       checkChildren(new Fqn(), null);
745       checkChildren(Fqn.fromString("/key0"), null);
746
747       loader.put(Fqn.fromString("/key0"), null);
748       checkChildren(new Fqn(), new String JavaDoc[] { "key0" });
749
750       loader.put(Fqn.fromString("/key1/x"), null);
751       checkChildren(new Fqn(), new String JavaDoc[] { "key0", "key1" });
752       checkChildren(Fqn.fromString("/key1"), new String JavaDoc[] { "x" });
753
754       loader.remove(Fqn.fromString("/key1/x"));
755       checkChildren(new Fqn(), new String JavaDoc[] { "key0", "key1" });
756       checkChildren(Fqn.fromString("/key0"), null);
757       checkChildren(Fqn.fromString("/key1"), null);
758
759       loader.put(Fqn.fromString("/key0/a"), null);
760       loader.put(Fqn.fromString("/key0/ab"), null);
761       loader.put(Fqn.fromString("/key0/abc"), null);
762       checkChildren(Fqn.fromString("/key0"),
763                     new String JavaDoc[] { "a", "ab", "abc" });
764
765       loader.put(Fqn.fromString("/key0/xxx"), null);
766       loader.put(Fqn.fromString("/key0/xx"), null);
767       loader.put(Fqn.fromString("/key0/x"), null);
768       checkChildren(Fqn.fromString("/key0"),
769                     new String JavaDoc[] { "a", "ab", "abc", "x", "xx", "xxx" });
770
771       loader.put(Fqn.fromString("/key0/a/1"), null);
772       loader.put(Fqn.fromString("/key0/a/2"), null);
773       loader.put(Fqn.fromString("/key0/a/2/1"), null);
774       checkChildren(Fqn.fromString("/key0/a/2"), new String JavaDoc[] { "1" });
775       checkChildren(Fqn.fromString("/key0/a"), new String JavaDoc[] { "1", "2" });
776       checkChildren(Fqn.fromString("/key0"),
777                     new String JavaDoc[] { "a", "ab", "abc", "x", "xx", "xxx" });
778 //
779
// loader.put(Fqn.fromString("/key0/\u0000"), null);
780
// loader.put(Fqn.fromString("/key0/\u0001"), null);
781
// checkChildren(Fqn.fromString("/key0"),
782
// new String[] { "a", "ab", "abc", "x", "xx", "xxx",
783
// "\u0000", "\u0001"});
784
//
785
// loader.put(Fqn.fromString("/\u0001"), null);
786
// checkChildren(new Fqn(), new String[] { "key0", "key1", "\u0001" });
787
//
788
// loader.put(Fqn.fromString("/\u0001/\u0001"), null);
789
// checkChildren(Fqn.fromString("/\u0001"), new String[] { "\u0001" });
790
//
791
// loader.put(Fqn.fromString("/\u0001/\uFFFF"), null);
792
// checkChildren(Fqn.fromString("/\u0001"),
793
// new String[] { "\u0001", "\uFFFF" });
794
//
795
// loader.put(Fqn.fromString("/\u0001/\uFFFF/\u0001"), null);
796
// checkChildren(Fqn.fromString("/\u0001/\uFFFF"),
797
// new String[] { "\u0001" });
798
}
799
800    /**
801     * Checks that the given list of children part names is returned.
802     */

803    private void checkChildren(Fqn fqn, String JavaDoc[] names)
804       throws Exception JavaDoc {
805
806       Set set = loader.getChildrenNames(fqn);
807       if (names != null) {
808          assertEquals(names.length, set.size());
809          for (int i = 0; i < names.length; i += 1) {
810             assertTrue(set.contains(names[i]));
811          }
812       } else {
813          assertNull(set);
814       }
815    }
816
817    /**
818     * Tests basic operations without a transaction.
819     */

820    public void testModifications()
821       throws Exception JavaDoc {
822
823       doTestModifications();
824    }
825
826    /**
827     * Tests basic operations with a transaction.
828     */

829    public void testModificationsTransactional()
830       throws Exception JavaDoc {
831       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
832       mgr.begin();
833       tx=mgr.getTransaction();
834       doTestModifications();
835       tx.commit();
836    }
837
838    /**
839     * Tests modifications.
840     */

841    private void doTestModifications()
842       throws Exception JavaDoc {
843
844       /* PUT_KEY_VALUE, PUT_DATA */
845       List list = createUpdates();
846       loader.put(list);
847       checkModifications(list);
848
849       /* REMOVE_KEY_VALUE */
850       list = new ArrayList();
851       Modification mod = new Modification();
852       mod.setType(Modification.REMOVE_KEY_VALUE);
853       mod.setFqn(FQN);
854       mod.setKey("one");
855       list.add(mod);
856       loader.put(list);
857       checkModifications(list);
858
859       /* REMOVE_NODE */
860       list = new ArrayList();
861       mod = new Modification();
862       mod.setType(Modification.REMOVE_NODE);
863       mod.setFqn(FQN);
864       list.add(mod);
865       loader.put(list);
866       checkModifications(list);
867       assertEquals(null, loader.get(FQN));
868
869       /* REMOVE_DATA */
870       loader.put(FQN, "one", "two");
871       list = new ArrayList();
872       mod = new Modification();
873       mod.setType(Modification.REMOVE_DATA);
874       mod.setFqn(FQN);
875       list.add(mod);
876       loader.put(list);
877       checkModifications(list);
878    }
879
880    /**
881     * Tests a one-phase transaction.
882     */

883    public void testOnePhaseTransaction()
884       throws Exception JavaDoc {
885       List mods = createUpdates();
886       loader.prepare(null, mods, true);
887       checkModifications(mods);
888    }
889
890    /**
891     * Tests a two-phase transaction.
892     */

893    public void testTwoPhaseTransaction()
894       throws Exception JavaDoc {
895
896       Object JavaDoc txnKey = new Object JavaDoc();
897       List mods = createUpdates();
898       loader.prepare(txnKey, mods, false);
899 // try {
900
// checkModifications(mods);
901
// // fail("Expected lock timeout");
902
// } catch (DeadlockException expected) {}
903
loader.commit(txnKey);
904       checkModifications(mods);
905    }
906
907    /**
908     * Tests rollback of a two-phase transaction.
909     */

910    public void testTransactionRollback()
911       throws Exception JavaDoc {
912
913       loader.remove(Fqn.fromString("/"));
914
915       int num=loader.loadEntireState().length;
916
917       Object JavaDoc txnKey = new Object JavaDoc();
918       List mods = createUpdates();
919       loader.prepare(txnKey, mods, false);
920       loader.rollback(txnKey);
921       assertEquals(num, loader.loadEntireState().length);
922    }
923
924    /**
925     * Creates a set of update (PUT_KEY_VALUE, PUT_DATA) modifications.
926     */

927    private List createUpdates() {
928
929       List list = new ArrayList();
930
931       Modification mod = new Modification();
932       mod.setType(Modification.PUT_KEY_VALUE);
933       mod.setFqn(FQN);
934       mod.setKey("one");
935       mod.setValue("two");
936       list.add(mod);
937
938       mod = new Modification();
939       mod.setType(Modification.PUT_KEY_VALUE);
940       mod.setFqn(FQN);
941       mod.setKey("three");
942       mod.setValue("four");
943       list.add(mod);
944
945       Map map = new HashMap();
946       map.put("five", "six");
947       map.put("seven", "eight");
948       mod = new Modification();
949       mod.setType(Modification.PUT_DATA);
950       mod.setFqn(FQN);
951       mod.setData(map);
952       list.add(mod);
953
954       return list;
955    }
956
957    /**
958     * Checks that a list of modifications was applied.
959     */

960    private void checkModifications(List list)
961       throws Exception JavaDoc {
962
963       for (int i = 0; i < list.size(); i += 1) {
964          Modification mod = (Modification) list.get(i);
965          Fqn fqn = mod.getFqn();
966          switch (mod.getType()) {
967          case Modification.PUT_KEY_VALUE:
968             assertEquals(mod.getValue(), loader.get(fqn, mod.getKey()));
969             break;
970          case Modification.PUT_DATA:
971             Map map = mod.getData();
972             for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
973                Object JavaDoc key = iter.next();
974                assertEquals(map.get(key), loader.get(fqn, key));
975             }
976             break;
977          case Modification.REMOVE_KEY_VALUE:
978             assertEquals(null, loader.get(fqn, mod.getKey()));
979             break;
980          case Modification.REMOVE_DATA:
981             map = loader.get(fqn);
982             assertNull(map);
983             break;
984          case Modification.REMOVE_NODE:
985             assertEquals(null, loader.get(fqn));
986             break;
987          default:
988             fail("unknown type: " + mod);
989             break;
990          }
991       }
992    }
993
994
995
996
997
998
999
1000   /**
1001    * Tests that null keys and values work as for a standard Java Map.
1002    */

1003   public void testNullKeysAndValues()
1004      throws Exception JavaDoc {
1005
1006      loader.put(FQN, null, "x");
1007      assertEquals("x", loader.get(FQN, null));
1008      Map map = loader.get(FQN);
1009      assertEquals(1, map.size());
1010      assertEquals("x", map.get(null));
1011
1012      loader.put(FQN, "y", null);
1013      assertEquals(null, loader.get(FQN, "y"));
1014      map = loader.get(FQN);
1015      assertEquals(2, map.size());
1016      assertEquals("x", map.get(null));
1017      assertEquals(null, map.get("y"));
1018
1019      loader.remove(FQN, null);
1020      assertEquals(null, loader.get(FQN, null));
1021      assertEquals(1, loader.get(FQN).size());
1022
1023      loader.remove(FQN, "y");
1024      assertEquals(null, loader.get(FQN, "y"));
1025      assertNull(loader.get(FQN));
1026
1027      map = new HashMap();
1028      map.put(null, null);
1029      loader.put(FQN, map);
1030      assertEquals(map, loader.get(FQN));
1031
1032      loader.remove(FQN);
1033      assertEquals(null, loader.get(FQN));
1034
1035      map = new HashMap();
1036      map.put("xyz", null);
1037      map.put(null, "abc");
1038      loader.put(FQN, map);
1039      assertEquals(map, loader.get(FQN));
1040
1041      loader.remove(FQN);
1042      assertEquals(null, loader.get(FQN));
1043   }
1044
1045   /**
1046    * Test non-default database name.
1047    */

1048   public void testDatabaseName()
1049      throws Exception JavaDoc {
1050
1051      loader.put(FQN, "one", "two");
1052      assertEquals("two", loader.get(FQN, "one"));
1053   }
1054
1055   /**
1056    * Test load/store state.
1057    */

1058   public void testLoadAndStore()
1059      throws Exception JavaDoc {
1060
1061      /* Empty state. */
1062      loader.remove(Fqn.fromString("/"));
1063      // assertEquals(0, loader.loadEntireState().length);
1064
// loader.storeEntireState(new byte[0]);
1065
// assertEquals(0, loader.loadEntireState().length);
1066
// loader.storeEntireState(null);
1067
// assertEquals(0, loader.loadEntireState().length);
1068
// assertEquals(null, loader.get(FQN));
1069

1070      /* Use a complex object to ensure that the class catalog is used. */
1071      Complex c1 = new Complex();
1072      Complex c2 = new Complex(c1);
1073
1074      /* Add objects. */
1075      loader.put(FQN, new Integer JavaDoc(1), c1);
1076      loader.put(FQN, new Integer JavaDoc(2), c2);
1077      assertEquals(c1, loader.get(FQN, new Integer JavaDoc(1)));
1078      assertEquals(c2, loader.get(FQN, new Integer JavaDoc(2)));
1079      assertEquals(2, loader.get(FQN).size());
1080
1081      /* Save state. */
1082      byte[] state = loader.loadEntireState();
1083      assertTrue(state.length > 0);
1084
1085
1086      /* Restore state. */
1087      loader.storeEntireState(state);
1088      assertEquals(c1, loader.get(FQN, new Integer JavaDoc(1)));
1089      assertEquals(c2, loader.get(FQN, new Integer JavaDoc(2)));
1090      assertEquals(2, loader.get(FQN).size());
1091   }
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106   /**
1107    * Complex object whose class description is stored in the class catalog.
1108    */

1109   private static class Complex implements Serializable JavaDoc {
1110
1111      Complex nested;
1112
1113      Complex() {
1114         this(null);
1115      }
1116
1117      Complex(Complex nested) {
1118         this.nested = nested;
1119      }
1120
1121      public boolean equals(Object JavaDoc o) {
1122         try {
1123            Complex x = (Complex) o;
1124            return (nested != null) ? nested.equals(x.nested)
1125                                    : (x.nested == null);
1126         } catch (ClassCastException JavaDoc e) {
1127            return false;
1128         }
1129      }
1130
1131      public int hashCode() {
1132         if(nested == null)
1133            return super.hashCode();
1134         else
1135            return 13 + nested.hashCode();
1136      }
1137   }
1138
1139
1140   public static Test suite() {
1141      return new TestSuite(CacheLoaderTestsBase.class);
1142   }
1143
1144   public static void main(String JavaDoc[] args) {
1145      junit.textui.TestRunner.run(suite());
1146   }
1147
1148}
1149
Popular Tags