/**
* Asserts that {@link DataStore#deleteAllOlderThan(long)} only deleted
* records older than argument passed.
*/
protected void doDeleteAllOlderThan() throws Exception {
CachingDataStore ds = createDataStore();
Random random = new Random(12345);
byte[] data = new byte[12345];
random.nextBytes(data);
DataRecord rec1 = ds.addRecord(new ByteArrayInputStream(data));
data = new byte[12345];
random.nextBytes(data);
DataRecord rec2 = ds.addRecord(new ByteArrayInputStream(data));
Thread.sleep(2000);
long updateTime = System.currentTimeMillis();
ds.updateModifiedDateOnAccess(updateTime);
data = new byte[12345];
random.nextBytes(data);
DataRecord rec3 = ds.addRecord(new ByteArrayInputStream(data));
data = new byte[12345];
random.nextBytes(data);
DataRecord rec4 = ds.addRecord(new ByteArrayInputStream(data));
rec1 = ds.getRecord(rec1.getIdentifier());
ds.clearInUse();
assertEquals("only rec2 should be deleted", 1,
ds.deleteAllOlderThan(updateTime));
assertNull("rec2 should be null",
ds.getRecordIfStored(rec2.getIdentifier()));
Iterator<DataIdentifier> itr = ds.getAllIdentifiers();
List<DataIdentifier> list = new ArrayList<DataIdentifier>();
list.add(rec1.getIdentifier());
list.add(rec3.getIdentifier());
list.add(rec4.getIdentifier());
while (itr.hasNext()) {
assertTrue("record found on list", list.remove(itr.next()));
}
assertEquals("touched records found", 0, list.size());
assertEquals("rec1 touched", true,
ds.getLastModified(rec1.getIdentifier()) > updateTime);
assertEquals("rec3 touched", true,
ds.getLastModified(rec3.getIdentifier()) > updateTime);
assertEquals("rec4 touched", true,
ds.getLastModified(rec4.getIdentifier()) > updateTime);
ds.close();
}