Added a method for retrieving the message body from the DB.

This commit is contained in:
akwizgran
2011-10-21 20:42:43 +01:00
parent 1173e0a563
commit 9ec8feec78
13 changed files with 217 additions and 93 deletions

View File

@@ -142,10 +142,10 @@ public class ProtocolIntegrationTest extends TestCase {
a.finish();
BatchWriter b = protocolWriterFactory.createBatchWriter(out1);
assertTrue(b.writeMessage(message.getSerialisedBytes()));
assertTrue(b.writeMessage(message1.getSerialisedBytes()));
assertTrue(b.writeMessage(message2.getSerialisedBytes()));
assertTrue(b.writeMessage(message3.getSerialisedBytes()));
assertTrue(b.writeMessage(message.getSerialised()));
assertTrue(b.writeMessage(message1.getSerialised()));
assertTrue(b.writeMessage(message2.getSerialised()));
assertTrue(b.writeMessage(message3.getSerialised()));
b.finish();
OfferWriter o = protocolWriterFactory.createOfferWriter(out1);
@@ -255,6 +255,6 @@ public class ProtocolIntegrationTest extends TestCase {
assertEquals(m1.getGroup(), m2.getGroup());
assertEquals(m1.getAuthor(), m2.getAuthor());
assertEquals(m1.getTimestamp(), m2.getTimestamp());
assertArrayEquals(m1.getSerialisedBytes(), m2.getSerialisedBytes());
assertArrayEquals(m1.getSerialised(), m2.getSerialised());
}
}

View File

@@ -4,6 +4,7 @@ import static org.junit.Assert.assertArrayEquals;
import java.io.File;
import java.sql.Connection;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
@@ -1606,6 +1607,46 @@ public class H2DatabaseTest extends TestCase {
db.close();
}
@Test
public void testGetMessageBody() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn, transports, secret));
db.addSubscription(txn, group);
// Store a couple of messages
int bodyLength = raw.length - 20;
Message message1 = new TestMessage(messageId, null, groupId, null,
subject, timestamp, raw, 5, bodyLength);
Message privateMessage1 = new TestMessage(privateMessageId, null, null,
null, subject, timestamp, raw, 10, bodyLength);
db.addGroupMessage(txn, message1);
db.addPrivateMessage(txn, privateMessage1, contactId);
// Calculate the expected message bodies
byte[] expectedBody = new byte[bodyLength];
System.arraycopy(raw, 5, expectedBody, 0, bodyLength);
assertFalse(Arrays.equals(expectedBody, new byte[bodyLength]));
byte[] expectedBody1 = new byte[bodyLength];
System.arraycopy(raw, 10, expectedBody1, 0, bodyLength);
System.arraycopy(raw, 10, expectedBody1, 0, bodyLength);
// Retrieve the raw messages
assertArrayEquals(raw, db.getMessage(txn, messageId));
assertArrayEquals(raw, db.getMessage(txn, privateMessageId));
// Retrieve the message bodies
byte[] body = db.getMessageBody(txn, messageId);
assertArrayEquals(expectedBody, body);
byte[] body1 = db.getMessageBody(txn, privateMessageId);
assertArrayEquals(expectedBody1, body1);
db.commitTransaction(txn);
db.close();
}
@Test
public void testExceptionHandling() throws Exception {
Database<Connection> db = open(false);

View File

@@ -16,9 +16,16 @@ class TestMessage implements Message {
private final String subject;
private final long timestamp;
private final byte[] raw;
private final int bodyStart, bodyLength;
public TestMessage(MessageId id, MessageId parent, GroupId group,
AuthorId author, String subject, long timestamp, byte[] raw) {
this(id, parent, group, author, subject, timestamp, raw, 0, raw.length);
}
public TestMessage(MessageId id, MessageId parent, GroupId group,
AuthorId author, String subject, long timestamp, byte[] raw,
int bodyStart, int bodyLength) {
this.id = id;
this.parent = parent;
this.group = group;
@@ -26,6 +33,8 @@ class TestMessage implements Message {
this.subject = subject;
this.timestamp = timestamp;
this.raw = raw;
this.bodyStart = bodyStart;
this.bodyLength = bodyLength;
}
public MessageId getId() {
@@ -56,10 +65,18 @@ class TestMessage implements Message {
return raw.length;
}
public byte[] getSerialisedBytes() {
public byte[] getSerialised() {
return raw;
}
public int getBodyStart() {
return bodyStart;
}
public int getBodyLength() {
return bodyLength;
}
public InputStream getSerialisedStream() {
return new ByteArrayInputStream(raw);
}

View File

@@ -87,7 +87,7 @@ public class ProtocolReadWriteTest extends TestCase {
a.finish();
BatchWriter b = writerFactory.createBatchWriter(out);
b.writeMessage(message.getSerialisedBytes());
b.writeMessage(message.getSerialised());
b.finish();
OfferWriter o = writerFactory.createOfferWriter(out);

View File

@@ -113,7 +113,7 @@ public class ConstantsTest extends TestCase {
ProtocolConstants.MAX_PACKET_LENGTH);
BatchWriter b = new BatchWriterImpl(out, serial, writerFactory,
crypto.getMessageDigest());
assertTrue(b.writeMessage(message.getSerialisedBytes()));
assertTrue(b.writeMessage(message.getSerialised()));
b.finish();
// Check the size of the serialised batch
assertTrue(out.size() > UniqueId.LENGTH + Group.MAX_NAME_LENGTH +