KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > tree > ValidateSubtreeDeleteTest


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

8
9 package com.sleepycat.je.tree;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import com.sleepycat.je.Database;
17 import com.sleepycat.je.DatabaseConfig;
18 import com.sleepycat.je.DatabaseEntry;
19 import com.sleepycat.je.DatabaseException;
20 import com.sleepycat.je.DbInternal;
21 import com.sleepycat.je.Environment;
22 import com.sleepycat.je.EnvironmentConfig;
23 import com.sleepycat.je.Transaction;
24 import com.sleepycat.je.config.EnvironmentParams;
25 import com.sleepycat.je.log.FileManager;
26 import com.sleepycat.je.util.TestUtils;
27
28 public class ValidateSubtreeDeleteTest extends TestCase {
29
30     private File JavaDoc envHome;
31     private Environment env;
32     private Database testDb;
33
34     public ValidateSubtreeDeleteTest() {
35         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
36     }
37
38     public void setUp()
39     throws IOException JavaDoc, DatabaseException {
40
41         TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
42
43
44         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
45         envConfig.setTransactional(true);
46         envConfig.setConfigParam(EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(),
47                                  "false");
48         envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
49         envConfig.setAllowCreate(true);
50         env = new Environment(envHome, envConfig);
51
52         DatabaseConfig dbConfig = new DatabaseConfig();
53         dbConfig.setTransactional(true);
54         dbConfig.setAllowCreate(true);
55         dbConfig.setSortedDuplicates(true);
56         testDb = env.openDatabase(null, "Test", dbConfig);
57     }
58     
59     public void tearDown() throws IOException JavaDoc, DatabaseException {
60         testDb.close();
61         if (env != null) {
62             try {
63                 env.close();
64             } catch (DatabaseException E) {
65             }
66         }
67         TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
68     }
69
70     public void testBasic()
71         throws Exception JavaDoc {
72         try {
73             /* Make a 3 level tree full of data */
74             DatabaseEntry key = new DatabaseEntry();
75             DatabaseEntry data = new DatabaseEntry();
76             byte [] testData = new byte[1];
77             testData[0] = 1;
78             data.setData(testData);
79
80             Transaction txn = env.beginTransaction(null, null);
81             for (int i = 0; i < 15; i ++) {
82                 key.setData(TestUtils.getTestArray(i));
83                 testDb.put(txn, key, data);
84             }
85
86             /* Should not be able to delete any of it */
87             assertFalse(DbInternal.dbGetDatabaseImpl(testDb).getTree().validateDelete(0));
88             assertFalse(DbInternal.dbGetDatabaseImpl(testDb).getTree().validateDelete(1));
89
90             /*
91              * Should be able to delete both, the txn is aborted and the data
92              * isn't there.
93              */

94             txn.abort();
95             assertTrue(DbInternal.dbGetDatabaseImpl(testDb).getTree().validateDelete(0));
96             assertTrue(DbInternal.dbGetDatabaseImpl(testDb).getTree().validateDelete(1));
97
98
99             /*
100              * Try explicit deletes.
101              */

102             txn = env.beginTransaction(null, null);
103             for (int i = 0; i < 15; i ++) {
104                 key.setData(TestUtils.getTestArray(i));
105                 testDb.put(txn, key, data);
106             }
107             for (int i = 0; i < 15; i ++) {
108                 key.setData(TestUtils.getTestArray(i));
109                 testDb.delete(txn, key);
110             }
111             assertFalse(DbInternal.dbGetDatabaseImpl(testDb).getTree().validateDelete(0));
112             assertFalse(DbInternal.dbGetDatabaseImpl(testDb).getTree().validateDelete(1));
113
114             // XXX, now commit the delete and compress and test that the
115
// subtree is deletable. Not finished yet! Also must test deletes.
116
txn.abort();
117         } catch (Exception JavaDoc e) {
118             e.printStackTrace();
119             throw e;
120         }
121     }
122
123     public void testDuplicates()
124         throws Exception JavaDoc {
125         try {
126             /* Make a 3 level tree full of data */
127             DatabaseEntry key = new DatabaseEntry();
128             DatabaseEntry data = new DatabaseEntry();
129             byte [] testData = new byte[1];
130             testData[0] = 1;
131             key.setData(testData);
132
133             Transaction txn = env.beginTransaction(null, null);
134             for (int i = 0; i < 4; i ++) {
135                 data.setData(TestUtils.getTestArray(i));
136                 testDb.put(txn, key, data);
137             }
138
139             /* Should not be able to delete any of it */
140             Tree tree = DbInternal.dbGetDatabaseImpl(testDb).getTree();
141             assertFalse(tree.validateDelete(0));
142
143             /*
144              * Should be able to delete, the txn is aborted and the data
145              * isn't there.
146              */

147             txn.abort();
148             assertTrue(tree.validateDelete(0));
149
150             /*
151              * Try explicit deletes.
152              */

153         } catch (Exception JavaDoc e) {
154             e.printStackTrace();
155             throw e;
156         }
157     }
158
159 }
160
Popular Tags