KickJava   Java API By Example, From Geeks To Geeks.

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


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

7 package org.jboss.cache.tests;
8
9 import com.sleepycat.je.Database;
10 import com.sleepycat.je.DatabaseConfig;
11 import com.sleepycat.je.DatabaseException;
12 import com.sleepycat.je.DeadlockException;
13 import com.sleepycat.je.Environment;
14
15 import junit.framework.Test;
16 import junit.framework.TestCase;
17 import junit.framework.TestSuite;
18 import org.jboss.cache.Fqn;
19 import org.jboss.cache.Modification;
20 import org.jboss.cache.TreeCache;
21 import org.jboss.cache.loader.CacheLoader;
22
23 import java.io.File JavaDoc;
24 import java.io.Serializable JavaDoc;
25 import java.io.FileFilter JavaDoc;
26 import java.util.*;
27
28 /**
29  * Tests BdbjeCacheLoader directly via the CacheLoader interface.
30  *
31  * <p>Run this test case with the current directory set to the JE environment
32  * directory. Any scratch directory will do, but beware that all files in
33  * the directory will be deleted by setUp().</p>
34  *
35  * @version $Revision: 1.3.2.4 $
36  */

37 public class BdbjeUnitTestCase extends TestCase {
38
39    private static final String JavaDoc envHome = ".";
40    private static final Fqn FQN = new Fqn("key");
41
42    private TreeCache treeCache;
43    private CacheLoader loader;
44
45    /**
46     * Creates a test case.
47     */

48    public BdbjeUnitTestCase(String JavaDoc name) {
49       super(name);
50    }
51
52    /**
53     * Deletes all files in the environment directory.
54     */

55    public void setUp() throws Exception JavaDoc {
56       super.setUp();
57
58       File JavaDoc dir = new File JavaDoc(envHome);
59
60       class MyFilter implements FileFilter JavaDoc {
61          public boolean accept(File JavaDoc file) {
62             return file.getName().endsWith(".jdb");
63          }
64       }
65
66       File JavaDoc[] files=dir.listFiles(new MyFilter());
67
68       if (files != null) {
69          for (int i = 0; i < files.length; i += 1) {
70             File JavaDoc file = files[i];
71             if (file.isFile()) {
72                if (!file.delete()) {
73                   System.err.println("Unable to delete: " + file);
74                }
75             }
76          }
77       }
78    }
79
80    /**
81     * Release all resources and ignore exceptions, to shutdown gracefully
82     * when an assertion fires.
83     */

84    public void tearDown() throws Exception JavaDoc {
85       super.tearDown();
86
87       if (loader != null) {
88          try {
89             loader.stop();
90          } catch (Exception JavaDoc ignored) {}
91          loader = null;
92       }
93       if (treeCache != null) {
94          try {
95             treeCache.stopService();
96          } catch (Exception JavaDoc ignored) {}
97          treeCache = null;
98       }
99    }
100
101    /**
102     * Creates and starts a loader.
103     * @param transactional whether to set the TransactionManagerLookupClass
104     * property.
105     * @param dbName a database name, or null to default to the cluster name.
106     */

107    private void startLoader(boolean transactional, String JavaDoc dbName)
108       throws Exception JavaDoc {
109
110       /*
111        * Create a dummy TreeCache object. This is used for setting the cluster
112        * name and TransactionManagerLookupClass (transactional) propertes only.
113        * the TreeCache object is not used otherwise during testing.
114        */

115       treeCache = new TreeCache();
116       treeCache.setClusterName("myCluster");
117       if (transactional) {
118          treeCache.setTransactionManagerLookupClass(
119             "org.jboss.cache.DummyTransactionManagerLookup");
120       }
121       treeCache.startService();
122
123       /* Derive the config string. */
124       String JavaDoc configStr;
125       if (dbName != null) {
126          configStr = envHome + '#' + dbName;
127       } else {
128          configStr = envHome;
129          dbName = "myCluster";
130       }
131
132       instantiateLoader();
133
134       /* Initialize and start the loader. */
135       loader.setCache(treeCache);
136       Properties props=new Properties();
137       props.setProperty("location", configStr);
138       loader.setConfig(props);
139       loader.create();
140       loader.start();
141
142       /* Verify the database name by trying to open it. */
143       Environment env = new Environment(new File JavaDoc(envHome), null);
144       DatabaseConfig dbConfig = new DatabaseConfig();
145       dbConfig.setTransactional(transactional);
146       Database db = env.openDatabase(null, dbName, dbConfig);
147       db.close();
148       env.close();
149    }
150
151    /**
152     * Creates the loader instance.
153     */

154    private void instantiateLoader()
155       throws Exception JavaDoc {
156
157       /* Create the cache loader as TreeCache would. */
158       Class JavaDoc cls =
159          Class.forName("org.jboss.cache.loader.bdbje.BdbjeCacheLoader");
160       loader = (CacheLoader) cls.newInstance();
161    }
162
163    /**
164     * Stops and destroys the loader.
165     */

166    private void stopLoader()
167       throws Exception JavaDoc {
168
169       loader.stop();
170       loader.destroy();
171    }
172
173    /**
174     * Tests basic operations without a transaction.
175     */

176    public void testBasicOperations()
177       throws Exception JavaDoc {
178
179       doTestBasicOperations(false);
180    }
181
182    /**
183     * Tests basic operations with a transaction.
184     */

185    public void testBasicOperationsTransactional()
186       throws Exception JavaDoc {
187
188       doTestBasicOperations(true);
189    }
190
191    /**
192     * Tests basic operations.
193     */

194    private void doTestBasicOperations(boolean transactional)
195       throws Exception JavaDoc {
196
197       startLoader(transactional, null);
198
199       /* One FQN only. */
200       doPutTests(new Fqn("key"));
201       doRemoveTests(new Fqn("key"));
202       assertEquals(0, loader.loadEntireState().length);
203
204       /* Add three FQNs, middle FQN last. */
205       doPutTests(new Fqn("key1"));
206       doPutTests(new Fqn("key3"));
207       doPutTests(new Fqn("key2"));
208       assertEquals(4, loader.get(new Fqn("key1")).size());
209       assertEquals(4, loader.get(new Fqn("key2")).size());
210       assertEquals(4, loader.get(new Fqn("key3")).size());
211
212       /* Remove middle FQN first, then the others. */
213       doRemoveTests(new Fqn("key2"));
214       doRemoveTests(new Fqn("key3"));
215       doRemoveTests(new Fqn("key1"));
216       assertEquals(null, loader.get(new Fqn("key1")));
217       assertEquals(null, loader.get(new Fqn("key2")));
218       assertEquals(null, loader.get(new Fqn("key3")));
219       assertEquals(0, loader.loadEntireState().length);
220
221       stopLoader();
222    }
223
224    /**
225     * Do basic put tests for a given FQN.
226     */

227    private void doPutTests(Fqn fqn)
228       throws Exception JavaDoc {
229
230       assertTrue(!loader.exists(fqn));
231
232       /* put(Fqn,Object,Object) and get(Fqn,Object) */
233       Object JavaDoc oldVal;
234       oldVal = loader.put(fqn, "one", "two");
235       assertNull(oldVal);
236       oldVal = loader.put(fqn, "three", "four");
237       assertNull(oldVal);
238       assertEquals("two", loader.get(fqn, "one"));
239       assertEquals("four", loader.get(fqn, "three"));
240       oldVal = loader.put(fqn, "one", "xxx");
241       assertEquals("two", oldVal);
242       oldVal = loader.put(fqn, "one", "two");
243       assertEquals("xxx", oldVal);
244
245       /* get(Fqn) */
246       Map map = loader.get(fqn);
247       assertEquals(2, map.size());
248       assertEquals("two", map.get("one"));
249       assertEquals("four", map.get("three"));
250
251       /* put(Fqn,Map) */
252       map.put("five", "six");
253       map.put("seven", "eight");
254       loader.put(fqn, map);
255       assertEquals("six", loader.get(fqn, "five"));
256       assertEquals("eight", loader.get(fqn, "seven"));
257       assertEquals(map, loader.get(fqn));
258       assertEquals(4, map.size());
259
260       assertTrue(loader.exists(fqn));
261    }
262
263    /**
264     * Do basic remove tests for a given FQN.
265     */

266    private void doRemoveTests(Fqn fqn)
267       throws Exception JavaDoc {
268
269       /* remove(Fqn,Object) */
270       Object JavaDoc oldVal;
271       oldVal = loader.remove(fqn, "one");
272       assertEquals("two", oldVal);
273       oldVal = loader.remove(fqn, "five");
274       assertEquals("six", oldVal);
275       assertEquals(null, loader.get(fqn, "one"));
276       assertEquals(null, loader.get(fqn, "five"));
277       assertEquals("four", loader.get(fqn, "three"));
278       assertEquals("eight", loader.get(fqn, "seven"));
279       Map map = loader.get(fqn);
280       assertEquals(2, map.size());
281       assertEquals("four", map.get("three"));
282       assertEquals("eight", map.get("seven"));
283
284       /* remove(Fqn) */
285       assertTrue(loader.exists(fqn));
286       loader.remove(fqn);
287       assertEquals(null, loader.get(fqn, "three"));
288       assertEquals(null, loader.get(fqn, "seven"));
289       map = loader.get(fqn);
290       assertEquals(null, map);
291       assertTrue(!loader.exists(fqn));
292    }
293
294    /**
295     * Tests creating implicit intermediate nodes when a leaf node is created,
296     * and tests removing subtrees.
297     */

298    public void testMultiLevelTree()
299       throws Exception JavaDoc {
300
301       startLoader(false, null);
302
303       /* Create top level node implicitly. */
304       assertTrue(!loader.exists(new Fqn("key0")));
305       loader.put(Fqn.fromString("/key0/level1/level2"), null);
306       assertTrue(loader.exists(Fqn.fromString("/key0/level1/level2")));
307       assertTrue(loader.exists(Fqn.fromString("/key0/level1")));
308       assertTrue(loader.exists(new Fqn("key0")));
309
310       /* Remove leaf, leaving implicitly created middle level. */
311       loader.put(Fqn.fromString("/key0/x/y"), null);
312       assertTrue(loader.exists(Fqn.fromString("/key0/x/y")));
313       assertTrue(loader.exists(Fqn.fromString("/key0/x")));
314       loader.remove(Fqn.fromString("/key0/x/y"));
315       assertTrue(!loader.exists(Fqn.fromString("/key0/x/y")));
316       assertTrue(loader.exists(Fqn.fromString("/key0/x")));
317
318       /* Delete top level to delete everything. */
319       loader.remove(new Fqn("key0"));
320       assertTrue(!loader.exists(new Fqn("key0")));
321       assertTrue(!loader.exists(Fqn.fromString("/key0/level1/level2")));
322       assertTrue(!loader.exists(Fqn.fromString("/key0/level1")));
323       assertTrue(!loader.exists(Fqn.fromString("/key0/x")));
324
325       /* Add three top level nodes as context. */
326       loader.put(new Fqn("key1"), null);
327       loader.put(new Fqn("key2"), null);
328       loader.put(new Fqn("key3"), null);
329       assertTrue(loader.exists(new Fqn("key1")));
330       assertTrue(loader.exists(new Fqn("key2")));
331       assertTrue(loader.exists(new Fqn("key3")));
332
333       /* Put /key3/level1/level2. level1 should be implicitly created. */
334       assertTrue(!loader.exists(Fqn.fromString("/key3/level1")));
335       assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2")));
336       loader.put(Fqn.fromString("/key3/level1/level2"), null);
337       assertTrue(loader.exists(Fqn.fromString("/key3/level1/level2")));
338       assertTrue(loader.exists(Fqn.fromString("/key3/level1")));
339
340       /* Context nodes should still be intact. */
341       assertTrue(loader.exists(new Fqn("key1")));
342       assertTrue(loader.exists(new Fqn("key2")));
343       assertTrue(loader.exists(new Fqn("key3")));
344
345       /* Remove middle level only. */
346       loader.remove(Fqn.fromString("/key3/level1"));
347       assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2")));
348       assertTrue(!loader.exists(Fqn.fromString("/key3/level1")));
349
350       /* Context nodes should still be intact. */
351       assertTrue(loader.exists(new Fqn("key1")));
352       assertTrue(loader.exists(new Fqn("key2")));
353       assertTrue(loader.exists(new Fqn("key3")));
354
355       /* Delete first root, leaving other roots. */
356       loader.remove(new Fqn("key1"));
357       assertTrue(!loader.exists(new Fqn("key1")));
358       assertTrue(loader.exists(new Fqn("key2")));
359       assertTrue(loader.exists(new Fqn("key3")));
360
361       /* Delete last root, leaving other roots. */
362       loader.remove(new Fqn("key3"));
363       assertTrue(loader.exists(new Fqn("key2")));
364       assertTrue(!loader.exists(new Fqn("key3")));
365
366       /* Delete final root, leaving none. */
367       loader.remove(new Fqn("key2"));
368       assertTrue(!loader.exists(new Fqn("key0")));
369       assertTrue(!loader.exists(new Fqn("key1")));
370       assertTrue(!loader.exists(new Fqn("key2")));
371       assertTrue(!loader.exists(new Fqn("key3")));
372
373       /* Repeat all tests above using put(Fqn,Object,Object) and get(Fqn) */
374
375       assertNull(loader.get(new Fqn("key0")));
376       loader.put(Fqn.fromString("/key0/level1/level2"), "a", "b");
377       assertNotNull(loader.get(Fqn.fromString("/key0/level1/level2")));
378       assertNull(loader.get(Fqn.fromString("/key0/level1")));
379       assertNull(loader.get(new Fqn("key0")));
380
381       loader.put(Fqn.fromString("/key0/x/y"), "a", "b");
382       assertNotNull(loader.get(Fqn.fromString("/key0/x/y")));
383       assertNull(loader.get(Fqn.fromString("/key0/x")));
384       loader.remove(Fqn.fromString("/key0/x/y"));
385       assertNull(loader.get(Fqn.fromString("/key0/x/y")));
386       assertNull(loader.get(Fqn.fromString("/key0/x")));
387
388       loader.remove(new Fqn("key0"));
389       assertNull(loader.get(new Fqn("key0")));
390       assertNull(loader.get(Fqn.fromString("/key0/level1/level2")));
391       assertNull(loader.get(Fqn.fromString("/key0/level1")));
392       assertNull(loader.get(Fqn.fromString("/key0/x")));
393
394       loader.put(new Fqn("key1"), "a", "b");
395       loader.put(new Fqn("key2"), "a", "b");
396       loader.put(new Fqn("key3"), "a", "b");
397       assertNotNull(loader.get(new Fqn("key1")));
398       assertNotNull(loader.get(new Fqn("key2")));
399       assertNotNull(loader.get(new Fqn("key3")));
400
401       assertNull(loader.get(Fqn.fromString("/key3/level1")));
402       assertNull(loader.get(Fqn.fromString("/key3/level1/level2")));
403       loader.put(Fqn.fromString("/key3/level1/level2"), "a", "b");
404       assertNotNull(loader.get(Fqn.fromString("/key3/level1/level2")));
405       assertNull(loader.get(Fqn.fromString("/key3/level1")));
406
407       assertNotNull(loader.get(new Fqn("key1")));
408       assertNotNull(loader.get(new Fqn("key2")));
409       assertNotNull(loader.get(new Fqn("key3")));
410
411       loader.remove(Fqn.fromString("/key3/level1"));
412       assertNull(loader.get(Fqn.fromString("/key3/level1/level2")));
413       assertNull(loader.get(Fqn.fromString("/key3/level1")));
414
415       assertNotNull(loader.get(new Fqn("key1")));
416       assertNotNull(loader.get(new Fqn("key2")));
417       assertNotNull(loader.get(new Fqn("key3")));
418
419       loader.remove(new Fqn("key1"));
420       assertNull(loader.get(new Fqn("key1")));
421       assertNotNull(loader.get(new Fqn("key2")));
422       assertNotNull(loader.get(new Fqn("key3")));
423
424       loader.remove(new Fqn("key3"));
425       assertNotNull(loader.get(new Fqn("key2")));
426       assertNull(loader.get(new Fqn("key3")));
427
428       loader.remove(new Fqn("key2"));
429       assertNull(loader.get(new Fqn("key0")));
430       assertNull(loader.get(new Fqn("key1")));
431       assertNull(loader.get(new Fqn("key2")));
432       assertNull(loader.get(new Fqn("key3")));
433
434       stopLoader();
435    }
436
437    /**
438     * Tests the getChildrenNames() method.
439     */

440    public void testGetChildrenNames()
441       throws Exception JavaDoc {
442
443       startLoader(false, null);
444
445       checkChildren(new Fqn(), null);
446       checkChildren(Fqn.fromString("/key0"), null);
447
448       loader.put(Fqn.fromString("/key0"), null);
449       checkChildren(new Fqn(), new String JavaDoc[] { "key0" });
450
451       loader.put(Fqn.fromString("/key1/x"), null);
452       checkChildren(new Fqn(), new String JavaDoc[] { "key0", "key1" });
453       checkChildren(Fqn.fromString("/key1"), new String JavaDoc[] { "x" });
454
455       loader.remove(Fqn.fromString("/key1/x"));
456       checkChildren(new Fqn(), new String JavaDoc[] { "key0", "key1" });
457       checkChildren(Fqn.fromString("/key0"), null);
458       checkChildren(Fqn.fromString("/key1"), null);
459
460       loader.put(Fqn.fromString("/key0/a"), null);
461       loader.put(Fqn.fromString("/key0/ab"), null);
462       loader.put(Fqn.fromString("/key0/abc"), null);
463       checkChildren(Fqn.fromString("/key0"),
464                     new String JavaDoc[] { "a", "ab", "abc" });
465
466       loader.put(Fqn.fromString("/key0/xxx"), null);
467       loader.put(Fqn.fromString("/key0/xx"), null);
468       loader.put(Fqn.fromString("/key0/x"), null);
469       checkChildren(Fqn.fromString("/key0"),
470                     new String JavaDoc[] { "a", "ab", "abc", "x", "xx", "xxx" });
471
472       loader.put(Fqn.fromString("/key0/a/1"), null);
473       loader.put(Fqn.fromString("/key0/a/2"), null);
474       loader.put(Fqn.fromString("/key0/a/2/1"), null);
475       checkChildren(Fqn.fromString("/key0/a/2"), new String JavaDoc[] { "1" });
476       checkChildren(Fqn.fromString("/key0/a"), new String JavaDoc[] { "1", "2" });
477       checkChildren(Fqn.fromString("/key0"),
478                     new String JavaDoc[] { "a", "ab", "abc", "x", "xx", "xxx" });
479
480       loader.put(Fqn.fromString("/key0/\u0000"), null);
481       loader.put(Fqn.fromString("/key0/\u0001"), null);
482       checkChildren(Fqn.fromString("/key0"),
483                     new String JavaDoc[] { "a", "ab", "abc", "x", "xx", "xxx",
484                                    "\u0000", "\u0001"});
485
486       loader.put(Fqn.fromString("/\u0001"), null);
487       checkChildren(new Fqn(), new String JavaDoc[] { "key0", "key1", "\u0001" });
488
489       loader.put(Fqn.fromString("/\u0001/\u0001"), null);
490       checkChildren(Fqn.fromString("/\u0001"), new String JavaDoc[] { "\u0001" });
491
492       loader.put(Fqn.fromString("/\u0001/\uFFFF"), null);
493       checkChildren(Fqn.fromString("/\u0001"),
494                     new String JavaDoc[] { "\u0001", "\uFFFF" });
495
496       loader.put(Fqn.fromString("/\u0001/\uFFFF/\u0001"), null);
497       checkChildren(Fqn.fromString("/\u0001/\uFFFF"),
498                     new String JavaDoc[] { "\u0001" });
499
500       stopLoader();
501    }
502
503    /**
504     * Checks that the given list of children part names is returned.
505     */

506    private void checkChildren(Fqn fqn, String JavaDoc[] names)
507       throws Exception JavaDoc {
508
509       Set set = loader.getChildrenNames(fqn);
510       if (names != null) {
511          assertEquals(names.length, set.size());
512          for (int i = 0; i < names.length; i += 1) {
513             assertTrue(set.contains(names[i]));
514          }
515       } else {
516          assertNull(set);
517       }
518    }
519
520    /**
521     * Tests basic operations without a transaction.
522     */

523    public void testModifications()
524       throws Exception JavaDoc {
525
526       doTestModifications(false);
527    }
528
529    /**
530     * Tests basic operations with a transaction.
531     */

532    public void testModificationsTransactional()
533       throws Exception JavaDoc {
534
535       doTestModifications(true);
536    }
537
538    /**
539     * Tests modifications.
540     */

541    private void doTestModifications(boolean transactional)
542       throws Exception JavaDoc {
543
544       startLoader(transactional, null);
545
546       /* PUT_KEY_VALUE, PUT_DATA */
547       List list = createUpdates();
548       loader.put(list);
549       checkModifications(list);
550
551       /* REMOVE_KEY_VALUE */
552       list = new ArrayList();
553       Modification mod = new Modification();
554       mod.setType(Modification.REMOVE_KEY_VALUE);
555       mod.setFqn(FQN);
556       mod.setKey("one");
557       list.add(mod);
558       loader.put(list);
559       checkModifications(list);
560
561       /* REMOVE_NODE */
562       list = new ArrayList();
563       mod = new Modification();
564       mod.setType(Modification.REMOVE_NODE);
565       mod.setFqn(FQN);
566       list.add(mod);
567       loader.put(list);
568       checkModifications(list);
569       assertEquals(null, loader.get(FQN));
570
571       /* REMOVE_DATA */
572       loader.put(FQN, "one", "two");
573       list = new ArrayList();
574       mod = new Modification();
575       mod.setType(Modification.REMOVE_DATA);
576       mod.setFqn(FQN);
577       list.add(mod);
578       loader.put(list);
579       checkModifications(list);
580       assertEquals(null, loader.get(FQN));
581
582       stopLoader();
583    }
584
585    /**
586     * Tests a one-phase transaction.
587     */

588    public void testOnePhaseTransaction()
589       throws Exception JavaDoc {
590
591       startLoader(true, null);
592
593       List mods = createUpdates();
594       loader.prepare(null, mods, true);
595       checkModifications(mods);
596
597       stopLoader();
598    }
599
600    /**
601     * Tests a two-phase transaction.
602     */

603    public void testTwoPhaseTransaction()
604       throws Exception JavaDoc {
605
606       startLoader(true, null);
607
608       Object JavaDoc txnKey = new Object JavaDoc();
609       List mods = createUpdates();
610       loader.prepare(txnKey, mods, false);
611       try {
612          checkModifications(mods);
613          fail("Expected lock timeout");
614       } catch (DeadlockException expected) {}
615       loader.commit(txnKey);
616       checkModifications(mods);
617
618       stopLoader();
619    }
620
621    /**
622     * Tests rollback of a two-phase transaction.
623     */

624    public void testTransactionRollback()
625       throws Exception JavaDoc {
626
627       startLoader(true, null);
628
629       Object JavaDoc txnKey = new Object JavaDoc();
630       List mods = createUpdates();
631       loader.prepare(txnKey, mods, false);
632       loader.rollback(txnKey);
633       assertEquals(0, loader.loadEntireState().length);
634
635       stopLoader();
636    }
637
638    /**
639     * Creates a set of update (PUT_KEY_VALUE, PUT_DATA) modifications.
640     */

641    private List createUpdates() {
642
643       List list = new ArrayList();
644
645       Modification mod = new Modification();
646       mod.setType(Modification.PUT_KEY_VALUE);
647       mod.setFqn(FQN);
648       mod.setKey("one");
649       mod.setValue("two");
650       list.add(mod);
651
652       mod = new Modification();
653       mod.setType(Modification.PUT_KEY_VALUE);
654       mod.setFqn(FQN);
655       mod.setKey("three");
656       mod.setValue("four");
657       list.add(mod);
658
659       Map map = new HashMap();
660       map.put("five", "six");
661       map.put("seven", "eight");
662       mod = new Modification();
663       mod.setType(Modification.PUT_DATA);
664       mod.setFqn(FQN);
665       mod.setData(map);
666       list.add(mod);
667
668       return list;
669    }
670
671    /**
672     * Checks that a list of modifications was applied.
673     */

674    private void checkModifications(List list)
675       throws Exception JavaDoc {
676
677       for (int i = 0; i < list.size(); i += 1) {
678          Modification mod = (Modification) list.get(i);
679          Fqn fqn = mod.getFqn();
680          switch (mod.getType()) {
681          case Modification.PUT_KEY_VALUE:
682             assertEquals(mod.getValue(), loader.get(fqn, mod.getKey()));
683             break;
684          case Modification.PUT_DATA:
685             Map map = mod.getData();
686             for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
687                Object JavaDoc key = iter.next();
688                assertEquals(map.get(key), loader.get(fqn, key));
689             }
690             break;
691          case Modification.REMOVE_KEY_VALUE:
692             assertEquals(null, loader.get(fqn, mod.getKey()));
693             break;
694          case Modification.REMOVE_DATA:
695             assertTrue(loader.exists(fqn));
696             assertEquals(null, loader.get(fqn));
697             break;
698          case Modification.REMOVE_NODE:
699             assertTrue(!loader.exists(fqn));
700             assertEquals(null, loader.get(fqn));
701             break;
702          default:
703             fail("unknown type: " + mod);
704             break;
705          }
706       }
707    }
708
709    /**
710     * Test exception cases and create/destroy/create/destroy sequence.
711     */

712    public void testBasicExceptions()
713       throws Exception JavaDoc {
714
715       instantiateLoader();
716       checkPreCreateExceptions();
717
718       startLoader(false, null);
719       checkPostCreateExceptions();
720       loader.put(FQN, "one", "two");
721       assertEquals("two", loader.get(FQN, "one"));
722
723       stopLoader();
724       checkPreCreateExceptions();
725
726       startLoader(false, null);
727       checkPostCreateExceptions();
728       loader.put(FQN, "one", "two");
729       assertEquals("two", loader.get(FQN, "one"));
730
731       stopLoader();
732       checkPreCreateExceptions();
733    }
734
735    /**
736     * Check exception cases that occur before create().
737     */

738    private void checkPreCreateExceptions()
739       throws Exception JavaDoc {
740
741       loader.setCache(new TreeCache());
742       loader.setConfig(null);
743       try {
744          loader.start();
745          fail();
746       } catch (IllegalStateException JavaDoc expected) {}
747
748       loader.setCache(null);
749       Properties props=new Properties();
750       props.setProperty("location", "xyz");
751       loader.setConfig(props);
752       try {
753          loader.start();
754          fail();
755       } catch (IllegalStateException JavaDoc expected) {}
756
757       loader.setCache(new TreeCache());
758       props=new Properties();
759       props.setProperty("location", "directory_that_does_not_exist");
760       loader.setConfig(props);
761       try {
762          loader.start();
763          fail();
764       } catch (DatabaseException expected) {}
765
766       try {
767          loader.put(FQN, "one", "two");
768          fail();
769       } catch (IllegalStateException JavaDoc expected) {}
770
771       try {
772          loader.put(FQN, new HashMap());
773          fail();
774       } catch (IllegalStateException JavaDoc expected) {}
775
776       try {
777          loader.put(new ArrayList());
778          fail();
779       } catch (IllegalStateException JavaDoc expected) {}
780
781       try {
782          loader.get(FQN, "one");
783          fail();
784       } catch (IllegalStateException JavaDoc expected) {}
785
786       try {
787          loader.get(FQN);
788          fail();
789       } catch (IllegalStateException JavaDoc expected) {}
790
791       try {
792          loader.remove(FQN);
793          fail();
794       } catch (IllegalStateException JavaDoc expected) {}
795
796       try {
797          loader.remove(FQN, "one");
798          fail();
799       } catch (IllegalStateException JavaDoc expected) {}
800
801       try {
802          loader.prepare(new Object JavaDoc(), new ArrayList(), false);
803          fail();
804       } catch (IllegalStateException JavaDoc expected) {}
805
806       try {
807          loader.commit(new Object JavaDoc());
808          fail();
809       } catch (IllegalStateException JavaDoc expected) {}
810
811       try {
812          loader.rollback(new Object JavaDoc());
813          fail();
814       } catch (IllegalStateException JavaDoc expected) {}
815
816       try {
817          loader.loadEntireState();
818          fail();
819       } catch (IllegalStateException JavaDoc expected) {}
820
821       try {
822          loader.storeEntireState(new byte[0]);
823          fail();
824       } catch (IllegalStateException JavaDoc expected) {}
825
826       /* Redundant stop and destroy are allowed. */
827       loader.stop();
828       loader.destroy();
829    }
830
831    /**
832     * Check exception cases that occur after create().
833     */

834    private void checkPostCreateExceptions()
835       throws Exception JavaDoc {
836
837       try {
838          loader.create();
839          fail();
840       } catch (IllegalStateException JavaDoc expected) {}
841
842       try {
843          loader.start();
844          fail();
845       } catch (IllegalStateException JavaDoc expected) {}
846
847       try {
848          loader.put(null, "one", "two");
849          fail();
850       } catch (NullPointerException JavaDoc expected) {}
851
852       try {
853          loader.put(null, new HashMap());
854          fail();
855       } catch (NullPointerException JavaDoc expected) {}
856
857       try {
858          loader.put(null);
859          fail();
860       } catch (NullPointerException JavaDoc expected) {}
861
862       try {
863          loader.get(null, "one");
864          fail();
865       } catch (NullPointerException JavaDoc expected) {}
866
867       try {
868          loader.get(null);
869          fail();
870       } catch (NullPointerException JavaDoc expected) {}
871
872       try {
873          loader.remove(null);
874          fail();
875       } catch (NullPointerException JavaDoc expected) {}
876
877       try {
878          loader.remove(null, "one");
879          fail();
880       } catch (NullPointerException JavaDoc expected) {}
881
882       try {
883          loader.prepare(null, new ArrayList(), false);
884          fail();
885       } catch (NullPointerException JavaDoc expected) {}
886       try {
887          loader.prepare(new Object JavaDoc(), null, false);
888          fail();
889       } catch (NullPointerException JavaDoc expected) {}
890
891       try {
892          loader.commit(null);
893          fail();
894       } catch (NullPointerException JavaDoc expected) {}
895
896       try {
897          loader.rollback(null);
898          fail();
899       } catch (NullPointerException JavaDoc expected) {}
900    }
901
902    /**
903     * Tests a non-transactional prepare.
904     */

905    public void testTransactionExceptions()
906       throws Exception JavaDoc {
907
908       List mods = createUpdates();
909
910       /* A non-transactional cache loader should not allow prepare(). */
911       startLoader(false, null);
912       try {
913          loader.prepare(new Object JavaDoc(), mods, false);
914          fail();
915       } catch (UnsupportedOperationException JavaDoc expected) {}
916       stopLoader();
917
918       startLoader(true, null);
919
920       /* Commit and rollback a non-prepared transaction. */
921       try {
922          loader.commit(new Object JavaDoc());
923          fail();
924       } catch (IllegalArgumentException JavaDoc expected) {}
925       try {
926          loader.rollback(new Object JavaDoc());
927          fail();
928       } catch (IllegalArgumentException JavaDoc expected) {}
929
930       /* Commit and rollback after commit. */
931       loader.storeEntireState(null);
932       Object JavaDoc txnKey = new Object JavaDoc();
933       loader.prepare(txnKey, mods, false);
934       loader.commit(txnKey);
935       try {
936          loader.commit(txnKey);
937          fail();
938       } catch (IllegalArgumentException JavaDoc expected) {}
939       try {
940          loader.rollback(txnKey);
941          fail();
942       } catch (IllegalArgumentException JavaDoc expected) {}
943
944       /* Commit and rollback after rollback. */
945       loader.storeEntireState(null);
946       txnKey = new Object JavaDoc();
947       loader.prepare(txnKey, mods, false);
948       loader.rollback(txnKey);
949       try {
950          loader.rollback(txnKey);
951          fail();
952       } catch (IllegalArgumentException JavaDoc expected) {}
953       try {
954          loader.rollback(txnKey);
955          fail();
956       } catch (IllegalArgumentException JavaDoc expected) {}
957
958       stopLoader();
959    }
960
961    /**
962     * Tests that null keys and values work as for a standard Java Map.
963     */

964    public void testNullKeysAndValues()
965       throws Exception JavaDoc {
966
967       startLoader(false, null);
968
969       loader.put(FQN, null, "x");
970       assertEquals("x", loader.get(FQN, null));
971       Map map = loader.get(FQN);
972       assertEquals(1, map.size());
973       assertEquals("x", map.get(null));
974
975       loader.put(FQN, "y", null);
976       assertEquals(null, loader.get(FQN, "y"));
977       map = loader.get(FQN);
978       assertEquals(2, map.size());
979       assertEquals("x", map.get(null));
980       assertEquals(null, map.get("y"));
981
982       loader.remove(FQN, null);
983       assertEquals(null, loader.get(FQN, null));
984       assertEquals(1, loader.get(FQN).size());
985
986       loader.remove(FQN, "y");
987       assertEquals(null, loader.get(FQN, "y"));
988       assertEquals(null, loader.get(FQN));
989
990       map = new HashMap();
991       map.put(null, null);
992       loader.put(FQN, map);
993       assertEquals(map, loader.get(FQN));
994
995       loader.remove(FQN);
996       assertEquals(null, loader.get(FQN));
997
998       map = new HashMap();
999       map.put("xyz", null);
1000      map.put(null, "abc");
1001      loader.put(FQN, map);
1002      assertEquals(map, loader.get(FQN));
1003
1004      loader.remove(FQN);
1005      assertEquals(null, loader.get(FQN));
1006
1007      stopLoader();
1008   }
1009
1010   /**
1011    * Test non-default database name.
1012    */

1013   public void testDatabaseName()
1014      throws Exception JavaDoc {
1015
1016      startLoader(false, "nonDefaultDbName");
1017      loader.put(FQN, "one", "two");
1018      assertEquals("two", loader.get(FQN, "one"));
1019      stopLoader();
1020   }
1021
1022   /**
1023    * Test load/store state.
1024    */

1025   public void testLoadAndStore()
1026      throws Exception JavaDoc {
1027
1028      startLoader(false, null);
1029
1030      /* Empty state. */
1031      assertEquals(0, loader.loadEntireState().length);
1032      loader.storeEntireState(new byte[0]);
1033      assertEquals(0, loader.loadEntireState().length);
1034      loader.storeEntireState(null);
1035      assertEquals(0, loader.loadEntireState().length);
1036      assertEquals(null, loader.get(FQN));
1037
1038      /* Use a complex object to ensure that the class catalog is used. */
1039      Complex c1 = new Complex();
1040      Complex c2 = new Complex(c1);
1041
1042      /* Add objects. */
1043      loader.put(FQN, new Integer JavaDoc(1), c1);
1044      loader.put(FQN, new Integer JavaDoc(2), c2);
1045      assertEquals(c1, loader.get(FQN, new Integer JavaDoc(1)));
1046      assertEquals(c2, loader.get(FQN, new Integer JavaDoc(2)));
1047      assertEquals(2, loader.get(FQN).size());
1048
1049      /* Save state. */
1050      byte[] state = loader.loadEntireState();
1051      assertTrue(state.length > 0);
1052
1053      /* Clear state. */
1054      loader.storeEntireState(null);
1055      assertEquals(0, loader.loadEntireState().length);
1056      assertEquals(null, loader.get(FQN));
1057
1058      /* Restore state. */
1059      loader.storeEntireState(state);
1060      assertEquals(c1, loader.get(FQN, new Integer JavaDoc(1)));
1061      assertEquals(c2, loader.get(FQN, new Integer JavaDoc(2)));
1062      assertEquals(2, loader.get(FQN).size());
1063
1064      stopLoader();
1065   }
1066
1067   public static Test suite()
1068      throws Exception JavaDoc {
1069
1070      return new TestSuite(BdbjeUnitTestCase.class);
1071   }
1072
1073   public static void main(String JavaDoc[] args)
1074      throws Exception JavaDoc {
1075
1076      junit.textui.TestRunner.run(suite());
1077   }
1078
1079   /**
1080    * Complex object whose class description is stored in the class catalog.
1081    */

1082   private static class Complex implements Serializable JavaDoc {
1083
1084      Complex nested;
1085
1086      Complex() {
1087         this(null);
1088      }
1089
1090      Complex(Complex nested) {
1091         this.nested = nested;
1092      }
1093
1094      public boolean equals(Object JavaDoc o) {
1095         try {
1096            Complex x = (Complex) o;
1097            return (nested != null) ? nested.equals(x.nested)
1098                                    : (x.nested == null);
1099         } catch (ClassCastException JavaDoc e) {
1100            return false;
1101         }
1102      }
1103   }
1104}
1105
Popular Tags