Code cleanup: import static, use == to compare enum values.

This commit is contained in:
akwizgran
2013-02-01 22:40:36 +00:00
parent adff37481f
commit 30a0269652
6 changed files with 110 additions and 97 deletions

View File

@@ -1,5 +1,10 @@
package net.sf.briar.db;
import static net.sf.briar.api.Rating.GOOD;
import static net.sf.briar.api.Rating.UNRATED;
import static net.sf.briar.db.Status.NEW;
import static net.sf.briar.db.Status.SEEN;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
@@ -10,7 +15,6 @@ import net.sf.briar.BriarTestCase;
import net.sf.briar.TestMessage;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.NoSuchContactException;
@@ -109,16 +113,16 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
will(returnValue(shutdownHandle));
// getRating(authorId)
oneOf(database).getRating(txn, authorId);
will(returnValue(Rating.UNRATED));
// setRating(authorId, Rating.GOOD)
oneOf(database).setRating(txn, authorId, Rating.GOOD);
will(returnValue(Rating.UNRATED));
will(returnValue(UNRATED));
// setRating(authorId, GOOD)
oneOf(database).setRating(txn, authorId, GOOD);
will(returnValue(UNRATED));
oneOf(database).getMessagesByAuthor(txn, authorId);
will(returnValue(Collections.emptyList()));
oneOf(listener).eventOccurred(with(any(RatingChangedEvent.class)));
// setRating(authorId, Rating.GOOD) again
oneOf(database).setRating(txn, authorId, Rating.GOOD);
will(returnValue(Rating.GOOD));
// setRating(authorId, GOOD) again
oneOf(database).setRating(txn, authorId, GOOD);
will(returnValue(GOOD));
// addContact()
oneOf(database).addContact(txn);
will(returnValue(contactId));
@@ -167,9 +171,9 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
db.open(false);
db.addListener(listener);
assertEquals(Rating.UNRATED, db.getRating(authorId));
db.setRating(authorId, Rating.GOOD); // First time - listeners called
db.setRating(authorId, Rating.GOOD); // Second time - not called
assertEquals(UNRATED, db.getRating(authorId));
db.setRating(authorId, GOOD); // First time - listeners called
db.setRating(authorId, GOOD); // Second time - not called
assertEquals(contactId, db.addContact());
assertEquals(Collections.singletonList(contactId), db.getContacts());
assertEquals(Collections.emptyMap(),
@@ -194,11 +198,11 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
context.checking(new Expectations() {{
// setRating(authorId, Rating.GOOD)
// setRating(authorId, GOOD)
allowing(database).startTransaction();
will(returnValue(txn));
oneOf(database).setRating(txn, authorId, Rating.GOOD);
will(returnValue(Rating.UNRATED));
oneOf(database).setRating(txn, authorId, GOOD);
will(returnValue(UNRATED));
// The sendability of the author's messages should be incremented
oneOf(database).getMessagesByAuthor(txn, authorId);
will(returnValue(Collections.singletonList(messageId)));
@@ -213,7 +217,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown);
db.setRating(authorId, Rating.GOOD);
db.setRating(authorId, GOOD);
context.assertIsSatisfied();
}
@@ -226,11 +230,11 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
context.checking(new Expectations() {{
// setRating(authorId, Rating.GOOD)
// setRating(authorId, GOOD)
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).setRating(txn, authorId, Rating.GOOD);
will(returnValue(Rating.UNRATED));
oneOf(database).setRating(txn, authorId, GOOD);
will(returnValue(UNRATED));
// The sendability of the author's messages should be incremented
oneOf(database).getMessagesByAuthor(txn, authorId);
will(returnValue(Collections.singletonList(messageId)));
@@ -249,7 +253,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown);
db.setRating(authorId, Rating.GOOD);
db.setRating(authorId, GOOD);
context.assertIsSatisfied();
}
@@ -263,11 +267,11 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
context.checking(new Expectations() {{
// setRating(authorId, Rating.GOOD)
// setRating(authorId, GOOD)
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).setRating(txn, authorId, Rating.GOOD);
will(returnValue(Rating.UNRATED));
oneOf(database).setRating(txn, authorId, GOOD);
will(returnValue(UNRATED));
// The sendability of the author's messages should be incremented
oneOf(database).getMessagesByAuthor(txn, authorId);
will(returnValue(Collections.singletonList(messageId)));
@@ -289,7 +293,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown);
db.setRating(authorId, Rating.GOOD);
db.setRating(authorId, GOOD);
context.assertIsSatisfied();
}
@@ -360,10 +364,10 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
will(returnValue(true));
oneOf(database).getContacts(txn);
will(returnValue(Collections.singletonList(contactId)));
oneOf(database).setStatus(txn, contactId, messageId, Status.NEW);
oneOf(database).setStatus(txn, contactId, messageId, NEW);
// The author is unrated and there are no sendable children
oneOf(database).getRating(txn, authorId);
will(returnValue(Rating.UNRATED));
will(returnValue(UNRATED));
oneOf(database).getNumberOfSendableChildren(txn, messageId);
will(returnValue(0));
oneOf(database).setSendability(txn, messageId, 0);
@@ -395,10 +399,10 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
will(returnValue(true));
oneOf(database).getContacts(txn);
will(returnValue(Collections.singletonList(contactId)));
oneOf(database).setStatus(txn, contactId, messageId, Status.NEW);
oneOf(database).setStatus(txn, contactId, messageId, NEW);
// The author is rated GOOD and there are two sendable children
oneOf(database).getRating(txn, authorId);
will(returnValue(Rating.GOOD));
will(returnValue(GOOD));
oneOf(database).getNumberOfSendableChildren(txn, messageId);
will(returnValue(2));
oneOf(database).setSendability(txn, messageId, 3);
@@ -456,7 +460,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// addLocalPrivateMessage(privateMessage, contactId)
oneOf(database).addPrivateMessage(txn, privateMessage, contactId);
will(returnValue(true));
oneOf(database).setStatus(txn, contactId, messageId, Status.NEW);
oneOf(database).setStatus(txn, contactId, messageId, NEW);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown);
@@ -918,7 +922,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// The message is stored
oneOf(database).addPrivateMessage(txn, privateMessage, contactId);
will(returnValue(true));
oneOf(database).setStatus(txn, contactId, messageId, Status.SEEN);
oneOf(database).setStatus(txn, contactId, messageId, SEEN);
// The message must be acked
oneOf(database).addMessageToAck(txn, contactId, messageId);
}});
@@ -1007,7 +1011,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// The message is stored, but it's a duplicate
oneOf(database).addGroupMessage(txn, message);
will(returnValue(false));
oneOf(database).setStatus(txn, contactId, messageId, Status.SEEN);
oneOf(database).setStatus(txn, contactId, messageId, SEEN);
// The message must be acked
oneOf(database).addMessageToAck(txn, contactId, messageId);
}});
@@ -1039,13 +1043,13 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// The message is stored, and it's not a duplicate
oneOf(database).addGroupMessage(txn, message);
will(returnValue(true));
oneOf(database).setStatus(txn, contactId, messageId, Status.SEEN);
oneOf(database).setStatus(txn, contactId, messageId, SEEN);
// Set the status to NEW for all other contacts (there are none)
oneOf(database).getContacts(txn);
will(returnValue(Collections.singletonList(contactId)));
// Calculate the sendability - zero, so ancestors aren't updated
oneOf(database).getRating(txn, authorId);
will(returnValue(Rating.UNRATED));
will(returnValue(UNRATED));
oneOf(database).getNumberOfSendableChildren(txn, messageId);
will(returnValue(0));
oneOf(database).setSendability(txn, messageId, 0);
@@ -1081,13 +1085,13 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// The message is stored, and it's not a duplicate
oneOf(database).addGroupMessage(txn, message);
will(returnValue(true));
oneOf(database).setStatus(txn, contactId, messageId, Status.SEEN);
oneOf(database).setStatus(txn, contactId, messageId, SEEN);
// 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
oneOf(database).getRating(txn, authorId);
will(returnValue(Rating.GOOD));
will(returnValue(GOOD));
oneOf(database).getNumberOfSendableChildren(txn, messageId);
will(returnValue(1));
oneOf(database).setSendability(txn, messageId, 2);
@@ -1210,9 +1214,9 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
will(returnValue(true));
oneOf(database).getContacts(txn);
will(returnValue(Collections.singletonList(contactId)));
oneOf(database).setStatus(txn, contactId, messageId, Status.NEW);
oneOf(database).setStatus(txn, contactId, messageId, NEW);
oneOf(database).getRating(txn, authorId);
will(returnValue(Rating.UNRATED));
will(returnValue(UNRATED));
oneOf(database).getNumberOfSendableChildren(txn, messageId);
will(returnValue(0));
oneOf(database).setSendability(txn, messageId, 0);
@@ -1246,7 +1250,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// addLocalPrivateMessage(privateMessage, contactId)
oneOf(database).addPrivateMessage(txn, privateMessage, contactId);
will(returnValue(true));
oneOf(database).setStatus(txn, contactId, messageId, Status.NEW);
oneOf(database).setStatus(txn, contactId, messageId, NEW);
// The message was added, so the listener should be called
oneOf(listener).eventOccurred(with(any(MessageAddedEvent.class)));
}});

View File

@@ -1,6 +1,11 @@
package net.sf.briar.db;
import static java.util.concurrent.TimeUnit.SECONDS;
import static net.sf.briar.api.Rating.GOOD;
import static net.sf.briar.api.Rating.UNRATED;
import static net.sf.briar.db.Status.NEW;
import static net.sf.briar.db.Status.SEEN;
import static net.sf.briar.db.Status.SENT;
import static org.junit.Assert.assertArrayEquals;
import java.io.File;
@@ -22,7 +27,6 @@ import net.sf.briar.TestDatabaseConfig;
import net.sf.briar.TestMessage;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.db.DbException;
@@ -173,11 +177,11 @@ public class H2DatabaseTest extends BriarTestCase {
Connection txn = db.startTransaction();
// Unknown authors should be unrated
assertEquals(Rating.UNRATED, db.getRating(txn, authorId));
assertEquals(UNRATED, db.getRating(txn, authorId));
// Store a rating
db.setRating(txn, authorId, Rating.GOOD);
db.setRating(txn, authorId, GOOD);
// Check that the rating was stored
assertEquals(Rating.GOOD, db.getRating(txn, authorId));
assertEquals(GOOD, db.getRating(txn, authorId));
db.commitTransaction(txn);
db.close();
@@ -236,7 +240,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertFalse(it.hasNext());
// Changing the status to NEW should make the message sendable
db.setStatus(txn, contactId, messageId1, Status.NEW);
db.setStatus(txn, contactId, messageId1, NEW);
assertTrue(db.hasSendableMessages(txn, contactId));
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertTrue(it.hasNext());
@@ -244,13 +248,13 @@ public class H2DatabaseTest extends BriarTestCase {
assertFalse(it.hasNext());
// Changing the status to SENT should make the message unsendable
db.setStatus(txn, contactId, messageId1, Status.SENT);
db.setStatus(txn, contactId, messageId1, SENT);
assertFalse(db.hasSendableMessages(txn, contactId));
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertFalse(it.hasNext());
// Changing the status to SEEN should also make the message unsendable
db.setStatus(txn, contactId, messageId1, Status.SEEN);
db.setStatus(txn, contactId, messageId1, SEEN);
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertFalse(it.hasNext());
@@ -267,7 +271,7 @@ public class H2DatabaseTest extends BriarTestCase {
// Add a contact and store a private message
assertEquals(contactId, db.addContact(txn));
db.addPrivateMessage(txn, privateMessage, contactId);
db.setStatus(txn, contactId, messageId1, Status.NEW);
db.setStatus(txn, contactId, messageId1, NEW);
// The message is sendable, but too large to send
assertTrue(db.hasSendableMessages(txn, contactId));
@@ -298,7 +302,7 @@ public class H2DatabaseTest extends BriarTestCase {
db.addVisibility(txn, contactId, groupId);
db.setSubscriptions(txn, contactId, Arrays.asList(group), 1);
db.addGroupMessage(txn, message);
db.setStatus(txn, contactId, messageId, Status.NEW);
db.setStatus(txn, contactId, messageId, NEW);
// The message should not be sendable
assertFalse(db.hasSendableMessages(txn, contactId));
@@ -344,8 +348,8 @@ public class H2DatabaseTest extends BriarTestCase {
db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertFalse(it.hasNext());
// Changing the status to Status.NEW should make the message sendable
db.setStatus(txn, contactId, messageId, Status.NEW);
// Changing the status to NEW should make the message sendable
db.setStatus(txn, contactId, messageId, NEW);
assertTrue(db.hasSendableMessages(txn, contactId));
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertTrue(it.hasNext());
@@ -353,13 +357,13 @@ public class H2DatabaseTest extends BriarTestCase {
assertFalse(it.hasNext());
// Changing the status to SENT should make the message unsendable
db.setStatus(txn, contactId, messageId, Status.SENT);
db.setStatus(txn, contactId, messageId, SENT);
assertFalse(db.hasSendableMessages(txn, contactId));
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertFalse(it.hasNext());
// Changing the status to SEEN should also make the message unsendable
db.setStatus(txn, contactId, messageId, Status.SEEN);
db.setStatus(txn, contactId, messageId, SEEN);
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertFalse(it.hasNext());
@@ -378,7 +382,7 @@ public class H2DatabaseTest extends BriarTestCase {
db.addVisibility(txn, contactId, groupId);
db.addGroupMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
db.setStatus(txn, contactId, messageId, NEW);
// The contact is not subscribed, so the message should not be sendable
assertFalse(db.hasSendableMessages(txn, contactId));
@@ -416,7 +420,7 @@ public class H2DatabaseTest extends BriarTestCase {
db.setSubscriptions(txn, contactId, Arrays.asList(group), 1);
db.addGroupMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
db.setStatus(txn, contactId, messageId, NEW);
// The message is sendable, but too large to send
assertTrue(db.hasSendableMessages(txn, contactId));
@@ -446,7 +450,7 @@ public class H2DatabaseTest extends BriarTestCase {
db.setSubscriptions(txn, contactId, Arrays.asList(group), 1);
db.addGroupMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
db.setStatus(txn, contactId, messageId, NEW);
// The subscription is not visible to the contact, so the message
// should not be sendable
@@ -530,7 +534,7 @@ public class H2DatabaseTest extends BriarTestCase {
db.setSubscriptions(txn, contactId, Arrays.asList(group), 1);
db.addGroupMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
db.setStatus(txn, contactId, messageId, NEW);
// Retrieve the message from the database and mark it as sent
Iterator<MessageId> it =
@@ -538,7 +542,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertTrue(it.hasNext());
assertEquals(messageId, it.next());
assertFalse(it.hasNext());
db.setStatus(txn, contactId, messageId, Status.SENT);
db.setStatus(txn, contactId, messageId, SENT);
db.addOutstandingMessages(txn, contactId,
Collections.singletonList(messageId));
@@ -911,7 +915,7 @@ public class H2DatabaseTest extends BriarTestCase {
// Set the sendability to > 0 and the status to SEEN
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.SEEN);
db.setStatus(txn, contactId, messageId, SEEN);
// The message is not sendable because its status is SEEN
assertNull(db.getRawMessageIfSendable(txn, contactId, messageId));
@@ -934,7 +938,7 @@ public class H2DatabaseTest extends BriarTestCase {
// Set the sendability to 0 and the status to NEW
db.setSendability(txn, messageId, 0);
db.setStatus(txn, contactId, messageId, Status.NEW);
db.setStatus(txn, contactId, messageId, NEW);
// The message is not sendable because its sendability is 0
assertNull(db.getRawMessageIfSendable(txn, contactId, messageId));
@@ -959,7 +963,7 @@ public class H2DatabaseTest extends BriarTestCase {
// Set the sendability to > 0 and the status to NEW
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
db.setStatus(txn, contactId, messageId, NEW);
// The message is not sendable because it's too old
assertNull(db.getRawMessageIfSendable(txn, contactId, messageId));
@@ -982,7 +986,7 @@ public class H2DatabaseTest extends BriarTestCase {
// Set the sendability to > 0 and the status to NEW
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
db.setStatus(txn, contactId, messageId, NEW);
// The message is sendable so it should be returned
byte[] b = db.getRawMessageIfSendable(txn, contactId, messageId);
@@ -1038,7 +1042,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group);
db.addGroupMessage(txn, message);
db.setStatus(txn, contactId, messageId, Status.NEW);
db.setStatus(txn, contactId, messageId, NEW);
// There's no contact subscription for the group
assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId));
@@ -1058,7 +1062,7 @@ public class H2DatabaseTest extends BriarTestCase {
db.addSubscription(txn, group);
db.addGroupMessage(txn, message);
db.setSubscriptions(txn, contactId, Arrays.asList(group), 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
db.setStatus(txn, contactId, messageId, NEW);
// The subscription is not visible
assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId));
@@ -1081,7 +1085,7 @@ public class H2DatabaseTest extends BriarTestCase {
db.addGroupMessage(txn, message);
// The message has already been seen by the contact
db.setStatus(txn, contactId, messageId, Status.SEEN);
db.setStatus(txn, contactId, messageId, SEEN);
assertTrue(db.setStatusSeenIfVisible(txn, contactId, messageId));
@@ -1103,7 +1107,7 @@ public class H2DatabaseTest extends BriarTestCase {
db.addGroupMessage(txn, message);
// The message has not been seen by the contact
db.setStatus(txn, contactId, messageId, Status.NEW);
db.setStatus(txn, contactId, messageId, NEW);
assertTrue(db.setStatusSeenIfVisible(txn, contactId, messageId));