KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.tests.loader;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.*;
7 import org.jboss.cache.loader.CacheLoader;
8 import org.jboss.cache.transaction.DummyTransactionManager;
9
10 import javax.transaction.NotSupportedException JavaDoc;
11 import javax.transaction.Transaction JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.util.*;
14
15 /**
16  * Commons tests for all CacheLoaders
17  * @author Bela Ban
18  * @version $Id: CacheLoaderTestsBase.java,v 1.4 2005/04/07 08:09:10 belaban Exp $
19  */

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

80       retval=cache.put(NODE, KEY, new Integer JavaDoc(30));
81       assertEquals(new Integer JavaDoc(20), retval);
82    }
83
84    public void testPut2() throws CacheException {
85       final String JavaDoc NODE="/a/b/c";
86       final String JavaDoc KEY="key";
87       Object JavaDoc retval=null;
88       cache.remove(NODE);
89       retval=cache.put(NODE, KEY, new Integer JavaDoc(10));
90       assertNull(retval);
91       retval=cache.put(NODE, KEY, new Integer JavaDoc(20));
92       assertEquals(new Integer JavaDoc(10), retval);
93       cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
94
cache.evict(Fqn.fromString("/a/b"));
95       cache.evict(Fqn.fromString("/a"));
96       retval=cache.put(NODE, KEY, new Integer JavaDoc(30));
97       assertEquals(new Integer JavaDoc(20), retval);
98    }
99
100
101    public void testSerialization() throws CacheException {
102       SamplePojo pojo=new SamplePojo(39, "Bela");
103       pojo.getHobbies().add("Running");
104       pojo.getHobbies().add("Beerathlon");
105       pojo.getHobbies().add("Triathlon");
106       cache.put("/mypojo", new Integer JavaDoc(322649), pojo);
107       assertNotNull(cache.get("/mypojo", new Integer JavaDoc(322649)));
108       cache.evict(Fqn.fromString("/mypojo"));
109       assertFalse(cache.exists("/mypojo"));
110       SamplePojo pojo2=(SamplePojo)cache.get("/mypojo", new Integer JavaDoc(322649)); // should fetch from CacheLoader
111
assertNotNull(pojo2);
112       assertEquals(39, pojo2.getAge());
113       assertEquals("Bela", pojo2.getName());
114       assertEquals(3, pojo2.getHobbies().size());
115    }
116
117    /** Just adds some data that wil be later retrieved. This test has to be run first */
118    public void testPopulate() {
119       try {
120          Map m=new HashMap();
121          for(int i=0; i < 10; i++)
122            m.put("key" + i, "val" + i);
123          cache.put("/a/b/c", m);
124          cache.load("/1/2/3/4/5");
125          cache.put("/1/2/3/4/5", null);
126          cache.put("/1/2/3/4/5/a", null);
127          cache.put("/1/2/3/4/5/b", null);
128          cache.put("/1/2/3/4/5/c", null);
129          cache.put("/1/2/3/4/5/d", null);
130          cache.put("/1/2/3/4/5/e", null);
131          cache.put("/1/2/3/4/5/d/one", null);
132          cache.put("/1/2/3/4/5/d/two", null);
133          cache.put("/1/2/3/4/5/d/three", null);
134          // cache.put("/a/b/c", "newKey", "newValue");
135
System.out.println("cache: " + cache);
136
137          assertTrue(cache.exists("/1/2/3/4"));
138          assertTrue(cache.exists("/a/b/c"));
139          assertFalse(cache.exists("/a/b/c/d"));
140       }
141       catch(Exception JavaDoc e) {
142          fail(e.toString());
143       }
144    }
145
146
147    public void testPreloading() throws CacheException {
148       cache.remove("/");
149       cache.put("1/2/3/4/5/d", "key", "val");
150       cache.evict(Fqn.fromString("1/2/3/4/5/d"));
151       System.out.println("-- checking for 1/2/3/4/5/d");
152       assertFalse(cache.exists("1/2/3/4/5/d")); // exists() doesn't load
153
cache.get("1/2/3/4/5/d"); // get *does* load
154
assertTrue(cache.exists("1/2/3/4/5/d"));
155       System.out.println("-- 1/2/3/4/5/d exists");
156    }
157
158
159
160    public void testCacheLoading2() throws CacheException {
161       Set keys=null;
162       cache.put("/a/b/c", "key", "val");
163       try {
164          keys=cache.getKeys("/a/b/c");
165          assertNotNull(keys);
166          assertEquals(1, keys.size());
167       }
168       catch(Exception JavaDoc e) {
169          fail(e.toString());
170       }
171
172       try {
173          keys.add("myKey");
174       }
175       catch(UnsupportedOperationException JavaDoc ex) {
176          fail("unsupported operation: " + ex);
177       }
178    }
179
180
181    public void testExists() throws Exception JavaDoc {
182       cache.put("/eins/zwei/drei", "key1", "val1");
183       assertTrue(cache.exists("/eins/zwei/drei"));
184       assertTrue(cache.exists("/eins/zwei/drei", "key1"));
185       assertFalse(cache.exists("/eins/zwei/drei", "key2"));
186       assertFalse(cache.exists("/uno/due/tre"));
187       assertFalse(cache.exists("/une/due/tre", "key1"));
188    }
189
190    public void testGetChildren() {
191       try {
192          cache.put("/1/2/3/4/5/d/one", null);
193          cache.put("/1/2/3/4/5/d/two", null);
194          cache.put("/1/2/3/4/5/d/three", null);
195          Set children=cache.getChildrenNames("/1/2/3/4/5/d");
196          assertNotNull(children);
197          assertEquals(3, children.size());
198          assertTrue(children.contains("one"));
199          assertTrue(children.contains("two"));
200          assertTrue(children.contains("three"));
201       }
202       catch(Exception JavaDoc e) {
203          fail(e.toString());
204       }
205    }
206
207
208    public void testGetChildrenWithEviction() throws CacheException {
209       cache.put("/a/b/c/1", null);
210       cache.put("/a/b/c/2", null);
211       cache.put("/a/b/c/3", null);
212       cache.evict(Fqn.fromString("/a/b/c/1"));
213       cache.evict(Fqn.fromString("/a/b/c/2"));
214       cache.evict(Fqn.fromString("/a/b/c/3"));
215       cache.evict(Fqn.fromString("/a/b/c"));
216       cache.evict(Fqn.fromString("/a/b"));
217       cache.evict(Fqn.fromString("/a"));
218       cache.evict(Fqn.fromString("/"));
219       Set children=cache.getChildrenNames("/a/b/c");
220       assertNotNull(children);
221       assertEquals(3, children.size());
222       assertTrue(children.contains("1"));
223       assertTrue(children.contains("2"));
224       assertTrue(children.contains("3"));
225    }
226
227    public void testGetChildren2() {
228       try {
229          cache.put("/1", null);
230          cache.put("a", null);
231          Set children=cache.getChildrenNames("/");
232          assertNotNull(children);
233          assertEquals(2, children.size());
234          assertTrue(children.contains("1"));
235          assertTrue(children.contains("a"));
236       }
237       catch(Exception JavaDoc e) {
238          fail(e.toString());
239       }
240    }
241
242    public void testGetChildren3() {
243       try {
244          cache.put("/1", null);
245          cache.put("a", null);
246          Set children=cache.getChildrenNames("");
247          assertNotNull(children);
248          assertEquals(2, children.size());
249          assertTrue(children.contains("1"));
250          assertTrue(children.contains("a"));
251       }
252       catch(Exception JavaDoc e) {
253          fail(e.toString());
254       }
255    }
256
257    public void testGetChildren4() {
258       try {
259          if(!cache.exists("/a/b/c"))
260             cache.put("/a/b/c", null);
261          Set children=cache.getChildrenNames((Fqn)null);
262          assertNull(children);
263       }
264       catch(Exception JavaDoc e) {
265          fail(e.toString());
266       }
267    }
268
269
270    public void testGetChildren5() {
271       try {
272          cache.put("/a/1", null);
273          cache.put("/a/2", null);
274          cache.put("/a/3", null);
275          System.out.println("cache is " + cache.printLockInfo());
276
277          Node n=cache.get("/a");
278          assertNotNull(n);
279
280          Set children=cache.getChildrenNames("/a");
281          assertNotNull(children);
282          assertEquals(3, children.size());
283       }
284       catch(Exception JavaDoc e) {
285          fail(e.toString());
286       }
287    }
288
289
290     public void testGetChildren6() {
291        try {
292           cache.put("/a/1", null);
293           cache.put("/a/2", null);
294           cache.put("/a/3", null);
295           System.out.println("cache is " + cache.printLockInfo());
296           cache.evict(Fqn.fromString("/a/1"));
297           cache.evict(Fqn.fromString("/a/2"));
298           cache.evict(Fqn.fromString("/a/3"));
299           cache.evict(Fqn.fromString("/a"));
300           System.out.println("cache is " + cache.printLockInfo());
301
302           assertNotNull(cache.get("/a"));
303
304           Set children=cache.getChildrenNames("/a");
305           assertNotNull("No children were loaded", children);
306           System.out.println("children: " + children);
307           assertEquals("3 children weren't loaded", 3, children.size());
308        }
309        catch(Exception JavaDoc e) {
310           fail(e.toString());
311        }
312     }
313
314    public void testGetChildren7() {
315       try {
316          cache.put("/a/1", null);
317          cache.put("/a/2", null);
318          cache.put("/a/3", null);
319          cache.put("/a", "test", "test");
320          System.out.println("cache is " + cache.printLockInfo());
321          cache.evict(Fqn.fromString("/a/1"));
322          cache.evict(Fqn.fromString("/a/2"));
323          cache.evict(Fqn.fromString("/a/3"));
324          cache.evict(Fqn.fromString("/a"));
325          System.out.println("cache is " + cache.printLockInfo());
326
327          Object JavaDoc val=cache.get("/a", "test");
328          assertEquals("attributes weren't loaded", "test", val);
329
330          Set children=cache.getChildrenNames("/a");
331          assertNotNull("No children were loaded", children);
332          System.out.println("children: " + children);
333          assertEquals("3 children weren't loaded", 3, children.size());
334       }
335       catch(Exception JavaDoc e) {
336          fail(e.toString());
337       }
338    }
339
340    public void testGetChildren8() {
341       try {
342          cache.put("/a/1", null);
343          cache.put("/a/2", null);
344          cache.put("/a/3", null);
345          System.out.println("cache is " + cache.printLockInfo());
346          cache.evict(Fqn.fromString("/a/1"));
347          cache.evict(Fqn.fromString("/a/2"));
348          cache.evict(Fqn.fromString("/a/3"));
349          cache.evict(Fqn.fromString("/a"));
350          System.out.println("cache is " + cache.printLockInfo());
351
352          assertNull(cache.get("/a", "test"));
353
354          cache.get("/a/1");
355          Set children=cache.getChildrenNames("/a");
356          assertNotNull("No children were loaded", children);
357          System.out.println("children: " + children);
358          assertEquals("3 children weren't loaded", 3, children.size());
359       }
360       catch(Exception JavaDoc e) {
361          fail(e.toString());
362       }
363    }
364
365    public void testGetChildren9() {
366       try {
367          cache.put("/a/1", null);
368          cache.put("/a/2", null);
369          cache.put("/a/3", null);
370          System.out.println("cache is " + cache.printLockInfo());
371          cache.evict(Fqn.fromString("/a/1"));
372          cache.evict(Fqn.fromString("/a/2"));
373          cache.evict(Fqn.fromString("/a/3"));
374          cache.evict(Fqn.fromString("/a"));
375          System.out.println("cache is " + cache.printLockInfo());
376
377          assertNull(cache.get("/a", "test"));
378
379          cache.get("/a/1");
380          Set children=cache.getChildrenNames("/a");
381          assertNotNull("No children were loaded", children);
382          System.out.println("children: " + children);
383          assertEquals("3 children weren't loaded", 3, children.size());
384
385          cache.evict(Fqn.fromString("/a/1"));
386          cache.evict(Fqn.fromString("/a/2"));
387          cache.evict(Fqn.fromString("/a/3"));
388          cache.evict(Fqn.fromString("/a"));
389          System.out.println("cache is " + cache.printLockInfo());
390
391          assertNull(cache.get("/a", "test"));
392
393          cache.get("/a/1");
394          children=cache.getChildrenNames("/a");
395          assertNotNull("No children were loaded", children);
396          System.out.println("children: " + children);
397          assertEquals("3 children weren't loaded", 3, children.size());
398       }
399       catch(Exception JavaDoc e) {
400          fail(e.toString());
401       }
402    }
403
404
405     public void testGetChildren10() {
406       try {
407          cache.put("/a/1", null);
408          cache.put("/a/2", null);
409          cache.put("/a/3", null);
410          System.out.println("cache is " + cache.printLockInfo());
411          cache.evict(Fqn.fromString("/a/1"));
412          cache.evict(Fqn.fromString("/a/2"));
413          cache.evict(Fqn.fromString("/a/3"));
414          cache.evict(Fqn.fromString("/a"));
415          System.out.println("cache is " + cache.printLockInfo());
416
417          assertNull(cache.get("/a", "test"));
418
419          cache.get("/a/1");
420          Set children=cache.getChildrenNames("/a");
421          assertNotNull("No children were loaded", children);
422          System.out.println("children: " + children);
423          assertEquals("3 children weren't loaded", 3, children.size());
424
425          children=cache.getChildrenNames("/a");
426          assertNotNull("No children were loaded", children);
427          System.out.println("children: " + children);
428          assertEquals("3 children weren't loaded", 3, children.size());
429       }
430       catch(Exception JavaDoc e) {
431          fail(e.toString());
432       }
433    }
434
435    public void testRemoveData() throws Exception JavaDoc {
436       String JavaDoc key="/x/y/z/";
437       cache.put(key, "keyA", "valA");
438       cache.put(key, "keyB", "valB");
439       cache.put(key, "keyC", "valC");
440       assertEquals(3, cache.getKeys(key).size());
441       cache.removeData(key);
442       Set keys=cache.getKeys(key);
443       assertNull(keys);
444       cache.remove("/x");
445       Object JavaDoc val=cache.get(key, "keyA");
446       assertNull(val);
447    }
448
449
450    public void testRemoveData2() throws Exception JavaDoc {
451       Set keys;
452       Fqn key=Fqn.fromString("/x/y/z/");
453       cache.put(key, "keyA", "valA");
454       cache.put(key, "keyB", "valB");
455       cache.put(key, "keyC", "valC");
456       keys=cache.getKeys(key);
457       assertEquals(3, keys.size());
458       cache.removeData(key);
459       cache.evict(key);
460       keys=cache.getKeys(key);
461       assertEquals(0, keys.size());
462    }
463
464    public void testRemoveData3() throws Exception JavaDoc {
465       Set keys;
466       Fqn key=Fqn.fromString("/x/y/z/");
467       cache.put(key, "keyA", "valA");
468       cache.put(key, "keyB", "valB");
469       cache.put(key, "keyC", "valC");
470       keys=cache.getKeys(key);
471       assertEquals(3, keys.size());
472       cache.evict(key);
473       cache.removeData(key);
474       keys=cache.getKeys(key);
475       assertNull(keys);
476    }
477
478    public void testRemoveKey() throws Exception JavaDoc {
479       String JavaDoc key="/x/y/z/";
480       cache.put(key, "keyA", "valA");
481       cache.put(key, "keyB", "valB");
482       cache.put(key, "keyC", "valC");
483       cache.remove(key, "keyA");
484       assertEquals(2, cache.getKeys(key).size());
485       cache.remove("/x");
486    }
487
488
489    public void testRemoveKey2() throws CacheException {
490       final String JavaDoc NODE="/test";
491       final String JavaDoc KEY="key";
492       Object JavaDoc retval=null;
493       cache.remove(NODE);
494       retval=cache.put(NODE, KEY, new Integer JavaDoc(10));
495       assertNull(retval);
496       retval=cache.remove(NODE, KEY);
497       assertEquals(new Integer JavaDoc(10), retval);
498       retval=cache.remove(NODE, KEY);
499       assertNull(retval);
500    }
501
502    public void testRemoveKey3() throws CacheException {
503       final String JavaDoc NODE="/test";
504       final String JavaDoc KEY="key";
505       Object JavaDoc retval=null;
506       cache.remove(NODE);
507       retval=cache.put(NODE, KEY, new Integer JavaDoc(10));
508       assertNull(retval);
509
510       cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
511
retval=cache.remove(NODE, KEY);
512       assertEquals(new Integer JavaDoc(10), retval);
513
514       cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
515
retval=cache.remove(NODE, KEY);
516       assertNull(retval);
517    }
518
519
520    public void testRemove() throws Exception JavaDoc {
521       String JavaDoc key="/x/y/z/";
522       cache.put(key, "keyA", "valA");
523       cache.put(key, "keyB", "valB");
524       cache.put(key, "keyC", "valC");
525       cache.remove("/x");
526       assertNull(cache.get(key, "keyA"));
527       Set keys=cache.getKeys(key);
528       assertNull(keys);
529       cache.remove("/x");
530    }
531
532
533    public void testRemoveRoot() throws Exception JavaDoc {
534       assertNull(cache.getKeys("/"));
535       cache.put("/1/2/3/4/5", null);
536       cache.put("uno/due/tre", null);
537       cache.put("1/2/3/a", null);
538       cache.put("/eins/zwei/drei", null);
539       cache.put("/one/two/three", null);
540       cache.remove("/");
541       assertEquals(null, cache.getKeys("/"));
542    }
543
544
545    public void testEvictionWithCacheLoader() throws Exception JavaDoc {
546       cache.put("/first/second", "key1", "val1"); // stored in cache loader
547
cache.put("/first/second/third", "key2", "val2"); // stored in cache loader
548
cache.evict(Fqn.fromString("/first/second")); // doesn't remove node, just data !
549
assertTrue(cache.exists("/first/second/third"));
550       assertTrue(cache.exists("/first/second"));
551       assertTrue(cache.exists("/first"));
552       String JavaDoc val=(String JavaDoc)cache.get("/first/second", "key1"); // should be loaded from cache loader
553
assertEquals("val1", val);
554       assertTrue(cache.exists("/first/second/third"));
555       assertTrue(cache.exists("/first/second"));
556       assertTrue(cache.exists("/first"));
557    }
558
559
560    public void testEvictionWithCacheLoader2() throws Exception JavaDoc {
561        cache.put("/first/second/third", "key1", "val1"); // stored in cache loader
562
cache.evict(Fqn.fromString("/first/second/third")); // removes node, because there are no children
563
assertFalse(cache.exists("/first/second/third"));
564        assertTrue(cache.exists("/first/second"));
565        assertTrue(cache.exists("/first"));
566        String JavaDoc val=(String JavaDoc)cache.get("/first/second/third", "key1"); // should be loaded from cache loader
567
assertEquals("val1", val);
568        assertTrue(cache.exists("/first/second/third"));
569        assertTrue(cache.exists("/first/second"));
570        assertTrue(cache.exists("/first"));
571     }
572
573
574
575    public void testEvictionWithGetChildrenNames() throws Exception JavaDoc {
576       cache.put("/a/1", null);
577       cache.put("/a/2", null);
578       cache.put("/a/3", null);
579       // cache.put("/a/1/tmp", null);
580
cache.evict(Fqn.fromString("/a/1"));
581       cache.evict(Fqn.fromString("/a/2"));
582       cache.evict(Fqn.fromString("/a/3"));
583       cache.evict(Fqn.fromString("/a"));
584
585       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
586       mgr.begin();
587       tx=mgr.getTransaction();
588       Set children=cache.getChildrenNames("/a");
589       assertEquals(3, children.size());
590       assertTrue(children.contains("1"));
591       assertTrue(children.contains("2"));
592       assertTrue(children.contains("3"));
593       assertEquals(4, cache.getNumberOfLocksHeld());
594       tx.commit();
595    }
596
597
598    public void testTxPutCommit() throws Exception JavaDoc, NotSupportedException JavaDoc {
599       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
600       mgr.begin();
601       tx=mgr.getTransaction();
602
603       cache.put("/one/two/three", "key1", "val1");
604       cache.put("/one/two/three/four", "key2", "val2");
605       tx.commit();
606       assertNotNull(cache.getKeys("/one/two/three"));
607       Set children=cache.getChildrenNames("/one");
608       assertEquals(1, children.size());
609       cache.remove("/");
610    }
611
612
613    public void testTxPutRollback() throws Exception JavaDoc, NotSupportedException JavaDoc {
614       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
615
616       cache.remove("/one");
617
618       mgr.begin();
619       tx=mgr.getTransaction();
620
621       cache.put("/one/two/three", "key1", "val1");
622       cache.put("/one/two/three/four", "key2", "val2");
623       tx.rollback();
624       assertNull(cache.getKeys("/one/two/three"));
625       Set children=cache.getChildrenNames("/one");
626       assertNull(children);
627    }
628
629
630
631
632
633
634
635
636    /**
637     * Tests basic operations without a transaction.
638     */

639    public void testBasicOperations()
640       throws Exception JavaDoc {
641
642       doTestBasicOperations();
643    }
644
645    /**
646     * Tests basic operations with a transaction.
647     */

648    public void testBasicOperationsTransactional()
649       throws Exception JavaDoc {
650
651       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
652       mgr.begin();
653       tx=mgr.getTransaction();
654       doTestBasicOperations();
655       tx.commit();
656    }
657
658    /**
659     * Tests basic operations.
660     */

661    private void doTestBasicOperations() throws Exception JavaDoc {
662
663       /* One FQN only. */
664       doPutTests(new Fqn("key"));
665       doRemoveTests(new Fqn("key"));
666       // assertEquals(0, loader.loadEntireState().length);
667

668       /* Add three FQNs, middle FQN last. */
669       doPutTests(new Fqn("key1"));
670       doPutTests(new Fqn("key3"));
671       doPutTests(new Fqn("key2"));
672       assertEquals(4, loader.get(new Fqn("key1")).size());
673       assertEquals(4, loader.get(new Fqn("key2")).size());
674       assertEquals(4, loader.get(new Fqn("key3")).size());
675
676       /* Remove middle FQN first, then the others. */
677       doRemoveTests(new Fqn("key2"));
678       doRemoveTests(new Fqn("key3"));
679       doRemoveTests(new Fqn("key1"));
680       assertEquals(null, loader.get(new Fqn("key1")));
681       assertEquals(null, loader.get(new Fqn("key2")));
682       assertEquals(null, loader.get(new Fqn("key3")));
683       // assertEquals(0, loader.loadEntireState().length);
684
}
685
686    /**
687     * Do basic put tests for a given FQN.
688     */

689    private void doPutTests(Fqn fqn)
690       throws Exception JavaDoc {
691
692       assertTrue(!loader.exists(fqn));
693
694       /* put(Fqn,Object,Object) and get(Fqn,Object) */
695       Object JavaDoc oldVal;
696       oldVal = loader.put(fqn, "one", "two");
697       assertNull(oldVal);
698       oldVal = loader.put(fqn, "three", "four");
699       assertNull(oldVal);
700       assertEquals("two", loader.get(fqn, "one"));
701       assertEquals("four", loader.get(fqn, "three"));
702       oldVal = loader.put(fqn, "one", "xxx");
703       assertEquals("two", oldVal);
704       oldVal = loader.put(fqn, "one", "two");
705       assertEquals("xxx", oldVal);
706
707       /* get(Fqn) */
708       Map map = loader.get(fqn);
709       assertEquals(2, map.size());
710       assertEquals("two", map.get("one"));
711       assertEquals("four", map.get("three"));
712
713       /* put(Fqn,Map) */
714       map.put("five", "six");
715       map.put("seven", "eight");
716       loader.put(fqn, map);
717       assertEquals("six", loader.get(fqn, "five"));
718       assertEquals("eight", loader.get(fqn, "seven"));
719       assertEquals(map, loader.get(fqn));
720       assertEquals(4, map.size());
721
722       assertTrue(loader.exists(fqn));
723    }
724
725    /**
726     * Do basic remove tests for a given FQN.
727     */

728    private void doRemoveTests(Fqn fqn)
729       throws Exception JavaDoc {
730
731       /* remove(Fqn,Object) */
732       Object JavaDoc oldVal;
733       oldVal = loader.remove(fqn, "one");
734       assertEquals("two", oldVal);
735       oldVal = loader.remove(fqn, "five");
736       assertEquals("six", oldVal);
737       assertEquals(null, loader.get(fqn, "one"));
738       assertEquals(null, loader.get(fqn, "five"));
739       assertEquals("four", loader.get(fqn, "three"));
740       assertEquals("eight", loader.get(fqn, "seven"));
741       Map map = loader.get(fqn);
742       assertEquals(2, map.size());
743       assertEquals("four", map.get("three"));
744       assertEquals("eight", map.get("seven"));
745
746       /* remove(Fqn) */
747       assertTrue(loader.exists(fqn));
748       loader.remove(fqn);
749       assertEquals(null, loader.get(fqn, "three"));
750       assertEquals(null, loader.get(fqn, "seven"));
751       map = loader.get(fqn);
752       assertEquals(null, map);
753       assertTrue(!loader.exists(fqn));
754    }
755
756    /**
757     * Tests creating implicit intermediate nodes when a leaf node is created,
758     * and tests removing subtrees.
759     */

760    public void testMultiLevelTree()
761       throws Exception JavaDoc {
762
763       /* Create top level node implicitly. */
764       assertTrue(!loader.exists(new Fqn("key0")));
765       loader.put(Fqn.fromString("/key0/level1/level2"), null);
766       assertTrue(loader.exists(Fqn.fromString("/key0/level1/level2")));
767       assertTrue(loader.exists(Fqn.fromString("/key0/level1")));
768       assertTrue(loader.exists(new Fqn("key0")));
769
770       /* Remove leaf, leaving implicitly created middle level. */
771       loader.put(Fqn.fromString("/key0/x/y"), null);
772       assertTrue(loader.exists(Fqn.fromString("/key0/x/y")));
773       assertTrue(loader.exists(Fqn.fromString("/key0/x")));
774       loader.remove(Fqn.fromString("/key0/x/y"));
775       assertTrue(!loader.exists(Fqn.fromString("/key0/x/y")));
776       assertTrue(loader.exists(Fqn.fromString("/key0/x")));
777
778       /* Delete top level to delete everything. */
779       loader.remove(new Fqn("key0"));
780       assertTrue(!loader.exists(new Fqn("key0")));
781       assertTrue(!loader.exists(Fqn.fromString("/key0/level1/level2")));
782       assertTrue(!loader.exists(Fqn.fromString("/key0/level1")));
783       assertTrue(!loader.exists(Fqn.fromString("/key0/x")));
784
785       /* Add three top level nodes as context. */
786       loader.put(new Fqn("key1"), null);
787       loader.put(new Fqn("key2"), null);
788       loader.put(new Fqn("key3"), null);
789       assertTrue(loader.exists(new Fqn("key1")));
790       assertTrue(loader.exists(new Fqn("key2")));
791       assertTrue(loader.exists(new Fqn("key3")));
792
793       /* Put /key3/level1/level2. level1 should be implicitly created. */
794       assertTrue(!loader.exists(Fqn.fromString("/key3/level1")));
795       assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2")));
796       loader.put(Fqn.fromString("/key3/level1/level2"), null);
797       assertTrue(loader.exists(Fqn.fromString("/key3/level1/level2")));
798       assertTrue(loader.exists(Fqn.fromString("/key3/level1")));
799
800       /* Context nodes should still be intact. */
801       assertTrue(loader.exists(new Fqn("key1")));
802       assertTrue(loader.exists(new Fqn("key2")));
803       assertTrue(loader.exists(new Fqn("key3")));
804
805       /* Remove middle level only. */
806       loader.remove(Fqn.fromString("/key3/level1"));
807       assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2")));
808       assertTrue(!loader.exists(Fqn.fromString("/key3/level1")));
809
810       /* Context nodes should still be intact. */
811       assertTrue(loader.exists(new Fqn("key1")));
812       assertTrue(loader.exists(new Fqn("key2")));
813       assertTrue(loader.exists(new Fqn("key3")));
814
815       /* Delete first root, leaving other roots. */
816       loader.remove(new Fqn("key1"));
817       assertTrue(!loader.exists(new Fqn("key1")));
818       assertTrue(loader.exists(new Fqn("key2")));
819       assertTrue(loader.exists(new Fqn("key3")));
820
821       /* Delete last root, leaving other roots. */
822       loader.remove(new Fqn("key3"));
823       assertTrue(loader.exists(new Fqn("key2")));
824       assertTrue(!loader.exists(new Fqn("key3")));
825
826       /* Delete final root, leaving none. */
827       loader.remove(new Fqn("key2"));
828       assertTrue(!loader.exists(new Fqn("key0")));
829       assertTrue(!loader.exists(new Fqn("key1")));
830       assertTrue(!loader.exists(new Fqn("key2")));
831       assertTrue(!loader.exists(new Fqn("key3")));
832
833       /* Repeat all tests above using put(Fqn,Object,Object) and get(Fqn) */
834
835       assertNull(loader.get(new Fqn("key0")));
836       loader.put(Fqn.fromString("/key0/level1/level2"), "a", "b");
837       assertNotNull(loader.get(Fqn.fromString("/key0/level1/level2")));
838       assertNull(loader.get(Fqn.fromString("/key0/level1