Retrieve all remote transports from the DB in a single call.

This commit is contained in:
akwizgran
2011-10-10 22:35:46 +01:00
parent 4059fbf863
commit 68b82ae826
8 changed files with 207 additions and 176 deletions

View File

@@ -55,6 +55,8 @@ public abstract class DatabaseComponentTest extends TestCase {
private final Group group;
private final TransportId transportId;
private final Map<TransportId, Map<String, String>> transports;
private final Map<TransportId, Map<ContactId, Map<String, String>>>
remoteTransports;
private final byte[] secret;
public DatabaseComponentTest() {
@@ -74,8 +76,10 @@ public abstract class DatabaseComponentTest extends TestCase {
new TestMessage(messageId, null, null, null, timestamp, raw);
group = new TestGroup(groupId, "The really exciting group", null);
transportId = new TransportId(123);
transports = Collections.singletonMap(transportId,
Collections.singletonMap("bar", "baz"));
Map<String, String> properties = Collections.singletonMap("foo", "bar");
transports = Collections.singletonMap(transportId, properties);
remoteTransports = Collections.singletonMap(transportId,
Collections.singletonMap(contactId, properties));
secret = new byte[123];
}
@@ -121,11 +125,9 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(true));
oneOf(database).getSharedSecret(txn, contactId);
will(returnValue(secret));
// getTransports(contactId)
oneOf(database).containsContact(txn, contactId);
will(returnValue(true));
oneOf(database).getTransports(txn, contactId);
will(returnValue(transports));
// getRemoteTransports()
oneOf(database).getRemoteTransports(txn);
will(returnValue(remoteTransports));
// subscribe(group)
oneOf(group).getId();
will(returnValue(groupId));
@@ -171,7 +173,7 @@ public abstract class DatabaseComponentTest extends TestCase {
assertEquals(connectionWindow,
db.getConnectionWindow(contactId, transportId));
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); // Second time - check listeners aren't called
assertEquals(Collections.singletonList(groupId), db.getSubscriptions());
@@ -468,11 +470,11 @@ public abstract class DatabaseComponentTest extends TestCase {
context.mock(TransportUpdate.class);
context.checking(new Expectations() {{
// 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));
exactly(18).of(database).containsContact(txn, contactId);
exactly(17).of(database).containsContact(txn, contactId);
will(returnValue(false));
exactly(18).of(database).commitTransaction(txn);
exactly(17).of(database).commitTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
@@ -522,11 +524,6 @@ public abstract class DatabaseComponentTest extends TestCase {
fail();
} catch(NoSuchContactException expected) {}
try {
db.getTransports(contactId);
fail();
} catch(NoSuchContactException expected) {}
try {
db.hasSendableMessages(contactId);
fail();
@@ -789,7 +786,7 @@ public abstract class DatabaseComponentTest extends TestCase {
allowing(database).containsContact(txn, contactId);
will(returnValue(true));
// Get the local transport properties
oneOf(database).getTransports(txn);
oneOf(database).getLocalTransports(txn);
will(returnValue(transports));
oneOf(database).setTransportTimestamp(with(txn), with(contactId),
with(any(long.class)));
@@ -1286,7 +1283,7 @@ public abstract class DatabaseComponentTest extends TestCase {
context.checking(new Expectations() {{
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).getTransports(txn);
oneOf(database).getLocalTransports(txn);
will(returnValue(Collections.singletonMap(transportId,
properties)));
oneOf(database).setTransportProperties(txn, transportId,
@@ -1315,7 +1312,7 @@ public abstract class DatabaseComponentTest extends TestCase {
context.checking(new Expectations() {{
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).getTransports(txn);
oneOf(database).getLocalTransports(txn);
will(returnValue(Collections.singletonMap(transportId,
properties)));
oneOf(database).commitTransaction(txn);

View File

@@ -9,6 +9,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.CountDownLatch;
@@ -71,6 +72,8 @@ public class H2DatabaseTest extends TestCase {
private final Group group;
private final TransportId transportId;
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 byte[] secret;
@@ -97,8 +100,10 @@ public class H2DatabaseTest extends TestCase {
new TestMessage(privateMessageId, null, null, null, timestamp, raw);
group = groupFactory.createGroup(groupId, "Group name", null);
transportId = new TransportId(0);
transports = Collections.singletonMap(transportId,
Collections.singletonMap("bar", "baz"));
Map<String, String> properties = Collections.singletonMap("foo", "bar");
transports = Collections.singletonMap(transportId, properties);
remoteTransports = Collections.singletonMap(transportId,
Collections.singletonMap(contactId, properties));
subscriptions = Collections.singletonMap(group, 0L);
secret = new byte[123];
}
@@ -132,7 +137,7 @@ public class H2DatabaseTest extends TestCase {
db = open(true);
txn = db.startTransaction();
assertTrue(db.containsContact(txn, contactId));
assertEquals(transports, db.getTransports(txn, contactId));
assertEquals(remoteTransports, db.getRemoteTransports(txn));
assertTrue(db.containsSubscription(txn, groupId));
assertTrue(db.containsMessage(txn, messageId));
byte[] raw1 = db.getMessage(txn, messageId);
@@ -152,7 +157,7 @@ public class H2DatabaseTest extends TestCase {
db = open(true);
txn = db.startTransaction();
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.containsMessage(txn, messageId));
assertFalse(db.containsMessage(txn, privateMessageId));
@@ -983,40 +988,49 @@ public class H2DatabaseTest extends TestCase {
@Test
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);
Connection txn = db.startTransaction();
// Add a contact with some transport properties
assertEquals(contactId, db.addContact(txn, transports, secret));
assertEquals(transports, db.getTransports(txn, contactId));
assertEquals(remoteTransports, db.getRemoteTransports(txn));
// Replace the transport properties
TransportId transportId1 = new TransportId(1);
Map<TransportId, Map<String, String>> transports1 =
new TreeMap<TransportId, Map<String, String>>();
transports1.put(transportId, Collections.singletonMap("bar", "baz"));
transports1.put(transportId1, Collections.singletonMap("baz", "quux"));
transports1.put(transportId, properties);
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);
assertEquals(transports1, db.getTransports(txn, contactId));
assertEquals(remoteTransports1, db.getRemoteTransports(txn));
// Remove the transport properties
db.setTransports(txn, contactId,
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
for(TransportId t : transports.keySet()) {
Map<String, String> properties = transports.get(t);
db.setTransportProperties(txn, transportId, properties);
for(Entry<TransportId, Map<String, String>> e : transports.entrySet()) {
db.setTransportProperties(txn, e.getKey(), e.getValue());
}
assertEquals(transports, db.getTransports(txn));
assertEquals(transports, db.getLocalTransports(txn));
// Remove the local transport properties
for(TransportId t : transports.keySet()) {
db.setTransportProperties(txn, t,
Collections.<String, String>emptyMap());
}
assertEquals(Collections.emptyMap(), db.getTransports(txn));
assertEquals(Collections.emptyMap(), db.getLocalTransports(txn));
db.commitTransaction(txn);
db.close();
@@ -1025,8 +1039,9 @@ public class H2DatabaseTest extends TestCase {
@Test
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");
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
@@ -1050,32 +1065,44 @@ public class H2DatabaseTest extends TestCase {
@Test
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);
Connection txn = db.startTransaction();
// Add a contact with some transport properties
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
TransportId transportId1 = new TransportId(1);
Map<TransportId, Map<String, String>> transports1 =
new TreeMap<TransportId, Map<String, String>>();
transports1.put(transportId, Collections.singletonMap("bar", "baz"));
transports1.put(transportId1, Collections.singletonMap("baz", "quux"));
transports1.put(transportId, properties);
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);
assertEquals(transports1, db.getTransports(txn, contactId));
assertEquals(remoteTransports1, db.getRemoteTransports(txn));
// Try to replace the transport properties using a timestamp of 1
TransportId transportId2 = new TransportId(2);
Map<TransportId, Map<String, String>> transports2 =
new TreeMap<TransportId, Map<String, String>>();
transports2.put(transportId1, Collections.singletonMap("baz", "quux"));
transports2.put(transportId2, Collections.singletonMap("quux", "etc"));
transports2.put(transportId1, properties1);
transports2.put(transportId2, properties2);
db.setTransports(txn, contactId, transports2, 1);
// The old properties should still be there
assertEquals(transports1, db.getTransports(txn, contactId));
assertEquals(remoteTransports1, db.getRemoteTransports(txn));
db.commitTransaction(txn);
db.close();

View File

@@ -139,7 +139,7 @@ public class InvitationWorkerTest extends TestCase {
oneOf(params).getPassword();
will(returnValue(new char[] {'x', 'y', 'z', 'z', 'y'}));
oneOf(callback).encryptingFile(invitation);
oneOf(database).getTransports();
oneOf(database).getLocalTransports();
will(returnValue(transports));
oneOf(writerFactory).createWriter(with(any(OutputStream.class)));
will(returnValue(writer));