|
| 1 | +package io.objectbox; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | + |
| 8 | +import io.objectbox.exception.PagesCorruptException; |
| 9 | +import io.objectbox.model.ValidateOnOpenMode; |
| 10 | +import org.greenrobot.essentials.io.IoUtils; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertNotNull; |
| 15 | +import static org.junit.Assert.assertTrue; |
| 16 | +import static org.junit.Assert.fail; |
| 17 | + |
| 18 | +/** |
| 19 | + * Tests validation (and recovery) options on opening a store. |
| 20 | + */ |
| 21 | +public class BoxStoreValidationTest extends AbstractObjectBoxTest { |
| 22 | + |
| 23 | + private BoxStoreBuilder builder; |
| 24 | + |
| 25 | + @Override |
| 26 | + protected BoxStore createBoxStore() { |
| 27 | + // Standard setup of store not required |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setUpBuilder() { |
| 33 | + BoxStore.clearDefaultStore(); |
| 34 | + builder = new BoxStoreBuilder(createTestModel(null)).directory(boxStoreDir); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void validateOnOpen() { |
| 39 | + // Create a database first; we must create the model only once (ID/UID sequences would be different 2nd time) |
| 40 | + byte[] model = createTestModel(null); |
| 41 | + builder = new BoxStoreBuilder(model).directory(boxStoreDir); |
| 42 | + builder.entity(new TestEntity_()); |
| 43 | + store = builder.build(); |
| 44 | + |
| 45 | + TestEntity object = new TestEntity(0); |
| 46 | + object.setSimpleString("hello hello"); |
| 47 | + long id = getTestEntityBox().put(object); |
| 48 | + store.close(); |
| 49 | + |
| 50 | + // Then re-open database with validation and ensure db is operational |
| 51 | + builder = new BoxStoreBuilder(model).directory(boxStoreDir); |
| 52 | + builder.entity(new TestEntity_()); |
| 53 | + builder.validateOnOpen(ValidateOnOpenMode.Full); |
| 54 | + store = builder.build(); |
| 55 | + assertNotNull(getTestEntityBox().get(id)); |
| 56 | + getTestEntityBox().put(new TestEntity(0)); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + @Test(expected = PagesCorruptException.class) |
| 61 | + public void validateOnOpenCorruptFile() throws IOException { |
| 62 | + File dir = prepareTempDir("object-store-test-corrupted"); |
| 63 | + File badDataFile = prepareBadDataFile(dir); |
| 64 | + |
| 65 | + builder = BoxStoreBuilder.createDebugWithoutModel().directory(dir); |
| 66 | + builder.validateOnOpen(ValidateOnOpenMode.Full); |
| 67 | + try { |
| 68 | + store = builder.build(); |
| 69 | + } finally { |
| 70 | + boolean delOk = badDataFile.delete(); |
| 71 | + delOk &= new File(dir, "lock.mdb").delete(); |
| 72 | + delOk &= dir.delete(); |
| 73 | + assertTrue(delOk); // Try to delete all before asserting |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void usePreviousCommitWithCorruptFile() throws IOException { |
| 79 | + File dir = prepareTempDir("object-store-test-corrupted"); |
| 80 | + prepareBadDataFile(dir); |
| 81 | + builder = BoxStoreBuilder.createDebugWithoutModel().directory(dir); |
| 82 | + builder.validateOnOpen(ValidateOnOpenMode.Full).usePreviousCommit(); |
| 83 | + store = builder.build(); |
| 84 | + String diagnoseString = store.diagnose(); |
| 85 | + assertTrue(diagnoseString.contains("entries=2")); |
| 86 | + store.validate(0, true); |
| 87 | + store.close(); |
| 88 | + assertTrue(store.deleteAllFiles()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void usePreviousCommitAfterFileCorruptException() throws IOException { |
| 93 | + File dir = prepareTempDir("object-store-test-corrupted"); |
| 94 | + prepareBadDataFile(dir); |
| 95 | + builder = BoxStoreBuilder.createDebugWithoutModel().directory(dir); |
| 96 | + builder.validateOnOpen(ValidateOnOpenMode.Full); |
| 97 | + try { |
| 98 | + store = builder.build(); |
| 99 | + fail("Should have thrown"); |
| 100 | + } catch (PagesCorruptException e) { |
| 101 | + builder.usePreviousCommit(); |
| 102 | + store = builder.build(); |
| 103 | + } |
| 104 | + |
| 105 | + String diagnoseString = store.diagnose(); |
| 106 | + assertTrue(diagnoseString.contains("entries=2")); |
| 107 | + store.validate(0, true); |
| 108 | + store.close(); |
| 109 | + assertTrue(store.deleteAllFiles()); |
| 110 | + } |
| 111 | + |
| 112 | + private File prepareBadDataFile(File dir) throws IOException { |
| 113 | + assertTrue(dir.mkdir()); |
| 114 | + File badDataFile = new File(dir, "data.mdb"); |
| 115 | + try (InputStream badIn = getClass().getResourceAsStream("corrupt-pageno-in-branch-data.mdb")) { |
| 116 | + try (FileOutputStream badOut = new FileOutputStream(badDataFile)) { |
| 117 | + IoUtils.copyAllBytes(badIn, badOut); |
| 118 | + } |
| 119 | + } |
| 120 | + return badDataFile; |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments