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)
throws DbException {
GroupId group = db.getGroup(txn, m);
int affected = 0;
boolean changed = true;
while(changed) {
// Stop if the message has no parent
MessageId parent = db.getParent(txn, m);
if(parent == null) break;
// Stop if the parent isn't in the database
if(!db.containsMessage(txn, parent)) break;
if(!db.getGroup(txn, m).equals(db.getGroup(txn, parent))) break;
Integer parentSendability = db.getSendability(txn, parent);
assert parentSendability != null;
// Stop if the message and the parent aren't in the same group
assert group != 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) {
parentSendability++;
changed = parentSendability == 1;
@@ -291,7 +297,9 @@ DatabaseCleaner.Callback {
if(changed) affected++;
}
db.setSendability(txn, parent, parentSendability);
// Move on to the parent's parent
m = parent;
group = parentGroup;
}
return affected;
}

View File

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

View File

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