KickJava   Java API By Example, From Geeks To Geeks.

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


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

8
9 package com.sleepycat.je.logversion;
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.DatabaseException;
17 import com.sleepycat.je.EnvironmentConfig;
18 import com.sleepycat.je.Environment;
19 import com.sleepycat.je.log.FileManager;
20 import com.sleepycat.je.log.LogException;
21 import com.sleepycat.je.util.TestUtils;
22
23 /**
24  * Tests log file header versioning. This test is used in conjunction with
25  * MakeLogHeaderVersionData, a main program that was used once to generate two
26  * log files with maximum and minimum valued header version numbers.
27  *
28  * @see MakeLogHeaderVersionData
29  */

30 public class LogHeaderVersionTest extends TestCase {
31
32     private File JavaDoc envHome;
33
34     public LogHeaderVersionTest() {
35         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
36     }
37
38     public void setUp()
39         throws IOException JavaDoc {
40
41         TestUtils.removeLogFiles("Setup", envHome, false);
42         TestUtils.removeFiles("Setup", envHome, FileManager.DEL_SUFFIX);
43     }
44     
45     public void tearDown()
46         throws Exception JavaDoc {
47                 
48         try {
49             //*
50
TestUtils.removeLogFiles("tearDown", envHome, true);
51             TestUtils.removeFiles("tearDown", envHome, FileManager.DEL_SUFFIX);
52             //*/
53
} catch (Throwable JavaDoc e) {
54             System.out.println("tearDown: " + e);
55         }
56
57         envHome = null;
58     }
59
60     /**
61      * Tests that an exception is thrown when a log header is read with a newer
62      * version than the current version. The maxversion.jdb log file is loaded
63      * as a resource by this test and written as a regular log file. When the
64      * environment is opened, we expect a LogException.
65      */

66     public void testGreaterVersionNotAllowed()
67         throws DatabaseException, IOException JavaDoc {
68
69         TestUtils.loadLog(getClass(), Utils.MAX_VERSION_NAME, envHome);
70
71         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
72         envConfig.setAllowCreate(false);
73         envConfig.setTransactional(true);
74
75         try {
76             Environment env = new Environment(envHome, envConfig);
77             try {
78                 env.close();
79             } catch (Exception JavaDoc ignore) {}
80         } catch (DatabaseException e) {
81             if (e.getCause() instanceof LogException) {
82                 /* Got LogException as expected. */
83                 return;
84             }
85         }
86         fail("Expected LogException");
87     }
88
89     /**
90      * Tests that when a file is opened with a lesser version than the current
91      * version, a new log file is started for writing new log entries. This is
92      * important so that the new header version is written even if no new log
93      * file is needed. If the new version were not written, an older version
94      * of JE would not recognize that there had been a version change.
95      */

96     public void testLesserVersionNotUpdated()
97         throws DatabaseException, IOException JavaDoc {
98
99         TestUtils.loadLog(getClass(), Utils.MIN_VERSION_NAME, envHome);
100         File JavaDoc logFile = new File JavaDoc(envHome, TestUtils.LOG_FILE_NAME);
101         long origFileSize = logFile.length();
102
103         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
104         envConfig.setAllowCreate(false);
105         envConfig.setTransactional(true);
106
107         Environment env = new Environment(envHome, envConfig);
108         env.sync();
109         env.close();
110
111         assertEquals(origFileSize, logFile.length());
112     }
113 }
114
Popular Tags