KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > test > core > tree > CMSCacheLoaderTestCase


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.test.core.tree;
10
11 import org.jboss.cache.tests.CacheLoaderTests;
12 import org.jboss.cache.tests.SamplePojo;
13 import org.jboss.cache.TreeCache;
14 import org.jboss.cache.Fqn;
15 import org.jboss.cache.Modification;
16 import org.jboss.cache.CacheException;
17 import org.jboss.cache.transaction.DummyTransactionManager;
18 import org.jboss.cache.loader.CacheLoader;
19 import org.jboss.portal.cms.CMS;
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22
23 import java.util.Properties JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.Serializable JavaDoc;
32
33 import junit.framework.TestCase;
34
35 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
36 import javax.transaction.Transaction JavaDoc;
37 import javax.transaction.NotSupportedException JavaDoc;
38
39 /**
40  * Stolen from jboss cache. Need to remove it later.
41  *
42  * @author Bela Ban
43  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
44  * @version $Revision: 1.2 $
45  */

46 public class CMSCacheLoaderTestCase extends TestCase
47 {
48
49    public CMSCacheLoaderTestCase(String JavaDoc name)
50    {
51       super(name);
52    }
53
54    protected CacheLoaderTests testCase;
55    protected CMS cms;
56
57    protected TreeCache cache;
58    protected CacheLoader loader;
59    protected Transaction JavaDoc tx;
60    protected static final Fqn FQN = new Fqn("key");
61
62    protected void setUp() throws Exception JavaDoc
63    {
64       ClassLoader JavaDoc tccl = Thread.currentThread().getContextClassLoader();
65       InputStream JavaDoc in = tccl.getResourceAsStream("cache/domain.xml");
66       Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
67       Element JavaDoc elt = doc.getDocumentElement();
68       cms = new CMS();
69       cms.setConfig(elt);
70       cms.create();
71       cms.start();
72
73       cache = new TreeCache();
74       cache.setCacheMode("local");
75       cache.setCacheLoaderClass("org.jboss.portal.core.impl.tree.loader.CMSCacheLoader");
76       Properties JavaDoc props = new Properties JavaDoc();
77       cache.setCacheLoaderConfig(props);
78
79       cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
80       cache.createService();
81       cache.startService();
82       loader = cache.getCacheLoader();
83    }
84
85    protected void tearDown() throws Exception JavaDoc
86    {
87       try
88       {
89          if (tx != null)
90          {
91             try
92             {
93                tx.commit();
94             }
95             catch (Throwable JavaDoc e)
96             {
97                e.printStackTrace();
98             }
99          }
100          cache.remove("/");
101          cache.stopService();
102          cache.destroyService();
103          cms.stop();
104          cms.destroy();
105       }
106       catch (Exception JavaDoc e)
107       {
108          e.printStackTrace();
109       }
110    }
111
112
113    public void testPrint() throws CacheException
114    {
115       final Fqn NODE = Fqn.fromString("/test");
116       final String JavaDoc KEY = "key";
117       cache.put(NODE, KEY, new Integer JavaDoc(10));
118       cache.evict(NODE);
119       String JavaDoc ret = cache.print(NODE);
120       assertNotNull(ret);
121    }
122
123    public void testPut() throws CacheException
124    {
125       final String JavaDoc NODE = "/test";
126       final String JavaDoc KEY = "key";
127       Object JavaDoc retval = null;
128       cache.remove(NODE);
129       retval = cache.put(NODE, KEY, new Integer JavaDoc(10));
130       assertNull(retval);
131       retval = cache.put(NODE, KEY, new Integer JavaDoc(20));
132       assertEquals(new Integer JavaDoc(10), retval);
133       cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
134

135       retval = cache.put(NODE, KEY, new Integer JavaDoc(30));
136       assertEquals(new Integer JavaDoc(20), retval);
137    }
138
139
140    public void _testSerialization() throws CacheException
141    {
142       SamplePojo pojo = new SamplePojo(39, "Bela");
143       pojo.getHobbies().add("Running");
144       pojo.getHobbies().add("Beerathlon");
145       pojo.getHobbies().add("Triathlon");
146       cache.put("/mypojo", new Integer JavaDoc(322649), pojo);
147       assertNotNull(cache.get("/mypojo", new Integer JavaDoc(322649)));
148       cache.evict(Fqn.fromString("/mypojo"));
149       assertFalse(cache.exists("/mypojo"));
150       SamplePojo pojo2 = (SamplePojo)cache.get("/mypojo", new Integer JavaDoc(322649)); // should fetch from CacheLoader
151
assertNotNull(pojo2);
152       assertEquals(39, pojo2.getAge());
153       assertEquals("Bela", pojo2.getName());
154       assertEquals(3, pojo2.getHobbies().size());
155    }
156
157    /**
158     * Just adds some data that wil be later retrieved. This test has to be run first
159     */

160    public void testPopulate()
161    {
162       try
163       {
164          Map JavaDoc m = new HashMap JavaDoc();
165          for (int i = 0; i < 10; i++)
166          {
167             m.put("key" + i, "val" + i);
168          }
169          cache.put("/a/b/c", m);
170          cache.load("/1/2/3/4/5");
171          cache.put("/1/2/3/4/5", null);
172          cache.put("/1/2/3/4/5/a", null);
173          cache.put("/1/2/3/4/5/b", null);
174          cache.put("/1/2/3/4/5/c", null);
175          cache.put("/1/2/3/4/5/d", null);
176          cache.put("/1/2/3/4/5/e", null);
177          cache.put("/1/2/3/4/5/d/one", null);
178          cache.put("/1/2/3/4/5/d/two", null);
179          cache.put("/1/2/3/4/5/d/three", null);
180          // cache.put("/a/b/c", "newKey", "newValue");
181
System.out.println("cache: " + cache);
182
183          assertTrue(cache.exists("/1/2/3/4"));
184          assertTrue(cache.exists("/a/b/c"));
185          assertFalse(cache.exists("/a/b/c/d"));
186       }
187       catch (Exception JavaDoc e)
188       {
189          fail(e.toString());
190       }
191    }
192
193
194    public void testPreloading() throws CacheException
195    {
196       cache.remove("/");
197       cache.put("1/2/3/4/5/d", "key", "val");
198       cache.evict(Fqn.fromString("1/2/3/4/5/d"));
199       System.out.println("-- checking for 1/2/3/4/5/d");
200       assertFalse(cache.exists("1/2/3/4/5/d")); // exists() doesn't load
201
cache.get("1/2/3/4/5/d"); // get *does* load
202
assertTrue(cache.exists("1/2/3/4/5/d"));
203       System.out.println("-- 1/2/3/4/5/d exists");
204    }
205
206
207    public void testCacheLoading2() throws CacheException
208    {
209       Set JavaDoc keys = null;
210       cache.put("/a/b/c", "key", "val");
211       try
212       {
213          keys = cache.getKeys("/a/b/c");
214          assertNotNull(keys);
215          assertEquals(1, keys.size());
216       }
217       catch (Exception JavaDoc e)
218       {
219          fail(e.toString());
220       }
221
222       try
223       {
224          keys.add("myKey");
225          fail("adding a key to an unmodifiable set should fail");
226       }
227       catch (UnsupportedOperationException JavaDoc ex)
228       {
229          assertTrue("adding a key to an unmodifiable set failed (as expected)", true);
230       }
231    }
232
233
234    public void testExists() throws Exception JavaDoc
235    {
236       cache.put("/eins/zwei/drei", "key1", "val1");
237       assertTrue(cache.exists("/eins/zwei/drei"));
238       assertTrue(cache.exists("/eins/zwei/drei", "key1"));
239       assertFalse(cache.exists("/eins/zwei/drei", "key2"));
240       assertFalse(cache.exists("/uno/due/tre"));
241       assertFalse(cache.exists("/une/due/tre", "key1"));
242    }
243
244    public void testGetChildren()
245    {
246       try
247       {
248          cache.put("/1/2/3/4/5/d/one", null);
249          cache.put("/1/2/3/4/5/d/two", null);
250          cache.put("/1/2/3/4/5/d/three", null);
251          Set JavaDoc children = cache.getChildrenNames("/1/2/3/4/5/d");
252          assertNotNull(children);
253          assertEquals(3, children.size());
254          assertTrue(children.contains("one"));
255          assertTrue(children.contains("two"));
256          assertTrue(children.contains("three"));
257       }
258       catch (Exception JavaDoc e)
259       {
260          e.printStackTrace();
261          fail(e.toString());
262       }
263    }
264
265    public void testGetChildren2()
266    {
267       try
268       {
269          cache.put("/1", null);
270          cache.put("a", null);
271          Set JavaDoc children = cache.getChildrenNames("/");
272          assertNotNull(children);
273          assertEquals(2, children.size());
274          assertTrue(children.contains("1"));
275          assertTrue(children.contains("a"));
276       }
277       catch (Exception JavaDoc e)
278       {
279          fail(e.toString());
280       }
281    }
282
283    public void testGetChildren3()
284    {
285       try
286       {
287          cache.put("/1", null);
288          cache.put("a", null);
289          Set JavaDoc children = cache.getChildrenNames("");
290          assertNotNull(children);
291          assertEquals(2, children.size());
292          assertTrue(children.contains("1"));
293          assertTrue(children.contains("a"));
294       }
295       catch (Exception JavaDoc e)
296       {
297          fail(e.toString());
298       }
299    }
300
301    public void testGetChildren4()
302    {
303       try
304       {
305          if (!cache.exists("/a/b/c"))
306          {
307             cache.put("/a/b/c", null);
308          }
309          Set JavaDoc children = cache.getChildrenNames((Fqn)null);
310          assertNull(children);
311       }
312       catch (Exception JavaDoc e)
313       {
314          fail(e.toString());
315       }
316    }
317
318
319    public void testRemoveData() throws Exception JavaDoc
320    {
321       String JavaDoc key = "/x/y/z/";
322       cache.put(key, "keyA", "valA");
323       cache.put(key, "keyB", "valB");
324       cache.put(key, "keyC", "valC");
325       assertEquals(3, cache.getKeys(key).size());
326       cache.removeData(key);
327       Set JavaDoc keys = cache.getKeys(key);
328       assertNull(keys);
329       cache.remove("/x");
330       Object JavaDoc val = cache.get(key, "keyA");
331       assertNull(val);
332    }
333
334
335    public void testRemoveData2() throws Exception JavaDoc
336    {
337       Set JavaDoc keys;
338       Fqn key = Fqn.fromString("/x/y/z/");
339       cache.put(key, "keyA", "valA");
340       cache.put(key, "keyB", "valB");
341       cache.put(key, "keyC", "valC");
342       keys = cache.getKeys(key);
343       assertEquals(3, keys.size());
344       cache.removeData(key);
345       cache.evict(key);
346       keys = cache.getKeys(key);
347       assertEquals(0, keys.size());
348    }
349
350    public void testRemoveData3() throws Exception JavaDoc
351    {
352       Set JavaDoc keys;
353       Fqn key = Fqn.fromString("/x/y/z/");
354       cache.put(key, "keyA", "valA");
355       cache.put(key, "keyB", "valB");
356       cache.put(key, "keyC", "valC");
357       keys = cache.getKeys(key);
358       assertEquals(3, keys.size());
359       cache.evict(key);
360       cache.removeData(key);
361       keys = cache.getKeys(key);
362       assertNull(keys);
363    }
364
365    public void testRemoveKey() throws Exception JavaDoc
366    {
367       String JavaDoc key = "/x/y/z/";
368       cache.put(key, "keyA", "valA");
369       cache.put(key, "keyB", "valB");
370       cache.put(key, "keyC", "valC");
371       cache.remove(key, "keyA");
372       assertEquals(2, cache.getKeys(key).size());
373       cache.remove("/x");
374    }
375
376
377    public void testRemoveKey2() throws CacheException
378    {
379       final String JavaDoc NODE = "/test";
380       final String JavaDoc KEY = "key";
381       Object JavaDoc retval = null;
382       cache.remove(NODE);
383       retval = cache.put(NODE, KEY, new Integer JavaDoc(10));
384       assertNull(retval);
385       retval = cache.remove(NODE, KEY);
386       assertEquals(new Integer JavaDoc(10), retval);
387       retval = cache.remove(NODE, KEY);
388       assertNull(retval);
389    }
390
391    public void testRemoveKey3() throws CacheException
392    {
393       final String JavaDoc NODE = "/test";
394       final String JavaDoc KEY = "key";
395       Object JavaDoc retval = null;
396       cache.remove(NODE);
397       retval = cache.put(NODE, KEY, new Integer JavaDoc(10));
398       assertNull(retval);
399
400       cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
401
retval = cache.remove(NODE, KEY);
402       assertEquals(new Integer JavaDoc(10), retval);
403
404       cache.evict(Fqn.fromString(NODE)); // evicts from memory, but *not* from store
405
retval = cache.remove(NODE, KEY);
406       assertNull(retval);
407    }
408
409
410    public void testRemove() throws Exception JavaDoc
411    {
412       String JavaDoc key = "/x/y/z/";
413       cache.put(key, "keyA", "valA");
414       cache.put(key, "keyB", "valB");
415       cache.put(key, "keyC", "valC");
416       cache.remove("/x");
417       assertNull(cache.get(key, "keyA"));
418       Set JavaDoc keys = cache.getKeys(key);
419       assertNull(keys);
420       cache.remove("/x");
421    }
422
423
424    public void testRemoveRoot() throws Exception JavaDoc
425    {
426       assertEquals(0, cache.getKeys("/").size());
427       cache.put("/1/2/3/4/5", null);
428       cache.put("uno/due/tre", null);
429       cache.put("1/2/3/a", null);
430       cache.put("/eins/zwei/drei", null);
431       cache.put("/one/two/three", null);
432       cache.remove("/");
433       assertEquals(0, cache.getKeys("/").size());
434    }
435
436
437    public void testEvictionWithCacheLoader() throws Exception JavaDoc
438    {
439       cache.put("/first/second", "key1", "val1"); // stored in cache loader
440
cache.put("/first/second/third", "key2", "val2"); // stored in cache loader
441
cache.evict(Fqn.fromString("/first/second")); // doesn't remove node, just data !
442
assertTrue(cache.exists("/first/second/third"));
443       assertTrue(cache.exists("/first/second"));
444       assertTrue(cache.exists("/first"));
445       String JavaDoc val = (String JavaDoc)cache.get("/first/second", "key1"); // should be loaded from cache loader
446
assertEquals("val1", val);
447       assertTrue(cache.exists("/first/second/third"));
448       assertTrue(cache.exists("/first/second"));
449       assertTrue(cache.exists("/first"));
450    }
451
452
453    public void testEvictionWithCacheLoader2() throws Exception JavaDoc
454    {
455       cache.put("/first/second/third", "key1", "val1"); // stored in cache loader
456
cache.evict(Fqn.fromString("/first/second/third")); // removes node, because there are no children
457
assertFalse(cache.exists("/first/second/third"));
458       assertTrue(cache.exists("/first/second"));
459       assertTrue(cache.exists("/first"));
460       String JavaDoc val = (String JavaDoc)cache.get("/first/second/third", "key1"); // should be loaded from cache loader
461
assertEquals("val1", val);
462       assertTrue(cache.exists("/first/second/third"));
463       assertTrue(cache.exists("/first/second"));
464       assertTrue(cache.exists("/first"));
465    }
466
467
468    public void testEvictionWithGetChildrenNames() throws Exception JavaDoc
469    {
470       cache.put("/a/1", null);
471       cache.put("/a/2", null);
472       cache.put("/a/3", null);
473       // cache.put("/a/1/tmp", null);
474
cache.evict(Fqn.fromString("/a/1"));
475       cache.evict(Fqn.fromString("/a/2"));
476       cache.evict(Fqn.fromString("/a/3"));
477       cache.evict(Fqn.fromString("/a"));
478       Set JavaDoc children = cache.getChildrenNames("/a");
479       assertNotNull(children);
480       assertEquals(3, children.size());
481       assertTrue(children.contains("1"));
482       assertTrue(children.contains("2"));
483       assertTrue(children.contains("3"));
484    }
485
486
487    public void testTxPutCommit() throws Exception JavaDoc, NotSupportedException JavaDoc
488    {
489       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
490       mgr.begin();
491       tx = mgr.getTransaction();
492
493       cache.put("/one/two/three", "key1", "val1");
494       cache.put("/one/two/three/four", "key2", "val2");
495       tx.commit();
496       assertNotNull(cache.getKeys("/one/two/three"));
497       Set JavaDoc children = cache.getChildrenNames("/one");
498       assertEquals(1, children.size());
499       cache.remove("/");
500    }
501
502
503    public void testTxPutRollback() throws Exception JavaDoc, NotSupportedException JavaDoc
504    {
505       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
506
507       cache.remove("/one");
508
509       mgr.begin();
510       tx = mgr.getTransaction();
511
512       cache.put("/one/two/three", "key1", "val1");
513       cache.put("/one/two/three/four", "key2", "val2");
514       tx.rollback();
515       assertNull(cache.getKeys("/one/two/three"));
516       Set JavaDoc children = cache.getChildrenNames("/one");
517       assertNull(children);
518    }
519
520
521    /**
522     * Tests basic operations without a transaction.
523     */

524    public void testBasicOperations()
525       throws Exception JavaDoc
526    {
527
528       doTestBasicOperations();
529    }
530
531    /**
532     * Tests basic operations with a transaction.
533     */

534    public void testBasicOperationsTransactional()
535       throws Exception JavaDoc
536    {
537
538       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
539       mgr.begin();
540       tx = mgr.getTransaction();
541       doTestBasicOperations();
542       tx.commit();
543    }
544
545    /**
546     * Tests basic operations.
547     */

548    private void doTestBasicOperations() throws Exception JavaDoc
549    {
550
551       /* One FQN only. */
552       doPutTests(new Fqn("key"));
553       doRemoveTests(new Fqn("key"));
554       // assertEquals(0, loader.loadEntireState().length);
555

556       /* Add three FQNs, middle FQN last. */
557       doPutTests(new Fqn("key1"));
558       doPutTests(new Fqn("key3"));
559       doPutTests(new Fqn("key2"));
560       assertEquals(4, loader.get(new Fqn("key1")).size());
561       assertEquals(4, loader.get(new Fqn("key2")).size());
562       assertEquals(4, loader.get(new Fqn("key3")).size());
563
564       /* Remove middle FQN first, then the others. */
565       doRemoveTests(new Fqn("key2"));
566       doRemoveTests(new Fqn("key3"));
567       doRemoveTests(new Fqn("key1"));
568       assertEquals(null, loader.get(new Fqn("key1")));
569       assertEquals(null, loader.get(new Fqn("key2")));
570       assertEquals(null, loader.get(new Fqn("key3")));
571       // assertEquals(0, loader.loadEntireState().length);
572
}
573
574    /**
575     * Do basic put tests for a given FQN.
576     */

577    private void doPutTests(Fqn fqn)
578       throws Exception JavaDoc
579    {
580
581       assertTrue(!loader.exists(fqn));
582
583       /* put(Fqn,Object,Object) and get(Fqn,Object) */
584       Object JavaDoc oldVal;
585       oldVal = loader.put(fqn, "one", "two");
586       assertNull(oldVal);
587       oldVal = loader.put(fqn, "three", "four");
588       assertNull(oldVal);
589       assertEquals("two", loader.get(fqn, "one"));
590       assertEquals("four", loader.get(fqn, "three"));
591       oldVal = loader.put(fqn, "one", "xxx");
592       assertEquals("two", oldVal);
593       oldVal = loader.put(fqn, "one", "two");
594       assertEquals("xxx", oldVal);
595
596       /* get(Fqn) */
597       Map JavaDoc map = loader.get(fqn);
598       assertEquals(2, map.size());
599       assertEquals("two", map.get("one"));
600       assertEquals("four", map.get("three"));
601
602       /* put(Fqn,Map) */
603       map.put("five", "six");
604       map.put("seven", "eight");
605       loader.put(fqn, map);
606       assertEquals("six", loader.get(fqn, "five"));
607       assertEquals("eight", loader.get(fqn, "seven"));
608       assertEquals(map, loader.get(fqn));
609       assertEquals(4, map.size());
610
611       assertTrue(loader.exists(fqn));
612    }
613
614    /**
615     * Do basic remove tests for a given FQN.
616     */

617    private void doRemoveTests(Fqn fqn)
618       throws Exception JavaDoc
619    {
620
621       /* remove(Fqn,Object) */
622       Object JavaDoc oldVal;
623       oldVal = loader.remove(fqn, "one");
624       assertEquals("two", oldVal);
625       oldVal = loader.remove(fqn, "five");
626       assertEquals("six", oldVal);
627       assertEquals(null, loader.get(fqn, "one"));
628       assertEquals(null, loader.get(fqn, "five"));
629       assertEquals("four", loader.get(fqn, "three"));
630       assertEquals("eight", loader.get(fqn, "seven"));
631       Map JavaDoc map = loader.get(fqn);
632       assertEquals(2, map.size());
633       assertEquals("four", map.get("three"));
634       assertEquals("eight", map.get("seven"));
635
636       /* remove(Fqn) */
637       assertTrue(loader.exists(fqn));
638       loader.remove(fqn);
639       assertEquals(null, loader.get(fqn, "three"));
640       assertEquals(null, loader.get(fqn, "seven"));
641       map = loader.get(fqn);
642       assertEquals(null, map);
643       assertTrue(!loader.exists(fqn));
644    }
645
646    /**
647     * Tests creating implicit intermediate nodes when a leaf node is created,
648     * and tests removing subtrees.
649     */

650    public void testMultiLevelTree()
651       throws Exception JavaDoc
652    {
653
654       /* Create top level node implicitly. */
655       assertTrue(!loader.exists(new Fqn("key0")));
656       loader.put(Fqn.fromString("/key0/level1/level2"), null);
657       assertTrue(loader.exists(Fqn.fromString("/key0/level1/level2")));
658       assertTrue(loader.exists(Fqn.fromString("/key0/level1")));
659       assertTrue(loader.exists(new Fqn("key0")));
660
661       /* Remove leaf, leaving implicitly created middle level. */
662       loader.put(Fqn.fromString("/key0/x/y"), null);
663       assertTrue(loader.exists(Fqn.fromString("/key0/x/y")));
664       assertTrue(loader.exists(Fqn.fromString("/key0/x")));
665       loader.remove(Fqn.fromString("/key0/x/y"));
666       assertTrue(!loader.exists(Fqn.fromString("/key0/x/y")));
667       assertTrue(loader.exists(Fqn.fromString("/key0/x")));
668
669       /* Delete top level to delete everything. */
670       loader.remove(new Fqn("key0"));
671       assertTrue(!loader.exists(new Fqn("key0")));
672       assertTrue(!loader.exists(Fqn.fromString("/key0/level1/level2")));
673       assertTrue(!loader.exists(Fqn.fromString("/key0/level1")));
674       assertTrue(!loader.exists(Fqn.fromString("/key0/x")));
675
676       /* Add three top level nodes as context. */
677       loader.put(new Fqn("key1"), null);
678       loader.put(new Fqn("key2"), null);
679       loader.put(new Fqn("key3"), null);
680       assertTrue(loader.exists(new Fqn("key1")));
681       assertTrue(loader.exists(new Fqn("key2")));
682       assertTrue(loader.exists(new Fqn("key3")));
683
684       /* Put /key3/level1/level2. level1 should be implicitly created. */
685       assertTrue(!loader.exists(Fqn.fromString("/key3/level1")));
686       assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2")));
687       loader.put(Fqn.fromString("/key3/level1/level2"), null);
688       assertTrue(loader.exists(Fqn.fromString("/key3/level1/level2")));
689       assertTrue(loader.exists(Fqn.fromString("/key3/level1")));
690
691       /* Context nodes should still be intact. */
692       assertTrue(loader.exists(new Fqn("key1")));
693       assertTrue(loader.exists(new Fqn("key2")));
694       assertTrue(loader.exists(new Fqn("key3")));
695
696       /* Remove middle level only. */
697       loader.remove(Fqn.fromString("/key3/level1"));
698       assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2")));
699       assertTrue(!loader.exists(Fqn.fromString("/key3/level1")));
700
701       /* Context nodes should still be intact. */
702       assertTrue(loader.exists(new Fqn("key1")));
703       assertTrue(loader.exists(new Fqn("key2")));
704       assertTrue(loader.exists(new Fqn("key3")));
705
706       /* Delete first root, leaving other roots. */
707       loader.remove(new Fqn("key1"));
708       assertTrue(!loader.exists(new Fqn("key1")));
709       assertTrue(loader.exists(new Fqn("key2")));
710       assertTrue(loader.exists(new Fqn("key3")));
711
712       /* Delete last root, leaving other roots. */
713       loader.remove(new Fqn("key3"));
714       assertTrue(loader.exists(new Fqn("key2")));
715       assertTrue(!loader.exists(new Fqn("key3")));
716
717       /* Delete final root, leaving none. */
718       loader.remove(new Fqn("key2"));
719       assertTrue(!loader.exists(new Fqn("key0")));
720       assertTrue(!loader.exists(new Fqn("key1")));
721       assertTrue(!loader.exists(new Fqn("key2")));
722       assertTrue(!loader.exists(new Fqn("key3")));
723
724       /* Repeat all tests above using put(Fqn,Object,Object) and get(Fqn) */
725
726       assertNull(loader.get(new Fqn("key0")));
727       loader.put(Fqn.fromString("/key0/level1/level2"), "a", "b");
728       assertNotNull(loader.get(Fqn.fromString("/key0/level1/level2")));
729       assertNull(loader.get(Fqn.fromString("/key0/level1")));
730       assertNull(loader.get(new Fqn("key0")));
731
732       loader.put(Fqn.fromString("/key0/x/y"), "a", "b");
733       assertNotNull(loader.get(Fqn.fromString("/key0/x/y")));
734       assertNull(loader.get(Fqn.fromString("/key0/x")));
735       loader.remove(Fqn.fromString("/key0/x/y"));
736       assertNull(loader.get(Fqn.fromString("/key0/x/y")));
737       assertNull(loader.get(Fqn.fromString("/key0/x")));
738
739       loader.remove(new Fqn("key0"));
740       assertNull(loader.get(new Fqn("key0")));
741       assertNull(loader.get(Fqn.fromString("/key0/level1/level2")));
742       assertNull(loader.get(Fqn.fromString("/key0/level1")));
743       assertNull(loader.get(Fqn.fromString("/key0/x")));
744
745       loader.put(new Fqn("key1"), "a", "b");
746       loader.put(new Fqn("key2"), "a", "b");
747       loader.put(new Fqn("key3"), "a", "b");
748       assertNotNull(loader.get(new Fqn("key1")));
749       assertNotNull(loader.get(new Fqn("key2")));
750       assertNotNull(loader.get(new Fqn("key3")));
751
752       assertNull(loader.get(Fqn.fromString("/key3/level1")));
753       assertNull(loader.get(Fqn.fromString("/key3/level1/level2")));
754       loader.put(Fqn.fromString("/key3/level1/level2"), "a", "b");
755       assertNotNull(loader.get(Fqn.fromString("/key3/level1/level2")));
756       assertNull(loader.get(Fqn.fromString("/key3/level1")));
757
758       assertNotNull(loader.get(new Fqn("key1")));
759       assertNotNull(loader.get(new Fqn("key2")));
760       assertNotNull(loader.get(new Fqn("key3")));
761
762       loader.remove(Fqn.fromString("/key3/level1"));
763       assertNull(loader.get(Fqn.fromString("/key3/level1/level2")));
764       assertNull(loader.get(Fqn.fromString("/key3/level1")));
765
766       assertNotNull(loader.get(new Fqn("key1")));
767       assertNotNull(loader.get(new Fqn("key2")));
768       assertNotNull(loader.get(new Fqn("key3")));
769
770       loader.remove(new Fqn("key1"));
771       assertNull(loader.get(new Fqn("key1")));
772       assertNotNull(loader.get(new Fqn("key2")));
773       assertNotNull(loader.get(new Fqn("key3")));
774
775       loader.remove(new Fqn("key3"));
776       assertNotNull(loader.get(new Fqn("key2")));
777       assertNull(loader.get(new Fqn("key3")));
778
779       loader.remove(new Fqn("key2"));
780       assertNull(loader.get(new Fqn("key0")));
781       assertNull(loader.get(new Fqn("key1")));
782       assertNull(loader.get(new Fqn("key2")));
783       assertNull(loader.get(new Fqn("key3")));
784    }
785
786    /**
787     * Tests the getChildrenNames() method.
788     */

789    public void testGetChildrenNames()
790       throws Exception JavaDoc
791    {
792
793       checkChildren(new Fqn(), null);
794       checkChildren(Fqn.fromString("/key0"), null);
795
796       loader.put(Fqn.fromString("/key0"), null);
797       checkChildren(new Fqn(), new String JavaDoc[]{"key0"});
798
799       loader.put(Fqn.fromString("/key1/x"), null);
800       checkChildren(new Fqn(), new String JavaDoc[]{"key0", "key1"});
801       checkChildren(Fqn.fromString("/key1"), new String JavaDoc[]{"x"});
802
803       loader.remove(Fqn.fromString("/key1/x"));
804       checkChildren(new Fqn(), new String JavaDoc[]{"key0", "key1"});
805       checkChildren(Fqn.fromString("/key0"), null);
806       checkChildren(Fqn.fromString("/key1"), null);
807
808       loader.put(Fqn.fromString("/key0/a"), null);
809       loader.put(Fqn.fromString("/key0/ab"), null);
810       loader.put(Fqn.fromString("/key0/abc"), null);
811       checkChildren(Fqn.fromString("/key0"),
812          new String JavaDoc[]{"a", "ab", "abc"});
813
814       loader.put(Fqn.fromString("/key0/xxx"), null);
815       loader.put(Fqn.fromString("/key0/xx"), null);
816       loader.put(Fqn.fromString("/key0/x"), null);
817       checkChildren(Fqn.fromString("/key0"),
818          new String JavaDoc[]{"a", "ab", "abc", "x", "xx", "xxx"});
819
820       loader.put(Fqn.fromString("/key0/a/1"), null);
821       loader.put(Fqn.fromString("/key0/a/2"), null);
822       loader.put(Fqn.fromString("/key0/a/2/1"), null);
823       checkChildren(Fqn.fromString("/key0/a/2"), new String JavaDoc[]{"1"});
824       checkChildren(Fqn.fromString("/key0/a"), new String JavaDoc[]{"1", "2"});
825       checkChildren(Fqn.fromString("/key0"),
826          new String JavaDoc[]{"a", "ab", "abc", "x", "xx", "xxx"});
827 //
828
// loader.put(Fqn.fromString("/key0/\u0000"), null);
829
// loader.put(Fqn.fromString("/key0/\u0001"), null);
830
// checkChildren(Fqn.fromString("/key0"),
831
// new String[] { "a", "ab", "abc", "x", "xx", "xxx",
832
// "\u0000", "\u0001"});
833
//
834
// loader.put(Fqn.fromString("/\u0001"), null);
835
// checkChildren(new Fqn(), new String[] { "key0", "key1", "\u0001" });
836
//
837
// loader.put(Fqn.fromString("/\u0001/\u0001"), null);
838
// checkChildren(Fqn.fromString("/\u0001"), new String[] { "\u0001" });
839
//
840
// loader.put(Fqn.fromString("/\u0001/\uFFFF"), null);
841
// checkChildren(Fqn.fromString("/\u0001"),
842
// new String[] { "\u0001", "\uFFFF" });
843
//
844
// loader.put(Fqn.fromString("/\u0001/\uFFFF/\u0001"), null);
845
// checkChildren(Fqn.fromString("/\u0001/\uFFFF"),
846
// new String[] { "\u0001" });
847
}
848
849    /**
850     * Checks that the given list of children part names is returned.
851     */

852    private void checkChildren(Fqn fqn, String JavaDoc[] names)
853       throws Exception JavaDoc
854    {
855
856       Set JavaDoc set = loader.getChildrenNames(fqn);
857       if (names != null)
858       {
859          assertEquals(names.length, set.size());
860          for (int i = 0; i < names.length; i += 1)
861          {
862             assertTrue(set.contains(names[i]));
863          }
864       }
865       else
866       {
867          assertNull(set);
868       }
869    }
870
871    /**
872     * Tests basic operations without a transaction.
873     */

874    public void testModifications()
875       throws Exception JavaDoc
876    {
877
878       doTestModifications();
879    }
880
881    /**
882     * Tests basic operations with a transaction.
883     */

884    public void testModificationsTransactional()
885       throws Exception JavaDoc
886    {
887       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
888       mgr.begin();
889       tx = mgr.getTransaction();
890       doTestModifications();
891       tx.commit();
892    }
893
894    /**
895     * Tests modifications.
896     */

897    private void doTestModifications()
898       throws Exception JavaDoc
899    {
900
901       /* PUT_KEY_VALUE, PUT_DATA */
902       List JavaDoc list = createUpdates();
903       loader.put(list);
904       checkModifications(list);
905
906       /* REMOVE_KEY_VALUE */
907       list = new ArrayList JavaDoc();
908       Modification mod = new Modification();
909       mod.setType(Modification.REMOVE_KEY_VALUE);
910       mod.setFqn(FQN);
911       mod.setKey("one");
912       list.add(mod);
913       loader.put(list);
914       checkModifications(list);
915
916       /* REMOVE_NODE */
917       list = new ArrayList JavaDoc();
918       mod = new Modification();
919       mod.setType(Modification.REMOVE_NODE);
920       mod.setFqn(FQN);
921       list.add(mod);
922       loader.put(list);
923       checkModifications(list);
924       assertEquals(null, loader.get(FQN));
925
926       /* REMOVE_DATA */
927       loader.put(FQN, "one", "two");
928       list = new ArrayList JavaDoc();
929       mod = new Modification();
930       mod.setType(Modification.REMOVE_DATA);
931       mod.setFqn(FQN);
932       list.add(mod);
933       loader.put(list);
934       checkModifications(list);
935    }
936
937    /**
938     * Tests a one-phase transaction.
939     */

940    public void testOnePhaseTransaction()
941       throws Exception JavaDoc
942    {
943       List JavaDoc mods = createUpdates();
944       loader.prepare(null, mods, true);
945       checkModifications(mods);
946    }
947
948    /**
949     * Tests a two-phase transaction.
950     */

951    public void testTwoPhaseTransaction()
952       throws Exception JavaDoc
953    {
954
955       Object JavaDoc txnKey = new Object JavaDoc();
956       List JavaDoc mods = createUpdates();
957       loader.prepare(txnKey, mods, false);
958 // try {
959
// checkModifications(mods);
960
// // fail("Expected lock timeout");
961
// } catch (DeadlockException expected) {}
962
loader.commit(txnKey);
963       checkModifications(mods);
964    }
965
966    /**
967     * Tests rollback of a two-phase transaction.
968     */

969    public void testTransactionRollback()
970       throws Exception JavaDoc
971    {
972
973       loader.remove(Fqn.fromString("/"));
974
975       int num = loader.loadEntireState().length;
976
977       Object JavaDoc txnKey = new Object JavaDoc();
978       List JavaDoc mods = createUpdates();
979       loader.prepare(txnKey, mods, false);
980       loader.rollback(txnKey);
981       assertEquals(num, loader.loadEntireState().length);
982    }
983
984    /**
985     * Creates a set of update (PUT_KEY_VALUE, PUT_DATA) modifications.
986     */

987    private List JavaDoc createUpdates()
988    {
989
990       List JavaDoc list = new ArrayList JavaDoc();
991
992       Modification mod = new Modification();
993       mod.setType(Modification.PUT_KEY_VALUE);
994       mod.setFqn(FQN);
995       mod.setKey("one");
996       mod.setValue("two");
997       list.add(mod);
998
999       mod = new Modification();
1000      mod.setType(Modification.PUT_KEY_VALUE);
1001      mod.setFqn(FQN);
1002      mod.setKey("three");
1003      mod.setValue("four");
1004      list.add(mod);
1005
1006      Map JavaDoc map = new HashMap JavaDoc();
1007      map.put("five", "six");
1008      map.put("seven", "eight");
1009      mod = new Modification();
1010      mod.setType(Modification.PUT_DATA);
1011      mod.setFqn(FQN);
1012      mod.setData(map);
1013      list.add(mod);
1014
1015      return list;
1016   }
1017
1018   /**
1019    * Checks that a list of modifications was applied.
1020    */

1021   private void checkModifications(List JavaDoc list)
1022      throws Exception JavaDoc
1023   {
1024
1025      for (int i = 0; i < list.size(); i += 1)
1026      {
1027         Modification mod = (Modification)list.get(i);
1028         Fqn fqn = mod.getFqn();
1029         switch (mod.getType())
1030         {
1031         case Modification.PUT_KEY_VALUE:
1032            assertEquals(mod.getValue(), loader.get(fqn, mod.getKey()));
1033            break;
1034         case Modification.PUT_DATA:
1035            Map JavaDoc map = mod.getData();
1036            for (Iterator JavaDoc iter = map.keySet().iterator(); iter.hasNext();)
1037            {
1038               Object JavaDoc key = iter.next();
1039               assertEquals(map.get(key), loader.get(fqn, key));
1040            }
1041            break;
1042         case Modification.REMOVE_KEY_VALUE:
1043            assertEquals(null, loader.get(fqn, mod.getKey()));
1044            break;
1045         case Modification.REMOVE_DATA:
1046            map = loader.get(fqn);
1047            assertNull(map);
1048            break;
1049         case Modification.REMOVE_NODE:
1050            assertEquals(null, loader.get(fqn));
1051            break;
1052         default:
1053            fail("unknown type: " + mod);
1054            break;
1055         }
1056      }
1057   }
1058
1059
1060   /**
1061    * Tests that null keys and values work as for a standard Java Map.
1062    */

1063   public void _testNullKeysAndValues()
1064      throws Exception JavaDoc
1065   {
1066
1067      loader.put(FQN, null, "x");
1068      assertEquals("x", loader.get(FQN, null));
1069      Map JavaDoc map = loader.get(FQN);
1070      assertEquals(1, map.size());
1071      assertEquals("x", map.get(null));
1072
1073      loader.put(FQN, "y", null);
1074      assertEquals(null, loader.get(FQN, "y"));
1075      map = loader.get(FQN);
1076      assertEquals(2, map.size());
1077      assertEquals("x", map.get(null));
1078      assertEquals(null, map.get("y"));
1079
1080      loader.remove(FQN, null);
1081      assertEquals(null, loader.get(FQN, null));
1082      assertEquals(1, loader.get(FQN).size());
1083
1084      loader.remove(FQN, "y");
1085      assertEquals(null, loader.get(FQN, "y"));
1086      assertNull(loader.get(FQN));
1087
1088      map = new HashMap JavaDoc();
1089      map.put(null, null);
1090      loader.put(FQN, map);
1091      assertEquals(map, loader.get(FQN));
1092
1093      loader.remove(FQN);
1094      assertEquals(null, loader.get(FQN));
1095
1096      map = new HashMap JavaDoc();
1097      map.put("xyz", null);
1098      map.put(null, "abc");
1099      loader.put(FQN, map);
1100      assertEquals(map, loader.get(FQN));
1101
1102      loader.remove(FQN);
1103      assertEquals(null, loader.get(FQN));
1104   }
1105
1106   /**
1107    * Test non-default database name.
1108    */

1109   public void testDatabaseName()
1110      throws Exception JavaDoc
1111   {
1112
1113      loader.put(FQN, "one", "two");
1114      assertEquals("two", loader.get(FQN, "one"));
1115   }
1116
1117   /**
1118    * Test load/store state.
1119    */

1120   public void testLoadAndStore()
1121      throws Exception JavaDoc
1122   {
1123
1124      /* Empty state. */
1125      loader.remove(Fqn.fromString("/"));
1126      // assertEquals(0, loader.loadEntireState().length);
1127
// loader.storeEntireState(new byte[0]);
1128
// assertEquals(0, loader.loadEntireState().length);
1129
// loader.storeEntireState(null);
1130
// assertEquals(0, loader.loadEntireState().length);
1131
// assertEquals(null, loader.get(FQN));
1132

1133      /* Use a complex object to ensure that the class catalog is used. */
1134      Complex c1 = new Complex();
1135      Complex c2 = new Complex(c1);
1136
1137      /* Add objects. */
1138      loader.put(FQN, "1", c1);
1139      loader.put(FQN, "2", c2);
1140      assertEquals(c1, loader.get(FQN, "1"));
1141      assertEquals(c2, loader.get(FQN, "2"));
1142      assertEquals(2, loader.get(FQN).size());
1143
1144      /* Save state. */
1145      byte[] state = loader.loadEntireState();
1146      assertTrue(state.length > 0);
1147
1148
1149      /* Restore state. */
1150      loader.storeEntireState(state);
1151      assertEquals(c1, loader.get(FQN, "1"));
1152      assertEquals(c2, loader.get(FQN, "2"));
1153      assertEquals(2, loader.get(FQN).size());
1154   }
1155
1156
1157   /**
1158    * Complex object whose class description is stored in the class catalog.
1159    */

1160   private static class Complex implements Serializable JavaDoc
1161   {
1162
1163      Complex nested;
1164
1165      Complex()
1166      {
1167         this(null);
1168      }
1169
1170      Complex(Complex nested)
1171      {
1172         this.nested = nested;
1173      }
1174
1175      public boolean equals(Object JavaDoc o)
1176      {
1177         try
1178         {
1179            Complex x = (Complex)o;
1180            return (nested != null) ? nested.equals(x.nested)
1181               : (x.nested == null);
1182         }
1183         catch (ClassCastException JavaDoc e)
1184         {
1185            return false;
1186         }
1187      }
1188
1189      public int hashCode()
1190      {
1191         if (nested == null)
1192         {
1193            return super.hashCode();
1194         }
1195         else
1196         {
1197            return 13 + nested.hashCode();
1198         }
1199      }
1200   }
1201}
1202
Popular Tags