diff --git a/briar-core/src/net/sf/briar/db/Database.java b/briar-core/src/net/sf/briar/db/Database.java index e0bd85aa003ed8171f2c29602b9e86de3e02ed23..f01b2c7799b6f015b177f0e57f42473055547bd2 100644 --- a/briar-core/src/net/sf/briar/db/Database.java +++ b/briar-core/src/net/sf/briar/db/Database.java @@ -523,9 +523,8 @@ interface Database<T> { throws DbException; /** - * Removes outstanding messages that have been acknowledged. Any of the - * messages that are still considered outstanding (Status.SENT) with - * respect to the given contact are now considered seen (Status.SEEN). + * Marks any of the given messages that are considered outstanding with + * respect to the given contact as seen by the contact. * <p> * Locking: contact read, message write. */ @@ -625,9 +624,8 @@ interface Database<T> { /** * If the database contains the given message and it belongs to a group - * that is visible to the given contact, sets the status of the message - * with respect to the contact to Status.SEEN and returns true; otherwise - * returns false. + * that is visible to the given contact, marks the message as seen by the + * contact and returns true; otherwise returns false. * <p> * Locking: contact read, message write, subscription read. */ diff --git a/briar-core/src/net/sf/briar/db/DatabaseComponentImpl.java b/briar-core/src/net/sf/briar/db/DatabaseComponentImpl.java index e54a3105363671f35e55808540dd1dc817e42d94..37c0c40809b942109dc80698dc7fe14f159a8732 100644 --- a/briar-core/src/net/sf/briar/db/DatabaseComponentImpl.java +++ b/briar-core/src/net/sf/briar/db/DatabaseComponentImpl.java @@ -1,11 +1,14 @@ package net.sf.briar.db; import static java.util.logging.Level.WARNING; +import static net.sf.briar.api.Rating.GOOD; import static net.sf.briar.db.DatabaseConstants.BYTES_PER_SWEEP; import static net.sf.briar.db.DatabaseConstants.CRITICAL_FREE_SPACE; import static net.sf.briar.db.DatabaseConstants.MAX_BYTES_BETWEEN_SPACE_CHECKS; import static net.sf.briar.db.DatabaseConstants.MAX_MS_BETWEEN_SPACE_CHECKS; import static net.sf.briar.db.DatabaseConstants.MIN_FREE_SPACE; +import static net.sf.briar.db.Status.NEW; +import static net.sf.briar.db.Status.SEEN; import java.io.IOException; import java.util.ArrayList; @@ -274,11 +277,11 @@ DatabaseCleaner.Callback { boolean stored = db.addGroupMessage(txn, m); // Mark the message as seen by the sender MessageId id = m.getId(); - if(sender != null) db.setStatus(txn, sender, id, Status.SEEN); + if(sender != null) db.setStatus(txn, sender, id, SEEN); if(stored) { // Mark the message as unseen by other contacts for(ContactId c : db.getContacts(txn)) { - if(!c.equals(sender)) db.setStatus(txn, c, id, Status.NEW); + if(!c.equals(sender)) db.setStatus(txn, c, id, NEW); } // Calculate and store the message's sendability int sendability = calculateSendability(txn, m); @@ -301,7 +304,7 @@ DatabaseCleaner.Callback { int sendability = 0; // One point for a good rating AuthorId a = m.getAuthor(); - if(a != null && db.getRating(txn, a) == Rating.GOOD) sendability++; + if(a != null && db.getRating(txn, a) == GOOD) sendability++; // One point per sendable child (backward inclusion) sendability += db.getNumberOfSendableChildren(txn, m.getId()); return sendability; @@ -438,8 +441,8 @@ DatabaseCleaner.Callback { if(m.getAuthor() != null) throw new IllegalArgumentException(); if(!db.addPrivateMessage(txn, m, c)) return false; MessageId id = m.getId(); - if(incoming) db.setStatus(txn, c, id, Status.SEEN); - else db.setStatus(txn, c, id, Status.NEW); + if(incoming) db.setStatus(txn, c, id, SEEN); + else db.setStatus(txn, c, id, NEW); // Count the bytes stored synchronized(spaceLock) { bytesStoredSinceLastCheck += m.getSerialised().length; @@ -518,7 +521,7 @@ DatabaseCleaner.Callback { messageLock.readLock().unlock(); } if(messages.isEmpty()) return null; - // Record the message as sent + // Record the messages as sent messageLock.writeLock().lock(); try { T txn = db.startTransaction(); @@ -1553,9 +1556,9 @@ DatabaseCleaner.Callback { Rating old = db.setRating(txn, a, r); changed = (old != r); // Update the sendability of the author's messages - if(r == Rating.GOOD && old != Rating.GOOD) + if(r == GOOD && old != GOOD) updateAuthorSendability(txn, a, true); - else if(r != Rating.GOOD && old == Rating.GOOD) + else if(r != GOOD && old == GOOD) updateAuthorSendability(txn, a, false); db.commitTransaction(txn); } catch(DbException e) { diff --git a/briar-core/src/net/sf/briar/db/JdbcDatabase.java b/briar-core/src/net/sf/briar/db/JdbcDatabase.java index 9c08f814e4889152aacc008238712c88ed010a4a..c2494b3e9500d26921684d56d28905e7904db25c 100644 --- a/briar-core/src/net/sf/briar/db/JdbcDatabase.java +++ b/briar-core/src/net/sf/briar/db/JdbcDatabase.java @@ -3,7 +3,11 @@ package net.sf.briar.db; import static java.sql.Types.BINARY; import static java.util.logging.Level.INFO; import static java.util.logging.Level.WARNING; +import static net.sf.briar.api.Rating.UNRATED; import static net.sf.briar.db.DatabaseConstants.RETENTION_MODULUS; +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 java.io.File; import java.io.FileNotFoundException; @@ -649,9 +653,9 @@ abstract class JdbcDatabase implements Database<Connection> { String sql = "UPDATE statuses SET status = ?" + " WHERE messageId = ? AND contactId = ? AND status = ?"; ps = txn.prepareStatement(sql); - ps.setShort(1, (short) Status.SENT.ordinal()); + ps.setShort(1, (short) SENT.ordinal()); ps.setInt(3, c.getInt()); - ps.setShort(4, (short) Status.NEW.ordinal()); + ps.setShort(4, (short) NEW.ordinal()); for(MessageId m : sent) { ps.setBytes(2, m.getBytes()); ps.addBatch(); @@ -1227,7 +1231,7 @@ abstract class JdbcDatabase implements Database<Connection> { + " ORDER BY timestamp DESC LIMIT ?"; ps = txn.prepareStatement(sql); ps.setInt(1, c.getInt()); - ps.setShort(2, (short) Status.NEW.ordinal()); + ps.setShort(2, (short) NEW.ordinal()); ps.setInt(3, maxMessages); rs = ps.executeQuery(); List<MessageId> ids = new ArrayList<MessageId>(); @@ -1255,7 +1259,7 @@ abstract class JdbcDatabase implements Database<Connection> { + " ORDER BY timestamp DESC LIMIT ?"; ps = txn.prepareStatement(sql); ps.setInt(1, c.getInt()); - ps.setShort(2, (short) Status.NEW.ordinal()); + ps.setShort(2, (short) NEW.ordinal()); ps.setInt(3, maxMessages - ids.size()); rs = ps.executeQuery(); while(rs.next()) ids.add(new MessageId(rs.getBytes(2))); @@ -1341,7 +1345,7 @@ abstract class JdbcDatabase implements Database<Connection> { rs = ps.executeQuery(); Rating r; if(rs.next()) r = Rating.values()[rs.getByte(1)]; - else r = Rating.UNRATED; + else r = UNRATED; if(rs.next()) throw new DbStateException(); rs.close(); ps.close(); @@ -1391,7 +1395,7 @@ abstract class JdbcDatabase implements Database<Connection> { ps = txn.prepareStatement(sql); ps.setBytes(1, m.getBytes()); ps.setInt(2, c.getInt()); - ps.setShort(3, (short) Status.NEW.ordinal()); + ps.setShort(3, (short) NEW.ordinal()); rs = ps.executeQuery(); byte[] raw = null; if(rs.next()) { @@ -1423,7 +1427,7 @@ abstract class JdbcDatabase implements Database<Connection> { ps = txn.prepareStatement(sql); ps.setBytes(1, m.getBytes()); ps.setInt(2, c.getInt()); - ps.setShort(3, (short) Status.NEW.ordinal()); + ps.setShort(3, (short) NEW.ordinal()); rs = ps.executeQuery(); if(rs.next()) { int length = rs.getInt(1); @@ -1638,7 +1642,7 @@ abstract class JdbcDatabase implements Database<Connection> { + " ORDER BY timestamp DESC"; ps = txn.prepareStatement(sql); ps.setInt(1, c.getInt()); - ps.setShort(2, (short) Status.NEW.ordinal()); + ps.setShort(2, (short) NEW.ordinal()); rs = ps.executeQuery(); List<MessageId> ids = new ArrayList<MessageId>(); int total = 0; @@ -1670,7 +1674,7 @@ abstract class JdbcDatabase implements Database<Connection> { + " ORDER BY timestamp DESC"; ps = txn.prepareStatement(sql); ps.setInt(1, c.getInt()); - ps.setShort(2, (short) Status.NEW.ordinal()); + ps.setShort(2, (short) NEW.ordinal()); rs = ps.executeQuery(); while(rs.next()) { int length = rs.getInt(1); @@ -1995,7 +1999,7 @@ abstract class JdbcDatabase implements Database<Connection> { + " LIMIT ?"; ps = txn.prepareStatement(sql); ps.setInt(1, c.getInt()); - ps.setShort(2, (short) Status.NEW.ordinal()); + ps.setShort(2, (short) NEW.ordinal()); ps.setInt(3, 1); rs = ps.executeQuery(); boolean found = rs.next(); @@ -2022,7 +2026,7 @@ abstract class JdbcDatabase implements Database<Connection> { + " LIMIT ?"; ps = txn.prepareStatement(sql); ps.setInt(1, c.getInt()); - ps.setShort(2, (short) Status.NEW.ordinal()); + ps.setShort(2, (short) NEW.ordinal()); ps.setInt(3, 1); rs = ps.executeQuery(); found = rs.next(); @@ -2100,9 +2104,9 @@ abstract class JdbcDatabase implements Database<Connection> { String sql = "UPDATE statuses SET status = ?" + " WHERE messageId = ? AND contactId = ? AND status = ?"; ps = txn.prepareStatement(sql); - ps.setShort(1, (short) Status.SEEN.ordinal()); + ps.setShort(1, (short) SEEN.ordinal()); ps.setInt(3, c.getInt()); - ps.setShort(4, (short) Status.SENT.ordinal()); + ps.setShort(4, (short) SENT.ordinal()); for(MessageId m : acked) { ps.setBytes(2, m.getBytes()); ps.addBatch(); @@ -2417,7 +2421,7 @@ abstract class JdbcDatabase implements Database<Connection> { if(rs.next()) throw new DbStateException(); rs.close(); ps.close(); - if(!old.equals(r)) { + if(old != r) { sql = "UPDATE ratings SET rating = ? WHERE authorId = ?"; ps = txn.prepareStatement(sql); ps.setShort(1, (short) r.ordinal()); @@ -2430,8 +2434,8 @@ abstract class JdbcDatabase implements Database<Connection> { // No rating row exists - create one if necessary rs.close(); ps.close(); - old = Rating.UNRATED; - if(!old.equals(r)) { + old = UNRATED; + if(old != r) { sql = "INSERT INTO ratings (authorId, rating)" + " VALUES (?, ?)"; ps = txn.prepareStatement(sql); @@ -2625,7 +2629,7 @@ abstract class JdbcDatabase implements Database<Connection> { if(rs.next()) throw new DbStateException(); rs.close(); ps.close(); - if(!old.equals(Status.SEEN) && !old.equals(s)) { + if(old != SEEN && old != s) { sql = "UPDATE statuses SET status = ?" + " WHERE messageId = ? AND contactId = ?"; ps = txn.prepareStatement(sql); @@ -2685,7 +2689,7 @@ abstract class JdbcDatabase implements Database<Connection> { sql = "UPDATE statuses SET status = ?" + " WHERE messageId = ? AND contactId = ?"; ps = txn.prepareStatement(sql); - ps.setShort(1, (short) Status.SEEN.ordinal()); + ps.setShort(1, (short) SEEN.ordinal()); ps.setBytes(2, m.getBytes()); ps.setInt(3, c.getInt()); int affected = ps.executeUpdate(); diff --git a/briar-core/src/net/sf/briar/messaging/duplex/DuplexConnection.java b/briar-core/src/net/sf/briar/messaging/duplex/DuplexConnection.java index 0fb3760d5dab6b100df4a74cce3e0312609c2e40..6d4984cef79825081d124351f8ad41076c654548 100644 --- a/briar-core/src/net/sf/briar/messaging/duplex/DuplexConnection.java +++ b/briar-core/src/net/sf/briar/messaging/duplex/DuplexConnection.java @@ -2,6 +2,7 @@ package net.sf.briar.messaging.duplex; import static java.util.logging.Level.INFO; import static java.util.logging.Level.WARNING; +import static net.sf.briar.api.Rating.GOOD; import java.io.IOException; import java.io.InputStream; @@ -21,7 +22,6 @@ import java.util.logging.Logger; import net.sf.briar.api.ContactId; import net.sf.briar.api.FormatException; -import net.sf.briar.api.Rating; import net.sf.briar.api.db.DatabaseComponent; import net.sf.briar.api.db.DatabaseExecutor; import net.sf.briar.api.db.DbException; @@ -147,7 +147,7 @@ abstract class DuplexConnection implements DatabaseListener { dbExecutor.execute(new GenerateAcks()); } else if(e instanceof RatingChangedEvent) { RatingChangedEvent r = (RatingChangedEvent) e; - if(r.getRating() == Rating.GOOD && canSendOffer.getAndSet(false)) + if(r.getRating() == GOOD && canSendOffer.getAndSet(false)) dbExecutor.execute(new GenerateOffer()); } else if(e instanceof RemoteRetentionTimeUpdatedEvent) { dbExecutor.execute(new GenerateRetentionAck()); diff --git a/briar-tests/src/net/sf/briar/db/DatabaseComponentTest.java b/briar-tests/src/net/sf/briar/db/DatabaseComponentTest.java index c19e6670c5bb1eb1b3b0f8eeae5925f02ab98d08..d3b04d168ba7890768a6a78ce3c02a71a6672866 100644 --- a/briar-tests/src/net/sf/briar/db/DatabaseComponentTest.java +++ b/briar-tests/src/net/sf/briar/db/DatabaseComponentTest.java @@ -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))); }}); diff --git a/briar-tests/src/net/sf/briar/db/H2DatabaseTest.java b/briar-tests/src/net/sf/briar/db/H2DatabaseTest.java index 8be2af1b08a205f4475bf686718122ab240a3be8..15d2ec9b61d2051621bd9061575f6c10dbcac6a6 100644 --- a/briar-tests/src/net/sf/briar/db/H2DatabaseTest.java +++ b/briar-tests/src/net/sf/briar/db/H2DatabaseTest.java @@ -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));