X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=test%2Fenterprise%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsupport%2Fpersistence%2FDatabaseUtils.java;h=b57cd46184e3c4293da6ca5e25c01f6aec8957cd;hb=c63e76e400f06f51ba235b9e6658bfa3149c1d48;hp=2beb9f1133124bbc518bf5f003728bd70316f17b;hpb=8de36ff0206c996baf3ee4adc3e2293b12ff5f39;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 2beb9f11..b57cd461 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 @@ -1,3 +1,18 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.wamblee.support.persistence; import java.sql.Connection; @@ -6,6 +21,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.logging.Level; import java.util.logging.Logger; import javax.sql.DataSource; @@ -49,55 +65,85 @@ public class DatabaseUtils { */ private static final String SCHEMA_PATTERN = "%"; private DataSource dataSource; - private ITableFilterSimple tables; - public DatabaseUtils(DataSource aDataSource, ITableFilterSimple aTables) { + private IDatabaseTester dbtester; + /** + * List of connections that were created for dbtesters. + * This list will be closed in the {@link #stop()} method. + */ + private List connections; + + /** + * Constructs the database utils. + * Before use, {@link #start()} must be called. + * @param aDataSource Datasource. + */ + public DatabaseUtils(DataSource aDataSource) { dataSource = aDataSource; - tables = aTables; + dbtester = new DataSourceDatabaseTester(dataSource); + connections = new ArrayList(); } - public IDatabaseTester createDbTester() throws Exception { - return createDbTester(getTableNames(tables)); + /** + * Starts the database utils. + */ + public void start() { + // Empty. No operation currently. } - public IDatabaseTester createDbTester(String[] aTables) throws Exception { - IDatabaseTester dbtester = new DataSourceDatabaseTester(dataSource); - dbtester.setDataSet(dbtester.getConnection().createDataSet(aTables)); - return dbtester; + /** + * Stops the database utils, closing any JDBC connections that were created + * by this utility. Note that connections obtained from the datasource directly + * must still be closed by the user. The involved connections are only those that + * are created by this utility. + */ + public void stop() { + for (IDatabaseConnection connection: connections) { + try { + connection.close(); + } catch (SQLException e) { + LOG.log(Level.WARNING, "Could not close connection", e); + } + } + connections.clear(); + } + + public IDatabaseTester createDbTester(ITableFilterSimple aTables) throws Exception { + return createDbTester(getTableNames(aTables)); } - public void cleanDatabase() throws Exception { - cleanDatabase(tables); + public IDatabaseTester createDbTester(String[] aTables) throws Exception { + IDatabaseConnection connection = dbtester.getConnection(); + connections.add(connection); + dbtester.setDataSet(connection.createDataSet(aTables)); + return dbtester; } 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; } }); - for (String table : tables) { - - } } 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; } @@ -108,6 +154,7 @@ public class DatabaseUtils { public T executeInTransaction(JdbcUnitOfWork aCallback) throws Exception { Connection connection = dataSource.getConnection(); + connection.setAutoCommit(false); try { T value = aCallback.execute(connection); connection.commit(); @@ -116,11 +163,7 @@ public class DatabaseUtils { connection.close(); } } - - public String[] getTableNames() throws Exception { - return getTableNames(tables); - } - + /** * @throws SQLException */ @@ -131,23 +174,20 @@ public class DatabaseUtils { LOG.fine("Getting database table names to clean (schema: '" + SCHEMA_PATTERN + "'"); - ResultSet tables = dataSource.getConnection().getMetaData().getTables( - null, SCHEMA_PATTERN, "%", new String[] { "TABLE" }); - while (tables.next()) { - String table = tables.getString("TABLE_NAME"); - if (aSelection.accept(table)) { - result.add(table); + Connection connection = dataSource.getConnection(); + try { + ResultSet tableNames = connection.getMetaData().getTables(null, + SCHEMA_PATTERN, "%", new String[] { "TABLE" }); + while (tableNames.next()) { + String table = tableNames.getString("TABLE_NAME"); + if (aSelection.accept(table)) { + result.add(table); + } } + return (String[]) result.toArray(new String[0]); + } finally { + connection.close(); } - return (String[]) result.toArray(new String[0]); - } - - public void emptyTables() throws Exception { - executeOnTables(tables, new TableSetOperation() { - public void execute(String aTable) throws Exception { - emptyTable(aTable); - } - }); } /** @@ -171,22 +211,23 @@ public class DatabaseUtils { executeSql("delete from " + aTable); } - public void dropTables() throws Exception { - executeOnTables(tables, new TableSetOperation() { - - public void execute(String aTable) throws Exception { - dropTable(aTable); - } - }); - } - public void dropTables(ITableFilterSimple aTables) throws Exception { - executeOnTables(aTables, new TableSetOperation() { + final String[] tableNames = getTableNames(aTables); + String[] sortedTables = executeInTransaction(new JdbcUnitOfWork() { - public void execute(String aTable) throws Exception { - dropTable(aTable); + public String[] execute(Connection aConnection) throws Exception { + IDatabaseConnection connection = new DatabaseConnection( + aConnection); + ITableFilter filter = new DatabaseSequenceFilter(connection, + tableNames); + IDataSet dataset = new FilteredDataSet(filter, connection + .createDataSet(tableNames)); + return dataset.getTableNames(); } }); + for (int i = sortedTables.length - 1; i >= 0; i--) { + dropTable(sortedTables[i]); + } } /** @@ -348,7 +389,7 @@ public class DatabaseUtils { if (aObject instanceof Integer) { aStatement.setInt(aIndex, ((Integer) aObject).intValue()); } else if (aObject instanceof Long) { - aStatement.setLong(aIndex, ((Integer) aObject).longValue()); + aStatement.setLong(aIndex, ((Long) aObject).longValue()); } else if (aObject instanceof String) { aStatement.setString(aIndex, (String) aObject); } else {