Unit tests for H2Database.

This commit is contained in:
akwizgran
2011-07-04 16:06:10 +01:00
parent 6384256c06
commit 390b316724
13 changed files with 646 additions and 20 deletions

View File

@@ -14,8 +14,8 @@ import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId;
/**
* A low-level interface to the database that is managed by a
* DatabaseComponent. Most operations take a transaction argument, which is
* A low-level interface to the database (DatabaseComponent provides a
* high-level interface). Most operations take a transaction argument, which is
* obtained by calling startTransaction(). Every transaction must be
* terminated by calling either abortTransaction() or commitTransaction(),
* even if an exception is thrown.

View File

@@ -10,31 +10,32 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.briar.api.crypto.Password;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DatabasePassword;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.protocol.MessageFactory;
import net.sf.briar.util.FileUtils;
import com.google.inject.Inject;
/** Contains all the H2-specific code for the database. */
class H2Database extends JdbcDatabase {
private static final Logger LOG =
Logger.getLogger(H2Database.class.getName());
private final Password password;
private final File home;
private final Password password;
private final String url;
private final long maxSize;
@Inject
H2Database(MessageFactory messageFactory,
@DatabasePassword Password password) {
H2Database(File dir, MessageFactory messageFactory,
@DatabasePassword Password password, long maxSize) {
super(messageFactory, "BINARY(32)");
home = new File(dir, "db");
this.password = password;
home = new File(FileUtils.getBriarDirectory(), "Data/db/db");
url = "jdbc:h2:split:" + home.getPath()
+ ";CIPHER=AES;DB_CLOSE_ON_EXIT=false";
this.maxSize = maxSize;
}
public void open(boolean resume) throws DbException {
@@ -54,7 +55,7 @@ class H2Database extends JdbcDatabase {
File dir = home.getParentFile();
long free = dir.getFreeSpace();
long used = getDiskSpace(dir);
long quota = DatabaseComponent.MAX_DB_SIZE - used;
long quota = maxSize - used;
long min = Math.min(free, quota);
if(LOG.isLoggable(Level.FINE)) LOG.fine("Free space: " + min);
return min;

View File

@@ -31,6 +31,10 @@ import net.sf.briar.api.protocol.MessageFactory;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.util.FileUtils;
/**
* A generic database implementation that can be used with any JDBC-compatible
* database library. (Tested with H2, Derby and HSQLDB.)
*/
abstract class JdbcDatabase implements Database<Connection> {
private static final String CREATE_LOCAL_SUBSCRIPTIONS =
@@ -945,7 +949,8 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " ON messages.groupId = contactSubscriptions.groupId"
+ " JOIN statuses ON messages.messageId = statuses.messageId"
+ " WHERE contactSubscriptions.contactId = ?"
+ " AND statuses.contactId = ? AND status = ?";
+ " AND statuses.contactId = ? AND status = ?"
+ " AND sendability > ZERO()";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
ps.setInt(2, c.getInt());

View File

@@ -5,7 +5,7 @@ import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId;
class MessageImpl implements Message {
public class MessageImpl implements Message {
private final MessageId id, parent;
private final GroupId group;