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(new NullStorageManager());
Assert.assertEquals(numberOfElements, msgs.size());
Assert.assertEquals(numberOfElements, impl.getNumberOfMessages());
for (int i = 0; i < msgs.size(); i++)
{
Assert.assertEquals(simpleDestination, msgs.get(i).getMessage().getAddress());
UnitTestCase.assertEqualsByteArrays(buffers.get(i).toByteBuffer().array(), msgs.get(i)
.getMessage()
.getBodyBuffer()
.toByteBuffer()
.array());
}
impl.delete(null);
Assert.assertEquals(0, factory.listFiles("page").size());
Assert.assertEquals(1, factory.listFiles("invalidPage").size());