X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=test%2Fenterprise%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsupport%2Fpersistence%2FDatabaseUtils.java;h=07dbca0f64363988f18c76da9bd599ba55ee2865;hb=ec9582b50e9c74b08b14c79d7d16de5d0fc2851e;hp=cd7b3e26dd0bd56cc1d943e37ef8262b478ce8ab;hpb=185601ca79008d99e5c2293bc3686fc501720484;p=utils diff --git a/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java b/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java index cd7b3e26..07dbca0f 100644 --- a/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java +++ b/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java @@ -45,15 +45,44 @@ import org.dbunit.operation.DatabaseOperation; */ public class DatabaseUtils { + /** + * Represents a set of tables. + * + * @author Erik Brakkee + */ public static interface TableSet { boolean contains(String aTableName); } + /** + * Represents a unit of work (transaction). + * + * @author Erik Brakkee + * + * @param Type of return value. + */ public static interface JdbcUnitOfWork { + /** + * Executes statement within a transaction. + * @param aConnection Connection. + * @return Result of the work. + * @throws Exception + */ T execute(Connection aConnection) throws Exception; } + /** + * Operation to be executed on a set of tables for each table + * individually. + * + * @author Erik Brakkee + */ public static interface TableSetOperation { + /** + * Executes on a table. + * @param aTable Table name. + * @throws Exception + */ void execute(String aTable) throws Exception; } @@ -65,7 +94,6 @@ public class DatabaseUtils { */ private static final String SCHEMA_PATTERN = "%"; private DataSource dataSource; - private ITableFilterSimple tables; private IDatabaseTester dbtester; /** @@ -77,12 +105,10 @@ public class DatabaseUtils { /** * Constructs the database utils. * Before use, {@link #start()} must be called. - * @param aDataSource Datasource. - * @param aTables Tables to work with. + * @param aDataSource Datasource. */ - public DatabaseUtils(DataSource aDataSource, ITableFilterSimple aTables) { + public DatabaseUtils(DataSource aDataSource) { dataSource = aDataSource; - tables = aTables; dbtester = new DataSourceDatabaseTester(dataSource); connections = new ArrayList(); } @@ -108,12 +134,25 @@ public class DatabaseUtils { LOG.log(Level.WARNING, "Could not close connection", e); } } + connections.clear(); } - public IDatabaseTester createDbTester() throws Exception { - return createDbTester(getTableNames(tables)); + /** + * Creates database tester. + * @param aTables Tables to create the tester for. + * @return Database tester. + * @throws Exception + */ + public IDatabaseTester createDbTester(ITableFilterSimple aTables) throws Exception { + return createDbTester(getTableNames(aTables)); } - + + /** + * Creates database tester. + * @param aTables Tables to create the tester for. + * @return Database tester. + * @throws Exception + */ public IDatabaseTester createDbTester(String[] aTables) throws Exception { IDatabaseConnection connection = dbtester.getConnection(); connections.add(connection); @@ -121,35 +160,43 @@ public class DatabaseUtils { return dbtester; } - public void cleanDatabase() throws Exception { - cleanDatabase(tables); - } - + /** + * Executes an operation on a set of tables. + * @param aTables Tables. + * @param aOperation Operation. + * @throws Exception + */ public void executeOnTables(ITableFilterSimple aTables, final TableSetOperation aOperation) throws Exception { - final String[] tables = getTableNames(aTables); + final String[] tableNames = getTableNames(aTables); executeInTransaction(new JdbcUnitOfWork() { public Void execute(Connection aConnection) throws Exception { - for (int i = tables.length - 1; i >= 0; i--) { - aOperation.execute(tables[i]); + for (int i = tableNames.length - 1; i >= 0; i--) { + aOperation.execute(tableNames[i]); } return null; } }); } + /** + * Cleans a number of database tables. This means deleting the content not dropping the tables. + * This may fail in case of cyclic dependencies between the tables (current limitation). + * @param aSelection Tables. + * @throws Exception + */ public void cleanDatabase(ITableFilterSimple aSelection) throws Exception { - final String[] tables = getTableNames(aSelection); + final String[] tableNames = getTableNames(aSelection); executeInTransaction(new JdbcUnitOfWork() { public Void execute(Connection aConnection) throws Exception { IDatabaseConnection connection = new DatabaseConnection( aConnection); ITableFilter filter = new DatabaseSequenceFilter(connection, - tables); + tableNames); IDataSet dataset = new FilteredDataSet(filter, connection - .createDataSet(tables)); + .createDataSet(tableNames)); DatabaseOperation.DELETE_ALL.execute(connection, dataset); return null; } @@ -157,25 +204,31 @@ public class DatabaseUtils { } - public T executeInTransaction(JdbcUnitOfWork aCallback) + /** + * Executes a unit of work within a transaction. + * @param Result type of th ework. + * @param aWork Unit of work. + * @return + * @throws Exception + */ + public T executeInTransaction(JdbcUnitOfWork aWork) throws Exception { Connection connection = dataSource.getConnection(); connection.setAutoCommit(false); try { - T value = aCallback.execute(connection); + T value = aWork.execute(connection); connection.commit(); return value; } finally { connection.close(); } } - - public String[] getTableNames() throws Exception { - return getTableNames(tables); - } - + /** - * @throws SQLException + * Returns table names based on a table filter. + * @param aSelection Table filter. + * @return Table names. + * @throws Exception */ public String[] getTableNames(ITableFilterSimple aSelection) throws Exception { @@ -186,10 +239,10 @@ public class DatabaseUtils { Connection connection = dataSource.getConnection(); try { - ResultSet tables = connection.getMetaData().getTables(null, + ResultSet tableNames = connection.getMetaData().getTables(null, SCHEMA_PATTERN, "%", new String[] { "TABLE" }); - while (tables.next()) { - String table = tables.getString("TABLE_NAME"); + while (tableNames.next()) { + String table = tableNames.getString("TABLE_NAME"); if (aSelection.accept(table)) { result.add(table); } @@ -200,18 +253,10 @@ public class DatabaseUtils { } } - public void emptyTables() throws Exception { - executeOnTables(tables, new TableSetOperation() { - public void execute(String aTable) throws Exception { - emptyTable(aTable); - } - }); - } - /** - * @return - * @throws SQLException + * Use {@link #cleanDatabase(ITableFilterSimple)} instead. */ + @Deprecated public void emptyTables(final ITableFilterSimple aSelection) throws Exception { executeOnTables(aSelection, new TableSetOperation() { @@ -222,28 +267,29 @@ public class DatabaseUtils { } /** - * @return - * @throws SQLException + * User {@link #cleanDatabase(ITableFilterSimple)} instead. */ + @Deprecated public void emptyTable(String aTable) throws Exception { executeSql("delete from " + aTable); } - public void dropTables() throws Exception { - dropTables(tables); - } - + /** + * Drops tables. This only works if there are no cyclic dependencies between the tables. + * @param aTables Tables to drop. + * @throws Exception + */ public void dropTables(ITableFilterSimple aTables) throws Exception { - final String[] tables = getTableNames(aTables); + final String[] tableNames = getTableNames(aTables); String[] sortedTables = executeInTransaction(new JdbcUnitOfWork() { public String[] execute(Connection aConnection) throws Exception { IDatabaseConnection connection = new DatabaseConnection( aConnection); ITableFilter filter = new DatabaseSequenceFilter(connection, - tables); + tableNames); IDataSet dataset = new FilteredDataSet(filter, connection - .createDataSet(tables)); + .createDataSet(tableNames)); return dataset.getTableNames(); } }); @@ -253,8 +299,9 @@ public class DatabaseUtils { } /** - * @return - * @throws SQLException + * Drops a table. + * @param aTable Table to drop. + * @throws Exception */ public void dropTable(final String aTable) throws Exception { executeInTransaction(new JdbcUnitOfWork() { @@ -365,6 +412,13 @@ public class DatabaseUtils { } } + /** + * Executes an update. + * @param aConnection Connection to use. + * @param aSql SQL update to use. + * @param aArgs Arguments to the update. + * @return Number of rows updated. + */ public int executeUpdate(Connection aConnection, final String aSql, final Object... aArgs) { try { @@ -422,7 +476,9 @@ public class DatabaseUtils { } /** - * @return + * Gets the table size. + * @param aTable Table. + * @return Table size. * @throws SQLException */ public int getTableSize(final String aTable) throws Exception { @@ -437,6 +493,12 @@ public class DatabaseUtils { } + /** + * Counts the results in a result set. + * @param aResultSet Resultset. + * @return Number of rows in the set. + * @throws SQLException + */ public int countResultSet(ResultSet aResultSet) throws SQLException { int count = 0;