Skip to content

Commit d1cc08f

Browse files
committed
add test validateOnOpenCorruptFile() based on corrupt-pageno-in-branch-data.mdb
1 parent b36795b commit d1cc08f

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,27 @@ public void setUp() throws IOException {
5656
Cursor.TRACK_CREATION_STACK = true;
5757
Transaction.TRACK_CREATION_STACK = true;
5858

59-
// This works with Android without needing any context
60-
File tempFile = File.createTempFile("object-store-test", "");
61-
//noinspection ResultOfMethodCallIgnored
62-
tempFile.delete();
63-
boxStoreDir = tempFile;
64-
6559
if (!printedVersionsOnce) {
6660
System.out.println("ObjectBox Java version: " + BoxStore.getVersion());
6761
System.out.println("ObjectBox Core version: " + BoxStore.getVersionNative());
6862
System.out.println("First DB dir: " + boxStoreDir);
6963
printedVersionsOnce = true;
7064
}
7165

66+
boxStoreDir = prepareTempDir("object-store-test");
7267
store = createBoxStore();
7368
runExtensiveTests = System.getProperty("extensive-tests") != null;
7469
}
7570

71+
/** This works with Android without needing any context. */
72+
protected File prepareTempDir(String prefix) throws IOException {
73+
File tempFile = File.createTempFile(prefix, "");
74+
if (!tempFile.delete()) {
75+
throw new IOException("Could not prep temp dir; file delete failed for " + tempFile.getAbsolutePath());
76+
}
77+
return tempFile;
78+
}
79+
7680
protected BoxStore createBoxStore() {
7781
return createBoxStore(false);
7882
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616

1717
package io.objectbox;
1818

19+
import io.objectbox.exception.FileCorruptException;
1920
import io.objectbox.model.ValidateOnOpenMode;
21+
import org.greenrobot.essentials.io.IoUtils;
2022
import org.junit.Before;
2123
import org.junit.Test;
2224

2325

26+
import java.io.File;
27+
import java.io.FileOutputStream;
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
2431
import static org.junit.Assert.assertSame;
2532
import static org.junit.Assert.assertTrue;
2633
import static org.junit.Assert.fail;
@@ -143,4 +150,27 @@ public void validateOnOpen() {
143150
assertNotNull(getTestEntityBox().get(id));
144151
getTestEntityBox().put(new TestEntity(0));
145152
}
153+
154+
155+
@Test(expected = FileCorruptException.class)
156+
public void validateOnOpenCorruptFile() throws IOException {
157+
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+
}
165+
builder = BoxStoreBuilder.createDebugWithoutModel().directory(dir);
166+
builder.validateOnOpen(ValidateOnOpenMode.AllBranches);
167+
try {
168+
store = builder.build();
169+
} finally {
170+
boolean delOk = badDataFile.delete();
171+
delOk &= new File(dir, "lock.mdb").delete();
172+
delOk &= dir.delete();
173+
assertTrue(delOk); // Try to delete all before asserting
174+
}
175+
}
146176
}

0 commit comments

Comments
 (0)