use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.
the class SequentialFileFactoryTestBase method testWriteandRead.
@Test
public void testWriteandRead() throws Exception {
SequentialFile sf = factory.createSequentialFile("write.amq");
sf.open();
String s1 = "aardvark";
byte[] bytes1 = s1.getBytes(StandardCharsets.UTF_8);
ActiveMQBuffer bb1 = wrapBuffer(bytes1);
String s2 = "hippopotamus";
byte[] bytes2 = s2.getBytes(StandardCharsets.UTF_8);
ActiveMQBuffer bb2 = wrapBuffer(bytes2);
String s3 = "echidna";
byte[] bytes3 = s3.getBytes(StandardCharsets.UTF_8);
ActiveMQBuffer bb3 = wrapBuffer(bytes3);
long initialPos = sf.position();
sf.write(bb1, true);
long bytesWritten = sf.position() - initialPos;
Assert.assertEquals(calculateRecordSize(bytes1.length, factory.getAlignment()), bytesWritten);
initialPos = sf.position();
sf.write(bb2, true);
bytesWritten = sf.position() - initialPos;
Assert.assertEquals(calculateRecordSize(bytes2.length, factory.getAlignment()), bytesWritten);
initialPos = sf.position();
sf.write(bb3, true);
bytesWritten = sf.position() - initialPos;
Assert.assertEquals(calculateRecordSize(bytes3.length, factory.getAlignment()), bytesWritten);
sf.position(0);
ByteBuffer rb1 = factory.newBuffer(bytes1.length);
ByteBuffer rb2 = factory.newBuffer(bytes2.length);
ByteBuffer rb3 = factory.newBuffer(bytes3.length);
int bytesRead = sf.read(rb1);
Assert.assertEquals(calculateRecordSize(bytes1.length, factory.getAlignment()), bytesRead);
for (int i = 0; i < bytes1.length; i++) {
Assert.assertEquals(bytes1[i], rb1.get(i));
}
bytesRead = sf.read(rb2);
Assert.assertEquals(calculateRecordSize(bytes2.length, factory.getAlignment()), bytesRead);
for (int i = 0; i < bytes2.length; i++) {
Assert.assertEquals(bytes2[i], rb2.get(i));
}
bytesRead = sf.read(rb3);
Assert.assertEquals(calculateRecordSize(bytes3.length, factory.getAlignment()), bytesRead);
for (int i = 0; i < bytes3.length; i++) {
Assert.assertEquals(bytes3[i], rb3.get(i));
}
sf.close();
}
use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.
the class SequentialFileFactoryTestBase method testOpenClose.
@Test
public void testOpenClose() throws Exception {
SequentialFile sf = factory.createSequentialFile("openclose.amq");
sf.open();
sf.fill(512);
String s1 = "cheesecake";
byte[] bytes1 = s1.getBytes(StandardCharsets.UTF_8);
long initialPos = sf.position();
sf.write(wrapBuffer(bytes1), true);
long bytesWritten = sf.position() - initialPos;
Assert.assertEquals(calculateRecordSize(bytes1.length, factory.getAlignment()), bytesWritten);
sf.close();
try {
sf.write(wrapBuffer(bytes1), true);
Assert.fail("Should throw exception");
} catch (Exception e) {
}
sf.open();
sf.write(wrapBuffer(bytes1), true);
sf.close();
}
use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.
the class PageTest method testDamagedPage.
protected void testDamagedPage(final SequentialFileFactory factory, final int numberOfElements) throws Exception {
SequentialFile file = factory.createSequentialFile("00010.page");
Page impl = new Page(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");
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");
file.open();
impl = new Page(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().getAddressSimpleString());
}
impl.delete(null);
Assert.assertEquals(0, factory.listFiles("page").size());
Assert.assertEquals(1, factory.listFiles("invalidPage").size());
}
use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.
the class PageTest method testAdd.
/**
* Validate if everything we add is recovered
*/
protected void testAdd(final SequentialFileFactory factory, final int numberOfElements) throws Exception {
SequentialFile file = factory.createSequentialFile("00010.page");
Page impl = new Page(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");
addPageElements(simpleDestination, impl, numberOfElements);
impl.sync();
impl.close();
file = factory.createSequentialFile("00010.page");
file.open();
impl = new Page(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().getAddressSimpleString());
}
impl.delete(null);
Assert.assertEquals(0, factory.listFiles(".page").size());
}
use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.
the class JDBCSequentialFileFactoryTest method testGetFileSizeWorksWhenNotOpen.
/**
* Using a real file system users are not required to call file.open() in order to read the file size. The file
* descriptor has enough information. However, with JDBC we do require that some information is loaded in order to
* get the underlying BLOB. This tests ensures that file.size() returns the correct value, without the user calling
* file.open() with JDBCSequentialFile.
*
* @throws Exception
*/
@Test
public void testGetFileSizeWorksWhenNotOpen() throws Exception {
// Create test file with some data.
int testFileSize = 1024;
String fileName = "testFile.txt";
SequentialFile file = factory.createSequentialFile(fileName);
file.open();
// Write some data to the file
ActiveMQBuffer buffer = ActiveMQBuffers.wrappedBuffer(new byte[1024]);
file.write(buffer, true);
file.close();
try {
// Create a new pointer to the test file and ensure file.size() returns the correct value.
SequentialFile file2 = factory.createSequentialFile(fileName);
assertEquals(testFileSize, file2.size());
} catch (Throwable t) {
t.printStackTrace();
}
}
Aggregations