KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > logversion > LogEntryVersionTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: LogEntryVersionTest.java,v 1.12 2006/11/03 02:38:54 mark Exp $
7  */

8
9 package com.sleepycat.je.logversion;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStreamReader JavaDoc;
14 import java.io.LineNumberReader JavaDoc;
15
16 import junit.framework.TestCase;
17
18 import com.sleepycat.je.Cursor;
19 import com.sleepycat.je.Database;
20 import com.sleepycat.je.DatabaseConfig;
21 import com.sleepycat.je.DatabaseEntry;
22 import com.sleepycat.je.DatabaseException;
23 import com.sleepycat.je.DbInternal;
24 import com.sleepycat.je.Environment;
25 import com.sleepycat.je.EnvironmentConfig;
26 import com.sleepycat.je.OperationStatus;
27 import com.sleepycat.je.VerifyConfig;
28 import com.sleepycat.je.log.FileManager;
29 import com.sleepycat.je.log.TestUtilLogReader;
30 import com.sleepycat.je.util.TestUtils;
31
32 /**
33  * Tests that prior versions of log entries can be read. This test is used in
34  * conjunction with MakeLogEntryVersionData, a main program that was used once
35  * to generate log files named je-x.y.z.jdb, where x.y.z is the version of JE
36  * used to create the log. When a new test log file is created with
37  * MakeLogEntryVersionData, add a new test_x_y_z() method to this class.
38  *
39  * @see MakeLogEntryVersionData
40  */

41 public class LogEntryVersionTest extends TestCase {
42
43     private File JavaDoc envHome;
44     private Environment env;
45     private Database db1;
46     private Database db2;
47
48     public LogEntryVersionTest() {
49         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
50     }
51
52     public void setUp()
53         throws IOException JavaDoc {
54
55         TestUtils.removeLogFiles("Setup", envHome, false);
56         TestUtils.removeFiles("Setup", envHome, FileManager.DEL_SUFFIX);
57     }
58     
59     public void tearDown()
60         throws Exception JavaDoc {
61
62         try {
63             if (env != null) {
64                 env.close();
65             }
66         } catch (Throwable JavaDoc e) {
67             System.out.println("tearDown: " + e);
68         }
69                 
70         try {
71             //*
72
TestUtils.removeLogFiles("tearDown", envHome, true);
73             TestUtils.removeFiles("tearDown", envHome, FileManager.DEL_SUFFIX);
74             //*/
75
} catch (Throwable JavaDoc e) {
76             System.out.println("tearDown: " + e);
77         }
78
79         envHome = null;
80         env = null;
81         db1 = null;
82         db2 = null;
83     }
84
85     private void openEnv(String JavaDoc jeVersion, boolean readOnly)
86         throws DatabaseException, IOException JavaDoc {
87
88         /* Copy log file resource to log file zero. */
89         String JavaDoc resName = "je-" + jeVersion + ".jdb";
90         TestUtils.loadLog(getClass(), resName, envHome);
91
92         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
93         envConfig.setAllowCreate(false);
94         envConfig.setReadOnly(readOnly);
95         envConfig.setTransactional(true);
96         env = new Environment(envHome, envConfig);
97         
98         DatabaseConfig dbConfig = new DatabaseConfig();
99         dbConfig.setAllowCreate(false);
100         dbConfig.setReadOnly(readOnly);
101         dbConfig.setSortedDuplicates(true);
102         db1 = env.openDatabase(null, Utils.DB1_NAME, dbConfig);
103         db2 = env.openDatabase(null, Utils.DB2_NAME, dbConfig);
104     }
105
106     private void closeEnv()
107         throws DatabaseException {
108
109         db1.close();
110         db1 = null;
111         db2.close();
112         db2 = null;
113         env.close();
114         env = null;
115     }
116
117     public void test_1_5_4()
118         throws DatabaseException, IOException JavaDoc {
119
120         doTest("1.5.4");
121     }
122
123     public void test_1_7_0()
124         throws DatabaseException, IOException JavaDoc {
125
126         doTest("1.7.0");
127     }
128
129     /**
130      * JE 2.0: FileHeader version 3.
131      */

132     public void test_2_0_0()
133         throws DatabaseException, IOException JavaDoc {
134
135         doTest("2.0.0");
136     }
137
138     /**
139      * JE 3.0.12: FileHeader version 4.
140      */

141     public void test_3_0_12()
142         throws DatabaseException, IOException JavaDoc {
143
144         /*
145          * The test was not run until JE 3.1.25, but no format changes were
146          * made between 3.0.12 and 3.1.25.
147          */

148         doTest("3.1.25");
149     }
150
151     private void doTest(String JavaDoc jeVersion)
152         throws DatabaseException, IOException JavaDoc {
153
154         openEnv(jeVersion, true /*readOnly*/);
155
156         VerifyConfig verifyConfig = new VerifyConfig();
157         verifyConfig.setAggressive(true);
158         assertTrue(env.verify(verifyConfig, System.err));
159
160         DatabaseEntry key = new DatabaseEntry();
161         DatabaseEntry data = new DatabaseEntry();
162         OperationStatus status;
163
164         /* Database 1 is empty because the txn was aborted. */
165         Cursor cursor = db1.openCursor(null, null);
166         try {
167             status = cursor.getFirst(key, data, null);
168             assertEquals(OperationStatus.NOTFOUND, status);
169         } finally {
170             cursor.close();
171         }
172
173         /* Database 2 has one record: {3, 0} */
174         cursor = db2.openCursor(null, null);
175         try {
176             status = cursor.getFirst(key, data, null);
177             assertEquals(OperationStatus.SUCCESS, status);
178             assertEquals(3, Utils.value(key));
179             assertEquals(0, Utils.value(data));
180             status = cursor.getNext(key, data, null);
181             assertEquals(OperationStatus.NOTFOUND, status);
182         } finally {
183             cursor.close();
184         }
185
186         /* Verify log entry types. */
187         String JavaDoc resName = "je-" + jeVersion + ".txt";
188         LineNumberReader JavaDoc textReader = new LineNumberReader JavaDoc
189             (new InputStreamReader JavaDoc(getClass().getResourceAsStream(resName)));
190         TestUtilLogReader logReader = new TestUtilLogReader
191             (DbInternal.envGetEnvironmentImpl(env));
192         while (logReader.readNextEntry()) {
193             String JavaDoc foundType = logReader.getEntryType().toString();
194             String JavaDoc expectedType = textReader.readLine();
195             assertNotNull
196                 ("No more expected types after line " +
197                  textReader.getLineNumber() + " but found: " + foundType,
198                  expectedType);
199             assertEquals
200                 ("At line " + textReader.getLineNumber(),
201                  expectedType.substring(0, expectedType.indexOf('/')),
202                  foundType.substring(0, foundType.indexOf('/')));
203         }
204         String JavaDoc remainingLine = textReader.readLine();
205         assertNull
206             ("Another expected type at line " +
207              textReader.getLineNumber() + " but no more found",
208              remainingLine);
209
210         assertTrue(env.verify(verifyConfig, System.err));
211         closeEnv();
212
213         /*
214          * Do enough inserts to cause a split and perform some other write
215          * operations for good measure.
216          */

217         openEnv(jeVersion, false /*readOnly*/);
218         for (int i = -127; i < 127; i += 1) {
219             status = db2.put(null, Utils.entry(i), Utils.entry(0));
220             assertEquals(OperationStatus.SUCCESS, status);
221         }
222         /* Do updates. */
223         for (int i = -127; i < 127; i += 1) {
224             status = db2.put(null, Utils.entry(i), Utils.entry(1));
225             assertEquals(OperationStatus.SUCCESS, status);
226         }
227         /* Do deletes. */
228         for (int i = -127; i < 127; i += 1) {
229             status = db2.delete(null, Utils.entry(i));
230             assertEquals(OperationStatus.SUCCESS, status);
231         }
232         /* Same for duplicates. */
233         for (int i = -127; i < 127; i += 1) {
234             status = db2.put(null, Utils.entry(0), Utils.entry(i));
235             assertEquals(OperationStatus.SUCCESS, status);
236         }
237         for (int i = -127; i < 127; i += 1) {
238             status = db2.put(null, Utils.entry(0), Utils.entry(i));
239             assertEquals(OperationStatus.SUCCESS, status);
240         }
241         cursor = db2.openCursor(null, null);
242         try {
243             status = cursor.getFirst(key, data, null);
244             while (status == OperationStatus.SUCCESS) {
245                 status = cursor.delete();
246                 assertEquals(OperationStatus.SUCCESS, status);
247                 status = cursor.getNext(key, data, null);
248             }
249         } finally {
250             cursor.close();
251         }
252
253         assertTrue(env.verify(verifyConfig, System.err));
254         closeEnv();
255     }
256 }
257
Popular Tags