@Test
public void shouldCleanTrashFilesWhenFilesBecomeUsed() throws Exception {
Set<Binary> binaries = new HashSet<Binary>();
int storedCount = 0;
for (int i = 0; i != CONTENT.length; ++i) {
Binary binary = storeAndCheck(i);
assertThat(binary, is(notNullValue()));
binaries.add(binary);
if (binary instanceof StoredBinaryValue) storedCount++;
}
// Make sure there are files for all stored values ...
assertThat(countStoredFiles(), is(storedCount));
assertThat(countTrashFiles(), is(0));
// Mark one of the files as being unused ...
String unused = binaries.iterator().next().getHexHash();
BinaryKey unusedKey = new BinaryKey(unused);
store.markAsUnused(Collections.singleton(unusedKey));
// Make sure the file was moved to the trash ...
assertThat(countStoredFiles(), is(storedCount));
assertThat(countTrashFiles(), is(1));
// Now access all the binary files which will not change there used/unused state
for (Binary binary : binaries) {
InputStream stream = binary.getStream();
String content = IoUtil.read(stream);
assertThat(content.length() != 0, is(true));
}
// Make sure there are files for all stored values ...