Added a unit test for batched DB operations.

This commit is contained in:
akwizgran
2013-05-01 12:44:19 +01:00
parent 4290e3d1e5
commit eeb1ce27ab
3 changed files with 66 additions and 34 deletions

View File

@@ -73,4 +73,13 @@ public class TestUtils {
} }
TestCase.assertEquals(b.length, offset); TestCase.assertEquals(b.length, offset);
} }
public static String createRandomString(int length) throws Exception {
StringBuilder s = new StringBuilder(length);
for(int i = 0; i < length; i++) {
int letter = (int) (Math.random() * 26);
s.append((char) ('a' + letter));
}
return s.toString();
}
} }

View File

@@ -41,27 +41,17 @@ public class BasicH2Test extends BriarTestCase {
} }
@Test @Test
public void testCreateTableAndAddRow() throws Exception { public void testCreateTableAddRowAndRetrieve() throws Exception {
// Create the table // Create the table
createTable(connection); createTable(connection);
// Generate an ID // Generate an ID and a name
byte[] id = new byte[32]; byte[] id = new byte[32];
new Random().nextBytes(id); new Random().nextBytes(id);
String name = TestUtils.createRandomString(50);
// Insert the ID and name into the table // Insert the ID and name into the table
addRow(id, "foo"); addRow(id, name);
}
@Test
public void testCreateTableAddAndRetrieveRow() throws Exception {
// Create the table
createTable(connection);
// Generate an ID
byte[] id = new byte[32];
new Random().nextBytes(id);
// Insert the ID and name into the table
addRow(id, "foo");
// Check that the name can be retrieved using the ID // Check that the name can be retrieved using the ID
assertEquals("foo", getName(id)); assertEquals(name, getName(id));
} }
@Test @Test
@@ -105,6 +95,24 @@ public class BasicH2Test extends BriarTestCase {
assertEquals("third", names.get(3)); assertEquals("third", names.get(3));
} }
@Test
public void testCreateTableAddBatchAndRetrieve() throws Exception {
// Create the table
createTable(connection);
// Generate some IDs and names
byte[][] ids = new byte[10][32];
String[] names = new String[10];
Random random = new Random();
for(int i = 0; i < 10; i++) {
random.nextBytes(ids[i]);
names[i] = TestUtils.createRandomString(50);
}
// Insert the IDs and names into the table as a batch
addBatch(ids, names);
// Check that the names can be retrieved using the IDs
for(int i = 0; i < 10; i++) assertEquals(names[i], getName(ids[i]));
}
private void createTable(Connection connection) throws SQLException { private void createTable(Connection connection) throws SQLException {
try { try {
Statement s = connection.createStatement(); Statement s = connection.createStatement();
@@ -123,9 +131,9 @@ public class BasicH2Test extends BriarTestCase {
if(id == null) ps.setNull(1, BINARY); if(id == null) ps.setNull(1, BINARY);
else ps.setBytes(1, id); else ps.setBytes(1, id);
ps.setString(2, name); ps.setString(2, name);
int rowsAffected = ps.executeUpdate(); int affected = ps.executeUpdate();
assertEquals(1, affected);
ps.close(); ps.close();
assertEquals(1, rowsAffected);
} catch(SQLException e) { } catch(SQLException e) {
connection.close(); connection.close();
throw e; throw e;
@@ -185,6 +193,29 @@ public class BasicH2Test extends BriarTestCase {
} }
} }
private void addBatch(byte[][] ids, String[] names) throws SQLException {
assertEquals(ids.length, names.length);
String sql = "INSERT INTO foo (uniqueId, name) VALUES (?, ?)";
try {
PreparedStatement ps = connection.prepareStatement(sql);
for(int i = 0; i < ids.length; i++) {
if(ids[i] == null) ps.setNull(1, BINARY);
else ps.setBytes(1, ids[i]);
ps.setString(2, names[i]);
ps.addBatch();
}
int[] batchAffected = ps.executeBatch();
assertEquals(ids.length, batchAffected.length);
for(int i = 0; i < batchAffected.length; i++) {
assertEquals(1, batchAffected[i]);
}
ps.close();
} catch(SQLException e) {
connection.close();
throw e;
}
}
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
if(connection != null) connection.close(); if(connection != null) connection.close();

View File

@@ -111,11 +111,12 @@ public class ConstantsTest extends BriarTestCase {
public void testMessageFitsIntoPacket() throws Exception { public void testMessageFitsIntoPacket() throws Exception {
MessageId parent = new MessageId(TestUtils.getRandomId()); MessageId parent = new MessageId(TestUtils.getRandomId());
// Create a maximum-length group // Create a maximum-length group
String groupName = createRandomString(MAX_GROUP_NAME_LENGTH); String groupName = TestUtils.createRandomString(MAX_GROUP_NAME_LENGTH);
byte[] groupPublic = new byte[MAX_PUBLIC_KEY_LENGTH]; byte[] groupPublic = new byte[MAX_PUBLIC_KEY_LENGTH];
Group group = groupFactory.createGroup(groupName, groupPublic); Group group = groupFactory.createGroup(groupName, groupPublic);
// Create a maximum-length author // Create a maximum-length author
String authorName = createRandomString(MAX_AUTHOR_NAME_LENGTH); String authorName =
TestUtils.createRandomString(MAX_AUTHOR_NAME_LENGTH);
byte[] authorPublic = new byte[MAX_PUBLIC_KEY_LENGTH]; byte[] authorPublic = new byte[MAX_PUBLIC_KEY_LENGTH];
Author author = authorFactory.createAuthor(authorName, authorPublic); Author author = authorFactory.createAuthor(authorName, authorPublic);
// Create a maximum-length message // Create a maximum-length message
@@ -123,7 +124,8 @@ public class ConstantsTest extends BriarTestCase {
crypto.generateSignatureKeyPair().getPrivate(); crypto.generateSignatureKeyPair().getPrivate();
PrivateKey authorPrivate = PrivateKey authorPrivate =
crypto.generateSignatureKeyPair().getPrivate(); crypto.generateSignatureKeyPair().getPrivate();
String contentType = createRandomString(MAX_CONTENT_TYPE_LENGTH); String contentType =
TestUtils.createRandomString(MAX_CONTENT_TYPE_LENGTH);
byte[] body = new byte[MAX_BODY_LENGTH]; byte[] body = new byte[MAX_BODY_LENGTH];
Message message = messageFactory.createPseudonymousMessage(parent, Message message = messageFactory.createPseudonymousMessage(parent,
group, groupPrivate, author, authorPrivate, contentType, body); group, groupPrivate, author, authorPrivate, contentType, body);
@@ -151,8 +153,8 @@ public class ConstantsTest extends BriarTestCase {
// Create the maximum number of properties with the maximum length // Create the maximum number of properties with the maximum length
TransportProperties p = new TransportProperties(); TransportProperties p = new TransportProperties();
for(int i = 0; i < MAX_PROPERTIES_PER_TRANSPORT; i++) { for(int i = 0; i < MAX_PROPERTIES_PER_TRANSPORT; i++) {
String key = createRandomString(MAX_PROPERTY_LENGTH); String key = TestUtils.createRandomString(MAX_PROPERTY_LENGTH);
String value = createRandomString(MAX_PROPERTY_LENGTH); String value = TestUtils.createRandomString(MAX_PROPERTY_LENGTH);
p.put(key, value); p.put(key, value);
} }
// Create a maximum-length transport update // Create a maximum-length transport update
@@ -171,7 +173,8 @@ public class ConstantsTest extends BriarTestCase {
// Create the maximum number of maximum-length groups // Create the maximum number of maximum-length groups
Collection<Group> subs = new ArrayList<Group>(); Collection<Group> subs = new ArrayList<Group>();
for(int i = 0; i < MAX_SUBSCRIPTIONS; i++) { for(int i = 0; i < MAX_SUBSCRIPTIONS; i++) {
String groupName = createRandomString(MAX_GROUP_NAME_LENGTH); String groupName =
TestUtils.createRandomString(MAX_GROUP_NAME_LENGTH);
byte[] groupPublic = new byte[MAX_PUBLIC_KEY_LENGTH]; byte[] groupPublic = new byte[MAX_PUBLIC_KEY_LENGTH];
subs.add(groupFactory.createGroup(groupName, groupPublic)); subs.add(groupFactory.createGroup(groupName, groupPublic));
} }
@@ -210,15 +213,4 @@ public class ConstantsTest extends BriarTestCase {
// Check the size of the serialised offer // Check the size of the serialised offer
assertTrue(out.size() <= length); assertTrue(out.size() <= length);
} }
private static String createRandomString(int length) throws Exception {
StringBuilder s = new StringBuilder(length);
for(int i = 0; i < length; i++) {
int digit = (int) (Math.random() * 10);
s.append((char) ('0' + digit));
}
String string = s.toString();
assertEquals(length, string.getBytes("UTF-8").length);
return string;
}
} }