Assertions and comments.

This commit is contained in:
akwizgran
2011-09-19 16:42:27 +01:00
parent 4b97be897e
commit 7aeb6029a6
3 changed files with 30 additions and 12 deletions

View File

@@ -271,15 +271,21 @@ DatabaseCleaner.Callback {
*/ */
private int updateAncestorSendability(T txn, MessageId m, boolean increment) private int updateAncestorSendability(T txn, MessageId m, boolean increment)
throws DbException { throws DbException {
GroupId group = db.getGroup(txn, m);
int affected = 0; int affected = 0;
boolean changed = true; boolean changed = true;
while(changed) { while(changed) {
// Stop if the message has no parent
MessageId parent = db.getParent(txn, m); MessageId parent = db.getParent(txn, m);
if(parent == null) break; if(parent == null) break;
// Stop if the parent isn't in the database
if(!db.containsMessage(txn, parent)) break; if(!db.containsMessage(txn, parent)) break;
if(!db.getGroup(txn, m).equals(db.getGroup(txn, parent))) break; // Stop if the message and the parent aren't in the same group
Integer parentSendability = db.getSendability(txn, parent); assert group != null;
assert parentSendability != null; GroupId parentGroup = db.getGroup(txn, parent);
if(!group.equals(parentGroup)) break;
// Increment or decrement the parent's sendability
int parentSendability = db.getSendability(txn, parent);
if(increment) { if(increment) {
parentSendability++; parentSendability++;
changed = parentSendability == 1; changed = parentSendability == 1;
@@ -291,7 +297,9 @@ DatabaseCleaner.Callback {
if(changed) affected++; if(changed) affected++;
} }
db.setSendability(txn, parent, parentSendability); db.setSendability(txn, parent, parentSendability);
// Move on to the parent's parent
m = parent; m = parent;
group = parentGroup;
} }
return affected; return affected;
} }

View File

@@ -105,6 +105,8 @@ public class DatabaseComponentImplTest extends DatabaseComponentTest {
will(returnValue(Collections.singleton(messageId))); will(returnValue(Collections.singleton(messageId)));
oneOf(database).getSendability(txn, messageId); oneOf(database).getSendability(txn, messageId);
will(returnValue(1)); will(returnValue(1));
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getParent(txn, messageId); oneOf(database).getParent(txn, messageId);
will(returnValue(null)); will(returnValue(null));
oneOf(database).removeMessage(txn, messageId); oneOf(database).removeMessage(txn, messageId);

View File

@@ -199,6 +199,8 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(0)); will(returnValue(0));
oneOf(database).setSendability(txn, messageId, 1); oneOf(database).setSendability(txn, messageId, 1);
// Backward inclusion stops when the message has no parent // Backward inclusion stops when the message has no parent
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getParent(txn, messageId); oneOf(database).getParent(txn, messageId);
will(returnValue(null)); will(returnValue(null));
oneOf(database).commitTransaction(txn); oneOf(database).commitTransaction(txn);
@@ -228,6 +230,8 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(0)); will(returnValue(0));
oneOf(database).setSendability(txn, messageId, 1); oneOf(database).setSendability(txn, messageId, 1);
// The parent exists // The parent exists
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getParent(txn, messageId); oneOf(database).getParent(txn, messageId);
will(returnValue(parentId)); will(returnValue(parentId));
// The parent isn't in the DB // The parent isn't in the DB
@@ -262,13 +266,13 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(0)); will(returnValue(0));
oneOf(database).setSendability(txn, messageId, 1); oneOf(database).setSendability(txn, messageId, 1);
// The parent exists and is in the database // The parent exists and is in the database
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getParent(txn, messageId); oneOf(database).getParent(txn, messageId);
will(returnValue(parentId)); will(returnValue(parentId));
oneOf(database).containsMessage(txn, parentId); oneOf(database).containsMessage(txn, parentId);
will(returnValue(true)); will(returnValue(true));
// The parent is in a different group // The parent is in a different group
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getGroup(txn, parentId); oneOf(database).getGroup(txn, parentId);
will(returnValue(groupId1)); will(returnValue(groupId1));
oneOf(database).commitTransaction(txn); oneOf(database).commitTransaction(txn);
@@ -298,13 +302,13 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(0)); will(returnValue(0));
oneOf(database).setSendability(txn, messageId, 1); oneOf(database).setSendability(txn, messageId, 1);
// The parent exists and is in the database // The parent exists and is in the database
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getParent(txn, messageId); oneOf(database).getParent(txn, messageId);
will(returnValue(parentId)); will(returnValue(parentId));
oneOf(database).containsMessage(txn, parentId); oneOf(database).containsMessage(txn, parentId);
will(returnValue(true)); will(returnValue(true));
// The parent is a private message // The parent is a private message
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getGroup(txn, parentId); oneOf(database).getGroup(txn, parentId);
will(returnValue(null)); will(returnValue(null));
oneOf(database).commitTransaction(txn); oneOf(database).commitTransaction(txn);
@@ -335,12 +339,12 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(0)); will(returnValue(0));
oneOf(database).setSendability(txn, messageId, 1); oneOf(database).setSendability(txn, messageId, 1);
// The parent exists, is in the DB, and is in the same group // The parent exists, is in the DB, and is in the same group
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getParent(txn, messageId); oneOf(database).getParent(txn, messageId);
will(returnValue(parentId)); will(returnValue(parentId));
oneOf(database).containsMessage(txn, parentId); oneOf(database).containsMessage(txn, parentId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getGroup(txn, parentId); oneOf(database).getGroup(txn, parentId);
will(returnValue(groupId)); will(returnValue(groupId));
// The parent is already sendable // The parent is already sendable
@@ -375,12 +379,12 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(0)); will(returnValue(0));
oneOf(database).setSendability(txn, messageId, 1); oneOf(database).setSendability(txn, messageId, 1);
// The parent exists, is in the DB, and is in the same group // The parent exists, is in the DB, and is in the same group
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getParent(txn, messageId); oneOf(database).getParent(txn, messageId);
will(returnValue(parentId)); will(returnValue(parentId));
oneOf(database).containsMessage(txn, parentId); oneOf(database).containsMessage(txn, parentId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getGroup(txn, parentId); oneOf(database).getGroup(txn, parentId);
will(returnValue(groupId)); will(returnValue(groupId));
// The parent is not already sendable // The parent is not already sendable
@@ -500,6 +504,8 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(2)); will(returnValue(2));
oneOf(database).setSendability(txn, messageId, 3); oneOf(database).setSendability(txn, messageId, 3);
// The sendability of the message's ancestors should be updated // The sendability of the message's ancestors should be updated
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getParent(txn, messageId); oneOf(database).getParent(txn, messageId);
will(returnValue(null)); will(returnValue(null));
oneOf(database).commitTransaction(txn); oneOf(database).commitTransaction(txn);
@@ -1134,12 +1140,14 @@ public abstract class DatabaseComponentTest extends TestCase {
// Set the status to NEW for all other contacts (there are none) // Set the status to NEW for all other contacts (there are none)
oneOf(database).getContacts(txn); oneOf(database).getContacts(txn);
will(returnValue(Collections.singletonList(contactId))); will(returnValue(Collections.singletonList(contactId)));
// Calculate the sendability -ancestors are updated // Calculate the sendability - ancestors are updated
oneOf(database).getRating(txn, authorId); oneOf(database).getRating(txn, authorId);
will(returnValue(Rating.GOOD)); will(returnValue(Rating.GOOD));
oneOf(database).getNumberOfSendableChildren(txn, messageId); oneOf(database).getNumberOfSendableChildren(txn, messageId);
will(returnValue(1)); will(returnValue(1));
oneOf(database).setSendability(txn, messageId, 2); oneOf(database).setSendability(txn, messageId, 2);
oneOf(database).getGroup(txn, messageId);
will(returnValue(groupId));
oneOf(database).getParent(txn, messageId); oneOf(database).getParent(txn, messageId);
will(returnValue(null)); will(returnValue(null));
// The batch needs to be acknowledged // The batch needs to be acknowledged