KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > DatabaseConfigTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DatabaseConfigTest.java,v 1.22 2006/10/30 21:14:41 bostic Exp $
7  */

8
9 package com.sleepycat.je;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.Serializable JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import junit.framework.TestCase;
18
19 import com.sleepycat.je.util.TestUtils;
20
21 /**
22  * Basic database configuration testing.
23  */

24 public class DatabaseConfigTest extends TestCase {
25     private static final boolean DEBUG = false;
26   
27     private File JavaDoc envHome;
28     private Environment env;
29
30     public DatabaseConfigTest() {
31         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
32     }
33
34     public void setUp()
35         throws IOException JavaDoc {
36
37         TestUtils.removeLogFiles("Setup", envHome, false);
38     }
39     
40     public void tearDown()
41         throws Exception JavaDoc {
42
43         try {
44             /* Close in case we hit an exception and didn't close. */
45             if (env != null) {
46                 env.close();
47             }
48         } catch (DatabaseException e) {
49             /* Ok if already closed */
50         }
51         env = null; // for JUNIT, to reduce memory usage when run in a suite.
52
TestUtils.removeLogFiles("TearDown", envHome, false);
53     }
54
55     /**
56      * Test that we can retrieve a database configuration and that it clones
57      * its configuration appropriately.
58      */

59     public void testConfig()
60         throws Throwable JavaDoc {
61
62         try {
63             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
64             envConfig.setAllowCreate(true);
65             env = new Environment(envHome, envConfig);
66
67             /*
68              * Make sure that the database keeps its own copy of the
69              * configuration object.
70              */

71             DatabaseConfig dbConfigA = new DatabaseConfig();
72             dbConfigA.setAllowCreate(true);
73             Database dbA = env.openDatabase(null, "foo", dbConfigA);
74
75             /* Change the original dbConfig */
76             dbConfigA.setAllowCreate(false);
77             DatabaseConfig getConfig1 = dbA.getConfig();
78             assertEquals(true, getConfig1.getAllowCreate());
79             assertEquals(false, getConfig1.getSortedDuplicates());
80
81             /*
82              * Change the retrieved config, ought to have no effect on what the
83              * Database is storing.
84              */

85             getConfig1.setSortedDuplicates(true);
86             DatabaseConfig getConfig2 = dbA.getConfig();
87             assertEquals(false, getConfig2.getSortedDuplicates());
88             
89             dbA.close();
90             env.close();
91         } catch (Throwable JavaDoc t) {
92             t.printStackTrace();
93             throw t;
94         }
95     }
96
97     public void testConfigMatching()
98         throws Throwable JavaDoc {
99
100         try {
101         /* DatabaseConfig matching. */
102
103             DatabaseConfig confA = new DatabaseConfig();
104             DatabaseConfig confB = new DatabaseConfig();
105
106         try {
107         confA.validate(confB);
108         } catch (Exception JavaDoc E) {
109         fail("expected valid match");
110         }
111
112         try {
113         confB.validate(confA);
114         } catch (Exception JavaDoc E) {
115         fail("expected valid match");
116         }
117
118         try {
119         confA.validate(null); // uses the DEFAULT config
120
} catch (Exception JavaDoc E) {
121         fail("expected valid match");
122         }
123
124             confA.setReadOnly(true);
125         try {
126         confA.validate(confB);
127         fail("expected exception");
128         } catch (DatabaseException E) {
129         // ok
130
}
131
132             confA.setReadOnly(false);
133         confA.setSortedDuplicates(true);
134         try {
135         confA.validate(confB);
136         fail("expected exception");
137         } catch (DatabaseException E) {
138         // ok
139
}
140         confA.setSortedDuplicates(false);
141
142             confA.setOverrideBtreeComparator(true);
143         confA.setBtreeComparator(TestComparator.class);
144             confB.setOverrideBtreeComparator(true);
145         confB.setBtreeComparator(TestComparator2.class);
146         try {
147         confA.validate(confB);
148         fail("expected exception");
149         } catch (DatabaseException E) {
150         // ok
151
}
152         confA.setBtreeComparator((Class JavaDoc) null);
153             confA.setOverrideBtreeComparator(false);
154         confB.setBtreeComparator((Class JavaDoc) null);
155             confB.setOverrideBtreeComparator(false);
156
157             confA.setOverrideDuplicateComparator(true);
158         confA.setDuplicateComparator(TestComparator.class);
159             confB.setOverrideDuplicateComparator(true);
160         confB.setDuplicateComparator(TestComparator2.class);
161         try {
162         confA.validate(confB);
163         fail("expected exception");
164         } catch (DatabaseException E) {
165         // ok
166
}
167
168             /* Same tests as above but for serialized comparators. */
169
170             confA.setOverrideBtreeComparator(true);
171         confA.setBtreeComparator(new TestSerialComparator());
172             confB.setOverrideBtreeComparator(true);
173         confB.setBtreeComparator(new TestSerialComparator2());
174         try {
175         confA.validate(confB);
176         fail("expected exception");
177         } catch (DatabaseException E) {
178         // ok
179
}
180         confA.setBtreeComparator((Comparator JavaDoc) null);
181             confA.setOverrideBtreeComparator(false);
182         confB.setBtreeComparator((Comparator JavaDoc) null);
183             confB.setOverrideBtreeComparator(false);
184
185             confA.setOverrideDuplicateComparator(true);
186         confA.setDuplicateComparator(new TestSerialComparator());
187             confB.setOverrideDuplicateComparator(true);
188         confB.setDuplicateComparator(new TestSerialComparator2());
189         try {
190         confA.validate(confB);
191         fail("expected exception");
192         } catch (DatabaseException E) {
193         // ok
194
}
195
196         /* SecondaryConfig matching. */
197
198             SecondaryConfig confC = new SecondaryConfig();
199             SecondaryConfig confD = new SecondaryConfig();
200         confC.setKeyCreator(new SecKeyCreator1());
201         confD.setKeyCreator(new SecKeyCreator1());
202
203         try {
204         confC.validate(confD);
205         } catch (Exception JavaDoc E) {
206         E.printStackTrace();
207         fail("expected valid match");
208         }
209
210         try {
211         confD.validate(confC);
212         } catch (Exception JavaDoc E) {
213         fail("expected valid match");
214         }
215
216         try {
217         confC.validate(null);
218         fail("expected exception");
219         } catch (DatabaseException E) {
220         // ok
221
}
222
223         confD.setKeyCreator(new SecKeyCreator2());
224         try {
225         confC.validate(confD);
226         fail("expected exception");
227         } catch (DatabaseException E) {
228         // ok
229
}
230         confD.setKeyCreator(new SecKeyCreator1());
231
232         confD.setMultiKeyCreator(new SecMultiKeyCreator1());
233         try {
234         confC.validate(confD);
235         fail("expected exception");
236         } catch (DatabaseException E) {
237         // ok
238
}
239         confD.setMultiKeyCreator(null);
240
241         confC.setForeignKeyDeleteAction(ForeignKeyDeleteAction.NULLIFY);
242         try {
243         confC.validate(confD);
244         fail("expected exception");
245         } catch (DatabaseException E) {
246         // ok
247
}
248         confC.setForeignKeyDeleteAction(ForeignKeyDeleteAction.ABORT);
249
250         confC.setForeignKeyNullifier(new ForeignKeyNullifier1());
251         try {
252         confC.validate(confD);
253         fail("expected exception");
254         } catch (DatabaseException E) {
255         // ok
256
}
257         confC.setForeignKeyNullifier(null);
258
259         confC.setForeignMultiKeyNullifier(new ForeignMultiKeyNullifier1());
260         try {
261         confC.validate(confD);
262         fail("expected exception");
263         } catch (DatabaseException E) {
264         // ok
265
}
266         confC.setForeignMultiKeyNullifier(null);
267
268         confC.setImmutableSecondaryKey(true);
269         try {
270         confC.validate(confD);
271         fail("expected exception");
272         } catch (DatabaseException E) {
273         // ok
274
}
275         confC.setImmutableSecondaryKey(false);
276         } catch (Throwable JavaDoc t) {
277             t.printStackTrace();
278             throw t;
279         }
280     }
281
282     /**
283      * Make sure we can instantiate a comparator at the time it's set.
284      */

285     public void testComparator()
286         throws Throwable JavaDoc {
287
288         try {
289             /* Can't be instantiated, a nested class */
290             try {
291                 DatabaseConfig config = new DatabaseConfig();
292                 config.setBtreeComparator(BadComparator1.class);
293                 fail("Comparator shouldn't be instantiated");
294             } catch (IllegalArgumentException JavaDoc e) {
295                 /* Expected. */
296                 if (DEBUG) {
297                     System.out.println(e);
298                 }
299             }
300
301             /* No zero-parameter constructor */
302             try {
303                 DatabaseConfig config = new DatabaseConfig();
304                 config.setBtreeComparator(BadComparator2.class);
305                 fail("Comparator shouldn't be instantiated");
306             } catch (IllegalArgumentException JavaDoc e) {
307                 /* Expected. */
308                 if (DEBUG) {
309                     System.out.println(e);
310                 }
311             }
312
313             /* Can't be serialized, not serializable */
314             try {
315                 DatabaseConfig config = new DatabaseConfig();
316                 config.setBtreeComparator(new BadSerialComparator1());
317                 fail("Comparator shouldn't be instantiated");
318             } catch (IllegalArgumentException JavaDoc e) {
319                 /* Expected. */
320                 if (DEBUG) {
321                     System.out.println(e);
322                 }
323             }
324
325             /* Can't be serialized, contains non-serializable field */
326             try {
327                 DatabaseConfig config = new DatabaseConfig();
328                 config.setBtreeComparator(new BadSerialComparator2());
329                 fail("Comparator shouldn't be instantiated");
330             } catch (IllegalArgumentException JavaDoc e) {
331                 /* Expected. */
332                 if (DEBUG) {
333                     System.out.println(e);
334                 }
335             }
336
337             /* Valid comparators */
338             DatabaseConfig config = new DatabaseConfig();
339             config.setBtreeComparator(TestComparator.class);
340             config.setBtreeComparator(TestComparator2.class);
341             config.setBtreeComparator(new TestSerialComparator());
342             config.setBtreeComparator(new TestSerialComparator2());
343
344         } catch (Throwable JavaDoc t) {
345             t.printStackTrace();
346             throw t;
347         }
348     }
349
350     /**
351      * Test that any conflicts between configuration object settings and the
352      * underlying impl object are detected.
353      */

354     public void testConfigConfict()
355         throws Throwable JavaDoc {
356         
357         try {
358             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
359             envConfig.setAllowCreate(true);
360             env = new Environment(envHome, envConfig);
361
362             /*
363              * Test conflicts of duplicate allowed configuration.
364              */

365
366             /* 1a. Create allowing duplicates. */
367             DatabaseConfig firstConfig = new DatabaseConfig();
368             firstConfig.setAllowCreate(true);
369             firstConfig.setSortedDuplicates(true);
370             Database firstHandle = env.openDatabase(null, "fooDups",
371                                                     firstConfig);
372             /* 1b. Try to open w/no duplicates. */
373             DatabaseConfig secondConfig = new DatabaseConfig();
374             secondConfig.setSortedDuplicates(false);
375             try {
376                     env.openDatabase(null, "fooDups", secondConfig);
377                 fail("Conflict in duplicates allowed should be detected.");
378             } catch (DatabaseException e) {
379             }
380             
381             firstHandle.close();
382             env.removeDatabase(null, "fooDups");
383
384             /* 2a. Create dis-allowing duplicates. */
385             firstConfig.setSortedDuplicates(false);
386             firstHandle = env.openDatabase(null, "fooDups", firstConfig);
387             /* 2b. Try to open w/duplicates. */
388             secondConfig.setSortedDuplicates(true);
389             try {
390                     env.openDatabase(null, "fooDups", secondConfig);
391                 fail("Conflict in duplicates allowed should be detected.");
392             } catch (DatabaseException e) {
393             }
394             firstHandle.close();
395
396             /*
397              * Test conflicts of read only. If the environment is read/write
398              * we should be able to open handles in read only or read/write
399              * mode. If the environment is readonly, the database handles
400              * must also be read only.
401              */

402             DatabaseConfig readOnlyConfig = new DatabaseConfig();
403             readOnlyConfig.setReadOnly(true);
404             Database roHandle = env.openDatabase(null, "fooDups",
405                                                  readOnlyConfig);
406             roHandle.close();
407
408             /* Open the environment in read only mode. */
409             env.close();
410             envConfig = TestUtils.initEnvConfig();
411             envConfig.setReadOnly(true);
412             env = new Environment(envHome, envConfig);
413             
414             /* Open a readOnly database handle, should succeed */
415             roHandle = env.openDatabase(null, "fooDups",
416                                         readOnlyConfig);
417             roHandle.close();
418
419             /* Open a read/write database handle, should succeed. */
420             try {
421                 env.openDatabase(null, "fooDups", null);
422                 fail("Should not be able to open read/write");
423             } catch (DatabaseException e) {
424                 if (DEBUG) {
425                     System.out.println(e);
426                 }
427             }
428             env.close();
429
430             /*
431              * Check comparator changes.
432              */

433             /* 1a. Open w/a null comparator */
434             env = new Environment(envHome, null);
435             firstConfig = new DatabaseConfig();
436             firstConfig.setAllowCreate(true);
437             firstHandle = env.openDatabase(null,
438                                            "fooComparator",
439                                            firstConfig);
440             DatabaseConfig firstRetrievedConfig = firstHandle.getConfig();
441             assertEquals(null, firstRetrievedConfig.getBtreeComparator());
442             assertEquals(null, firstRetrievedConfig.getDuplicateComparator());
443
444             /*
445              * 1b. Open a db w/a different comparator, shouldn't take effect
446              * because override is not set.
447              */

448             secondConfig = new DatabaseConfig();
449             Comparator JavaDoc btreeComparator = new TestComparator();
450             Comparator JavaDoc dupComparator = new TestComparator();
451             secondConfig.setBtreeComparator(btreeComparator.getClass());
452             secondConfig.setDuplicateComparator(dupComparator.getClass());
453             Database secondHandle =
454         env.openDatabase(null, "fooComparator", secondConfig);
455             DatabaseConfig retrievedConfig = secondHandle.getConfig();
456             assertEquals(null, retrievedConfig.getBtreeComparator());
457             assertEquals(null, retrievedConfig.getDuplicateComparator());
458             secondHandle.close();
459
460             /* Same as above but with a serialized comparator. */
461             secondConfig = new DatabaseConfig();
462             btreeComparator = new TestSerialComparator();
463             dupComparator = new TestSerialComparator();
464             secondConfig.setBtreeComparator(btreeComparator);
465             secondConfig.setDuplicateComparator(dupComparator);
466             secondHandle =
467         env.openDatabase(null, "fooComparator", secondConfig);
468             retrievedConfig = secondHandle.getConfig();
469             assertEquals(null, retrievedConfig.getBtreeComparator());
470             assertEquals(null, retrievedConfig.getDuplicateComparator());
471             secondHandle.close();
472
473             /* 1c. Allow override */
474             secondConfig.setOverrideBtreeComparator(true);
475             secondConfig.setOverrideDuplicateComparator(true);
476             btreeComparator = new TestComparator();
477             dupComparator = new TestComparator();
478             secondConfig.setBtreeComparator(btreeComparator.getClass());
479             secondConfig.setDuplicateComparator(dupComparator.getClass());
480             secondHandle = env.openDatabase(null,
481                                             "fooComparator",
482                                             secondConfig);
483
484             retrievedConfig = secondHandle.getConfig();
485             assertEquals(btreeComparator.getClass(),
486                          retrievedConfig.getBtreeComparator().getClass());
487             assertEquals(dupComparator.getClass(),
488                          retrievedConfig.getDuplicateComparator().getClass());
489             secondHandle.close();
490
491             /* Same as above but with a serialized comparator. */
492             secondConfig.setOverrideBtreeComparator(true);
493             secondConfig.setOverrideDuplicateComparator(true);
494             btreeComparator = new TestSerialComparator();
495             dupComparator = new TestSerialComparator();
496             secondConfig.setBtreeComparator(btreeComparator);
497             secondConfig.setDuplicateComparator(dupComparator);
498             secondHandle = env.openDatabase(null,
499                                             "fooComparator",
500                                             secondConfig);
501
502             retrievedConfig = secondHandle.getConfig();
503             assertEquals(btreeComparator,
504                          retrievedConfig.getBtreeComparator());
505             assertEquals(dupComparator,
506                          retrievedConfig.getDuplicateComparator());
507             secondHandle.close();
508
509             firstHandle.close();
510             env.close();
511         } catch (Throwable JavaDoc t) {
512             t.printStackTrace();
513             env.close();
514             throw t;
515         }
516     }
517
518     public void testIsTransactional()
519         throws Throwable JavaDoc {
520
521         try {
522             /* Open environment in transactional mode.*/
523             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
524             envConfig.setTransactional(true);
525             envConfig.setAllowCreate(true);
526             env = new Environment(envHome, envConfig);
527
528             /* Create a db, open transactionally with implied auto-commit. */
529             DatabaseConfig dbConfig = new DatabaseConfig();
530             dbConfig.setAllowCreate(true);
531             dbConfig.setTransactional(true);
532             Database myDb = env.openDatabase(null, "testDB", dbConfig);
533             assertTrue(myDb.isTransactional());
534             assertTrue(myDb.getConfig().getTransactional());
535             myDb.close();
536
537             /* Open an existing db, can open it non-transactionally. */
538             dbConfig.setTransactional(false);
539             myDb = env.openDatabase(null, "testDB", null);
540             assertFalse(myDb.isTransactional());
541             assertFalse(myDb.getConfig().getTransactional());
542             myDb.close();
543
544             /* Open another db, pass an explicit transaction. */
545             dbConfig.setTransactional(true);
546             Transaction txn = env.beginTransaction(null, null);
547             myDb = env.openDatabase(txn, "testDB2", dbConfig);
548             assertTrue(myDb.isTransactional());
549             assertTrue(myDb.getConfig().getTransactional());
550
551             DatabaseEntry key = new DatabaseEntry();
552             DatabaseEntry data = new DatabaseEntry();
553             key.setData(TestUtils.getTestArray(0));
554             data.setData(TestUtils.getTestArray(0));
555             try {
556                 myDb.put(null, key, data);
557             } catch (DatabaseException DBE) {
558                 fail("didn't expect DatabaseException, implied autocommit");
559             }
560
561             key.setData(TestUtils.getTestArray(1));
562             data.setData(TestUtils.getTestArray(1));
563             try {
564                 myDb.put(txn, key, data);
565             } catch (DatabaseException DBE) {
566                 fail("didn't expect DatabaseException with txn passed");
567             }
568
569             try {
570                 myDb.get(txn, key, data, LockMode.DEFAULT);
571             } catch (DatabaseException DBE) {
572                 fail("didn't expect DatabaseException with txn passed");
573             }
574
575             txn.commit();
576
577             try {
578                 myDb.get(null, key, data, LockMode.DEFAULT);
579             } catch (DatabaseException DBE) {
580                 fail("didn't expect DatabaseException because no txn passed");
581             }
582
583             myDb.close();
584
585             env.close();
586         } catch (Throwable JavaDoc t) {
587             t.printStackTrace();
588             throw t;
589         }
590     }
591
592     public void testOpenReadOnly()
593         throws Throwable JavaDoc {
594
595         try {
596             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
597             envConfig.setTransactional(true);
598             envConfig.setAllowCreate(true);
599             env = new Environment(envHome, envConfig);
600
601             DatabaseEntry key = new DatabaseEntry();
602             DatabaseEntry data = new DatabaseEntry();
603
604             Transaction txn = env.beginTransaction(null, null);
605             DatabaseConfig dbConfig = new DatabaseConfig();
606             dbConfig.setTransactional(true);
607             dbConfig.setAllowCreate(true);
608             Database myDb = env.openDatabase(txn, "testDB2", dbConfig);
609
610             key.setData(TestUtils.getTestArray(0));
611             data.setData(TestUtils.getTestArray(0));
612             try {
613                 myDb.put(txn, key, data);
614             } catch (DatabaseException DBE) {
615                 fail("unexpected DatabaseException during put");
616             }
617
618             txn.commit();
619             myDb.close();
620
621             dbConfig = new DatabaseConfig();
622             dbConfig.setTransactional(true);
623             dbConfig.setReadOnly(true);
624             txn = env.beginTransaction(null, null);
625             myDb = env.openDatabase(txn, "testDB2", dbConfig);
626             assertTrue(myDb.isTransactional());
627             assertTrue(myDb.getConfig().getTransactional());
628
629             key.setData(TestUtils.getTestArray(0));
630             data.setData(TestUtils.getTestArray(0));
631             try {
632                 myDb.put(txn, key, data);
633                 fail("expected DatabaseException because open RDONLY");
634             } catch (DatabaseException DBE) {
635             }
636
637             key.setData(TestUtils.getTestArray(0));
638             data.setData(TestUtils.getTestArray(0));
639             assertEquals(OperationStatus.SUCCESS,
640                          myDb.get(txn, key, data, LockMode.DEFAULT));
641
642             Cursor cursor = myDb.openCursor(txn, null);
643
644             assertEquals(OperationStatus.SUCCESS,
645                          cursor.getFirst(key, data, LockMode.DEFAULT));
646
647             try {
648                 cursor.delete();
649                 fail("expected Exception from delete on RD_ONLY db");
650             } catch (DatabaseException DBE) {
651             }
652
653             key.setData(TestUtils.getTestArray(1));
654             data.setData(TestUtils.getTestArray(1));
655             try {
656                 myDb.put(txn, key, data);
657                 fail("expected DatabaseException because open RDONLY");
658             } catch (DatabaseException DBE) {
659             }
660
661         cursor.close();
662             txn.commit();
663             myDb.close();
664
665             env.close();
666         } catch (Throwable JavaDoc t) {
667             t.printStackTrace();
668             throw t;
669         }
670     }
671
672     /**
673      * Test exclusive creation.
674      */

675     public void testExclusive()
676         throws Throwable JavaDoc {
677
678         try {
679             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
680
681             /*
682              * Make sure that the database keeps its own copy of the
683              * configuration object.
684              */

685             envConfig.setAllowCreate(true);
686             env = new Environment(envHome, envConfig);
687             DatabaseConfig dbConfig = new DatabaseConfig();
688             dbConfig.setAllowCreate(true);
689             dbConfig.setExclusiveCreate(true);
690
691             /* Should succeed and create the database. */
692             Database dbA = env.openDatabase(null, "foo", dbConfig);
693             dbA.close();
694                                             
695             /* Should not succeed, because the database exists. */
696             try {
697                 env.openDatabase(null, "foo", dbConfig);
698                 fail("Database already exists");
699             } catch (DatabaseException e) {
700             }
701             env.close();
702         } catch (Throwable JavaDoc t) {
703             t.printStackTrace();
704             throw t;
705         }
706     }
707
708     /*
709      * This Comparator can't be instantiated because it's private and not
710      * static.
711      */

712     private class BadComparator1 implements Comparator JavaDoc {
713         public BadComparator1(int foo) {
714         }
715         
716         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
717             return 0;
718         }
719     }
720
721     /*
722      * This Comparator can't be instantiated because it doesn't have zero
723      * parameter constructor.
724      */

725     public static class BadComparator2 implements Comparator JavaDoc {
726         public BadComparator2(int i) {
727         }
728
729         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
730             return 0;
731         }
732     }
733
734     /*
735      * OK comparator for setting comparators.
736      */

737     public static class TestComparator implements Comparator JavaDoc {
738         public TestComparator() {
739         }
740
741         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
742             return 0;
743         }
744     }
745
746     /*
747      * OK comparator for setting comparators.
748      */

749     public static class TestComparator2 implements Comparator JavaDoc {
750         public TestComparator2() {
751         }
752
753         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
754             return 0;
755         }
756     }
757
758     /*
759      * This Comparator can't be serialized because it's not serializable.
760      */

761     public class BadSerialComparator1 implements Comparator JavaDoc {
762
763         public BadSerialComparator1() {
764         }
765         
766         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
767             return 0;
768         }
769     }
770
771     /*
772      * This Comparator can't be serialized because it contains a reference to
773      * an object that's not serializable.
774      */

775     public class BadSerialComparator2 implements Comparator JavaDoc, Serializable JavaDoc {
776
777         private BadSerialComparator1 o = new BadSerialComparator1();
778
779         public BadSerialComparator2() {
780         }
781         
782         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
783             return 0;
784         }
785     }
786
787     /*
788      * OK comparator for setting comparators -- private class, private
789      * constructor, and serializable fields are allowed.
790      */

791     private static class TestSerialComparator
792         implements Comparator JavaDoc, Serializable JavaDoc {
793
794         private String JavaDoc s = "sss";
795
796         private TestSerialComparator() {
797         }
798
799         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
800             return 0;
801         }
802
803         public boolean equals(Object JavaDoc other) {
804             TestSerialComparator o = (TestSerialComparator) other;
805             return s.equals(o.s);
806         }
807     }
808
809     /*
810      * OK comparator for setting comparators.
811      */

812     public static class TestSerialComparator2
813         implements Comparator JavaDoc, Serializable JavaDoc {
814
815         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
816             return 0;
817         }
818     }
819
820     public static class SecKeyCreator1 implements SecondaryKeyCreator {
821     public boolean createSecondaryKey(SecondaryDatabase secondary,
822                       DatabaseEntry key,
823                       DatabaseEntry data,
824                       DatabaseEntry result)
825         throws DatabaseException {
826
827         return true;
828     }
829
830     public boolean equals(Object JavaDoc o) {
831         if (o == null) {
832         return false;
833         }
834         return (o.getClass() == getClass());
835     }
836     }
837
838     public static class SecKeyCreator2 implements SecondaryKeyCreator {
839     public boolean createSecondaryKey(SecondaryDatabase secondary,
840                       DatabaseEntry key,
841                       DatabaseEntry data,
842                       DatabaseEntry result)
843         throws DatabaseException {
844
845         return true;
846     }
847
848     public boolean equals(Object JavaDoc o) {
849         if (o == null) {
850         return false;
851         }
852         return (o.getClass() == getClass());
853     }
854     }
855
856     public static class SecMultiKeyCreator1
857         implements SecondaryMultiKeyCreator {
858     public void createSecondaryKeys(SecondaryDatabase secondary,
859                     DatabaseEntry key,
860                     DatabaseEntry data,
861                     Set JavaDoc results)
862         throws DatabaseException {
863     }
864
865     public boolean equals(Object JavaDoc o) {
866         if (o == null) {
867         return false;
868         }
869         return (o.getClass() == getClass());
870     }
871     }
872
873     public static class ForeignKeyNullifier1 implements ForeignKeyNullifier {
874     public boolean nullifyForeignKey(SecondaryDatabase secondary,
875                      DatabaseEntry data)
876         throws DatabaseException {
877
878         return true;
879     }
880     }
881
882     public static class ForeignMultiKeyNullifier1
883         implements ForeignMultiKeyNullifier {
884     public boolean nullifyForeignKey(SecondaryDatabase secondary,
885                      DatabaseEntry key,
886                      DatabaseEntry data,
887                      DatabaseEntry secKey)
888         throws DatabaseException {
889
890         return true;
891     }
892     }
893 }
894
Popular Tags