X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=system%2Fspring%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fspring%2Fcomponent%2FDatabaseTesterComponent.java;h=2da00a83400d61cc70ea8c56b04689d89353351c;hb=HEAD;hp=ac9d6f6fc5a6f46a83cc12303e90df6fa84b8a74;hpb=0d8d8f24656e585ee75558cfd6a4c661f8f14985;p=utils
diff --git a/system/spring/src/test/java/org/wamblee/system/spring/component/DatabaseTesterComponent.java b/system/spring/src/test/java/org/wamblee/system/spring/component/DatabaseTesterComponent.java
index ac9d6f6f..2da00a83 100644
--- a/system/spring/src/test/java/org/wamblee/system/spring/component/DatabaseTesterComponent.java
+++ b/system/spring/src/test/java/org/wamblee/system/spring/component/DatabaseTesterComponent.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2005 the original author or authors.
+ * 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.
@@ -12,40 +12,34 @@
* 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.system.spring.component;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
-
-import javax.sql.DataSource;
-
import junit.framework.TestCase;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.dbunit.DatabaseUnitException;
+
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.DatabaseSequenceFilter;
import org.dbunit.database.IDatabaseConnection;
+
import org.dbunit.dataset.FilteredDataSet;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.filter.ITableFilter;
+
import org.dbunit.operation.DatabaseOperation;
+
import org.hibernate.SessionFactory;
+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+
import org.springframework.context.ApplicationContext;
+
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
+
import org.springframework.orm.hibernate3.HibernateTemplate;
+
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
@@ -53,17 +47,31 @@ import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
+
import org.wamblee.test.spring.TestTransactionCallback;
import org.wamblee.test.spring.TestTransactionCallbackWithoutResult;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.logging.Logger;
+
+import javax.sql.DataSource;
+
/**
* Test support class for database testing. Currently, this still requires the
* spring platform transaction manager and hibernate template.
*/
public class DatabaseTesterComponent {
-
- private static final Log LOG = LogFactory
- .getLog(DatabaseTesterComponent.class);
+ private static final Logger LOG = Logger
+ .getLogger(DatabaseTesterComponent.class.getName());
/**
* Schema pattern.
@@ -81,15 +89,19 @@ public class DatabaseTesterComponent {
private DataSource dataSource;
+ /**
+ * Creates a new DatabaseTesterComponent object.
+ *
+ */
public DatabaseTesterComponent(HibernateTemplate aHibernateTemplate,
- PlatformTransactionManager aTransactionManager,
- DataSource aDataSource) {
+ PlatformTransactionManager aTransactionManager, DataSource aDataSource) {
hibernateTemplate = aHibernateTemplate;
transactionManager = aTransactionManager;
dataSource = aDataSource;
}
/**
+ *
* @return Hibernate session factory.
*/
protected SessionFactory getSessionFactory() {
@@ -99,7 +111,7 @@ public class DatabaseTesterComponent {
/**
* Performs common initialization for test cases:
*
- * - Cleaning the database.
+ * - Cleaning the database.
*
*
* @throws Exception
@@ -121,6 +133,7 @@ public class DatabaseTesterComponent {
}
/**
+ *
* @return Transaction manager
*/
protected PlatformTransactionManager getTransactionManager() {
@@ -128,6 +141,7 @@ public class DatabaseTesterComponent {
}
/**
+ *
* @return Starts a new transaction.
*/
protected TransactionStatus getTransaction() {
@@ -150,7 +164,6 @@ public class DatabaseTesterComponent {
/**
* Flushes the session. Should be called after some Hibernate work and
* before JDBC is used to check results.
- *
*/
public void flush() {
getTemplate().flush();
@@ -160,7 +173,6 @@ public class DatabaseTesterComponent {
* Flushes the session first and then removes all objects from the Session
* cache. Should be called after some Hibernate work and before JDBC is used
* to check results.
- *
*/
public void clear() {
flush();
@@ -189,7 +201,6 @@ public class DatabaseTesterComponent {
}
public void cleanDatabase() throws SQLException {
-
if (!isDatabaseConfigured()) {
return;
}
@@ -198,10 +209,10 @@ public class DatabaseTesterComponent {
try {
IDatabaseConnection connection = new DatabaseConnection(
- getConnection());
+ getConnection());
ITableFilter filter = new DatabaseSequenceFilter(connection, tables);
IDataSet dataset = new FilteredDataSet(filter, connection
- .createDataSet(tables));
+ .createDataSet(tables));
DatabaseOperation.DELETE_ALL.execute(connection, dataset);
} catch (DatabaseUnitException e) {
@@ -212,42 +223,49 @@ public class DatabaseTesterComponent {
}
/**
+ *
+ *
* @throws SQLException
*/
public String[] getTableNames() throws SQLException {
-
List result = new ArrayList();
- LOG.debug("Getting database table names to clean (schema: '"
- + SCHEMA_PATTERN + "'");
+ LOG.fine("Getting database table names to clean (schema: '" +
+ SCHEMA_PATTERN + "'");
ResultSet tables = getConnection().getMetaData().getTables(null,
- SCHEMA_PATTERN, "%", new String[] { "TABLE" });
+ SCHEMA_PATTERN, "%", new String[] { "TABLE" });
+
while (tables.next()) {
String table = tables.getString("TABLE_NAME");
+
// Make sure we do not touch hibernate's specific
// infrastructure tables.
if (!table.toLowerCase().startsWith("hibernate")) {
result.add(table);
- LOG.debug("Adding " + table
- + " to list of tables to be cleaned.");
+ LOG.fine("Adding " + table +
+ " to list of tables to be cleaned.");
}
}
+
return (String[]) result.toArray(new String[0]);
}
/**
- * @return
+ *
+ *
* @throws SQLException
*/
public void emptyTables(List aTableList) throws SQLException {
Iterator liTable = aTableList.iterator();
+
while (liTable.hasNext()) {
emptyTable((String) liTable.next());
}
}
/**
- * @return
+ *
+ *
* @throws SQLException
*/
public void emptyTable(String aTable) throws SQLException {
@@ -255,7 +273,8 @@ public class DatabaseTesterComponent {
}
/**
- * @return
+ *
+ *
* @throws SQLException
*/
public void dropTable(String aTable) throws SQLException {
@@ -267,6 +286,7 @@ public class DatabaseTesterComponent {
*
* @param aSql
* SQL statement.
+ *
* @return Return code of the corresponding JDBC call.
*/
public int executeSql(final String aSql) {
@@ -282,6 +302,7 @@ public class DatabaseTesterComponent {
* SQL statement.
* @param aArg
* Argument of the sql statement.
+ *
* @return Return code of the corresponding JDBC call.
*/
public int executeSql(final String aSql, final Object aArg) {
@@ -297,6 +318,7 @@ public class DatabaseTesterComponent {
* SQL query to execute.
* @param aArgs
* Arguments.
+ *
* @return Number of rows updated.
*/
public int executeSql(final String aSql, final Object[] aArgs) {
@@ -320,11 +342,13 @@ public class DatabaseTesterComponent {
*
* @param aCallback
* Callback to do your transactional work.
+ *
* @return Result.
*/
public Object executeTransaction(TransactionCallback aCallback) {
TransactionTemplate lTemplate = new TransactionTemplate(
- getTransactionManager());
+ getTransactionManager());
+
return lTemplate.execute(aCallback);
}
@@ -336,7 +360,7 @@ public class DatabaseTesterComponent {
*/
public void executeTransaction(TransactionCallbackWithoutResult aCallback) {
TransactionTemplate template = new TransactionTemplate(
- getTransactionManager());
+ getTransactionManager());
template.execute(aCallback);
}
@@ -346,7 +370,9 @@ public class DatabaseTesterComponent {
*
* @param aCallback
* Code to be executed within the transaction.
+ *
* @return Result.
+ *
*/
public Map executeTransaction(final TestTransactionCallback aCallback) {
return (Map) executeTransaction(new TransactionCallback() {
@@ -368,9 +394,10 @@ public class DatabaseTesterComponent {
*
* @param aCallback
* Code to be executed within the transaction.
+ *
*/
public void executeTransaction(
- final TestTransactionCallbackWithoutResult aCallback) {
+ final TestTransactionCallbackWithoutResult aCallback) {
executeTransaction(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(TransactionStatus aArg) {
try {
@@ -388,6 +415,7 @@ public class DatabaseTesterComponent {
*
* @param aSql
* Query to execute.
+ *
* @return Result set.
*/
public ResultSet executeQuery(String aSql) {
@@ -403,6 +431,7 @@ public class DatabaseTesterComponent {
* Query.
* @param aArg
* Argument.
+ *
* @return Result set.
*/
public ResultSet executeQuery(String aSql, Object aArg) {
@@ -418,7 +447,9 @@ public class DatabaseTesterComponent {
* Sql query.
* @param aArgs
* Arguments to the query.
+ *
* @return Result set.
+ *
*/
public ResultSet executeQuery(final String aSql, final Object[] aArgs) {
try {
@@ -442,10 +473,11 @@ public class DatabaseTesterComponent {
* Arguments to the prepared statement.
* @param aStatement
* Prepared statement
+ *
* @throws SQLException
*/
private void setPreparedParams(final Object[] aArgs,
- PreparedStatement aStatement) throws SQLException {
+ PreparedStatement aStatement) throws SQLException {
for (int i = 1; i <= aArgs.length; i++) {
setPreparedParam(i, aStatement, aArgs[i - 1]);
}
@@ -459,11 +491,12 @@ public class DatabaseTesterComponent {
* @param aStatement
* Prepared statement.
* @param aObject
- * Value Must be of type Integer, Long, or String.
+ * Value Must be of type Integer, Long, or String.
+ *
* @throws SQLException
*/
private void setPreparedParam(int aIndex, PreparedStatement aStatement,
- Object aObject) throws SQLException {
+ Object aObject) throws SQLException {
if (aObject instanceof Integer) {
aStatement.setInt(aIndex, ((Integer) aObject).intValue());
} else if (aObject instanceof Long) {
@@ -471,9 +504,9 @@ public class DatabaseTesterComponent {
} else if (aObject instanceof String) {
aStatement.setString(aIndex, (String) aObject);
} else {
- TestCase.fail("Unsupported object type for prepared statement: "
- + aObject.getClass() + " value: " + aObject
- + " statement: " + aStatement);
+ TestCase.fail("Unsupported object type for prepared statement: " +
+ aObject.getClass() + " value: " + aObject + " statement: " +
+ aStatement);
}
}
@@ -483,10 +516,12 @@ public class DatabaseTesterComponent {
} catch (NoSuchBeanDefinitionException e) {
return false;
}
+
return true;
}
/**
+ *
* @return Returns the dataSource.
*/
public DataSource getDataSource() {
@@ -494,17 +529,20 @@ public class DatabaseTesterComponent {
}
/**
+ *
+ *
* @return
+ *
* @throws SQLException
*/
public int getTableSize(final String aTable) throws SQLException {
-
ResultSet resultSet = executeQuery("select * from " + aTable);
int count = 0;
while (resultSet.next()) {
count++;
}
+
return count;
}
@@ -517,5 +555,4 @@ public class DatabaseTesterComponent {
return count;
}
-
}