• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/db-4.7.25.NC/test/scr016/src/com/sleepycat/db/test/
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 */
7
8package com.sleepycat.db.test;
9
10import org.junit.After;
11import org.junit.AfterClass;
12import org.junit.Before;
13import org.junit.BeforeClass;
14import org.junit.Ignore;
15import org.junit.Test;
16import static org.junit.Assert.assertEquals;
17import static org.junit.Assert.fail;
18
19import com.sleepycat.db.*;
20
21import java.io.File;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24
25import com.sleepycat.db.test.TestUtils;
26public class PartialGetTest {
27    public static final String PARTIALGETTEST_DBNAME = "partialgettest.db";
28    public static final byte[] data_64chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890<>".getBytes();
29    @BeforeClass public static void ClassInit() {
30        TestUtils.loadConfig(null);
31        TestUtils.check_file_removed(TestUtils.getDBFileName(PARTIALGETTEST_DBNAME), true, true);
32        TestUtils.removeall(true, true, TestUtils.BASETEST_DBDIR, TestUtils.getDBFileName(PARTIALGETTEST_DBNAME));
33    }
34
35    @AfterClass public static void ClassShutdown() {
36        TestUtils.check_file_removed(TestUtils.getDBFileName(PARTIALGETTEST_DBNAME), true, true);
37        TestUtils.removeall(true, true, TestUtils.BASETEST_DBDIR, TestUtils.getDBFileName(PARTIALGETTEST_DBNAME));
38    }
39
40    @Before public void PerTestInit()
41        throws Exception {
42    }
43
44    @After public void PerTestShutdown()
45        throws Exception {
46        TestUtils.check_file_removed(TestUtils.getDBFileName(PARTIALGETTEST_DBNAME), true, true);
47    }
48    /*
49     * Test case implementations.
50     * To disable a test mark it with @Ignore
51     * To set a timeout(ms) notate like: @Test(timeout=1000)
52     * To indicate an expected exception notate like: (expected=Exception)
53     */
54
55    /*
56     * Simple partial gets on a record which is on a single page.
57     */
58    @Test public void test1()
59        throws DatabaseException, FileNotFoundException
60    {
61        DatabaseEntry key = new DatabaseEntry("key".getBytes());
62        Database db = setupDb1(key, data_64chars);
63
64        StringEntry partialData = new StringEntry();
65        partialData.setPartial(true);
66        partialData.setPartial(0, 12, true);
67
68        if (db.get(null, key, partialData, LockMode.DEFAULT) !=
69            OperationStatus.SUCCESS)
70            fail("Failed doing partial retrieval, first part of entry on single page.");
71        // Validate the data.
72        if (!MatchData(data_64chars, partialData.getString(), 12))
73            fail("Data mismatch from partial get.");
74
75        partialData.setPartial(12, 12, true);
76        if (db.get(null, key, partialData, LockMode.DEFAULT) !=
77            OperationStatus.SUCCESS)
78            fail("Failed doing partial retrieval, second part of entry on single page.");
79        // Validate the data.
80        if (!MatchData(new String(data_64chars, 12, 12), partialData.getString(), 12))
81            fail("Data mismatch from partial get.");
82
83        db.close(false);
84    }
85
86    /*
87     * Retrieve entry using different DB_DBT_alloc flags.
88     * Verify results.
89     */
90    @Test public void test2()
91        throws DatabaseException, FileNotFoundException
92    {
93        DatabaseEntry key = new DatabaseEntry("key".getBytes());
94        Database db = setupDb1(key, data_64chars);
95        StringEntry partialData = new StringEntry();
96        partialData.setPartial(true);
97        partialData.setPartial(0, 12, true);
98
99        if (db.get(null, key, partialData, LockMode.DEFAULT) !=
100            OperationStatus.SUCCESS)
101            fail("Failed doing partial retrieval.");
102        // Validate the data.
103        if (!MatchData(data_64chars, partialData.getString(), 12))
104            fail("Data mismatch from partial get.");
105
106        partialData.setReuseBuffer(true);
107        if (db.get(null, key, partialData, LockMode.DEFAULT) !=
108            OperationStatus.SUCCESS)
109        if (!MatchData(data_64chars, partialData.getString(), 12))
110            fail("Data mismatch from partial get.");
111
112        partialData.setReuseBuffer(false);
113        partialData.setUserBuffer(64, true);
114        partialData.setData(new byte[64]);
115        if (db.get(null, key, partialData, LockMode.DEFAULT) !=
116            OperationStatus.SUCCESS)
117        if (!MatchData(data_64chars, partialData.getString(), 12))
118            fail("Data mismatch from partial get.");
119
120        partialData.setPartial(12, 12, true);
121        if (db.get(null, key, partialData, LockMode.DEFAULT) !=
122            OperationStatus.SUCCESS)
123            fail("Failed doing partial retrieval.");
124        // Validate the data.
125        if (!MatchData(new String(data_64chars, 12, 12), partialData.getString(), 12))
126            fail("Data mismatch from partial get.");
127
128        db.close(false);
129    }
130
131    /* Retrieve entry that spans multiple pages. */
132
133    @Test public void test3()
134        throws DatabaseException, FileNotFoundException
135    {
136        DatabaseEntry key = new DatabaseEntry("key".getBytes());
137        StringBuffer sb = new StringBuffer(1024*100);
138        for(int i = 0; i < 1024; i++) {
139            sb.append("abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_+-=");
140            sb.append("abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_+-=");
141        }
142        Database db = setupDb1(key, sb.toString().getBytes());
143
144        StringEntry partialData = new StringEntry();
145        partialData.setPartial(true);
146        partialData.setPartial(0, 12, true);
147
148        if (db.get(null, key, partialData, LockMode.DEFAULT) !=
149            OperationStatus.SUCCESS)
150            fail("Failed doing partial retrieval.");
151        // Validate the data.
152        if (!MatchData(data_64chars, partialData.getString(), 12))
153            fail("Data mismatch from partial get.");
154
155        // retrieve a chunk larger than a page size, starting at offset 0.
156        partialData.setPartial(0, 2048, true);
157        if (db.get(null, key, partialData, LockMode.DEFAULT) !=
158            OperationStatus.SUCCESS)
159            fail("Failed doing partial retrieval.");
160        // Validate the data.
161        if (!MatchData(sb.substring(0, 2048), partialData.getString(), 2048))
162            fail("Data mismatch from partial get.");
163
164        // retrieve a chunk larger than a page size, starting at offset greater than 0.
165        partialData.setPartial(10, 2048, true);
166        if (db.get(null, key, partialData, LockMode.DEFAULT) !=
167            OperationStatus.SUCCESS)
168            fail("Failed doing partial retrieval.");
169        // Validate the data.
170        if (!MatchData(sb.substring(10, 2048+10), partialData.getString(), 12))
171            fail("Data mismatch from partial get.");
172
173        db.close(false);
174    }
175
176    /*
177     * Test partial retrieval using a cursor.
178     */
179    @Test public void test4()
180        throws DatabaseException, FileNotFoundException
181    {
182    }
183
184    /*
185     * Test partial retrieval using different DB types.
186     */
187    @Test public void test5()
188        throws DatabaseException, FileNotFoundException
189    {
190    }
191
192    /*
193     * Test partial retrieval .
194     */
195    @Test public void test6()
196        throws DatabaseException, FileNotFoundException
197    {
198    }
199
200    /*
201     * Helper methods and classes follow.
202     */
203
204    private Database setupDb1(DatabaseEntry key, byte[] dataData)
205        throws DatabaseException, FileNotFoundException
206    {
207        DatabaseConfig dbConfig = new DatabaseConfig();
208        dbConfig.setErrorStream(TestUtils.getErrorStream());
209        dbConfig.setErrorPrefix("PartialGetTest");
210        dbConfig.setType(DatabaseType.BTREE);
211        dbConfig.setPageSize(1024);
212        dbConfig.setAllowCreate(true);
213        Database db = new Database(TestUtils.getDBFileName(PARTIALGETTEST_DBNAME), null, dbConfig);
214
215        DatabaseEntry data = new DatabaseEntry(dataData);
216
217        if(db.putNoOverwrite(null, key, data) != OperationStatus.SUCCESS)
218            TestUtils.ERR("Failed to create standard entry in database.");
219
220        return db;
221    }
222
223    /* Save converting String to do data comparisons. */
224    private boolean MatchData(byte[] data1, byte[] data2, int len)
225    {
226        return MatchData(new String(data1), new String(data2), len);
227    }
228    private boolean MatchData(String data1, byte[] data2, int len)
229    {
230        return MatchData(data1, new String(data2), len);
231    }
232    private boolean MatchData(byte[] data1, String data2, int len)
233    {
234        return MatchData(new String(data1), data2, len);
235    }
236    private boolean MatchData(String data1, String data2, int len)
237    {
238        if(data1.length() < len || data2.length() < len)
239            return false;
240        TestUtils.DEBUGOUT(0, "data1: " +data1.substring(0, 12));
241        TestUtils.DEBUGOUT(0, "data2: " +data2.substring(0, 12));
242        return data1.regionMatches(0, data2, 0, len);
243    }
244
245    static /*inner*/
246    class StringEntry extends DatabaseEntry {
247        StringEntry() {
248        }
249
250        StringEntry (String value) {
251            setString(value);
252        }
253
254        void setString(String value) {
255            byte[] data = value.getBytes();
256            setData(data);
257            setSize(data.length);
258        }
259
260        String getString() {
261            return new String(getData(), getOffset(), getSize());
262        }
263    }
264}
265