KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DatabaseComparatorsTest.java,v 1.6 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.util.Comparator JavaDoc;
14
15 import junit.framework.TestCase;
16
17 import com.sleepycat.bind.tuple.IntegerBinding;
18 import com.sleepycat.je.config.EnvironmentParams;
19 import com.sleepycat.je.util.TestUtils;
20
21 public class DatabaseComparatorsTest extends TestCase {
22     
23     private File JavaDoc envHome;
24     private Environment env;
25     private Database db;
26     private boolean DEBUG = false;
27
28     public DatabaseComparatorsTest() {
29         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
30     }
31
32     public void setUp()
33     throws IOException JavaDoc {
34
35         TestUtils.removeLogFiles("Setup", envHome, false);
36     }
37     
38     public void tearDown()
39     throws IOException JavaDoc, DatabaseException {
40
41         closeDb();
42         TestUtils.removeLogFiles("TearDown", envHome, false);
43     }
44
45     public void testSR12517()
46         throws Exception JavaDoc {
47
48         openEnv();
49
50         DatabaseConfig dbConfig = new DatabaseConfig();
51         dbConfig.setAllowCreate(true);
52         Comparator JavaDoc reverse = new ReverseComparator();
53         dbConfig.setBtreeComparator(reverse.getClass());
54         dbConfig.setDuplicateComparator(reverse.getClass());
55         db = env.openDatabase(null, "testDB", dbConfig);
56
57         DatabaseEntry key = new DatabaseEntry();
58         DatabaseEntry data = new DatabaseEntry();
59
60         /* Insert 5 items. */
61         for (int i = 0; i < 5; i++) {
62             IntegerBinding.intToEntry(i, key);
63             IntegerBinding.intToEntry(i, data);
64             assertEquals(OperationStatus.SUCCESS, db.put(null, key, data));
65         /* Add a dup. */
66             IntegerBinding.intToEntry(i * 2, data);
67             assertEquals(OperationStatus.SUCCESS, db.put(null, key, data));
68         }
69         read();
70
71         db.close();
72         env.close();
73
74         openEnv();
75         db = env.openDatabase(null, "testDB", dbConfig);
76
77         read();
78     }
79
80     private void read()
81         throws DatabaseException {
82
83         DatabaseEntry key = new DatabaseEntry();
84         DatabaseEntry data = new DatabaseEntry();
85
86         /* Iterate */
87         Cursor c = db.openCursor(null, null);
88         int expected = 4;
89         while (c.getNext(key, data, LockMode.DEFAULT) ==
90                OperationStatus.SUCCESS) {
91             assertEquals(expected, IntegerBinding.entryToInt(key));
92             expected--;
93         if (DEBUG) {
94         System.out.println("cursor: k=" +
95                    IntegerBinding.entryToInt(key) +
96                    " d=" +
97                    IntegerBinding.entryToInt(data));
98         }
99         }
100     assertEquals(expected, -1);
101
102         c.close();
103
104         /* Retrieve 5 items */
105         for (int i = 0; i < 5; i++) {
106             IntegerBinding.intToEntry(i, key);
107             assertEquals(OperationStatus.SUCCESS,
108                          db.get(null, key, data, LockMode.DEFAULT));
109             assertEquals(i, IntegerBinding.entryToInt(key));
110             assertEquals(i * 2, IntegerBinding.entryToInt(data));
111         if (DEBUG) {
112         System.out.println("k=" +
113                    IntegerBinding.entryToInt(key) +
114                    " d=" +
115                    IntegerBinding.entryToInt(data));
116         }
117         }
118     }
119
120     private void openEnv()
121         throws DatabaseException {
122
123         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
124         envConfig.setAllowCreate(true);
125         envConfig.setConfigParam(EnvironmentParams.ENV_CHECK_LEAKS.getName(),
126                                  "true");
127         env = new Environment(envHome, envConfig);
128     }
129
130     private void closeDb()
131         throws DatabaseException {
132
133         if (db != null) {
134             db.close();
135             db = null;
136         }
137         if (env != null) {
138             env.close();
139             env = null;
140         }
141     }
142
143     public static class ReverseComparator implements Comparator JavaDoc {
144
145     public ReverseComparator() {
146     }
147
148     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
149
150             DatabaseEntry arg1 = new DatabaseEntry((byte[]) o1);
151             DatabaseEntry arg2 = new DatabaseEntry((byte[]) o2);
152             int val1 = IntegerBinding.entryToInt(arg1);
153             int val2 = IntegerBinding.entryToInt(arg2);
154
155             if (val1 < val2) {
156                 return 1;
157             } else if (val1 > val2) {
158                 return -1;
159             } else {
160                 return 0;
161             }
162     }
163     }
164 }
165
Popular Tags