KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > dbi > DbCursorDupTest


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

8
9 package com.sleepycat.je.dbi;
10
11 import java.io.IOException JavaDoc;
12
13 import com.sleepycat.je.Cursor;
14 import com.sleepycat.je.Database;
15 import com.sleepycat.je.DatabaseConfig;
16 import com.sleepycat.je.DatabaseEntry;
17 import com.sleepycat.je.DatabaseException;
18 import com.sleepycat.je.LockMode;
19 import com.sleepycat.je.OperationStatus;
20 import com.sleepycat.je.util.StringDbt;
21
22 /**
23  * Various unit tests for CursorImpl.dup().
24  */

25 public class DbCursorDupTest extends DbCursorTestBase {
26
27     public DbCursorDupTest()
28         throws DatabaseException {
29
30         super();
31     }
32
33     public void testCursorDupAndCloseDb()
34     throws DatabaseException {
35
36         initEnv(false);
37         DatabaseConfig dbConfig = new DatabaseConfig();
38         dbConfig.setAllowCreate(true);
39         Database myDb = exampleEnv.openDatabase(null, "fooDb", dbConfig);
40
41     myDb.put(null, new StringDbt("blah"), new StringDbt("blort"));
42     Cursor cursor = myDb.openCursor(null, null);
43     OperationStatus status = cursor.getNext(new DatabaseEntry(),
44                                                 new DatabaseEntry(),
45                                                 LockMode.DEFAULT);
46     Cursor cursorDup = cursor.dup(true);
47     cursor.close();
48     cursorDup.close();
49     myDb.close();
50     }
51
52     public void testDupInitialized()
53         throws DatabaseException {
54
55         /* Open db. */
56         initEnv(false);
57         DatabaseConfig dbConfig = new DatabaseConfig();
58         dbConfig.setAllowCreate(true);
59         Database myDb = exampleEnv.openDatabase(null, "fooDb", dbConfig);
60
61         /* Open uninitialized cursor. */
62         Cursor c1 = myDb.openCursor(null, null);
63         try {
64             c1.getCurrent(new DatabaseEntry(), new DatabaseEntry(), null);
65             fail();
66         } catch (DatabaseException expected) {}
67
68         /* Dup uninitialized cursor with samePosition=false. */
69         Cursor c2 = c1.dup(false);
70         try {
71             c2.getCurrent(new DatabaseEntry(), new DatabaseEntry(), null);
72             fail();
73         } catch (DatabaseException expected) {}
74
75         /* Dup uninitialized cursor with samePosition=true. */
76         Cursor c3 = c1.dup(true);
77         try {
78             c3.getCurrent(new DatabaseEntry(), new DatabaseEntry(), null);
79             fail();
80         } catch (DatabaseException expected) {}
81
82         /* Ensure dup'ed cursors are usable. */
83         assertEquals(OperationStatus.SUCCESS,
84                      c1.put(new DatabaseEntry(new byte[0]),
85                             new DatabaseEntry(new byte[0])));
86         assertEquals(OperationStatus.SUCCESS,
87                      c2.getFirst(new DatabaseEntry(), new DatabaseEntry(),
88                                  null));
89         assertEquals(OperationStatus.NOTFOUND,
90                      c2.getNext(new DatabaseEntry(), new DatabaseEntry(),
91                                  null));
92         assertEquals(OperationStatus.SUCCESS,
93                      c3.getFirst(new DatabaseEntry(), new DatabaseEntry(),
94                                  null));
95         assertEquals(OperationStatus.NOTFOUND,
96                      c3.getNext(new DatabaseEntry(), new DatabaseEntry(),
97                                  null));
98
99         /* Close db. */
100         c3.close();
101         c2.close();
102         c1.close();
103     myDb.close();
104     }
105
106     /**
107      * Create some duplicate data.
108      *
109      * Pass 1, walk over the data and with each iteration, dup() the
110      * cursor at the same position. Ensure that the dup points to the
111      * same key/data pair. Advance the dup'd cursor and ensure that
112      * the data is different (key may be the same since it's a
113      * duplicate set). Then dup() the cursor without maintaining
114      * position. Ensure that getCurrent() throws a Cursor Not Init'd
115      * exception.
116      *
117      * Pass 2, iterate through the data, and dup the cursor in the
118      * same position. Advance the original cursor and ensure that the
119      * dup()'d points to the original data and the original cursor
120      * points at new data.
121      */

122     public void testCursorDupSamePosition()
123         throws IOException JavaDoc, DatabaseException {
124
125         initEnv(true);
126     createRandomDuplicateData(null, false);
127
128     DataWalker dw = new DataWalker(null) {
129         void perData(String JavaDoc foundKey, String JavaDoc foundData)
130                     throws DatabaseException {
131             DatabaseEntry keyDbt = new DatabaseEntry();
132             DatabaseEntry dataDbt = new DatabaseEntry();
133             Cursor cursor2 = cursor.dup(true);
134             cursor2.getCurrent(keyDbt, dataDbt, LockMode.DEFAULT);
135             String JavaDoc c2Key = new String JavaDoc(keyDbt.getData());
136             String JavaDoc c2Data = new String JavaDoc(dataDbt.getData());
137             assertTrue(c2Key.equals(foundKey));
138             assertTrue(c2Data.equals(foundData));
139             if (cursor2.getNext(keyDbt,
140                     dataDbt,
141                     LockMode.DEFAULT) ==
142                         OperationStatus.SUCCESS) {
143             /* Keys can be the same because we have duplicates. */
144             /*
145               assertFalse(new String(keyDbt.getData()).
146               equals(foundKey));
147             */

148             assertFalse(new String JavaDoc(dataDbt.getData()).
149                     equals(foundData));
150             }
151             cursor2.close();
152             try {
153             cursor2 = cursor.dup(false);
154             cursor2.getCurrent(keyDbt, dataDbt, LockMode.DEFAULT);
155             fail("didn't catch Cursor not initialized exception");
156             } catch (DatabaseException DBE) {
157             }
158             cursor2.close();
159         }
160         };
161     dw.setIgnoreDataMap(true);
162     dw.walkData();
163
164     dw = new DataWalker(null) {
165         void perData(String JavaDoc foundKey, String JavaDoc foundData)
166                     throws DatabaseException {
167             DatabaseEntry keyDbt = new DatabaseEntry();
168             DatabaseEntry dataDbt = new DatabaseEntry();
169             DatabaseEntry key2Dbt = new DatabaseEntry();
170             DatabaseEntry data2Dbt = new DatabaseEntry();
171             Cursor cursor2 = cursor.dup(true);
172
173             OperationStatus status =
174             cursor.getNext(keyDbt, dataDbt, LockMode.DEFAULT);
175
176             cursor2.getCurrent(key2Dbt, data2Dbt, LockMode.DEFAULT);
177             String JavaDoc c2Key = new String JavaDoc(key2Dbt.getData());
178             String JavaDoc c2Data = new String JavaDoc(data2Dbt.getData());
179             assertTrue(c2Key.equals(foundKey));
180             assertTrue(c2Data.equals(foundData));
181             if (status == OperationStatus.SUCCESS) {
182             assertFalse(new String JavaDoc(dataDbt.getData()).
183                     equals(foundData));
184             assertFalse(new String JavaDoc(dataDbt.getData()).
185                     equals(c2Data));
186             }
187             cursor2.close();
188         }
189         };
190     dw.setIgnoreDataMap(true);
191     dw.walkData();
192     }
193 }
194
Popular Tags