From eeb1ce27abfe6cfb700a6529658a156a15ca2f72 Mon Sep 17 00:00:00 2001
From: akwizgran <michael@briarproject.org>
Date: Wed, 1 May 2013 12:44:19 +0100
Subject: [PATCH] Added a unit test for batched DB operations.

---
 briar-tests/src/net/sf/briar/TestUtils.java   |  9 +++
 .../src/net/sf/briar/db/BasicH2Test.java      | 65 ++++++++++++++-----
 .../net/sf/briar/messaging/ConstantsTest.java | 26 +++-----
 3 files changed, 66 insertions(+), 34 deletions(-)

diff --git a/briar-tests/src/net/sf/briar/TestUtils.java b/briar-tests/src/net/sf/briar/TestUtils.java
index cedb5d4281..16e05461b1 100644
--- a/briar-tests/src/net/sf/briar/TestUtils.java
+++ b/briar-tests/src/net/sf/briar/TestUtils.java
@@ -73,4 +73,13 @@ public class TestUtils {
 		}
 		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();
+	}
 }
diff --git a/briar-tests/src/net/sf/briar/db/BasicH2Test.java b/briar-tests/src/net/sf/briar/db/BasicH2Test.java
index f592a9c395..e6f398a8fc 100644
--- a/briar-tests/src/net/sf/briar/db/BasicH2Test.java
+++ b/briar-tests/src/net/sf/briar/db/BasicH2Test.java
@@ -41,27 +41,17 @@ public class BasicH2Test extends BriarTestCase {
 	}
 
 	@Test
-	public void testCreateTableAndAddRow() throws Exception {
+	public void testCreateTableAddRowAndRetrieve() throws Exception {
 		// Create the table
 		createTable(connection);
-		// Generate an ID
+		// Generate an ID and a name
 		byte[] id = new byte[32];
 		new Random().nextBytes(id);
+		String name = TestUtils.createRandomString(50);
 		// Insert the ID and name into the table
-		addRow(id, "foo");
-	}
-
-	@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");
+		addRow(id, name);
 		// Check that the name can be retrieved using the ID
-		assertEquals("foo", getName(id));
+		assertEquals(name, getName(id));
 	}
 
 	@Test
@@ -105,6 +95,24 @@ public class BasicH2Test extends BriarTestCase {
 		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 {
 		try {
 			Statement s = connection.createStatement();
@@ -123,9 +131,9 @@ public class BasicH2Test extends BriarTestCase {
 			if(id == null) ps.setNull(1, BINARY);
 			else ps.setBytes(1, id);
 			ps.setString(2, name);
-			int rowsAffected = ps.executeUpdate();
+			int affected = ps.executeUpdate();
+			assertEquals(1, affected);
 			ps.close();
-			assertEquals(1, rowsAffected);
 		} catch(SQLException e) {
 			connection.close();
 			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
 	public void tearDown() throws Exception {
 		if(connection != null) connection.close();
diff --git a/briar-tests/src/net/sf/briar/messaging/ConstantsTest.java b/briar-tests/src/net/sf/briar/messaging/ConstantsTest.java
index 3f25455791..c85f2d313e 100644
--- a/briar-tests/src/net/sf/briar/messaging/ConstantsTest.java
+++ b/briar-tests/src/net/sf/briar/messaging/ConstantsTest.java
@@ -111,11 +111,12 @@ public class ConstantsTest extends BriarTestCase {
 	public void testMessageFitsIntoPacket() throws Exception {
 		MessageId parent = new MessageId(TestUtils.getRandomId());
 		// 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];
 		Group group = groupFactory.createGroup(groupName, groupPublic);
 		// 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];
 		Author author = authorFactory.createAuthor(authorName, authorPublic);
 		// Create a maximum-length message
@@ -123,7 +124,8 @@ public class ConstantsTest extends BriarTestCase {
 				crypto.generateSignatureKeyPair().getPrivate();
 		PrivateKey authorPrivate =
 				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];
 		Message message = messageFactory.createPseudonymousMessage(parent,
 				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
 		TransportProperties p = new TransportProperties();
 		for(int i = 0; i < MAX_PROPERTIES_PER_TRANSPORT; i++) {
-			String key = createRandomString(MAX_PROPERTY_LENGTH);
-			String value = createRandomString(MAX_PROPERTY_LENGTH);
+			String key = TestUtils.createRandomString(MAX_PROPERTY_LENGTH);
+			String value = TestUtils.createRandomString(MAX_PROPERTY_LENGTH);
 			p.put(key, value);
 		}
 		// Create a maximum-length transport update
@@ -171,7 +173,8 @@ public class ConstantsTest extends BriarTestCase {
 		// Create the maximum number of maximum-length groups
 		Collection<Group> subs = new ArrayList<Group>();
 		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];
 			subs.add(groupFactory.createGroup(groupName, groupPublic));
 		}
@@ -210,15 +213,4 @@ public class ConstantsTest extends BriarTestCase {
 		// Check the size of the serialised offer
 		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;
-	}
 }
-- 
GitLab