Skip to content

Commit 6141201

Browse files
committed
add usePreviousCommitWithCorruptFile() test
1 parent afa7521 commit 6141201

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

tests/objectbox-java-test/src/test/java/io/objectbox/BoxStoreBuilderTest.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,10 @@ public void validateOnOpen() {
155155
@Test(expected = FileCorruptException.class)
156156
public void validateOnOpenCorruptFile() throws IOException {
157157
File dir = prepareTempDir("object-store-test-corrupted");
158-
assertTrue(dir.mkdir());
159-
File badDataFile = new File(dir, "data.mdb");
160-
try (InputStream badIn = getClass().getResourceAsStream("corrupt-pageno-in-branch-data.mdb")) {
161-
try (FileOutputStream badOut = new FileOutputStream(badDataFile)) {
162-
IoUtils.copyAllBytes(badIn, badOut);
163-
}
164-
}
158+
File badDataFile = prepareBadDataFile(dir);
159+
165160
builder = BoxStoreBuilder.createDebugWithoutModel().directory(dir);
166-
builder.validateOnOpen(ValidateOnOpenMode.AllBranches);
161+
builder.validateOnOpen(ValidateOnOpenMode.Full).usePreviousCommit();
167162
try {
168163
store = builder.build();
169164
} finally {
@@ -173,4 +168,29 @@ public void validateOnOpenCorruptFile() throws IOException {
173168
assertTrue(delOk); // Try to delete all before asserting
174169
}
175170
}
171+
172+
@Test
173+
public void usePreviousCommitWithCorruptFile() throws IOException {
174+
File dir = prepareTempDir("object-store-test-corrupted");
175+
prepareBadDataFile(dir);
176+
builder = BoxStoreBuilder.createDebugWithoutModel().directory(dir);
177+
builder.validateOnOpen(ValidateOnOpenMode.Full).usePreviousCommit();
178+
store = builder.build();
179+
String diagnoseString = store.diagnose();
180+
assertTrue(diagnoseString.contains("entries=2"));
181+
store.validate(0, true);
182+
store.close();
183+
assertTrue(store.deleteAllFiles());
184+
}
185+
186+
private File prepareBadDataFile(File dir) throws IOException {
187+
assertTrue(dir.mkdir());
188+
File badDataFile = new File(dir, "data.mdb");
189+
try (InputStream badIn = getClass().getResourceAsStream("corrupt-pageno-in-branch-data.mdb")) {
190+
try (FileOutputStream badOut = new FileOutputStream(badDataFile)) {
191+
IoUtils.copyAllBytes(badIn, badOut);
192+
}
193+
}
194+
return badDataFile;
195+
}
176196
}

0 commit comments

Comments
 (0)