mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +01:00
Retrieve all remote transports from the DB in a single call.
This commit is contained in:
@@ -115,9 +115,17 @@ public interface DatabaseComponent {
|
|||||||
/** Returns the IDs of all contacts. */
|
/** Returns the IDs of all contacts. */
|
||||||
Collection<ContactId> getContacts() throws DbException;
|
Collection<ContactId> getContacts() throws DbException;
|
||||||
|
|
||||||
|
/** Returns all local transport properties. */
|
||||||
|
Map<TransportId, Map<String, String>> getLocalTransports()
|
||||||
|
throws DbException;
|
||||||
|
|
||||||
/** Returns the user's rating for the given author. */
|
/** Returns the user's rating for the given author. */
|
||||||
Rating getRating(AuthorId a) throws DbException;
|
Rating getRating(AuthorId a) throws DbException;
|
||||||
|
|
||||||
|
/** Returns all remote transport properties. */
|
||||||
|
Map<TransportId, Map<ContactId, Map<String, String>>> getRemoteTransports()
|
||||||
|
throws DbException;
|
||||||
|
|
||||||
/** Returns the secret shared with the given contact. */
|
/** Returns the secret shared with the given contact. */
|
||||||
byte[] getSharedSecret(ContactId c) throws DbException;
|
byte[] getSharedSecret(ContactId c) throws DbException;
|
||||||
|
|
||||||
@@ -127,13 +135,6 @@ public interface DatabaseComponent {
|
|||||||
/** Returns the configuration for the given transport. */
|
/** Returns the configuration for the given transport. */
|
||||||
Map<String, String> getTransportConfig(TransportId t) throws DbException;
|
Map<String, String> getTransportConfig(TransportId t) throws DbException;
|
||||||
|
|
||||||
/** Returns all local transport properties. */
|
|
||||||
Map<TransportId, Map<String, String>> getTransports() throws DbException;
|
|
||||||
|
|
||||||
/** Returns all transport properties for the given contact. */
|
|
||||||
Map<TransportId, Map<String, String>> getTransports(ContactId c)
|
|
||||||
throws DbException;
|
|
||||||
|
|
||||||
/** Returns the contacts to which the given group is visible. */
|
/** Returns the contacts to which the given group is visible. */
|
||||||
Collection<ContactId> getVisibility(GroupId g) throws DbException;
|
Collection<ContactId> getVisibility(GroupId g) throws DbException;
|
||||||
|
|
||||||
|
|||||||
@@ -211,6 +211,14 @@ interface Database<T> {
|
|||||||
*/
|
*/
|
||||||
MessageId getGroupMessageParent(T txn, MessageId m) throws DbException;
|
MessageId getGroupMessageParent(T txn, MessageId m) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all local transport properties.
|
||||||
|
* <p>
|
||||||
|
* Locking: transports read.
|
||||||
|
*/
|
||||||
|
Map<TransportId, Map<String, String>> getLocalTransports(T txn)
|
||||||
|
throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the IDs of any batches sent to the given contact that should now
|
* Returns the IDs of any batches sent to the given contact that should now
|
||||||
* be considered lost.
|
* be considered lost.
|
||||||
@@ -269,6 +277,14 @@ interface Database<T> {
|
|||||||
*/
|
*/
|
||||||
Rating getRating(T txn, AuthorId a) throws DbException;
|
Rating getRating(T txn, AuthorId a) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all remote transport properties.
|
||||||
|
* <p>
|
||||||
|
* Locking: contacts read, transports read.
|
||||||
|
*/
|
||||||
|
Map<TransportId, Map<ContactId, Map<String, String>>>
|
||||||
|
getRemoteTransports(T txn) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the sendability score of the given group message.
|
* Returns the sendability score of the given group message.
|
||||||
* <p>
|
* <p>
|
||||||
@@ -325,22 +341,6 @@ interface Database<T> {
|
|||||||
Map<String, String> getTransportConfig(T txn, TransportId t)
|
Map<String, String> getTransportConfig(T txn, TransportId t)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all local transport properties.
|
|
||||||
* <p>
|
|
||||||
* Locking: transports read.
|
|
||||||
*/
|
|
||||||
Map<TransportId, Map<String, String>> getTransports(T txn)
|
|
||||||
throws DbException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all transport properties for the given contact.
|
|
||||||
* <p>
|
|
||||||
* Locking: contacts read, transports read.
|
|
||||||
*/
|
|
||||||
Map<TransportId, Map<String, String>> getTransports(T txn, ContactId c)
|
|
||||||
throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the contacts to which the given group is visible.
|
* Returns the contacts to which the given group is visible.
|
||||||
* <p>
|
* <p>
|
||||||
|
|||||||
@@ -615,7 +615,7 @@ DatabaseCleaner.Callback {
|
|||||||
try {
|
try {
|
||||||
T txn = db.startTransaction();
|
T txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
transports = db.getTransports(txn);
|
transports = db.getLocalTransports(txn);
|
||||||
timestamp = System.currentTimeMillis();
|
timestamp = System.currentTimeMillis();
|
||||||
db.setTransportTimestamp(txn, c, timestamp);
|
db.setTransportTimestamp(txn, c, timestamp);
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
@@ -699,6 +699,25 @@ DatabaseCleaner.Callback {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<TransportId, Map<String, String>> getLocalTransports()
|
||||||
|
throws DbException {
|
||||||
|
transportLock.readLock().lock();
|
||||||
|
try {
|
||||||
|
T txn = db.startTransaction();
|
||||||
|
try {
|
||||||
|
Map<TransportId, Map<String, String>> transports =
|
||||||
|
db.getLocalTransports(txn);
|
||||||
|
db.commitTransaction(txn);
|
||||||
|
return transports;
|
||||||
|
} catch(DbException e) {
|
||||||
|
db.abortTransaction(txn);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
transportLock.readLock().unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Rating getRating(AuthorId a) throws DbException {
|
public Rating getRating(AuthorId a) throws DbException {
|
||||||
ratingLock.readLock().lock();
|
ratingLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
@@ -716,6 +735,30 @@ DatabaseCleaner.Callback {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<TransportId, Map<ContactId, Map<String, String>>>
|
||||||
|
getRemoteTransports() throws DbException {
|
||||||
|
contactLock.readLock().lock();
|
||||||
|
try {
|
||||||
|
transportLock.readLock().lock();
|
||||||
|
try {
|
||||||
|
T txn = db.startTransaction();
|
||||||
|
try {
|
||||||
|
Map<TransportId, Map<ContactId, Map<String, String>>>
|
||||||
|
transports = db.getRemoteTransports(txn);
|
||||||
|
db.commitTransaction(txn);
|
||||||
|
return transports;
|
||||||
|
} catch(DbException e) {
|
||||||
|
db.abortTransaction(txn);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
transportLock.readLock().unlock();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
contactLock.readLock().unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public byte[] getSharedSecret(ContactId c) throws DbException {
|
public byte[] getSharedSecret(ContactId c) throws DbException {
|
||||||
contactLock.readLock().lock();
|
contactLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
@@ -769,50 +812,6 @@ DatabaseCleaner.Callback {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<TransportId, Map<String, String>> getTransports()
|
|
||||||
throws DbException {
|
|
||||||
transportLock.readLock().lock();
|
|
||||||
try {
|
|
||||||
T txn = db.startTransaction();
|
|
||||||
try {
|
|
||||||
Map<TransportId, Map<String, String>> transports =
|
|
||||||
db.getTransports(txn);
|
|
||||||
db.commitTransaction(txn);
|
|
||||||
return transports;
|
|
||||||
} catch(DbException e) {
|
|
||||||
db.abortTransaction(txn);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
transportLock.readLock().unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<TransportId, Map<String, String>> getTransports(ContactId c)
|
|
||||||
throws DbException {
|
|
||||||
contactLock.readLock().lock();
|
|
||||||
try {
|
|
||||||
if(!containsContact(c)) throw new NoSuchContactException();
|
|
||||||
transportLock.readLock().lock();
|
|
||||||
try {
|
|
||||||
T txn = db.startTransaction();
|
|
||||||
try {
|
|
||||||
Map<TransportId, Map<String, String>> transports =
|
|
||||||
db.getTransports(txn, c);
|
|
||||||
db.commitTransaction(txn);
|
|
||||||
return transports;
|
|
||||||
} catch(DbException e) {
|
|
||||||
db.abortTransaction(txn);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
transportLock.readLock().unlock();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
contactLock.readLock().unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Collection<ContactId> getVisibility(GroupId g) throws DbException {
|
public Collection<ContactId> getVisibility(GroupId g) throws DbException {
|
||||||
contactLock.readLock().lock();
|
contactLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
@@ -1252,7 +1251,7 @@ DatabaseCleaner.Callback {
|
|||||||
T txn = db.startTransaction();
|
T txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
Map<TransportId, Map<String, String>> transports =
|
Map<TransportId, Map<String, String>> transports =
|
||||||
db.getTransports(txn);
|
db.getLocalTransports(txn);
|
||||||
Map<String, String> old = transports.get(t);
|
Map<String, String> old = transports.get(t);
|
||||||
if(!properties.equals(old)) {
|
if(!properties.equals(old)) {
|
||||||
db.setTransportProperties(txn, t, properties);
|
db.setTransportProperties(txn, t, properties);
|
||||||
|
|||||||
@@ -916,6 +916,37 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
} else return f.length();
|
} else return f.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<TransportId, Map<String, String>> getLocalTransports(
|
||||||
|
Connection txn) throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
String sql = "SELECT transportId, key, value FROM transports"
|
||||||
|
+ " ORDER BY transportId";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
rs = ps.executeQuery();
|
||||||
|
Map<TransportId, Map<String, String>> transports =
|
||||||
|
new TreeMap<TransportId, Map<String, String>>();
|
||||||
|
Map<String, String> properties = null;
|
||||||
|
TransportId lastId = null;
|
||||||
|
while(rs.next()) {
|
||||||
|
TransportId id = new TransportId(rs.getInt(1));
|
||||||
|
if(!id.equals(lastId)) {
|
||||||
|
properties = new TreeMap<String, String>();
|
||||||
|
transports.put(id, properties);
|
||||||
|
}
|
||||||
|
properties.put(rs.getString(2), rs.getString(3));
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
return transports;
|
||||||
|
} catch(SQLException e) {
|
||||||
|
tryToClose(rs);
|
||||||
|
tryToClose(ps);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Collection<BatchId> getLostBatches(Connection txn, ContactId c)
|
public Collection<BatchId> getLostBatches(Connection txn, ContactId c)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
@@ -1181,6 +1212,46 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<TransportId, Map<ContactId, Map<String, String>>>
|
||||||
|
getRemoteTransports(Connection txn) throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
String sql = "SELECT transportId, contactId, key, value"
|
||||||
|
+ " FROM contactTransports"
|
||||||
|
+ " ORDER BY transportId";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
rs = ps.executeQuery();
|
||||||
|
Map<TransportId, Map<ContactId, Map<String, String>>> transports =
|
||||||
|
new TreeMap<TransportId, Map<ContactId, Map<String, String>>>();
|
||||||
|
Map<ContactId, Map<String, String>> contacts = null;
|
||||||
|
Map<String, String> properties = null;
|
||||||
|
TransportId lastTransportId = null;
|
||||||
|
ContactId lastContactId = null;
|
||||||
|
while(rs.next()) {
|
||||||
|
TransportId transportId = new TransportId(rs.getInt(1));
|
||||||
|
if(!transportId.equals(lastTransportId)) {
|
||||||
|
contacts = new HashMap<ContactId, Map<String, String>>();
|
||||||
|
transports.put(transportId, contacts);
|
||||||
|
lastContactId = null;
|
||||||
|
}
|
||||||
|
ContactId contactId = new ContactId(rs.getInt(2));
|
||||||
|
if(!contactId.equals(lastContactId)) {
|
||||||
|
properties = new TreeMap<String, String>();
|
||||||
|
contacts.put(contactId, properties);
|
||||||
|
}
|
||||||
|
properties.put(rs.getString(3), rs.getString(4));
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
return transports;
|
||||||
|
} catch(SQLException e) {
|
||||||
|
tryToClose(rs);
|
||||||
|
tryToClose(ps);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public byte[] getSharedSecret(Connection txn, ContactId c)
|
public byte[] getSharedSecret(Connection txn, ContactId c)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
@@ -1418,70 +1489,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<TransportId, Map<String, String>> getTransports(Connection txn)
|
|
||||||
throws DbException {
|
|
||||||
PreparedStatement ps = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
try {
|
|
||||||
String sql = "SELECT transportId, key, value FROM transports"
|
|
||||||
+ " ORDER BY transportId";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
rs = ps.executeQuery();
|
|
||||||
Map<TransportId, Map<String, String>> transports =
|
|
||||||
new TreeMap<TransportId, Map<String, String>>();
|
|
||||||
Map<String, String> properties = null;
|
|
||||||
TransportId lastId = null;
|
|
||||||
while(rs.next()) {
|
|
||||||
TransportId id = new TransportId(rs.getInt(1));
|
|
||||||
if(!id.equals(lastId)) {
|
|
||||||
properties = new TreeMap<String, String>();
|
|
||||||
transports.put(id, properties);
|
|
||||||
}
|
|
||||||
properties.put(rs.getString(2), rs.getString(3));
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
return transports;
|
|
||||||
} catch(SQLException e) {
|
|
||||||
tryToClose(rs);
|
|
||||||
tryToClose(ps);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<TransportId, Map<String, String>> getTransports(Connection txn,
|
|
||||||
ContactId c) throws DbException {
|
|
||||||
PreparedStatement ps = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
try {
|
|
||||||
String sql = "SELECT transportId, key, value FROM contactTransports"
|
|
||||||
+ " WHERE contactId = ?"
|
|
||||||
+ " ORDER BY transportId";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setInt(1, c.getInt());
|
|
||||||
rs = ps.executeQuery();
|
|
||||||
Map<TransportId, Map<String, String>> transports =
|
|
||||||
new TreeMap<TransportId, Map<String, String>>();
|
|
||||||
Map<String, String> properties = null;
|
|
||||||
TransportId lastId = null;
|
|
||||||
while(rs.next()) {
|
|
||||||
TransportId id = new TransportId(rs.getInt(1));
|
|
||||||
if(!id.equals(lastId)) {
|
|
||||||
properties = new TreeMap<String, String>();
|
|
||||||
transports.put(id, properties);
|
|
||||||
}
|
|
||||||
properties.put(rs.getString(2), rs.getString(3));
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
return transports;
|
|
||||||
} catch(SQLException e) {
|
|
||||||
tryToClose(rs);
|
|
||||||
tryToClose(ps);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Collection<ContactId> getVisibility(Connection txn, GroupId g)
|
public Collection<ContactId> getVisibility(Connection txn, GroupId g)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ class InvitationWorker implements Runnable {
|
|||||||
// FIXME: Create a real invitation
|
// FIXME: Create a real invitation
|
||||||
Map<TransportId, Map<String, String>> transports;
|
Map<TransportId, Map<String, String>> transports;
|
||||||
try {
|
try {
|
||||||
transports = db.getTransports();
|
transports = db.getLocalTransports();
|
||||||
} catch(DbException e) {
|
} catch(DbException e) {
|
||||||
throw new IOException(e.getMessage());
|
throw new IOException(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,6 +55,8 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
private final Group group;
|
private final Group group;
|
||||||
private final TransportId transportId;
|
private final TransportId transportId;
|
||||||
private final Map<TransportId, Map<String, String>> transports;
|
private final Map<TransportId, Map<String, String>> transports;
|
||||||
|
private final Map<TransportId, Map<ContactId, Map<String, String>>>
|
||||||
|
remoteTransports;
|
||||||
private final byte[] secret;
|
private final byte[] secret;
|
||||||
|
|
||||||
public DatabaseComponentTest() {
|
public DatabaseComponentTest() {
|
||||||
@@ -74,8 +76,10 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
new TestMessage(messageId, null, null, null, timestamp, raw);
|
new TestMessage(messageId, null, null, null, timestamp, raw);
|
||||||
group = new TestGroup(groupId, "The really exciting group", null);
|
group = new TestGroup(groupId, "The really exciting group", null);
|
||||||
transportId = new TransportId(123);
|
transportId = new TransportId(123);
|
||||||
transports = Collections.singletonMap(transportId,
|
Map<String, String> properties = Collections.singletonMap("foo", "bar");
|
||||||
Collections.singletonMap("bar", "baz"));
|
transports = Collections.singletonMap(transportId, properties);
|
||||||
|
remoteTransports = Collections.singletonMap(transportId,
|
||||||
|
Collections.singletonMap(contactId, properties));
|
||||||
secret = new byte[123];
|
secret = new byte[123];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,11 +125,9 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
oneOf(database).getSharedSecret(txn, contactId);
|
oneOf(database).getSharedSecret(txn, contactId);
|
||||||
will(returnValue(secret));
|
will(returnValue(secret));
|
||||||
// getTransports(contactId)
|
// getRemoteTransports()
|
||||||
oneOf(database).containsContact(txn, contactId);
|
oneOf(database).getRemoteTransports(txn);
|
||||||
will(returnValue(true));
|
will(returnValue(remoteTransports));
|
||||||
oneOf(database).getTransports(txn, contactId);
|
|
||||||
will(returnValue(transports));
|
|
||||||
// subscribe(group)
|
// subscribe(group)
|
||||||
oneOf(group).getId();
|
oneOf(group).getId();
|
||||||
will(returnValue(groupId));
|
will(returnValue(groupId));
|
||||||
@@ -171,7 +173,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
assertEquals(connectionWindow,
|
assertEquals(connectionWindow,
|
||||||
db.getConnectionWindow(contactId, transportId));
|
db.getConnectionWindow(contactId, transportId));
|
||||||
assertEquals(secret, db.getSharedSecret(contactId));
|
assertEquals(secret, db.getSharedSecret(contactId));
|
||||||
assertEquals(transports, db.getTransports(contactId));
|
assertEquals(remoteTransports, db.getRemoteTransports());
|
||||||
db.subscribe(group); // First time - check listeners are called
|
db.subscribe(group); // First time - check listeners are called
|
||||||
db.subscribe(group); // Second time - check listeners aren't called
|
db.subscribe(group); // Second time - check listeners aren't called
|
||||||
assertEquals(Collections.singletonList(groupId), db.getSubscriptions());
|
assertEquals(Collections.singletonList(groupId), db.getSubscriptions());
|
||||||
@@ -468,11 +470,11 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
context.mock(TransportUpdate.class);
|
context.mock(TransportUpdate.class);
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
// Check whether the contact is still in the DB (which it's not)
|
// Check whether the contact is still in the DB (which it's not)
|
||||||
exactly(18).of(database).startTransaction();
|
exactly(17).of(database).startTransaction();
|
||||||
will(returnValue(txn));
|
will(returnValue(txn));
|
||||||
exactly(18).of(database).containsContact(txn, contactId);
|
exactly(17).of(database).containsContact(txn, contactId);
|
||||||
will(returnValue(false));
|
will(returnValue(false));
|
||||||
exactly(18).of(database).commitTransaction(txn);
|
exactly(17).of(database).commitTransaction(txn);
|
||||||
}});
|
}});
|
||||||
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
||||||
|
|
||||||
@@ -522,11 +524,6 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
fail();
|
fail();
|
||||||
} catch(NoSuchContactException expected) {}
|
} catch(NoSuchContactException expected) {}
|
||||||
|
|
||||||
try {
|
|
||||||
db.getTransports(contactId);
|
|
||||||
fail();
|
|
||||||
} catch(NoSuchContactException expected) {}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
db.hasSendableMessages(contactId);
|
db.hasSendableMessages(contactId);
|
||||||
fail();
|
fail();
|
||||||
@@ -789,7 +786,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
allowing(database).containsContact(txn, contactId);
|
allowing(database).containsContact(txn, contactId);
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
// Get the local transport properties
|
// Get the local transport properties
|
||||||
oneOf(database).getTransports(txn);
|
oneOf(database).getLocalTransports(txn);
|
||||||
will(returnValue(transports));
|
will(returnValue(transports));
|
||||||
oneOf(database).setTransportTimestamp(with(txn), with(contactId),
|
oneOf(database).setTransportTimestamp(with(txn), with(contactId),
|
||||||
with(any(long.class)));
|
with(any(long.class)));
|
||||||
@@ -1286,7 +1283,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(database).startTransaction();
|
oneOf(database).startTransaction();
|
||||||
will(returnValue(txn));
|
will(returnValue(txn));
|
||||||
oneOf(database).getTransports(txn);
|
oneOf(database).getLocalTransports(txn);
|
||||||
will(returnValue(Collections.singletonMap(transportId,
|
will(returnValue(Collections.singletonMap(transportId,
|
||||||
properties)));
|
properties)));
|
||||||
oneOf(database).setTransportProperties(txn, transportId,
|
oneOf(database).setTransportProperties(txn, transportId,
|
||||||
@@ -1315,7 +1312,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(database).startTransaction();
|
oneOf(database).startTransaction();
|
||||||
will(returnValue(txn));
|
will(returnValue(txn));
|
||||||
oneOf(database).getTransports(txn);
|
oneOf(database).getLocalTransports(txn);
|
||||||
will(returnValue(Collections.singletonMap(transportId,
|
will(returnValue(Collections.singletonMap(transportId,
|
||||||
properties)));
|
properties)));
|
||||||
oneOf(database).commitTransaction(txn);
|
oneOf(database).commitTransaction(txn);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import java.util.Collections;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
@@ -71,6 +72,8 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
private final Group group;
|
private final Group group;
|
||||||
private final TransportId transportId;
|
private final TransportId transportId;
|
||||||
private final Map<TransportId, Map<String, String>> transports;
|
private final Map<TransportId, Map<String, String>> transports;
|
||||||
|
private final Map<TransportId, Map<ContactId, Map<String, String>>>
|
||||||
|
remoteTransports;
|
||||||
private final Map<Group, Long> subscriptions;
|
private final Map<Group, Long> subscriptions;
|
||||||
private final byte[] secret;
|
private final byte[] secret;
|
||||||
|
|
||||||
@@ -97,8 +100,10 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
new TestMessage(privateMessageId, null, null, null, timestamp, raw);
|
new TestMessage(privateMessageId, null, null, null, timestamp, raw);
|
||||||
group = groupFactory.createGroup(groupId, "Group name", null);
|
group = groupFactory.createGroup(groupId, "Group name", null);
|
||||||
transportId = new TransportId(0);
|
transportId = new TransportId(0);
|
||||||
transports = Collections.singletonMap(transportId,
|
Map<String, String> properties = Collections.singletonMap("foo", "bar");
|
||||||
Collections.singletonMap("bar", "baz"));
|
transports = Collections.singletonMap(transportId, properties);
|
||||||
|
remoteTransports = Collections.singletonMap(transportId,
|
||||||
|
Collections.singletonMap(contactId, properties));
|
||||||
subscriptions = Collections.singletonMap(group, 0L);
|
subscriptions = Collections.singletonMap(group, 0L);
|
||||||
secret = new byte[123];
|
secret = new byte[123];
|
||||||
}
|
}
|
||||||
@@ -132,7 +137,7 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
db = open(true);
|
db = open(true);
|
||||||
txn = db.startTransaction();
|
txn = db.startTransaction();
|
||||||
assertTrue(db.containsContact(txn, contactId));
|
assertTrue(db.containsContact(txn, contactId));
|
||||||
assertEquals(transports, db.getTransports(txn, contactId));
|
assertEquals(remoteTransports, db.getRemoteTransports(txn));
|
||||||
assertTrue(db.containsSubscription(txn, groupId));
|
assertTrue(db.containsSubscription(txn, groupId));
|
||||||
assertTrue(db.containsMessage(txn, messageId));
|
assertTrue(db.containsMessage(txn, messageId));
|
||||||
byte[] raw1 = db.getMessage(txn, messageId);
|
byte[] raw1 = db.getMessage(txn, messageId);
|
||||||
@@ -152,7 +157,7 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
db = open(true);
|
db = open(true);
|
||||||
txn = db.startTransaction();
|
txn = db.startTransaction();
|
||||||
assertFalse(db.containsContact(txn, contactId));
|
assertFalse(db.containsContact(txn, contactId));
|
||||||
assertEquals(Collections.emptyMap(), db.getTransports(txn, contactId));
|
assertEquals(Collections.emptyMap(), db.getRemoteTransports(txn));
|
||||||
assertFalse(db.containsSubscription(txn, groupId));
|
assertFalse(db.containsSubscription(txn, groupId));
|
||||||
assertFalse(db.containsMessage(txn, messageId));
|
assertFalse(db.containsMessage(txn, messageId));
|
||||||
assertFalse(db.containsMessage(txn, privateMessageId));
|
assertFalse(db.containsMessage(txn, privateMessageId));
|
||||||
@@ -983,40 +988,49 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateTransportProperties() throws Exception {
|
public void testUpdateTransportProperties() throws Exception {
|
||||||
|
Map<String, String> properties = Collections.singletonMap("foo", "bar");
|
||||||
|
Map<String, String> properties1 =
|
||||||
|
Collections.singletonMap("baz", "bam");
|
||||||
|
|
||||||
Database<Connection> db = open(false);
|
Database<Connection> db = open(false);
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact with some transport properties
|
// Add a contact with some transport properties
|
||||||
assertEquals(contactId, db.addContact(txn, transports, secret));
|
assertEquals(contactId, db.addContact(txn, transports, secret));
|
||||||
assertEquals(transports, db.getTransports(txn, contactId));
|
assertEquals(remoteTransports, db.getRemoteTransports(txn));
|
||||||
|
|
||||||
// Replace the transport properties
|
// Replace the transport properties
|
||||||
TransportId transportId1 = new TransportId(1);
|
TransportId transportId1 = new TransportId(1);
|
||||||
Map<TransportId, Map<String, String>> transports1 =
|
Map<TransportId, Map<String, String>> transports1 =
|
||||||
new TreeMap<TransportId, Map<String, String>>();
|
new TreeMap<TransportId, Map<String, String>>();
|
||||||
transports1.put(transportId, Collections.singletonMap("bar", "baz"));
|
transports1.put(transportId, properties);
|
||||||
transports1.put(transportId1, Collections.singletonMap("baz", "quux"));
|
transports1.put(transportId1, properties1);
|
||||||
|
Map<TransportId, Map<ContactId, Map<String, String>>> remoteTransports1
|
||||||
|
= new TreeMap<TransportId, Map<ContactId, Map<String, String>>>();
|
||||||
|
remoteTransports1.put(transportId, Collections.singletonMap(contactId,
|
||||||
|
properties));
|
||||||
|
remoteTransports1.put(transportId1, Collections.singletonMap(contactId,
|
||||||
|
properties1));
|
||||||
db.setTransports(txn, contactId, transports1, 1);
|
db.setTransports(txn, contactId, transports1, 1);
|
||||||
assertEquals(transports1, db.getTransports(txn, contactId));
|
assertEquals(remoteTransports1, db.getRemoteTransports(txn));
|
||||||
|
|
||||||
// Remove the transport properties
|
// Remove the transport properties
|
||||||
db.setTransports(txn, contactId,
|
db.setTransports(txn, contactId,
|
||||||
Collections.<TransportId, Map<String, String>>emptyMap(), 2);
|
Collections.<TransportId, Map<String, String>>emptyMap(), 2);
|
||||||
assertEquals(Collections.emptyMap(), db.getTransports(txn, contactId));
|
assertEquals(Collections.emptyMap(), db.getRemoteTransports(txn));
|
||||||
|
|
||||||
// Set the local transport properties
|
// Set the local transport properties
|
||||||
for(TransportId t : transports.keySet()) {
|
for(Entry<TransportId, Map<String, String>> e : transports.entrySet()) {
|
||||||
Map<String, String> properties = transports.get(t);
|
db.setTransportProperties(txn, e.getKey(), e.getValue());
|
||||||
db.setTransportProperties(txn, transportId, properties);
|
|
||||||
}
|
}
|
||||||
assertEquals(transports, db.getTransports(txn));
|
assertEquals(transports, db.getLocalTransports(txn));
|
||||||
|
|
||||||
// Remove the local transport properties
|
// Remove the local transport properties
|
||||||
for(TransportId t : transports.keySet()) {
|
for(TransportId t : transports.keySet()) {
|
||||||
db.setTransportProperties(txn, t,
|
db.setTransportProperties(txn, t,
|
||||||
Collections.<String, String>emptyMap());
|
Collections.<String, String>emptyMap());
|
||||||
}
|
}
|
||||||
assertEquals(Collections.emptyMap(), db.getTransports(txn));
|
assertEquals(Collections.emptyMap(), db.getLocalTransports(txn));
|
||||||
|
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
db.close();
|
db.close();
|
||||||
@@ -1025,8 +1039,9 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateTransportConfig() throws Exception {
|
public void testUpdateTransportConfig() throws Exception {
|
||||||
Map<String, String> config = Collections.singletonMap("bar", "baz");
|
Map<String, String> config = Collections.singletonMap("foo", "bar");
|
||||||
Map<String, String> config1 = Collections.singletonMap("baz", "bam");
|
Map<String, String> config1 = Collections.singletonMap("baz", "bam");
|
||||||
|
|
||||||
Database<Connection> db = open(false);
|
Database<Connection> db = open(false);
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
@@ -1050,32 +1065,44 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTransportsNotUpdatedIfTimestampIsOld() throws Exception {
|
public void testTransportsNotUpdatedIfTimestampIsOld() throws Exception {
|
||||||
|
Map<String, String> properties = Collections.singletonMap("foo", "bar");
|
||||||
|
Map<String, String> properties1 =
|
||||||
|
Collections.singletonMap("baz", "bam");
|
||||||
|
Map<String, String> properties2 =
|
||||||
|
Collections.singletonMap("quux", "etc");
|
||||||
|
|
||||||
Database<Connection> db = open(false);
|
Database<Connection> db = open(false);
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact with some transport properties
|
// Add a contact with some transport properties
|
||||||
assertEquals(contactId, db.addContact(txn, transports, secret));
|
assertEquals(contactId, db.addContact(txn, transports, secret));
|
||||||
assertEquals(transports, db.getTransports(txn, contactId));
|
assertEquals(remoteTransports, db.getRemoteTransports(txn));
|
||||||
|
|
||||||
// Replace the transport properties using a timestamp of 2
|
// Replace the transport properties using a timestamp of 2
|
||||||
TransportId transportId1 = new TransportId(1);
|
TransportId transportId1 = new TransportId(1);
|
||||||
Map<TransportId, Map<String, String>> transports1 =
|
Map<TransportId, Map<String, String>> transports1 =
|
||||||
new TreeMap<TransportId, Map<String, String>>();
|
new TreeMap<TransportId, Map<String, String>>();
|
||||||
transports1.put(transportId, Collections.singletonMap("bar", "baz"));
|
transports1.put(transportId, properties);
|
||||||
transports1.put(transportId1, Collections.singletonMap("baz", "quux"));
|
transports1.put(transportId1, properties1);
|
||||||
|
Map<TransportId, Map<ContactId, Map<String, String>>> remoteTransports1
|
||||||
|
= new TreeMap<TransportId, Map<ContactId, Map<String, String>>>();
|
||||||
|
remoteTransports1.put(transportId, Collections.singletonMap(contactId,
|
||||||
|
properties));
|
||||||
|
remoteTransports1.put(transportId1, Collections.singletonMap(contactId,
|
||||||
|
properties1));
|
||||||
db.setTransports(txn, contactId, transports1, 2);
|
db.setTransports(txn, contactId, transports1, 2);
|
||||||
assertEquals(transports1, db.getTransports(txn, contactId));
|
assertEquals(remoteTransports1, db.getRemoteTransports(txn));
|
||||||
|
|
||||||
// Try to replace the transport properties using a timestamp of 1
|
// Try to replace the transport properties using a timestamp of 1
|
||||||
TransportId transportId2 = new TransportId(2);
|
TransportId transportId2 = new TransportId(2);
|
||||||
Map<TransportId, Map<String, String>> transports2 =
|
Map<TransportId, Map<String, String>> transports2 =
|
||||||
new TreeMap<TransportId, Map<String, String>>();
|
new TreeMap<TransportId, Map<String, String>>();
|
||||||
transports2.put(transportId1, Collections.singletonMap("baz", "quux"));
|
transports2.put(transportId1, properties1);
|
||||||
transports2.put(transportId2, Collections.singletonMap("quux", "etc"));
|
transports2.put(transportId2, properties2);
|
||||||
db.setTransports(txn, contactId, transports2, 1);
|
db.setTransports(txn, contactId, transports2, 1);
|
||||||
|
|
||||||
// The old properties should still be there
|
// The old properties should still be there
|
||||||
assertEquals(transports1, db.getTransports(txn, contactId));
|
assertEquals(remoteTransports1, db.getRemoteTransports(txn));
|
||||||
|
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
db.close();
|
db.close();
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ public class InvitationWorkerTest extends TestCase {
|
|||||||
oneOf(params).getPassword();
|
oneOf(params).getPassword();
|
||||||
will(returnValue(new char[] {'x', 'y', 'z', 'z', 'y'}));
|
will(returnValue(new char[] {'x', 'y', 'z', 'z', 'y'}));
|
||||||
oneOf(callback).encryptingFile(invitation);
|
oneOf(callback).encryptingFile(invitation);
|
||||||
oneOf(database).getTransports();
|
oneOf(database).getLocalTransports();
|
||||||
will(returnValue(transports));
|
will(returnValue(transports));
|
||||||
oneOf(writerFactory).createWriter(with(any(OutputStream.class)));
|
oneOf(writerFactory).createWriter(with(any(OutputStream.class)));
|
||||||
will(returnValue(writer));
|
will(returnValue(writer));
|
||||||
|
|||||||
Reference in New Issue
Block a user