KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DatabaseEntryTest.java,v 1.30 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.Arrays JavaDoc;
14
15 import junit.framework.TestCase;
16
17 import com.sleepycat.je.DbInternal;
18 import com.sleepycat.je.config.EnvironmentParams;
19 import com.sleepycat.je.util.TestUtils;
20
21 public class DatabaseEntryTest extends TestCase {
22     
23     private File JavaDoc envHome;
24     private Environment env;
25     private Database db;
26
27     public DatabaseEntryTest() {
28         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
29     }
30
31     public void setUp()
32     throws IOException JavaDoc {
33
34         TestUtils.removeLogFiles("Setup", envHome, false);
35     }
36     
37     public void tearDown()
38     throws IOException JavaDoc {
39
40         TestUtils.removeLogFiles("TearDown", envHome, false);
41     }
42
43     public void testBasic()
44         throws Exception JavaDoc {
45         
46         /* Constructor that takes a byte array. */
47         int size = 10;
48         byte [] foo = new byte[size];
49         byte val = 1;
50         Arrays.fill(foo, val);
51
52         DatabaseEntry dbtA = new DatabaseEntry(foo);
53         assertEquals(foo.length, dbtA.getSize());
54         assertTrue(Arrays.equals(foo, dbtA.getData()));
55
56         /* Set the data to null */
57         dbtA.setData(null);
58         assertEquals(0, dbtA.getSize());
59         assertFalse(Arrays.equals(foo, dbtA.getData()));
60
61         /* Constructor that sets the data later */
62         DatabaseEntry dbtLater = new DatabaseEntry();
63         assertTrue(dbtLater.getData() == null);
64         assertEquals(0, dbtLater.getSize());
65         dbtLater.setData(foo);
66         assertTrue(Arrays.equals(foo, dbtLater.getData()));
67
68         /* Set offset, then reset data and offset should be reset. */
69         DatabaseEntry dbtOffset = new DatabaseEntry(foo, 1, 1);
70         assertEquals(1, dbtOffset.getOffset());
71         assertEquals(1, dbtOffset.getSize());
72         dbtOffset.setData(foo);
73         assertEquals(0, dbtOffset.getOffset());
74         assertEquals(foo.length, dbtOffset.getSize());
75     }
76
77     public void testOffset()
78     throws DatabaseException {
79
80     final int N_BYTES = 30;
81
82         openDb(false);
83
84     DatabaseEntry originalKey = new DatabaseEntry(new byte[N_BYTES]);
85     DatabaseEntry originalData = new DatabaseEntry(new byte[N_BYTES]);
86     for (int i = 0; i < N_BYTES; i++) {
87         originalKey.getData()[i] = (byte) i;
88         originalData.getData()[i] = (byte) i;
89     }
90
91     originalKey.setSize(10);
92     originalKey.setOffset(10);
93     originalData.setSize(10);
94     originalData.setOffset(10);
95
96     db.put(null, originalKey, originalData);
97
98     Cursor cursor = db.openCursor(null, CursorConfig.DEFAULT);
99
100     DatabaseEntry foundKey = new DatabaseEntry();
101     DatabaseEntry foundData = new DatabaseEntry();
102
103     assertEquals(OperationStatus.SUCCESS,
104                      cursor.getFirst(foundKey, foundData,
105                                      LockMode.DEFAULT));
106
107     assertEquals(0, foundKey.getOffset());
108     assertEquals(0, foundData.getOffset());
109     assertEquals(10, foundKey.getSize());
110     assertEquals(10, foundData.getSize());
111     for (int i = 0; i < 10; i++) {
112         assertEquals(i + 10, foundKey.getData()[i]);
113         assertEquals(i + 10, foundData.getData()[i]);
114     }
115
116     cursor.close();
117         closeDb();
118     }
119
120     public void testPartial()
121     throws DatabaseException {
122
123         openDb(false);
124
125     DatabaseEntry originalKey = new DatabaseEntry(new byte[20]);
126     DatabaseEntry originalData = new DatabaseEntry(new byte[20]);
127     for (int i = 0; i < 20; i++) {
128         originalKey.getData()[i] = (byte) i;
129         originalData.getData()[i] = (byte) i;
130     }
131
132     originalData.setPartial(true);
133     originalData.setPartialLength(10);
134     originalData.setPartialOffset(10);
135
136     db.put(null, originalKey, originalData);
137
138     Cursor cursor = db.openCursor(null, CursorConfig.DEFAULT);
139
140     DatabaseEntry foundKey = new DatabaseEntry();
141     DatabaseEntry foundData = new DatabaseEntry();
142
143     assertEquals(OperationStatus.SUCCESS,
144                      cursor.getFirst(foundKey, foundData,
145                                      LockMode.DEFAULT));
146
147     assertEquals(0, foundKey.getOffset());
148     assertEquals(20, foundKey.getSize());
149     for (int i = 0; i < 20; i++) {
150         assertEquals(i, foundKey.getData()[i]);
151     }
152
153     assertEquals(0, foundData.getOffset());
154     assertEquals(30, foundData.getSize());
155     for (int i = 0; i < 10; i++) {
156         assertEquals(0, foundData.getData()[i]);
157     }
158     for (int i = 0; i < 20; i++) {
159         assertEquals(i, foundData.getData()[i + 10]);
160     }
161
162         foundKey.setPartial(5, 10, true);
163         foundData.setPartial(5, 20, true);
164
165     assertEquals(OperationStatus.SUCCESS,
166                      cursor.getFirst(foundKey, foundData,
167                                      LockMode.DEFAULT));
168     assertEquals(0, foundKey.getOffset());
169     assertEquals(10, foundKey.getSize());
170     for (int i = 0; i < 10; i++) {
171         assertEquals(i + 5, foundKey.getData()[i]);
172     }
173
174     assertEquals(0, foundData.getOffset());
175     assertEquals(20, foundData.getSize());
176     for (int i = 0; i < 5; i++) {
177         assertEquals(0, foundData.getData()[i]);
178     }
179     for (int i = 0; i < 15; i++) {
180         assertEquals(i, foundData.getData()[i + 5]);
181     }
182
183         /* Check that partial keys on put() is not allowed. */
184
185     originalKey.setPartial(true);
186     originalKey.setPartialLength(10);
187     originalKey.setPartialOffset(10);
188
189         try {
190             db.put(null, originalKey, originalData);
191             fail();
192         } catch (IllegalArgumentException JavaDoc expected) {}
193         try {
194             db.putNoOverwrite(null, originalKey, originalData);
195             fail();
196         } catch (IllegalArgumentException JavaDoc expected) {}
197         try {
198             db.putNoDupData(null, originalKey, originalData);
199             fail();
200         } catch (IllegalArgumentException JavaDoc expected) {}
201
202         try {
203             cursor.put(originalKey, originalData);
204             fail();
205         } catch (IllegalArgumentException JavaDoc expected) {}
206         try {
207             cursor.putNoOverwrite(originalKey, originalData);
208             fail();
209         } catch (IllegalArgumentException JavaDoc expected) {}
210         try {
211             cursor.putNoDupData(originalKey, originalData);
212             fail();
213         } catch (IllegalArgumentException JavaDoc expected) {}
214
215     cursor.close();
216         closeDb();
217     }
218
219     public void testPartialCursorPuts()
220     throws DatabaseException {
221
222         openDb(false);
223
224     DatabaseEntry originalKey = new DatabaseEntry(new byte[20]);
225     DatabaseEntry originalData = new DatabaseEntry(new byte[20]);
226     for (int i = 0; i < 20; i++) {
227         originalKey.getData()[i] = (byte) i;
228         originalData.getData()[i] = (byte) i;
229     }
230
231     /* Put 20 bytes of key and data. */
232     db.put(null, originalKey, originalData);
233
234     Cursor cursor = db.openCursor(null, CursorConfig.DEFAULT);
235
236     DatabaseEntry foundKey = new DatabaseEntry();
237     DatabaseEntry foundData = new DatabaseEntry();
238
239     assertEquals(OperationStatus.SUCCESS,
240                      cursor.getFirst(foundKey, foundData,
241                                      LockMode.DEFAULT));
242
243     assertEquals(0, foundKey.getOffset());
244     assertEquals(20, foundKey.getSize());
245     for (int i = 0; i < 20; i++) {
246         assertEquals(i, foundKey.getData()[i]);
247     }
248
249     assertEquals(0, foundData.getOffset());
250     assertEquals(20, foundData.getSize());
251
252     for (int i = 0; i < 20; i++) {
253         assertEquals(i, foundData.getData()[i]);
254     }
255
256     for (int i = 0; i < 10; i++) {
257         foundData.getData()[i] = (byte) (i + 50);
258     }
259
260     foundData.setPartial(true);
261     foundData.setPartialLength(10);
262     foundData.setPartialOffset(10);
263
264     cursor.putCurrent(foundData);
265
266     foundData = new DatabaseEntry();
267
268     assertEquals(OperationStatus.SUCCESS,
269                      cursor.getFirst(foundKey, foundData,
270                                      LockMode.DEFAULT));
271     assertEquals(0, foundKey.getOffset());
272     assertEquals(20, foundKey.getSize());
273     assertEquals(0, foundData.getOffset());
274     assertEquals(30, foundData.getSize());
275     for (int i = 0; i < 10; i++) {
276         assertEquals(foundData.getData()[i], i);
277         assertEquals(foundData.getData()[i + 10], (i + 50));
278         assertEquals(foundData.getData()[i + 20], (i + 10));
279     }
280
281     cursor.close();
282         closeDb();
283     }
284
285     private void openDb(boolean dups)
286         throws DatabaseException {
287
288         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
289     DbInternal.disableParameterValidation(envConfig);
290         envConfig.setTransactional(true);
291         envConfig.setAllowCreate(true);
292         envConfig.setConfigParam(EnvironmentParams.LOG_FILE_MAX.getName(),
293                                  "1024");
294         envConfig.setConfigParam(EnvironmentParams.ENV_CHECK_LEAKS.getName(),
295                                  "true");
296     envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(),
297                                  "6");
298         env = new Environment(envHome, envConfig);
299
300         DatabaseConfig dbConfig = new DatabaseConfig();
301         dbConfig.setAllowCreate(true);
302         dbConfig.setSortedDuplicates(dups);
303         db = env.openDatabase(null, "testDB", dbConfig);
304     }
305
306     private void closeDb()
307         throws DatabaseException {
308
309         if (db != null) {
310             db.close();
311             db = null;
312         }
313         if (env != null) {
314             env.close();
315             env = null;
316         }
317     }
318 }
319
Popular Tags