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")));
839       assertNull(loader.get(new Fqn("key0")));
840
841       loader.put(Fqn.fromString("/key0/x/y"), "a", "b");
842       assertNotNull(loader.get(Fqn.fromString("/key0/x/y")));
843       assertNull(loader.get(Fqn.fromString("/key0/x")));
844       loader.remove(Fqn.fromString("/key0/x/y"));
845       assertNull(loader.get(Fqn.fromString("/key0/x/y")));
846       assertNull(loader.get(Fqn.fromString("/key0/x")));
847
848       loader.remove(new Fqn("key0"));
849       assertNull(loader.get(new Fqn("key0")));
850       assertNull(loader.get(Fqn.fromString("/key0/level1/level2")));
851       assertNull(loader.get(Fqn.fromString("/key0/level1")));
852       assertNull(loader.get(Fqn.fromString("/key0/x")));
853
854       loader.put(new Fqn("key1"), "a", "b");
855       loader.put(new Fqn("key2"), "a", "b");
856       loader.put(new Fqn("key3"), "a", "b");
857       assertNotNull(loader.get(new Fqn("key1")));
858       assertNotNull(loader.get(new Fqn("key2")));
859       assertNotNull(loader.get(new Fqn("key3")));
860
861       assertNull(loader.get(Fqn.fromString("/key3/level1")));
862       assertNull(loader.get(Fqn.fromString("/key3/level1/level2")));
863       loader.put(Fqn.fromString("/key3/level1/level2"), "a", "b");
864       assertNotNull(loader.get(Fqn.fromString("/key3/level1/level2")));
865       assertNull(loader.get(Fqn.fromString("/key3/level1")));
866
867       assertNotNull(loader.get(new Fqn("key1")));
868       assertNotNull(loader.get(new Fqn("key2")));
869       assertNotNull(loader.get(new Fqn("key3")));
870
871       loader.remove(Fqn.fromString("/key3/level1"));
872       assertNull(loader.get(Fqn.fromString("/key3/level1/level2")));
873       assertNull(loader.get(Fqn.fromString("/key3/level1")));
874
875       assertNotNull(loader.get(new Fqn("key1")));
876       assertNotNull(loader.get(new Fqn("key2")));
877       assertNotNull(loader.get(new Fqn("key3")));
878
879       loader.remove(new Fqn("key1"));
880       assertNull(loader.get(new Fqn("key1")));
881       assertNotNull(loader.get(new Fqn("key2")));
882       assertNotNull(loader.get(new Fqn("key3")));
883
884       loader.remove(new Fqn("key3"));
885       assertNotNull(loader.get(new Fqn("key2")));
886       assertNull(loader.get(new Fqn("key3")));
887
888       loader.remove(new Fqn("key2"));
889       assertNull(loader.get(new Fqn("key0")));
890       assertNull(loader.get(new Fqn("key1")));
891       assertNull(loader.get(new Fqn("key2")));
892       assertNull(loader.get(new Fqn("key3")));
893    }
894
895    /**
896     * Tests the getChildrenNames() method.
897     */

898    public void testGetChildrenNames()
899       throws Exception JavaDoc {
900
901       checkChildren(new Fqn(), null);
902       checkChildren(Fqn.fromString("/key0"), null);
903
904       loader.put(Fqn.fromString("/key0"), null);
905       checkChildren(new Fqn(), new String JavaDoc[] { "key0" });
906
907       loader.put(Fqn.fromString("/key1/x"), null);
908       checkChildren(new Fqn(), new String JavaDoc[] { "key0", "key1" });
909       checkChildren(Fqn.fromString("/key1"), new String JavaDoc[] { "x" });
910
911       loader.remove(Fqn.fromString("/key1/x"));
912       checkChildren(new Fqn(), new String JavaDoc[] { "key0", "key1" });
913       checkChildren(Fqn.fromString("/key0"), null);
914       checkChildren(Fqn.fromString("/key1"), null);
915
916       loader.put(Fqn.fromString("/key0/a"), null);
917       loader.put(Fqn.fromString("/key0/ab"), null);
918       loader.put(Fqn.fromString("/key0/abc"), null);
919       checkChildren(Fqn.fromString("/key0"),
920                     new String JavaDoc[] { "a", "ab", "abc" });
921
922       loader.put(Fqn.fromString("/key0/xxx"), null);
923       loader.put(Fqn.fromString("/key0/xx"), null);
924       loader.put(Fqn.fromString("/key0/x"), null);
925       checkChildren(Fqn.fromString("/key0"),
926                     new String JavaDoc[] { "a", "ab", "abc", "x", "xx", "xxx" });
927
928       loader.put(Fqn.fromString("/key0/a/1"), null);
929       loader.put(Fqn.fromString("/key0/a/2"), null);
930       loader.put(Fqn.fromString("/key0/a/2/1"), null);
931       checkChildren(Fqn.fromString("/key0/a/2"), new String JavaDoc[] { "1" });
932       checkChildren(Fqn.fromString("/key0/a"), new String JavaDoc[] { "1", "2" });
933       checkChildren(Fqn.fromString("/key0"),
934                     new String JavaDoc[] { "a", "ab", "abc", "x", "xx", "xxx" });
935 //
936
// loader.put(Fqn.fromString("/key0/\u0000"), null);
937
// loader.put(Fqn.fromString("/key0/\u0001"), null);
938
// checkChildren(Fqn.fromString("/key0"),
939
// new String[] { "a", "ab", "abc", "x", "xx", "xxx",
940
// "\u0000", "\u0001"});
941
//
942
// loader.put(Fqn.fromString("/\u0001"), null);
943
// checkChildren(new Fqn(), new String[] { "key0", "key1", "\u0001" });
944
//
945
// loader.put(Fqn.fromString("/\u0001/\u0001"), null);
946
// checkChildren(Fqn.fromString("/\u0001"), new String[] { "\u0001" });
947
//
948
// loader.put(Fqn.fromString("/\u0001/\uFFFF"), null);
949
// checkChildren(Fqn.fromString("/\u0001"),
950
// new String[] { "\u0001", "\uFFFF" });
951
//
952
// loader.put(Fqn.fromString("/\u0001/\uFFFF/\u0001"), null);
953
// checkChildren(Fqn.fromString("/\u0001/\uFFFF"),
954
// new String[] { "\u0001" });
955
}
956
957    /**
958     * Checks that the given list of children part names is returned.
959     */

960    private void checkChildren(Fqn fqn, String JavaDoc[] names)
961       throws Exception JavaDoc {
962
963       Set set = loader.getChildrenNames(fqn);
964       if (names != null) {
965          assertEquals(names.length, set.size());
966          for (int i = 0; i < names.length; i += 1) {
967             assertTrue(set.contains(names[i]));
968          }
969       } else {
970          assertNull(set);
971       }
972    }
973
974    /**
975     * Tests basic operations without a transaction.
976     */

977    public void testModifications()
978       throws Exception JavaDoc {
979
980       doTestModifications();
981    }
982
983    /**
984     * Tests basic operations with a transaction.
985     */

986    public void testModificationsTransactional()
987       throws Exception JavaDoc {
988       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
989       mgr.begin();
990       tx=mgr.getTransaction();
991       doTestModifications();
992       tx.commit();
993    }
994
995    /**
996     * Tests modifications.
997     */

998    private void doTestModifications()
999       throws Exception JavaDoc {
1000
1001      /* PUT_KEY_VALUE, PUT_DATA */
1002      List list = createUpdates();
1003      loader.put(list);
1004      checkModifications(list);
1005
1006      /* REMOVE_KEY_VALUE */
1007      list = new ArrayList();
1008      Modification mod = new Modification();
1009      mod.setType(Modification.REMOVE_KEY_VALUE);
1010      mod.setFqn(FQN);
1011      mod.setKey("one");
1012      list.add(mod);
1013      loader.put(list);
1014      checkModifications(list);
1015
1016      /* REMOVE_NODE */
1017      list = new ArrayList();
1018      mod = new Modification();
1019      mod.setType(Modification.REMOVE_NODE);
1020      mod.setFqn(FQN);
1021      list.add(mod);
1022      loader.put(list);
1023      checkModifications(list);
1024      assertEquals(null, loader.get(FQN));
1025
1026      /* REMOVE_DATA */
1027      loader.put(FQN, "one", "two");
1028      list = new ArrayList();
1029      mod = new Modification();
1030      mod.setType(Modification.REMOVE_DATA);
1031      mod.setFqn(FQN);
1032      list.add(mod);
1033      loader.put(list);
1034      checkModifications(list);
1035   }
1036
1037   /**
1038    * Tests a one-phase transaction.
1039    */

1040   public void testOnePhaseTransaction()
1041      throws Exception JavaDoc {
1042      List mods = createUpdates();
1043      loader.prepare(null, mods, true);
1044      checkModifications(mods);
1045   }
1046
1047   /**
1048    * Tests a two-phase transaction.
1049    */

1050   public void testTwoPhaseTransaction()
1051      throws Exception JavaDoc {
1052
1053      Object JavaDoc txnKey = new Object JavaDoc();
1054      List mods = createUpdates();
1055      loader.prepare(txnKey, mods, false);
1056// try {
1057
// checkModifications(mods);
1058
// // fail("Expected lock timeout");
1059
// } catch (DeadlockException expected) {}
1060
loader.commit(txnKey);
1061      checkModifications(mods);
1062   }
1063
1064   /**
1065    * Tests rollback of a two-phase transaction.
1066    */

1067   public void testTransactionRollback()
1068      throws Exception JavaDoc {
1069
1070      loader.remove(Fqn.fromString("/"));
1071
1072      int num=loader.loadEntireState().length;
1073
1074      Object JavaDoc txnKey = new Object JavaDoc();
1075      List mods = createUpdates();
1076      loader.prepare(txnKey, mods, false);
1077      loader.rollback(txnKey);
1078      assertEquals(num, loader.loadEntireState().length);
1079   }
1080
1081   /**
1082    * Creates a set of update (PUT_KEY_VALUE, PUT_DATA) modifications.
1083    */

1084   private List createUpdates() {
1085
1086      List list = new ArrayList();
1087
1088      Modification mod = new Modification();
1089      mod.setType(Modification.PUT_KEY_VALUE);
1090      mod.setFqn(FQN);
1091      mod.setKey("one");
1092      mod.setValue("two");
1093      list.add(mod);
1094
1095      mod = new Modification();
1096      mod.setType(Modification.PUT_KEY_VALUE);
1097      mod.setFqn(FQN);
1098      mod.setKey("three");
1099      mod.setValue("four");
1100      list.add(mod);
1101
1102      Map map = new HashMap();
1103      map.put("five", "six");
1104      map.put("seven", "eight");
1105      mod = new Modification();
1106      mod.setType(Modification.PUT_DATA);
1107      mod.setFqn(FQN);
1108      mod.setData(map);
1109      list.add(mod);
1110
1111      return list;
1112   }
1113
1114   /**
1115    * Checks that a list of modifications was applied.
1116    */

1117   private void checkModifications(List list)
1118      throws Exception JavaDoc {
1119
1120      for (int i = 0; i < list.size(); i += 1) {
1121         Modification mod = (Modification) list.get(i);
1122         Fqn fqn = mod.getFqn();
1123         switch (mod.getType()) {
1124         case Modification.PUT_KEY_VALUE:
1125            assertEquals(mod.getValue(), loader.get(fqn, mod.getKey()));
1126            break;
1127         case Modification.PUT_DATA:
1128            Map map = mod.getData();
1129            for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
1130               Object JavaDoc key = iter.next();
1131               assertEquals(map.get(key), loader.get(fqn, key));
1132            }
1133            break;
1134         case Modification.REMOVE_KEY_VALUE:
1135            assertEquals(null, loader.get(fqn, mod.getKey()));
1136            break;
1137         case Modification.REMOVE_DATA:
1138            map = loader.get(fqn);
1139            assertNull(map);
1140            break;
1141         case Modification.REMOVE_NODE:
1142            assertEquals(null, loader.get(fqn));
1143            break;
1144         default:
1145            fail("unknown type: " + mod);
1146            break;
1147         }
1148      }
1149   }
1150
1151
1152
1153
1154
1155
1156
1157   /**
1158    * Tests that null keys and values work as for a standard Java Map.
1159    */

1160   public void testNullKeysAndValues()
1161      throws Exception JavaDoc {
1162
1163      loader.put(FQN, null, "x");
1164      assertEquals("x", loader.get(FQN, null));
1165      Map map = loader.get(FQN);
1166      assertEquals(1, map.size());
1167      assertEquals("x", map.get(null));
1168
1169      loader.put(FQN, "y", null);
1170      assertEquals(null, loader.get(FQN, "y"));
1171      map = loader.get(FQN);
1172      assertEquals(2, map.size());
1173      assertEquals("x", map.get(null));
1174      assertEquals(null, map.get("y"));
1175
1176      loader.remove(FQN, null);
1177      assertEquals(null, loader.get(FQN, null));
1178      assertEquals(1, loader.get(FQN).size());
1179
1180      loader.remove(FQN, "y");
1181      assertEquals(null, loader.get(FQN, "y"));
1182      assertNull(loader.get(FQN));
1183
1184      map = new HashMap();
1185      map.put(null, null);
1186      loader.put(FQN, map);
1187      assertEquals(map, loader.get(FQN));
1188
1189      loader.remove(FQN);
1190      assertEquals(null, loader.get(FQN));
1191
1192      map = new HashMap();
1193      map.put("xyz", null);
1194      map.put(null, "abc");
1195      loader.put(FQN, map);
1196      assertEquals(map, loader.get(FQN));
1197
1198      loader.remove(FQN);
1199      assertEquals(null, loader.get(FQN));
1200   }
1201
1202   /**
1203    * Test non-default database name.
1204    */

1205   public void testDatabaseName()
1206      throws Exception JavaDoc {
1207
1208      loader.put(FQN, "one", "two");
1209      assertEquals("two", loader.get(FQN, "one"));
1210   }
1211
1212   /**
1213    * Test load/store state.
1214    */

1215   public void testLoadAndStore()
1216      throws Exception JavaDoc {
1217
1218      /* Empty state. */
1219      loader.remove(Fqn.fromString("/"));
1220      // assertEquals(0, loader.loadEntireState().length);
1221
// loader.storeEntireState(new byte[0]);
1222
// assertEquals(0, loader.loadEntireState().length);
1223
// loader.storeEntireState(null);
1224
// assertEquals(0, loader.loadEntireState().length);
1225
// assertEquals(null, loader.get(FQN));
1226

1227      /* Use a complex object to ensure that the class catalog is used. */
1228      Complex c1 = new Complex();
1229      Complex c2 = new Complex(c1);
1230
1231      /* Add objects. */
1232      loader.put(FQN, new Integer JavaDoc(1), c1);
1233      loader.put(FQN, new Integer JavaDoc(2), c2);
1234      assertEquals(c1, loader.get(FQN, new Integer JavaDoc(1)));
1235      assertEquals(c2, loader.get(FQN, new Integer JavaDoc(2)));
1236      assertEquals(2, loader.get(FQN).size());
1237
1238      /* Save state. */
1239      byte[] state = loader.loadEntireState();
1240      assertTrue(state.length > 0);
1241
1242
1243      /* Restore state. */
1244      loader.storeEntireState(state);
1245      assertEquals(c1, loader.get(FQN, new Integer JavaDoc(1)));
1246      assertEquals(c2, loader.get(FQN, new Integer JavaDoc(2)));
1247      assertEquals(2, loader.get(FQN).size());
1248   }
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263   /**
1264    * Complex object whose class description is stored in the class catalog.
1265    */

1266   private static class Complex implements Serializable JavaDoc {
1267
1268      Complex nested;
1269
1270      Complex() {
1271         this(null);
1272      }
1273
1274      Complex(Complex nested) {
1275         this.nested = nested;
1276      }
1277
1278      public boolean equals(Object JavaDoc o) {
1279         try {
1280            Complex x = (Complex) o;
1281            return (nested != null) ? nested.equals(x.nested)
1282                                    : (x.nested == null);
1283         } catch (ClassCastException JavaDoc e) {
1284            return false;
1285         }
1286      }
1287
1288      public int hashCode() {
1289         if(nested == null)
1290            return super.hashCode();
1291         else
1292            return 13 + nested.hashCode();
1293      }
1294   }
1295
1296
1297   public static Test suite() {
1298      return new TestSuite(CacheLoaderTestsBase.class);
1299   }
1300
1301   public static void main(String JavaDoc[] args) {
1302      junit.textui.TestRunner.run(suite());
1303   }
1304
1305}
1306
Popular Tags