unit-testingjunitlucene

Lucene Purposely Corrupt Index For Unit Test


I have some code that deletes and rebuilds a Lucene index if it is caught as corrupt and I would like to unit test this functionality. What is the best way to purposely corrupt a test FSDirectory and then perform unit tests on the corrupted index?

I am assuming only the File System directory can be corrupted, but if there is a way to simulate the same thing with the RAM Directory that would be even better as I prefer to use the RAM in my lucene unit tests.


Solution

  • You can corrupt stuff in any directory, just use the Directory api directly to mess up index files.

    For example, take a look at the logic in Lucene's MockDirectoryWrapper.crash() (used in unit tests) to simulate a machine crash (by screwing up any not-yet-fsynced files): it simulates this by truncating the file, zeroing part of the file, deleting the file completely, or fully truncating the file to a 0 byte file.

    https://github.com/apache/lucene/blob/branch_10x/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockDirectoryWrapper.java