Changed the format of transport properties from (key, value) pairs to

(transport name, key, value) triples. This makes it possible for each
transport plugin to update its locally stored properties atomically.
This commit is contained in:
akwizgran
2011-08-04 11:07:28 +01:00
parent 5be9d953ee
commit ec29c4d1d3
16 changed files with 264 additions and 172 deletions

View File

@@ -52,7 +52,7 @@ public abstract class DatabaseComponentTest extends TestCase {
private final byte[] raw;
private final Message message;
private final Group group;
private final Map<String, String> transports;
private final Map<String, Map<String, String>> transports;
public DatabaseComponentTest() {
super();
@@ -68,7 +68,8 @@ public abstract class DatabaseComponentTest extends TestCase {
message = new TestMessage(messageId, MessageId.NONE, groupId, authorId,
timestamp, raw);
group = new TestGroup(groupId, "The really exciting group", null);
transports = Collections.singletonMap("foo", "bar");
transports = Collections.singletonMap("foo",
Collections.singletonMap("bar", "baz"));
}
protected abstract <T> DatabaseComponent createDatabaseComponent(
@@ -1112,9 +1113,9 @@ public abstract class DatabaseComponentTest extends TestCase {
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).getTransports(txn);
will(returnValue(Collections.singletonMap("foo", "bar")));
oneOf(database).setTransports(txn,
Collections.singletonMap("bar", "baz"));
will(returnValue(transports));
oneOf(database).setTransports(txn, "bar",
Collections.singletonMap("baz", "bam"));
oneOf(database).commitTransaction(txn);
oneOf(listener).eventOccurred(
DatabaseListener.Event.TRANSPORTS_UPDATED);
@@ -1122,7 +1123,7 @@ public abstract class DatabaseComponentTest extends TestCase {
DatabaseComponent db = createDatabaseComponent(database, cleaner);
db.addListener(listener);
db.setTransports(Collections.singletonMap("bar", "baz"));
db.setTransports("bar", Collections.singletonMap("baz", "bam"));
context.assertIsSatisfied();
}
@@ -1138,13 +1139,13 @@ public abstract class DatabaseComponentTest extends TestCase {
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).getTransports(txn);
will(returnValue(Collections.singletonMap("bar", "baz")));
will(returnValue(transports));
oneOf(database).commitTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
db.addListener(listener);
db.setTransports(Collections.singletonMap("bar", "baz"));
db.setTransports("foo", transports.get("foo"));
context.assertIsSatisfied();
}

View File

@@ -60,6 +60,7 @@ public class H2DatabaseTest extends TestCase {
private final byte[] raw;
private final Message message;
private final Group group;
private final Map<String, Map<String, String>> transports;
public H2DatabaseTest() throws Exception {
super();
@@ -78,6 +79,8 @@ public class H2DatabaseTest extends TestCase {
message = new TestMessage(messageId, MessageId.NONE, groupId, authorId,
timestamp, raw);
group = groupFactory.createGroup(groupId, "Group name", null);
transports = Collections.singletonMap("foo",
Collections.singletonMap("bar", "baz"));
}
@Before
@@ -91,7 +94,6 @@ public class H2DatabaseTest extends TestCase {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
assertFalse(db.containsContact(txn, contactId));
Map<String, String> transports = Collections.singletonMap("foo", "bar");
assertEquals(contactId, db.addContact(txn, transports));
assertTrue(db.containsContact(txn, contactId));
assertFalse(db.containsSubscription(txn, groupId));
@@ -107,8 +109,7 @@ public class H2DatabaseTest extends TestCase {
db = open(true);
txn = db.startTransaction();
assertTrue(db.containsContact(txn, contactId));
transports = db.getTransports(txn, contactId);
assertEquals(Collections.singletonMap("foo", "bar"), transports);
assertEquals(transports, db.getTransports(txn, contactId));
assertTrue(db.containsSubscription(txn, groupId));
assertTrue(db.containsMessage(txn, messageId));
byte[] raw1 = db.getMessage(txn, messageId);
@@ -141,20 +142,20 @@ public class H2DatabaseTest extends TestCase {
// Create three contacts
assertFalse(db.containsContact(txn, contactId));
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
assertTrue(db.containsContact(txn, contactId));
assertFalse(db.containsContact(txn, contactId1));
assertEquals(contactId1, db.addContact(txn, null));
assertEquals(contactId1, db.addContact(txn, transports));
assertTrue(db.containsContact(txn, contactId1));
assertFalse(db.containsContact(txn, contactId2));
assertEquals(contactId2, db.addContact(txn, null));
assertEquals(contactId2, db.addContact(txn, transports));
assertTrue(db.containsContact(txn, contactId2));
// Delete one of the contacts
db.removeContact(txn, contactId1);
assertFalse(db.containsContact(txn, contactId1));
// Add another contact - a new ID should be created
assertFalse(db.containsContact(txn, contactId3));
assertEquals(contactId3, db.addContact(txn, null));
assertEquals(contactId3, db.addContact(txn, transports));
assertTrue(db.containsContact(txn, contactId3));
db.commitTransaction(txn);
@@ -201,7 +202,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singleton(contactId));
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
@@ -239,7 +240,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singleton(contactId));
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
@@ -281,7 +282,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singleton(contactId));
db.addMessage(txn, message);
@@ -318,7 +319,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singleton(contactId));
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
@@ -349,7 +350,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
db.addMessage(txn, message);
@@ -382,7 +383,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and some batches to ack
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addBatchToAck(txn, contactId, batchId);
db.addBatchToAck(txn, contactId, batchId1);
db.commitTransaction(txn);
@@ -410,7 +411,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and receive the same batch twice
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addBatchToAck(txn, contactId, batchId);
db.addBatchToAck(txn, contactId, batchId);
db.commitTransaction(txn);
@@ -437,7 +438,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singleton(contactId));
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
@@ -474,7 +475,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singleton(contactId));
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
@@ -517,7 +518,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
// Add some outstanding batches, a few ms apart
for(int i = 0; i < ids.length; i++) {
db.addOutstandingBatch(txn, contactId, ids[i],
@@ -556,7 +557,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
// Add some outstanding batches, a few ms apart
for(int i = 0; i < ids.length; i++) {
db.addOutstandingBatch(txn, contactId, ids[i],
@@ -784,24 +785,28 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact with some transport properties
Map<String, String> transports = Collections.singletonMap("foo", "bar");
assertEquals(contactId, db.addContact(txn, transports));
assertEquals(transports, db.getTransports(txn, contactId));
// Replace the transport properties
transports = new TreeMap<String, String>();
transports.put("foo", "bar baz");
transports.put("bar", "baz quux");
db.setTransports(txn, contactId, transports, 1);
assertEquals(transports, db.getTransports(txn, contactId));
Map<String, Map<String, String>> transports1 =
new TreeMap<String, Map<String, String>>();
transports1.put("foo", Collections.singletonMap("bar", "baz"));
transports1.put("bar", Collections.singletonMap("baz", "quux"));
db.setTransports(txn, contactId, transports1, 1);
assertEquals(transports1, db.getTransports(txn, contactId));
// Remove the transport properties
db.setTransports(txn, contactId,
Collections.<String, String>emptyMap(), 2);
Collections.<String, Map<String, String>>emptyMap(), 2);
assertEquals(Collections.emptyMap(), db.getTransports(txn, contactId));
// Set the local transport properties
db.setTransports(txn, transports);
for(String s : transports.keySet()) {
db.setTransports(txn, s, transports.get(s));
}
assertEquals(transports, db.getTransports(txn));
// Remove the local transport properties
db.setTransports(txn, Collections.<String, String>emptyMap());
for(String s : transports.keySet()) {
db.setTransports(txn, s, Collections.<String, String>emptyMap());
}
assertEquals(Collections.emptyMap(), db.getTransports(txn));
db.commitTransaction(txn);
@@ -814,19 +819,20 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact with some transport properties
Map<String, String> transports = Collections.singletonMap("foo", "bar");
assertEquals(contactId, db.addContact(txn, transports));
assertEquals(transports, db.getTransports(txn, contactId));
// Replace the transport properties using a timestamp of 2
Map<String, String> transports1 = new TreeMap<String, String>();
transports1.put("foo", "bar baz");
transports1.put("bar", "baz quux");
Map<String, Map<String, String>> transports1 =
new TreeMap<String, Map<String, String>>();
transports1.put("foo", Collections.singletonMap("bar", "baz"));
transports1.put("bar", Collections.singletonMap("baz", "quux"));
db.setTransports(txn, contactId, transports1, 2);
assertEquals(transports1, db.getTransports(txn, contactId));
// Try to replace the transport properties using a timestamp of 1
Map<String, String> transports2 = new TreeMap<String, String>();
transports2.put("bar", "baz");
transports2.put("quux", "fnord");
Map<String, Map<String, String>> transports2 =
new TreeMap<String, Map<String, String>>();
transports2.put("bar", Collections.singletonMap("baz", "quux"));
transports2.put("baz", Collections.singletonMap("quux", "fnord"));
db.setTransports(txn, contactId, transports2, 1);
// The old properties should still be there
assertEquals(transports1, db.getTransports(txn, contactId));
@@ -844,7 +850,6 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact
Map<String, String> transports = Collections.emptyMap();
assertEquals(contactId, db.addContact(txn, transports));
// Add some subscriptions
Collection<Group> subs = Collections.singletonList(group);
@@ -869,7 +874,6 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact
Map<String, String> transports = Collections.emptyMap();
assertEquals(contactId, db.addContact(txn, transports));
// Add some subscriptions
Collection<Group> subs = Collections.singletonList(group);
@@ -892,7 +896,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
@@ -910,7 +914,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
db.addMessage(txn, message);
@@ -933,7 +937,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
db.addMessage(txn, message);
@@ -955,7 +959,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singleton(contactId));
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
@@ -980,7 +984,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singleton(contactId));
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
@@ -999,7 +1003,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and a neighbour subscription
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
// There's no local subscription for the group
@@ -1016,7 +1020,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.addMessage(txn, message);
db.setStatus(txn, contactId, messageId, Status.NEW);
@@ -1035,7 +1039,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.addMessage(txn, message);
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
@@ -1055,7 +1059,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singleton(contactId));
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
@@ -1076,7 +1080,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
db.setVisibility(txn, groupId, Collections.singleton(contactId));
db.setSubscriptions(txn, contactId, Collections.singleton(group), 1);
@@ -1096,7 +1100,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn, null));
assertEquals(contactId, db.addContact(txn, transports));
db.addSubscription(txn, group);
// The group should not be visible to the contact

View File

@@ -10,6 +10,7 @@ import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
@@ -72,6 +73,7 @@ public class FileReadWriteTest extends TestCase {
private final Message message, message1, message2, message3;
private final String authorName = "Alice";
private final String messageBody = "Hello world";
private final Map<String, Map<String, String>> transports;
public FileReadWriteTest() throws Exception {
super();
@@ -111,6 +113,8 @@ public class FileReadWriteTest extends TestCase {
message3 = messageEncoder.encodeMessage(MessageId.NONE, group1,
groupKeyPair.getPrivate(), author, authorKeyPair.getPrivate(),
messageBody.getBytes("UTF-8"));
transports = Collections.singletonMap("foo",
Collections.singletonMap("bar", "baz"));
}
@Before
@@ -154,7 +158,7 @@ public class FileReadWriteTest extends TestCase {
s.writeSubscriptions(subs);
TransportWriter t = packetWriterFactory.createTransportWriter(out);
t.writeTransports(Collections.singletonMap("foo", "bar"));
t.writeTransports(transports);
out.close();
assertTrue(file.exists());
@@ -229,7 +233,7 @@ public class FileReadWriteTest extends TestCase {
assertTrue(reader.hasUserDefined(Tags.TRANSPORTS));
Transports t = reader.readUserDefined(Tags.TRANSPORTS,
Transports.class);
assertEquals(Collections.singletonMap("foo", "bar"), t.getTransports());
assertEquals(transports, t.getTransports());
assertTrue(t.getTimestamp() > start);
assertTrue(t.getTimestamp() <= System.currentTimeMillis());