source code formatting.
[utils] / system / spring / src / test / java / org / wamblee / system / spring / component / DatabaseTesterComponent.java
index ac9d6f6fc5a6f46a83cc12303e90df6fa84b8a74..09d5725bdfa7b2bced98a10e7b6666f1e76bcbd4 100644 (file)
@@ -1,51 +1,48 @@
 /*
  * Copyright 2005 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.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 +50,33 @@ 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 javax.sql.DataSource;
+
+
 /**
- * Test support class for database testing. Currently, this still requires the
- * spring platform transaction manager and hibernate template.
+ * 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);
+    /**
+     * DOCUMENT ME!
+     */
+    private static final Log LOG = LogFactory.getLog(DatabaseTesterComponent.class);
 
     /**
      * Schema pattern.
@@ -75,21 +88,38 @@ public class DatabaseTesterComponent {
      */
     private ApplicationContext context;
 
+    /**
+     * DOCUMENT ME!
+     */
     private HibernateTemplate hibernateTemplate;
 
+    /**
+     * DOCUMENT ME!
+     */
     private PlatformTransactionManager transactionManager;
 
+    /**
+     * DOCUMENT ME!
+     */
     private DataSource dataSource;
 
+/**
+     * Creates a new DatabaseTesterComponent object.
+     *
+     * @param aHibernateTemplate DOCUMENT ME!
+     * @param aTransactionManager DOCUMENT ME!
+     * @param aDataSource DOCUMENT ME!
+     */
     public DatabaseTesterComponent(HibernateTemplate aHibernateTemplate,
-            PlatformTransactionManager aTransactionManager,
-            DataSource aDataSource) {
-        hibernateTemplate = aHibernateTemplate;
-        transactionManager = aTransactionManager;
-        dataSource = aDataSource;
+        PlatformTransactionManager aTransactionManager, DataSource aDataSource) {
+        hibernateTemplate      = aHibernateTemplate;
+        transactionManager     = aTransactionManager;
+        dataSource             = aDataSource;
     }
 
     /**
+     * DOCUMENT ME!
+     *
      * @return Hibernate session factory.
      */
     protected SessionFactory getSessionFactory() {
@@ -98,10 +128,10 @@ public class DatabaseTesterComponent {
 
     /**
      * Performs common initialization for test cases:
-     * <ul>
-     * <li>Cleaning the database. </li>
-     * </ul>
-     * 
+     *  <ul>
+     *      <li>Cleaning the database.</li>
+     *  </ul>
+     *
      * @throws Exception
      */
     public void setUp() throws Exception {
@@ -111,9 +141,9 @@ public class DatabaseTesterComponent {
     }
 
     /**
-     * Performs common tear down after execution of a test case. Currenlty this
-     * method does nothing.
-     * 
+     * Performs common tear down after execution of a test case.
+     * Currenlty this method does nothing.
+     *
      * @throws Exception
      */
     protected void tearDown() throws Exception {
@@ -121,6 +151,8 @@ public class DatabaseTesterComponent {
     }
 
     /**
+     * DOCUMENT ME!
+     *
      * @return Transaction manager
      */
     protected PlatformTransactionManager getTransactionManager() {
@@ -128,6 +160,8 @@ public class DatabaseTesterComponent {
     }
 
     /**
+     * DOCUMENT ME!
+     *
      * @return Starts a new transaction.
      */
     protected TransactionStatus getTransaction() {
@@ -140,7 +174,7 @@ public class DatabaseTesterComponent {
     /**
      * Returns the hibernate template for executing hibernate-specific
      * functionality.
-     * 
+     *
      * @return Hibernate template.
      */
     public HibernateTemplate getTemplate() {
@@ -148,19 +182,17 @@ public class DatabaseTesterComponent {
     }
 
     /**
-     * Flushes the session. Should be called after some Hibernate work and
-     * before JDBC is used to check results.
-     * 
+     * Flushes the session. Should be called after some Hibernate work
+     * and before JDBC is used to check results.
      */
     public void flush() {
         getTemplate().flush();
     }
 
     /**
-     * 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.
-     * 
+     * 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();
@@ -170,9 +202,9 @@ public class DatabaseTesterComponent {
     /**
      * Evicts the object from the session. This is essential for the
      * implementation of unit tests where first an object is saved and is
-     * retrieved later. By removing the object from the session, Hibernate must
-     * retrieve the object again from the database.
-     * 
+     * retrieved later. By removing the object from the session, Hibernate
+     * must retrieve the object again from the database.
+     *
      * @param aObject
      */
     protected void evict(Object aObject) {
@@ -181,15 +213,19 @@ public class DatabaseTesterComponent {
 
     /**
      * Gets the connection.
-     * 
+     *
      * @return Connection.
      */
     public Connection getConnection() {
         return DataSourceUtils.getConnection(getDataSource());
     }
 
+    /**
+     * DOCUMENT ME!
+     *
+     * @throws SQLException DOCUMENT ME!
+     */
     public void cleanDatabase() throws SQLException {
-
         if (!isDatabaseConfigured()) {
             return;
         }
@@ -197,11 +233,11 @@ public class DatabaseTesterComponent {
         String[] tables = getTableNames();
 
         try {
-            IDatabaseConnection connection = new DatabaseConnection(
-                    getConnection());
-            ITableFilter filter = new DatabaseSequenceFilter(connection, tables);
-            IDataSet dataset = new FilteredDataSet(filter, connection
-                    .createDataSet(tables));
+            IDatabaseConnection connection = new DatabaseConnection(getConnection());
+            ITableFilter        filter     = new DatabaseSequenceFilter(connection,
+                    tables);
+            IDataSet            dataset    = new FilteredDataSet(filter,
+                    connection.createDataSet(tables));
 
             DatabaseOperation.DELETE_ALL.execute(connection, dataset);
         } catch (DatabaseUnitException e) {
@@ -212,42 +248,55 @@ public class DatabaseTesterComponent {
     }
 
     /**
+     * DOCUMENT ME!
+     *
+     * @return DOCUMENT ME!
+     *
      * @throws SQLException
      */
     public String[] getTableNames() throws SQLException {
-
         List<String> result = new ArrayList<String>();
         LOG.debug("Getting database table names to clean (schema: '"
-                + SCHEMA_PATTERN + "'");
+            + SCHEMA_PATTERN + "'");
+
+        ResultSet tables = getConnection().getMetaData()
+            .getTables(null, SCHEMA_PATTERN, "%", new String[] { "TABLE" });
 
-        ResultSet tables = getConnection().getMetaData().getTables(null,
-                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.");
+                    + " to list of tables to be cleaned.");
             }
         }
+
         return (String[]) result.toArray(new String[0]);
     }
 
     /**
-     * @return
+     * DOCUMENT ME!
+     *
+     * @param aTableList DOCUMENT ME!
+     *
      * @throws SQLException
      */
     public void emptyTables(List aTableList) throws SQLException {
         Iterator liTable = aTableList.iterator();
+
         while (liTable.hasNext()) {
             emptyTable((String) liTable.next());
         }
     }
 
     /**
-     * @return
+     * DOCUMENT ME!
+     *
+     * @param aTable DOCUMENT ME!
+     *
      * @throws SQLException
      */
     public void emptyTable(String aTable) throws SQLException {
@@ -255,7 +304,10 @@ public class DatabaseTesterComponent {
     }
 
     /**
-     * @return
+     * DOCUMENT ME!
+     *
+     * @param aTable DOCUMENT ME!
+     *
      * @throws SQLException
      */
     public void dropTable(String aTable) throws SQLException {
@@ -264,9 +316,9 @@ public class DatabaseTesterComponent {
 
     /**
      * Executes an SQL statement within a transaction.
-     * 
-     * @param aSql
-     *            SQL statement.
+     *
+     * @param aSql SQL statement.
+     *
      * @return Return code of the corresponding JDBC call.
      */
     public int executeSql(final String aSql) {
@@ -274,14 +326,13 @@ public class DatabaseTesterComponent {
     }
 
     /**
-     * Executes an SQL statement within a transaction. See
-     * {@link #setPreparedParam(int, PreparedStatement, Object)}for details on
+     * Executes an SQL statement within a transaction. See {@link
+     * #setPreparedParam(int, PreparedStatement, Object)}for details on
      * supported argument types.
-     * 
-     * @param aSql
-     *            SQL statement.
-     * @param aArg
-     *            Argument of the sql statement.
+     *
+     * @param aSql 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) {
@@ -289,105 +340,105 @@ public class DatabaseTesterComponent {
     }
 
     /**
-     * Executes an sql statement. See
-     * {@link #setPreparedParam(int, PreparedStatement, Object)}for details on
-     * supported argument types.
-     * 
-     * @param aSql
-     *            SQL query to execute.
-     * @param aArgs
-     *            Arguments.
+     * Executes an sql statement. See {@link #setPreparedParam(int,
+     * PreparedStatement, Object)}for details on supported argument types.
+     *
+     * @param aSql SQL query to execute.
+     * @param aArgs Arguments.
+     *
      * @return Number of rows updated.
      */
     public int executeSql(final String aSql, final Object[] aArgs) {
         Map results = executeTransaction(new TestTransactionCallback() {
-            public Map execute() throws Exception {
-                JdbcTemplate template = new JdbcTemplate(getDataSource());
-                int result = template.update(aSql, aArgs);
+                    public Map execute() throws Exception {
+                        JdbcTemplate         template = new JdbcTemplate(getDataSource());
+                        int                  result   = template.update(aSql,
+                                aArgs);
 
-                Map<String, Integer> map = new TreeMap<String, Integer>();
-                map.put("result", new Integer(result));
+                        Map<String, Integer> map      = new TreeMap<String, Integer>();
+                        map.put("result", new Integer(result));
 
-                return map;
-            }
-        });
+                        return map;
+                    }
+                });
 
         return ((Integer) results.get("result")).intValue();
     }
 
     /**
      * Executes a transaction with a result.
-     * 
-     * @param aCallback
-     *            Callback to do your transactional work.
+     *
+     * @param aCallback Callback to do your transactional work.
+     *
      * @return Result.
      */
     public Object executeTransaction(TransactionCallback aCallback) {
-        TransactionTemplate lTemplate = new TransactionTemplate(
-                getTransactionManager());
+        TransactionTemplate lTemplate = new TransactionTemplate(getTransactionManager());
+
         return lTemplate.execute(aCallback);
     }
 
     /**
      * Executes a transaction without a result.
-     * 
-     * @param aCallback
-     *            Callback to do your transactional work. .
+     *
+     * @param aCallback Callback to do your transactional work. .
      */
     public void executeTransaction(TransactionCallbackWithoutResult aCallback) {
-        TransactionTemplate template = new TransactionTemplate(
-                getTransactionManager());
+        TransactionTemplate template = new TransactionTemplate(getTransactionManager());
         template.execute(aCallback);
     }
 
     /**
-     * Executes a transaction with a result, causing the testcase to fail if any
-     * type of exception is thrown.
-     * 
-     * @param aCallback
-     *            Code to be executed within the transaction.
+     * Executes a transaction with a result, causing the testcase to
+     * fail if any type of exception is thrown.
+     *
+     * @param aCallback Code to be executed within the transaction.
+     *
      * @return Result.
+     *
+     * @throws RuntimeException DOCUMENT ME!
      */
     public Map executeTransaction(final TestTransactionCallback aCallback) {
         return (Map) executeTransaction(new TransactionCallback() {
-            public Object doInTransaction(TransactionStatus aArg) {
-                try {
-                    return aCallback.execute();
-                } catch (Exception e) {
-                    // test case must fail.
-                    e.printStackTrace();
-                    throw new RuntimeException(e);
+                public Object doInTransaction(TransactionStatus aArg) {
+                    try {
+                        return aCallback.execute();
+                    } catch (Exception e) {
+                        // test case must fail.
+                        e.printStackTrace();
+                        throw new RuntimeException(e);
+                    }
                 }
-            }
-        });
+            });
     }
 
     /**
-     * Executes a transaction with a result, causing the testcase to fail if any
-     * type of exception is thrown.
-     * 
-     * @param aCallback
-     *            Code to be executed within the transaction.
+     * Executes a transaction with a result, causing the testcase to
+     * fail if any type of exception is thrown.
+     *
+     * @param aCallback Code to be executed within the transaction.
+     *
+     * @throws RuntimeException DOCUMENT ME!
      */
     public void executeTransaction(
-            final TestTransactionCallbackWithoutResult aCallback) {
+        final TestTransactionCallbackWithoutResult aCallback) {
         executeTransaction(new TransactionCallbackWithoutResult() {
-            public void doInTransactionWithoutResult(TransactionStatus aArg) {
-                try {
-                    aCallback.execute();
-                } catch (Exception e) {
-                    // test case must fail.
-                    throw new RuntimeException(e.getMessage(), e);
+                public void doInTransactionWithoutResult(TransactionStatus aArg) {
+                    try {
+                        aCallback.execute();
+                    } catch (Exception e) {
+                        // test case must fail.
+                        throw new RuntimeException(e.getMessage(), e);
+                    }
                 }
-            }
-        });
+            });
     }
 
     /**
      * Executes an SQL query.
-     * 
-     * @param aSql
-     *            Query to execute.
+     *
+     * @param aSql Query to execute.
+     *
      * @return Result set.
      */
     public ResultSet executeQuery(String aSql) {
@@ -395,14 +446,13 @@ public class DatabaseTesterComponent {
     }
 
     /**
-     * Executes a query with a single argument. See
-     * {@link #setPreparedParam(int, PreparedStatement, Object)}for details on
+     * Executes a query with a single argument. See {@link
+     * #setPreparedParam(int, PreparedStatement, Object)}for details on
      * supported argument types.
-     * 
-     * @param aSql
-     *            Query.
-     * @param aArg
-     *            Argument.
+     *
+     * @param aSql Query.
+     * @param aArg Argument.
+     *
      * @return Result set.
      */
     public ResultSet executeQuery(String aSql, Object aArg) {
@@ -410,21 +460,21 @@ public class DatabaseTesterComponent {
     }
 
     /**
-     * Executes a query. See
-     * {@link #setPreparedParam(int, PreparedStatement, Object)}for details on
-     * supported argument types.
-     * 
-     * @param aSql
-     *            Sql query.
-     * @param aArgs
-     *            Arguments to the query.
+     * Executes a query. See {@link #setPreparedParam(int,
+     * PreparedStatement, Object)}for details on supported argument types.
+     *
+     * @param aSql Sql query.
+     * @param aArgs Arguments to the query.
+     *
      * @return Result set.
+     *
+     * @throws RuntimeException DOCUMENT ME!
      */
     public ResultSet executeQuery(final String aSql, final Object[] aArgs) {
         try {
-            Connection connection = getConnection();
+            Connection        connection = getConnection();
 
-            PreparedStatement statement = connection.prepareStatement(aSql);
+            PreparedStatement statement  = connection.prepareStatement(aSql);
             setPreparedParams(aArgs, statement);
 
             return statement.executeQuery();
@@ -434,18 +484,17 @@ public class DatabaseTesterComponent {
     }
 
     /**
-     * Sets the values of a prepared statement. See
-     * {@link #setPreparedParam(int, PreparedStatement, Object)}for details on
+     * Sets the values of a prepared statement. See {@link
+     * #setPreparedParam(int, PreparedStatement, Object)}for details on
      * supported argument types.
-     * 
-     * @param aArgs
-     *            Arguments to the prepared statement.
-     * @param aStatement
-     *            Prepared statement
+     *
+     * @param aArgs 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]);
         }
@@ -453,17 +502,15 @@ public class DatabaseTesterComponent {
 
     /**
      * Sets a prepared statement parameter.
-     * 
-     * @param aIndex
-     *            Index of the parameter.
-     * @param aStatement
-     *            Prepared statement.
-     * @param aObject
-     *            Value Must be of type Integer, Long, or String. 
+     *
+     * @param aIndex Index of the parameter.
+     * @param aStatement Prepared statement.
+     * @param aObject 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) {
@@ -472,21 +519,29 @@ public class DatabaseTesterComponent {
             aStatement.setString(aIndex, (String) aObject);
         } else {
             TestCase.fail("Unsupported object type for prepared statement: "
-                    + aObject.getClass() + " value: " + aObject
-                    + " statement: " + aStatement);
+                + aObject.getClass() + " value: " + aObject + " statement: "
+                + aStatement);
         }
     }
 
+    /**
+     * DOCUMENT ME!
+     *
+     * @return DOCUMENT ME!
+     */
     private boolean isDatabaseConfigured() {
         try {
             getDataSource();
         } catch (NoSuchBeanDefinitionException e) {
             return false;
         }
+
         return true;
     }
 
     /**
+     * DOCUMENT ME!
+     *
      * @return Returns the dataSource.
      */
     public DataSource getDataSource() {
@@ -494,20 +549,34 @@ public class DatabaseTesterComponent {
     }
 
     /**
+     * DOCUMENT ME!
+     *
+     * @param aTable DOCUMENT ME!
+     *
      * @return
+     *
      * @throws SQLException
      */
     public int getTableSize(final String aTable) throws SQLException {
-
         ResultSet resultSet = executeQuery("select * from " + aTable);
-        int count = 0;
+        int       count     = 0;
 
         while (resultSet.next()) {
             count++;
         }
+
         return count;
     }
 
+    /**
+     * DOCUMENT ME!
+     *
+     * @param aResultSet DOCUMENT ME!
+     *
+     * @return DOCUMENT ME!
+     *
+     * @throws SQLException DOCUMENT ME!
+     */
     public int countResultSet(ResultSet aResultSet) throws SQLException {
         int count = 0;
 
@@ -517,5 +586,4 @@ public class DatabaseTesterComponent {
 
         return count;
     }
-
 }