protected void testDamagedPage(final SequentialFileFactory factory, final int numberOfElements) throws Exception
{
SequentialFile file = factory.createSequentialFile("00010.page", 1);
PageImpl impl = new PageImpl(new SimpleString("something"), new NullStorageManager(), factory, file, 10);
Assert.assertEquals(10, impl.getPageId());
impl.open();
Assert.assertEquals(1, factory.listFiles("page").size());
SimpleString simpleDestination = new SimpleString("Test");
ArrayList<HornetQBuffer> buffers = addPageElements(simpleDestination, impl, numberOfElements);
impl.sync();
long positionA = file.position();
// Add one record that will be damaged
addPageElements(simpleDestination, impl, 1);
long positionB = file.position();
// Add more 10 as they will need to be ignored
addPageElements(simpleDestination, impl, 10);
// Damage data... position the file on the middle between points A and B
file.position(positionA + (positionB - positionA) / 2);
ByteBuffer buffer = ByteBuffer.allocate((int)(positionB - file.position()));
for (int i = 0; i < buffer.capacity(); i++)
{
buffer.put((byte)'Z');
}
buffer.rewind();
file.writeDirect(buffer, true);
impl.close();
file = factory.createSequentialFile("00010.page", 1);
file.open();
impl = new PageImpl(new SimpleString("something"), new NullStorageManager(), factory, file, 10);
List<PagedMessage> msgs = impl.read();
Assert.assertEquals(numberOfElements, msgs.size());