From 1ea385e7afe84a1739e85364d1c382d543bd086d Mon Sep 17 00:00:00 2001 From: akwizgran Date: Tue, 26 Jul 2011 16:46:50 +0100 Subject: [PATCH] Unit tests and a bugfix for the new database methods. --- components/net/sf/briar/db/JdbcDatabase.java | 2 + test/net/sf/briar/db/H2DatabaseTest.java | 185 +++++++++++++++++++ 2 files changed, 187 insertions(+) diff --git a/components/net/sf/briar/db/JdbcDatabase.java b/components/net/sf/briar/db/JdbcDatabase.java index 93fd24bef..f5bef00e4 100644 --- a/components/net/sf/briar/db/JdbcDatabase.java +++ b/components/net/sf/briar/db/JdbcDatabase.java @@ -1405,6 +1405,8 @@ abstract class JdbcDatabase implements Database { + " ON messages.groupId = contactSubscriptions.groupId" + " WHERE messageId = ? AND contactId = ?"; ps = txn.prepareStatement(sql); + ps.setBytes(1, m.getBytes()); + ps.setInt(2, c.getInt()); rs = ps.executeQuery(); boolean found = rs.next(); assert found; diff --git a/test/net/sf/briar/db/H2DatabaseTest.java b/test/net/sf/briar/db/H2DatabaseTest.java index 2706af2b7..174c025bd 100644 --- a/test/net/sf/briar/db/H2DatabaseTest.java +++ b/test/net/sf/briar/db/H2DatabaseTest.java @@ -218,6 +218,7 @@ public class H2DatabaseTest extends TestCase { it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator(); assertTrue(it.hasNext()); assertEquals(messageId, it.next()); + assertFalse(it.hasNext()); // Changing the sendability to 0 should make the message unsendable db.setSendability(txn, messageId, 0); @@ -250,6 +251,7 @@ public class H2DatabaseTest extends TestCase { it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator(); assertTrue(it.hasNext()); assertEquals(messageId, it.next()); + assertFalse(it.hasNext()); // Changing the status to SENT should make the message unsendable db.setStatus(txn, contactId, messageId, Status.SENT); @@ -287,6 +289,7 @@ public class H2DatabaseTest extends TestCase { it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator(); assertTrue(it.hasNext()); assertEquals(messageId, it.next()); + assertFalse(it.hasNext()); // The contact unsubscribing should make the message unsendable db.setSubscriptions(txn, contactId, Collections.emptySet(), 2); @@ -319,6 +322,7 @@ public class H2DatabaseTest extends TestCase { it = db.getSendableMessages(txn, contactId, size).iterator(); assertTrue(it.hasNext()); assertEquals(messageId, it.next()); + assertFalse(it.hasNext()); db.commitTransaction(txn); db.close(); @@ -804,6 +808,187 @@ public class H2DatabaseTest extends TestCase { db.close(); } + @Test + public void testGetMessageIfSendableReturnsNullIfNotInDatabase() + throws DbException { + Database db = open(false); + Connection txn = db.startTransaction(); + + // Add a contact and subscribe to a group + assertEquals(contactId, db.addContact(txn, null)); + db.addSubscription(txn, group); + db.setSubscriptions(txn, contactId, Collections.singleton(group), 1); + + // The message is not in the database + assertNull(db.getMessageIfSendable(txn, contactId, messageId)); + + db.commitTransaction(txn); + db.close(); + } + + @Test + public void testGetMessageIfSendableReturnsNullIfSeen() + throws DbException { + Database db = open(false); + Connection txn = db.startTransaction(); + + // Add a contact, subscribe to a group and store a message + assertEquals(contactId, db.addContact(txn, null)); + db.addSubscription(txn, group); + db.setSubscriptions(txn, contactId, Collections.singleton(group), 1); + db.addMessage(txn, message); + // Set the sendability to > 0 + db.setSendability(txn, messageId, 1); + // Set the status to SEEN + db.setStatus(txn, contactId, messageId, Status.SEEN); + + // The message is not sendable because its status is SEEN + assertNull(db.getMessageIfSendable(txn, contactId, messageId)); + + db.commitTransaction(txn); + db.close(); + } + + @Test + public void testGetMessageIfSendableReturnsNullIfNotSendable() + throws DbException { + Database db = open(false); + Connection txn = db.startTransaction(); + + // Add a contact, subscribe to a group and store a message + assertEquals(contactId, db.addContact(txn, null)); + db.addSubscription(txn, group); + db.setSubscriptions(txn, contactId, Collections.singleton(group), 1); + db.addMessage(txn, message); + // Set the sendability to 0 + db.setSendability(txn, messageId, 0); + // Set the status to NEW + db.setStatus(txn, contactId, messageId, Status.NEW); + + // The message is not sendable because its sendability is 0 + assertNull(db.getMessageIfSendable(txn, contactId, messageId)); + + db.commitTransaction(txn); + db.close(); + } + + @Test + public void testGetMessageIfSendableReturnsMessage() throws DbException { + Database db = open(false); + Connection txn = db.startTransaction(); + + // Add a contact, subscribe to a group and store a message + assertEquals(contactId, db.addContact(txn, null)); + db.addSubscription(txn, group); + db.setSubscriptions(txn, contactId, Collections.singleton(group), 1); + db.addMessage(txn, message); + // Set the sendability to > 0 + db.setSendability(txn, messageId, 1); + // Set the status to NEW + db.setStatus(txn, contactId, messageId, Status.NEW); + + // The message is sendable so it should be returned + byte[] b = db.getMessageIfSendable(txn, contactId, messageId); + assertTrue(Arrays.equals(raw, b)); + + db.commitTransaction(txn); + db.close(); + } + + @Test + public void testSetStatusSeenIfVisibleRequiresMessageInDatabase() + throws DbException { + Database db = open(false); + Connection txn = db.startTransaction(); + + // Add a contact and subscribe to a group + assertEquals(contactId, db.addContact(txn, null)); + db.addSubscription(txn, group); + db.setSubscriptions(txn, contactId, Collections.singleton(group), 1); + + // The message is not in the database + assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId)); + + db.commitTransaction(txn); + db.close(); + } + + @Test + public void testSetStatusSeenIfVisibleRequiresLocalSubscription() + throws DbException { + Database db = open(false); + Connection txn = db.startTransaction(); + + // Add a contact and a neighbour subscription + assertEquals(contactId, db.addContact(txn, null)); + db.setSubscriptions(txn, contactId, Collections.singleton(group), 1); + + // There's no local subscription for the group + assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId)); + + db.commitTransaction(txn); + db.close(); + } + + @Test + public void testSetStatusSeenIfVisibleRequiresContactSubscription() + throws DbException { + Database db = open(false); + Connection txn = db.startTransaction(); + + // Add a contact, subscribe to a group and store a message + assertEquals(contactId, db.addContact(txn, null)); + db.addSubscription(txn, group); + db.addMessage(txn, message); + db.setStatus(txn, contactId, messageId, Status.NEW); + + // There's no contact subscription for the group + assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId)); + + db.commitTransaction(txn); + db.close(); + } + + @Test + public void testSetStatusSeenIfVisibleReturnsTrueIfAlreadySeen() + throws DbException { + Database db = open(false); + Connection txn = db.startTransaction(); + + // Add a contact, subscribe to a group and store a message + assertEquals(contactId, db.addContact(txn, null)); + db.addSubscription(txn, group); + db.setSubscriptions(txn, contactId, Collections.singleton(group), 1); + db.addMessage(txn, message); + // The message has already been seen by the contact + db.setStatus(txn, contactId, messageId, Status.SEEN); + + assertTrue(db.setStatusSeenIfVisible(txn, contactId, messageId)); + + db.commitTransaction(txn); + db.close(); + } + + @Test + public void testSetStatusSeenIfVisibleReturnsTrueIfNew() + throws DbException { + Database db = open(false); + Connection txn = db.startTransaction(); + + // Add a contact, subscribe to a group and store a message + assertEquals(contactId, db.addContact(txn, null)); + db.addSubscription(txn, group); + db.setSubscriptions(txn, contactId, Collections.singleton(group), 1); + db.addMessage(txn, message); + // The message has not been seen by the contact + db.setStatus(txn, contactId, messageId, Status.NEW); + + assertTrue(db.setStatusSeenIfVisible(txn, contactId, messageId)); + + db.commitTransaction(txn); + db.close(); + } + private Database open(boolean resume) throws DbException { Database db = new H2Database(testDir, password, MAX_SIZE, groupFactory);