Repository navigation
Expand file tree
/
Copy pathBrokenDBTest.java
More file actions
150 lines (128 loc) · 4.67 KB
/
Copy pathBrokenDBTest.java
File metadata and controls
150 lines (128 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package org.mapdb;
import org.junit.*;
import org.mapdb.Volume.MappedFileVol;
import java.io.*;
import java.util.Arrays;
public class BrokenDBTest {
File index;
File log;
@Before
public void before() throws IOException {
index = TT.tempDbFile();
log = new File(index.getPath() + "wal.0");
}
/*
* Verify that DB files are properly closed when opening the database fails, allowing an
* application to recover by purging the database and starting over.
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void canDeleteDBOnBrokenIndex() throws FileNotFoundException, IOException {
for (final File f : Arrays.asList(index, log)) {
final FileOutputStream fos = new FileOutputStream(f);
fos.write("Some Junk".getBytes());
fos.close();
}
try {
DBMaker.fileDB(index).make();
Assert.fail("Expected exception not thrown");
} catch (final DBException.DataCorruption e) {
// will fail!
Assert.assertTrue("Wrong message", e.getMessage().contains("wrong header in file"));
}
index.delete();
log.delete();
// assert that we can delete the db files
Assert.assertFalse("Can't delete index", index.exists());
Assert.assertFalse("Can't delete log", log.exists());
}
/*
* Verify that DB files are properly closed when opening the database fails, allowing an
* application to recover by purging the database and starting over.
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void canDeleteDBOnBrokenLog() throws IOException {
// init empty, but valid DB
DBMaker.fileDB(index).make().close();
// corrupt file
MappedFileVol physVol = new Volume.MappedFileVol(index, false, false, CC.VOLUME_PAGE_SHIFT,false, 0L);
physVol.ensureAvailable(32);
//TODO corrupt file somehow
// physVol.putInt(0, StoreDirect.HEADER);
// physVol.putUnsignedShort(4, StoreDirect.STORE_VERSION);
// physVol.putLong(8, StoreWAL.LOG_SEAL);
physVol.putLong(16, 123456789L);
physVol.sync();
physVol.close();
try {
DBMaker.fileDB(index).make();
Assert.fail("Expected exception not thrown");
} catch (final DBException.HeadChecksumBroken e) {
// expected
}
index.delete();
log.delete();
// assert that we can delete the db files
Assert.assertFalse("Can't delete index", index.exists());
Assert.assertFalse("Can't delete log", log.exists());
}
@After
public void after() throws IOException {
if (index != null)
index.deleteOnExit();
if (log != null)
log.deleteOnExit();
}
public static class SomeDataObject implements Serializable {
private static final long serialVersionUID = 1L;
public int someField = 42;
}
/*
* Verify that DB files are properly closed when opening the database fails, allowing an
* application to recover by purging the database and starting over.
*
* @throws FileNotFoundException
* @throws IOException
*
*
*/
@Test @Ignore //TODO reenable this
public void canDeleteDBOnBrokenContent() throws IOException {
// init empty, but valid DB
DB db = DBMaker.fileDB(index).make();
db.hashMap("foo").put("foo", new SomeDataObject());
db.commit();
db.close();
// Fudge the content so that the data refers to an undefined field in SomeDataObject.
RandomAccessFile dataFile = new RandomAccessFile(index, "rw");
byte grep[] = "someField".getBytes();
int p = 0, read;
while ((read = dataFile.read()) >= 0)
if (((byte) read) == grep[p]) {
if (++p == grep.length) {
dataFile.seek(dataFile.getFilePointer() - grep.length);
dataFile.write("xxxxField".getBytes());
break;
}
} else
p = 0;
dataFile.close();
try {
DBMaker.fileDB(index).make();
Assert.fail("Expected exception not thrown");
} catch (final RuntimeException e) {
// will fail!
Assert.assertTrue("Wrong message", e.getMessage().contains("Could not set field value"));
}
index.delete();
log.delete();
// assert that we can delete the db files
Assert.assertFalse("Can't delete index", index.exists());
Assert.assertFalse("Can't delete log", log.exists());
}
}