mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
Found the bug, it was just a SQL syntax error.
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
<path refid='test-classes'/>
|
||||
<path refid='util-classes'/>
|
||||
</classpath>
|
||||
<test name='net.sf.briar.db.BasicH2Test'/>
|
||||
<test name='net.sf.briar.db.DatabaseCleanerImplTest'/>
|
||||
<test name='net.sf.briar.db.H2DatabaseTest'/>
|
||||
<test name='net.sf.briar.db.ReadWriteLockDatabaseComponentTest'/>
|
||||
|
||||
117
test/net/sf/briar/db/BasicH2Test.java
Normal file
117
test/net/sf/briar/db/BasicH2Test.java
Normal file
@@ -0,0 +1,117 @@
|
||||
package net.sf.briar.db;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Random;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import net.sf.briar.TestUtils;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class BasicH2Test extends TestCase {
|
||||
|
||||
private static final String CREATE_TABLE =
|
||||
"CREATE TABLE foo"
|
||||
+ " (uniqueId BINARY(32) NOT NULL,"
|
||||
+ " name VARCHAR NOT NULL,"
|
||||
+ " PRIMARY KEY (uniqueId))";
|
||||
|
||||
private final File testDir = TestUtils.getTestDirectory();
|
||||
private final File db = new File(testDir, "db");
|
||||
private final String url = "jdbc:h2:" + db.getPath();
|
||||
|
||||
private Connection connection = null;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
testDir.mkdirs();
|
||||
Class.forName("org.h2.Driver");
|
||||
connection = DriverManager.getConnection(url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateTableAndAddRow() throws Exception {
|
||||
// Create the table
|
||||
createTable(connection);
|
||||
// Generate a unique ID
|
||||
byte[] uniqueId = new byte[32];
|
||||
new Random().nextBytes(uniqueId);
|
||||
// Insert the unique ID and name into the table
|
||||
addRow(uniqueId, "foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateTableAddAndRetrieveRow() throws Exception {
|
||||
// Create the table
|
||||
createTable(connection);
|
||||
// Generate a unique ID
|
||||
byte[] uniqueId = new byte[32];
|
||||
new Random().nextBytes(uniqueId);
|
||||
// Insert the unique ID and name into the table
|
||||
addRow(uniqueId, "foo");
|
||||
// Check that the name can be retrieved using the unique ID
|
||||
assertEquals("foo", getName(uniqueId));
|
||||
}
|
||||
|
||||
private void addRow(byte[] uniqueId, String name) throws SQLException {
|
||||
String sql = "INSERT INTO foo (uniqueId, name) VALUES (?, ?)";
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
ps = connection.prepareStatement(sql);
|
||||
ps.setBytes(1, uniqueId);
|
||||
ps.setString(2, name);
|
||||
int rowsAffected = ps.executeUpdate();
|
||||
ps.close();
|
||||
assertEquals(1, rowsAffected);
|
||||
} catch(SQLException e) {
|
||||
connection.close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private String getName(byte[] uniqueId) throws SQLException {
|
||||
String sql = "SELECT name FROM foo WHERE uniqueID = ?";
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
ps = connection.prepareStatement(sql);
|
||||
ps.setBytes(1, uniqueId);
|
||||
rs = ps.executeQuery();
|
||||
assertTrue(rs.next());
|
||||
String name = rs.getString(1);
|
||||
assertFalse(rs.next());
|
||||
rs.close();
|
||||
ps.close();
|
||||
return name;
|
||||
} catch(SQLException e) {
|
||||
connection.close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private void createTable(Connection connection) throws SQLException {
|
||||
Statement s;
|
||||
try {
|
||||
s = connection.createStatement();
|
||||
s.executeUpdate(CREATE_TABLE);
|
||||
s.close();
|
||||
} catch(SQLException e) {
|
||||
connection.close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
if(connection != null) connection.close();
|
||||
TestUtils.deleteTestDirectory(testDir);
|
||||
}
|
||||
}
|
||||
@@ -468,6 +468,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
||||
will(returnValue(true));
|
||||
oneOf(ackWriter).addBatchId(batchId1);
|
||||
will(returnValue(false));
|
||||
oneOf(ackWriter).finish();
|
||||
// Record the batch that was acked
|
||||
oneOf(database).removeBatchesToAck(txn, contactId, acks);
|
||||
}});
|
||||
|
||||
@@ -76,8 +76,8 @@ public class H2DatabaseTest extends TestCase {
|
||||
random.nextBytes(raw);
|
||||
message = new TestMessage(messageId, MessageId.NONE, groupId, authorId,
|
||||
timestamp, raw);
|
||||
group = groupFactory.createGroup(groupId, "Group name",
|
||||
TestUtils.getRandomId(), null);
|
||||
group = groupFactory.createGroup(groupId, "Group name", false,
|
||||
TestUtils.getRandomId());
|
||||
}
|
||||
|
||||
@Before
|
||||
@@ -533,7 +533,7 @@ public class H2DatabaseTest extends TestCase {
|
||||
MessageId childId3 = new MessageId(TestUtils.getRandomId());
|
||||
GroupId groupId1 = new GroupId(TestUtils.getRandomId());
|
||||
Group group1 = groupFactory.createGroup(groupId1, "Another group name",
|
||||
TestUtils.getRandomId(), null);
|
||||
false, TestUtils.getRandomId());
|
||||
Message child1 = new TestMessage(childId1, messageId, groupId,
|
||||
authorId, timestamp, raw);
|
||||
Message child2 = new TestMessage(childId2, messageId, groupId,
|
||||
@@ -758,7 +758,7 @@ public class H2DatabaseTest extends TestCase {
|
||||
public void testUpdateSubscriptions() throws DbException {
|
||||
GroupId groupId1 = new GroupId(TestUtils.getRandomId());
|
||||
Group group1 = groupFactory.createGroup(groupId1, "Another group name",
|
||||
TestUtils.getRandomId(), null);
|
||||
false, TestUtils.getRandomId());
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
@@ -783,7 +783,7 @@ public class H2DatabaseTest extends TestCase {
|
||||
throws DbException {
|
||||
GroupId groupId1 = new GroupId(TestUtils.getRandomId());
|
||||
Group group1 = groupFactory.createGroup(groupId1, "Another group name",
|
||||
TestUtils.getRandomId(), null);
|
||||
false, TestUtils.getRandomId());
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user