Nudge the database API in the direction of sanity.

This commit is contained in:
akwizgran
2011-10-11 19:08:10 +01:00
parent a49a95347f
commit 631f4e74b5
9 changed files with 307 additions and 345 deletions

View File

@@ -57,8 +57,7 @@ public abstract class DatabaseComponentTest extends TestCase {
private final Group group;
private final TransportId transportId;
private final Map<TransportId, TransportProperties> transports;
private final Map<TransportId, Map<ContactId, TransportProperties>>
remoteTransports;
private final Map<ContactId, TransportProperties> remoteProperties;
private final byte[] secret;
public DatabaseComponentTest() {
@@ -81,8 +80,7 @@ public abstract class DatabaseComponentTest extends TestCase {
TransportProperties p =
new TransportProperties(Collections.singletonMap("foo", "bar"));
transports = Collections.singletonMap(transportId, p);
remoteTransports = Collections.singletonMap(transportId,
Collections.singletonMap(contactId, p));
remoteProperties = Collections.singletonMap(contactId, p);
secret = new byte[123];
}
@@ -128,9 +126,9 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(true));
oneOf(database).getSharedSecret(txn, contactId);
will(returnValue(secret));
// getRemoteTransports()
oneOf(database).getRemoteTransports(txn);
will(returnValue(remoteTransports));
// getTransportProperties(transportId)
oneOf(database).getRemoteProperties(txn, transportId);
will(returnValue(remoteProperties));
// subscribe(group)
oneOf(group).getId();
will(returnValue(groupId));
@@ -176,7 +174,7 @@ public abstract class DatabaseComponentTest extends TestCase {
assertEquals(connectionWindow,
db.getConnectionWindow(contactId, transportId));
assertEquals(secret, db.getSharedSecret(contactId));
assertEquals(remoteTransports, db.getRemoteTransports());
assertEquals(remoteProperties, db.getRemoteProperties(transportId));
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());
@@ -1289,7 +1287,7 @@ public abstract class DatabaseComponentTest extends TestCase {
oneOf(database).getLocalTransports(txn);
will(returnValue(Collections.singletonMap(transportId,
properties)));
oneOf(database).setTransportProperties(txn, transportId,
oneOf(database).setLocalProperties(txn, transportId,
properties1);
oneOf(database).commitTransaction(txn);
oneOf(listener).eventOccurred(Event.TRANSPORTS_UPDATED);
@@ -1297,7 +1295,7 @@ public abstract class DatabaseComponentTest extends TestCase {
DatabaseComponent db = createDatabaseComponent(database, cleaner);
db.addListener(listener);
db.setTransportProperties(transportId, properties1);
db.setLocalProperties(transportId, properties1);
context.assertIsSatisfied();
}
@@ -1323,7 +1321,7 @@ public abstract class DatabaseComponentTest extends TestCase {
DatabaseComponent db = createDatabaseComponent(database, cleaner);
db.addListener(listener);
db.setTransportProperties(transportId, properties);
db.setLocalProperties(transportId, properties);
context.assertIsSatisfied();
}
@@ -1342,16 +1340,16 @@ public abstract class DatabaseComponentTest extends TestCase {
context.checking(new Expectations() {{
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).getTransportConfig(txn, transportId);
oneOf(database).getConfig(txn, transportId);
will(returnValue(config));
oneOf(database).setTransportConfig(txn, transportId, config1);
oneOf(database).setConfig(txn, transportId, config1);
oneOf(database).commitTransaction(txn);
oneOf(listener).eventOccurred(Event.TRANSPORTS_UPDATED);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
db.addListener(listener);
db.setTransportConfig(transportId, config1);
db.setConfig(transportId, config1);
context.assertIsSatisfied();
}
@@ -1369,14 +1367,14 @@ public abstract class DatabaseComponentTest extends TestCase {
context.checking(new Expectations() {{
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).getTransportConfig(txn, transportId);
oneOf(database).getConfig(txn, transportId);
will(returnValue(config));
oneOf(database).commitTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
db.addListener(listener);
db.setTransportConfig(transportId, config);
db.setConfig(transportId, config);
context.assertIsSatisfied();
}

View File

@@ -11,7 +11,6 @@ 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;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -73,9 +72,9 @@ public class H2DatabaseTest extends TestCase {
private final Message message, privateMessage;
private final Group group;
private final TransportId transportId;
private final TransportProperties properties;
private final Map<TransportId, TransportProperties> transports;
private final Map<TransportId, Map<ContactId, TransportProperties>>
remoteTransports;
private final Map<ContactId, TransportProperties> remoteProperties;
private final Map<Group, Long> subscriptions;
private final byte[] secret;
@@ -102,11 +101,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);
TransportProperties p =
new TransportProperties(Collections.singletonMap("foo", "bar"));
transports = Collections.singletonMap(transportId, p);
remoteTransports = Collections.singletonMap(transportId,
Collections.singletonMap(contactId, p));
properties = new TransportProperties(
Collections.singletonMap("foo", "bar"));
transports = Collections.singletonMap(transportId, properties);
remoteProperties = Collections.singletonMap(contactId, properties);
subscriptions = Collections.singletonMap(group, 0L);
secret = new byte[123];
}
@@ -140,7 +138,8 @@ public class H2DatabaseTest extends TestCase {
db = open(true);
txn = db.startTransaction();
assertTrue(db.containsContact(txn, contactId));
assertEquals(remoteTransports, db.getRemoteTransports(txn));
assertEquals(remoteProperties,
db.getRemoteProperties(txn, transportId));
assertTrue(db.containsSubscription(txn, groupId));
assertTrue(db.containsMessage(txn, messageId));
byte[] raw1 = db.getMessage(txn, messageId);
@@ -160,7 +159,8 @@ public class H2DatabaseTest extends TestCase {
db = open(true);
txn = db.startTransaction();
assertFalse(db.containsContact(txn, contactId));
assertEquals(Collections.emptyMap(), db.getRemoteTransports(txn));
assertEquals(Collections.emptyMap(),
db.getRemoteProperties(txn, transportId));
assertFalse(db.containsSubscription(txn, groupId));
assertFalse(db.containsMessage(txn, messageId));
assertFalse(db.containsMessage(txn, privateMessageId));
@@ -991,47 +991,40 @@ public class H2DatabaseTest extends TestCase {
@Test
public void testUpdateTransportProperties() throws Exception {
TransportProperties properties =
new TransportProperties(Collections.singletonMap("foo", "bar"));
TransportProperties properties1 =
new TransportProperties(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(remoteTransports, db.getRemoteTransports(txn));
assertEquals(remoteProperties,
db.getRemoteProperties(txn, transportId));
// Replace the transport properties
TransportId transportId1 = new TransportId(1);
TransportProperties properties1 =
new TransportProperties(Collections.singletonMap("baz", "bam"));
Map<TransportId, TransportProperties> transports1 =
new TreeMap<TransportId, TransportProperties>();
transports1.put(transportId, properties);
transports1.put(transportId1, properties1);
Map<TransportId, Map<ContactId, TransportProperties>> remoteTransports1
= new TreeMap<TransportId, Map<ContactId, TransportProperties>>();
remoteTransports1.put(transportId, Collections.singletonMap(contactId,
properties));
remoteTransports1.put(transportId1, Collections.singletonMap(contactId,
properties1));
Collections.singletonMap(transportId, properties1);
Map<ContactId, TransportProperties> remoteProperties1 =
Collections.singletonMap(contactId, properties1);
db.setTransports(txn, contactId, transports1, 1);
assertEquals(remoteTransports1, db.getRemoteTransports(txn));
assertEquals(remoteProperties1,
db.getRemoteProperties(txn, transportId));
// Remove the transport properties
db.setTransports(txn, contactId,
Collections.<TransportId, TransportProperties>emptyMap(), 2);
assertEquals(Collections.emptyMap(), db.getRemoteTransports(txn));
assertEquals(Collections.emptyMap(),
db.getRemoteProperties(txn, transportId));
// Set the local transport properties
for(Entry<TransportId, TransportProperties> e : transports.entrySet()) {
db.setTransportProperties(txn, e.getKey(), e.getValue());
db.setLocalProperties(txn, e.getKey(), e.getValue());
}
assertEquals(transports, db.getLocalTransports(txn));
// Remove the local transport properties
for(TransportId t : transports.keySet()) {
db.setTransportProperties(txn, t, new TransportProperties());
db.setLocalProperties(txn, t, new TransportProperties());
}
assertEquals(Collections.emptyMap(), db.getLocalTransports(txn));
@@ -1051,17 +1044,17 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Set the transport config
db.setTransportConfig(txn, transportId, config);
assertEquals(config, db.getTransportConfig(txn, transportId));
db.setConfig(txn, transportId, config);
assertEquals(config, db.getConfig(txn, transportId));
// Update the transport config
db.setTransportConfig(txn, transportId, config1);
assertEquals(config1, db.getTransportConfig(txn, transportId));
db.setConfig(txn, transportId, config1);
assertEquals(config1, db.getConfig(txn, transportId));
// Remove the transport config
db.setTransportConfig(txn, transportId, new TransportConfig());
db.setConfig(txn, transportId, new TransportConfig());
assertEquals(Collections.emptyMap(),
db.getTransportConfig(txn, transportId));
db.getConfig(txn, transportId));
db.commitTransaction(txn);
db.close();
@@ -1069,45 +1062,35 @@ public class H2DatabaseTest extends TestCase {
@Test
public void testTransportsNotUpdatedIfTimestampIsOld() throws Exception {
TransportProperties properties =
new TransportProperties(Collections.singletonMap("foo", "bar"));
TransportProperties properties1 =
new TransportProperties(Collections.singletonMap("baz", "bam"));
TransportProperties properties2 =
new TransportProperties(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(remoteTransports, db.getRemoteTransports(txn));
assertEquals(remoteProperties,
db.getRemoteProperties(txn, transportId));
// Replace the transport properties using a timestamp of 2
TransportId transportId1 = new TransportId(1);
TransportProperties properties1 =
new TransportProperties(Collections.singletonMap("baz", "bam"));
Map<TransportId, TransportProperties> transports1 =
new TreeMap<TransportId, TransportProperties>();
transports1.put(transportId, properties);
transports1.put(transportId1, properties1);
Map<TransportId, Map<ContactId, TransportProperties>> remoteTransports1
= new TreeMap<TransportId, Map<ContactId, TransportProperties>>();
remoteTransports1.put(transportId, Collections.singletonMap(contactId,
properties));
remoteTransports1.put(transportId1, Collections.singletonMap(contactId,
properties1));
Collections.singletonMap(transportId, properties1);
Map<ContactId, TransportProperties> remoteProperties1 =
Collections.singletonMap(contactId, properties1);
db.setTransports(txn, contactId, transports1, 2);
assertEquals(remoteTransports1, db.getRemoteTransports(txn));
assertEquals(remoteProperties1,
db.getRemoteProperties(txn, transportId));
// Try to replace the transport properties using a timestamp of 1
TransportId transportId2 = new TransportId(2);
TransportProperties properties2 =
new TransportProperties(Collections.singletonMap("quux", "etc"));
Map<TransportId, TransportProperties> transports2 =
new TreeMap<TransportId, TransportProperties>();
transports2.put(transportId1, properties1);
transports2.put(transportId2, properties2);
Collections.singletonMap(transportId, properties2);
db.setTransports(txn, contactId, transports2, 1);
// The old properties should still be there
assertEquals(remoteTransports1, db.getRemoteTransports(txn));
assertEquals(remoteProperties1,
db.getRemoteProperties(txn, transportId));
db.commitTransaction(txn);
db.close();

View File

@@ -4,7 +4,6 @@ import java.io.ByteArrayOutputStream;
import java.security.PrivateKey;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
@@ -193,7 +192,7 @@ public class ConstantsTest extends TestCase {
// Create the maximum number of plugins, each with the maximum number
// of maximum-length properties
Map<TransportId, TransportProperties> transports =
new TreeMap<TransportId, TransportProperties>();
new HashMap<TransportId, TransportProperties>();
for(int i = 0; i < TransportUpdate.MAX_PLUGINS_PER_UPDATE; i++) {
TransportProperties p = new TransportProperties();
for(int j = 0; j < TransportUpdate.MAX_PROPERTIES_PER_PLUGIN; j++) {