1 8 9 package com.sleepycat.je.logversion; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.io.InputStreamReader ; 14 import java.io.LineNumberReader ; 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 41 public class LogEntryVersionTest extends TestCase { 42 43 private File envHome; 44 private Environment env; 45 private Database db1; 46 private Database db2; 47 48 public LogEntryVersionTest() { 49 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 50 } 51 52 public void setUp() 53 throws IOException { 54 55 TestUtils.removeLogFiles("Setup", envHome, false); 56 TestUtils.removeFiles("Setup", envHome, FileManager.DEL_SUFFIX); 57 } 58 59 public void tearDown() 60 throws Exception { 61 62 try { 63 if (env != null) { 64 env.close(); 65 } 66 } catch (Throwable e) { 67 System.out.println("tearDown: " + e); 68 } 69 70 try { 71 TestUtils.removeLogFiles("tearDown", envHome, true); 73 TestUtils.removeFiles("tearDown", envHome, FileManager.DEL_SUFFIX); 74 } catch (Throwable 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 jeVersion, boolean readOnly) 86 throws DatabaseException, IOException { 87 88 89 String 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 { 119 120 doTest("1.5.4"); 121 } 122 123 public void test_1_7_0() 124 throws DatabaseException, IOException { 125 126 doTest("1.7.0"); 127 } 128 129 132 public void test_2_0_0() 133 throws DatabaseException, IOException { 134 135 doTest("2.0.0"); 136 } 137 138 141 public void test_3_0_12() 142 throws DatabaseException, IOException { 143 144 148 doTest("3.1.25"); 149 } 150 151 private void doTest(String jeVersion) 152 throws DatabaseException, IOException { 153 154 openEnv(jeVersion, true ); 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 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 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 187 String resName = "je-" + jeVersion + ".txt"; 188 LineNumberReader textReader = new LineNumberReader 189 (new InputStreamReader (getClass().getResourceAsStream(resName))); 190 TestUtilLogReader logReader = new TestUtilLogReader 191 (DbInternal.envGetEnvironmentImpl(env)); 192 while (logReader.readNextEntry()) { 193 String foundType = logReader.getEntryType().toString(); 194 String 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 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 217 openEnv(jeVersion, false ); 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 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 228 for (int i = -127; i < 127; i += 1) { 229 status = db2.delete(null, Utils.entry(i)); 230 assertEquals(OperationStatus.SUCCESS, status); 231 } 232 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 |