1 /*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002, 2014 Oracle and/or its affiliates. All rights reserved. 5 * 6 */ 7 8 package com.sleepycat.je.logversion; 9 10 import static org.junit.Assert.assertEquals; 11 import static org.junit.Assert.fail; 12 13 import java.io.File; 14 import java.io.IOException; 15 16 import org.junit.After; 17 import org.junit.Test; 18 19 import com.sleepycat.je.DatabaseException; 20 import com.sleepycat.je.Environment; 21 import com.sleepycat.je.EnvironmentConfig; 22 import com.sleepycat.je.VersionMismatchException; 23 import com.sleepycat.je.util.TestUtils; 24 import com.sleepycat.util.test.SharedTestUtils; 25 import com.sleepycat.util.test.TestBase; 26 27 /** 28 * Tests log file header versioning. This test is used in conjunction with 29 * MakeLogHeaderVersionData, a main program that was used once to generate two 30 * log files with maximum and minimum valued header version numbers. 31 * 32 * @see MakeLogHeaderVersionData 33 */ 34 public class LogHeaderVersionTest extends TestBase { 35 36 private File envHome; 37 LogHeaderVersionTest()38 public LogHeaderVersionTest() { 39 envHome = SharedTestUtils.getTestDir(); 40 } 41 42 @After tearDown()43 public void tearDown() { 44 45 envHome = null; 46 } 47 48 /** 49 * Tests that an exception is thrown when a log header is read with a newer 50 * version than the current version. The maxversion.jdb log file is loaded 51 * as a resource by this test and written as a regular log file. When the 52 * environment is opened, we expect a VersionMismatchException. 53 */ 54 @Test testGreaterVersionNotAllowed()55 public void testGreaterVersionNotAllowed() 56 throws IOException { 57 58 TestUtils.loadLog(getClass(), Utils.MAX_VERSION_NAME, envHome); 59 60 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 61 envConfig.setAllowCreate(false); 62 envConfig.setTransactional(true); 63 64 try { 65 Environment env = new Environment(envHome, envConfig); 66 try { 67 env.close(); 68 } catch (Exception ignore) {} 69 } catch (VersionMismatchException e) { 70 /* Got VersionMismatchException as expected. */ 71 return; 72 } 73 fail("Expected VersionMismatchException"); 74 } 75 76 /** 77 * Tests that when a file is opened with a lesser version than the current 78 * version, a new log file is started for writing new log entries. This is 79 * important so that the new header version is written even if no new log 80 * file is needed. If the new version were not written, an older version 81 * of JE would not recognize that there had been a version change. 82 */ 83 @Test testLesserVersionNotUpdated()84 public void testLesserVersionNotUpdated() 85 throws DatabaseException, IOException { 86 87 TestUtils.loadLog(getClass(), Utils.MIN_VERSION_NAME, envHome); 88 File logFile = new File(envHome, TestUtils.LOG_FILE_NAME); 89 long origFileSize = logFile.length(); 90 91 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 92 envConfig.setAllowCreate(false); 93 envConfig.setTransactional(true); 94 95 Environment env = new Environment(envHome, envConfig); 96 env.sync(); 97 env.close(); 98 99 assertEquals(origFileSize, logFile.length()); 100 } 101 } 102