KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > RunRecoveryFailureTest


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

8
9 package com.sleepycat.je;
10
11 import java.io.File JavaDoc;
12 import java.io.RandomAccessFile JavaDoc;
13 import java.nio.ByteBuffer JavaDoc;
14 import java.nio.channels.FileChannel JavaDoc;
15
16 import junit.framework.TestCase;
17
18 import com.sleepycat.je.DbInternal;
19 import com.sleepycat.je.config.EnvironmentParams;
20 import com.sleepycat.je.log.FileManager;
21 import com.sleepycat.je.util.TestUtils;
22
23 public class RunRecoveryFailureTest extends TestCase {
24
25     private Environment env;
26     private File JavaDoc envHome;
27
28     public RunRecoveryFailureTest() {
29         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
30     }
31
32     public void setUp()
33         throws Exception JavaDoc {
34
35         TestUtils.removeLogFiles("Setup", envHome, false);
36         openEnv();
37
38     }
39     
40     public void tearDown()
41         throws Exception JavaDoc {
42
43         /*
44      * Close down environments in case the unit test failed so that the log
45      * files can be removed.
46          */

47         try {
48             if (env != null) {
49                 env.close();
50                 env = null;
51             }
52         } catch (RunRecoveryException e) {
53             /* ok, the test hosed it. */
54             return;
55         } catch (DatabaseException e) {
56             /* ok, the test closed it */
57     }
58
59         TestUtils.removeLogFiles("TearDown", envHome, false);
60     }
61
62     private void openEnv()
63         throws DatabaseException {
64
65         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
66         envConfig.setTransactional(true);
67
68         /*
69          * Run with tiny log buffers, so we can go to disk more (and see the
70          * checksum errors)
71          */

72     DbInternal.disableParameterValidation(envConfig);
73         envConfig.setConfigParam
74         (EnvironmentParams.NUM_LOG_BUFFERS.getName(), "2");
75         envConfig.setConfigParam
76         (EnvironmentParams.LOG_MEM_SIZE.getName(),
77          EnvironmentParams.LOG_MEM_SIZE_MIN_STRING);
78         envConfig.setConfigParam
79         (EnvironmentParams.LOG_FILE_MAX.getName(), "1024");
80     envConfig.setConfigParam
81         (EnvironmentParams.JE_LOGGING_LEVEL.getName(), "CONFIG");
82         envConfig.setAllowCreate(true);
83         env = new Environment(envHome, envConfig);
84     }
85
86     /*
87      * Corrupt an environment while open, make sure we get a
88      * RunRecoveryException.
89      */

90     public void testInvalidateEnvMidStream()
91         throws Throwable JavaDoc {
92
93         try {
94         
95             /* Make a new db in this env and flush the file. */
96             Transaction txn =
97         env.beginTransaction(null, TransactionConfig.DEFAULT);
98             DatabaseConfig dbConfig = new DatabaseConfig();
99             dbConfig.setTransactional(true);
100             dbConfig.setAllowCreate(true);
101             env.openDatabase(txn, "foo", dbConfig);
102
103             env.getEnvironmentImpl().getLogManager().flush();
104             env.getEnvironmentImpl().getFileManager().clear();
105
106             /*
107          * Corrupt the file, then abort the txn in order to force it to
108              * re-read. Should get a checksum error, which should invalidate
109              * the environment.
110              */

111             File JavaDoc file = new File JavaDoc(envHome, "00000001" + FileManager.JE_SUFFIX);
112             RandomAccessFile JavaDoc starterFile = new RandomAccessFile JavaDoc(file, "rw");
113             FileChannel JavaDoc channel = starterFile.getChannel();
114             long fileSize = channel.size();
115             channel.position(FileManager.firstLogEntryOffset());
116             ByteBuffer JavaDoc junkBuffer = ByteBuffer.allocate((int) fileSize);
117             int written = channel.write(junkBuffer,
118                                         FileManager.firstLogEntryOffset());
119             assertTrue(written > 0);
120             starterFile.close();
121
122             try {
123                 txn.abort();
124                 fail("Should see a run recovery exception");
125             } catch (RunRecoveryException e) {
126             }
127
128             try {
129                 env.getDatabaseNames();
130                 fail("Should see a run recovery exception again");
131             } catch (RunRecoveryException e) {
132             }
133         } catch (Throwable JavaDoc t) {
134             t.printStackTrace();
135             throw t;
136         }
137     }
138 }
139
Popular Tags