2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.test.spring;
19 import java.sql.Connection;
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
27 import java.util.TreeMap;
29 import javax.sql.DataSource;
31 import junit.framework.TestCase;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.dbunit.DatabaseUnitException;
36 import org.dbunit.database.DatabaseConnection;
37 import org.dbunit.database.DatabaseSequenceFilter;
38 import org.dbunit.database.IDatabaseConnection;
39 import org.dbunit.dataset.FilteredDataSet;
40 import org.dbunit.dataset.IDataSet;
41 import org.dbunit.dataset.filter.ITableFilter;
42 import org.dbunit.operation.DatabaseOperation;
43 import org.hibernate.SessionFactory;
44 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
45 import org.springframework.beans.factory.config.BeanDefinition;
46 import org.springframework.beans.factory.support.RootBeanDefinition;
47 import org.springframework.context.ApplicationContext;
48 import org.springframework.context.support.ClassPathXmlApplicationContext;
49 import org.springframework.context.support.GenericApplicationContext;
50 import org.springframework.jdbc.core.JdbcTemplate;
51 import org.springframework.jdbc.datasource.DataSourceUtils;
52 import org.springframework.jdbc.datasource.DriverManagerDataSource;
53 import org.springframework.orm.hibernate3.HibernateTemplate;
54 import org.springframework.transaction.PlatformTransactionManager;
55 import org.springframework.transaction.TransactionDefinition;
56 import org.springframework.transaction.TransactionStatus;
57 import org.springframework.transaction.support.DefaultTransactionDefinition;
58 import org.springframework.transaction.support.TransactionCallback;
59 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
60 import org.springframework.transaction.support.TransactionTemplate;
61 import org.wamblee.general.BeanKernel;
62 import org.wamblee.persistence.hibernate.HibernateMappingFiles;
65 * Test case support class for spring tests.
67 public class SpringTestCase extends TestCase {
69 private static final Log LOG = LogFactory.getLog(SpringTestCase.class);
72 * Session factory bean name.
74 private static final String SESSION_FACTORY = "sessionFactory";
77 * Data source bean name.
79 private static final String DATA_SOURCE = "dataSource";
82 * Transaction manager bean name.
84 private static final String TRANSACTION_MANAGER = "transactionManager";
87 * Name of the ConfigFileList bean that describes the Hibernate mapping
90 private static final String HIBERNATE_CONFIG_FILES = "hibernateMappingFiles";
95 private static final String SCHEMA_PATTERN = "%";
98 * List of (String) configuration file locations for spring.
100 private String[] _configLocations;
103 * Application context for storing bean definitions that vary on a test by
104 * test basis and cannot be hardcoded in the spring configuration files.
106 private GenericApplicationContext _parentContext;
109 * Cached spring application context.
111 private ApplicationContext _context;
113 public SpringTestCase(Class<? extends SpringConfigFiles> aSpringFiles,
114 Class<? extends HibernateMappingFiles> aMappingFiles) {
116 SpringConfigFiles springFiles = aSpringFiles.newInstance();
117 _configLocations = springFiles.toArray(new String[0]);
118 } catch (Exception e) {
119 fail("Could not construct spring config files class '"
120 + aSpringFiles.getName() + "'");
123 // Register the Hibernate mapping files as a bean.
124 _parentContext = new GenericApplicationContext();
125 BeanDefinition lDefinition = new RootBeanDefinition(aMappingFiles);
126 _parentContext.registerBeanDefinition(HIBERNATE_CONFIG_FILES,
128 _parentContext.refresh();
133 * Gets the spring context.
135 * @return Spring context.
137 protected synchronized ApplicationContext getSpringContext() {
138 if (_context == null) {
139 _context = new ClassPathXmlApplicationContext(
140 (String[]) _configLocations, _parentContext);
146 * @return Hibernate session factory.
148 protected SessionFactory getSessionFactory() {
149 SessionFactory factory = (SessionFactory) getSpringContext().getBean(
151 assertNotNull(factory);
155 protected void setUp() throws Exception {
156 LOG.info("Performing setUp()");
160 _context = null; // make sure we get a new application context for
164 BeanKernel.overrideBeanFactory(new TestSpringBeanFactory(
165 getSpringContext()));
173 * @see junit.framework.TestCase#tearDown()
176 protected void tearDown() throws Exception {
180 LOG.info("tearDown() complete");
185 * @return Transaction manager
187 protected PlatformTransactionManager getTransactionManager() {
188 PlatformTransactionManager manager = (PlatformTransactionManager) getSpringContext()
189 .getBean(TRANSACTION_MANAGER);
190 assertNotNull(manager);
195 * @return Starts a new transaction.
197 protected TransactionStatus getTransaction() {
198 DefaultTransactionDefinition def = new DefaultTransactionDefinition();
199 def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
201 return getTransactionManager().getTransaction(def);
205 * Returns the hibernate template for executing hibernate-specific
208 * @return Hibernate template.
210 protected HibernateTemplate getTemplate() {
211 HibernateTemplate template = (HibernateTemplate) getSpringContext()
212 .getBean(HibernateTemplate.class.getName());
213 assertNotNull(template);
218 * Flushes the session. Should be called after some Hibernate work and
219 * before JDBC is used to check results.
222 protected void flush() {
223 getTemplate().flush();
227 * Flushes the session first and then removes all objects from the Session
228 * cache. Should be called after some Hibernate work and before JDBC is used
232 protected void clear() {
234 getTemplate().clear();
238 * Evicts the object from the session. This is essential for the
239 * implementation of unit tests where first an object is saved and is
240 * retrieved later. By removing the object from the session, Hibernate must
241 * retrieve the object again from the database.
245 protected void evict(Object aObject) {
246 getTemplate().evict(aObject);
250 * Gets the connection.
252 * @return Connection.
254 public Connection getConnection() {
255 return DataSourceUtils.getConnection(getDataSource());
258 public void cleanDatabase() throws SQLException {
260 if (!isDatabaseConfigured()) {
264 String[] tables = getTableNames();
267 IDatabaseConnection connection = new DatabaseConnection(
269 ITableFilter filter = new DatabaseSequenceFilter(connection, tables);
270 IDataSet dataset = new FilteredDataSet(filter, connection
271 .createDataSet(tables));
273 DatabaseOperation.DELETE_ALL.execute(connection, dataset);
274 } catch (DatabaseUnitException e) {
275 SQLException exc = new SQLException(e.getMessage());
282 * @throws SQLException
284 public String[] getTableNames() throws SQLException {
286 List<String> result = new ArrayList<String>();
287 LOG.debug("Getting database table names to clean (schema: '"
288 + SCHEMA_PATTERN + "'");
290 ResultSet tables = getConnection().getMetaData().getTables(null,
291 SCHEMA_PATTERN, "%", new String[] { "TABLE" });
292 while (tables.next()) {
293 String table = tables.getString("TABLE_NAME");
294 // Make sure we do not touch hibernate's specific
295 // infrastructure tables.
296 if (!table.toLowerCase().startsWith("hibernate")) {
298 LOG.debug("Adding " + table
299 + " to list of tables to be cleaned.");
302 return (String[]) result.toArray(new String[0]);
307 * @throws SQLException
309 public void emptyTables(List aTableList) throws SQLException {
310 Iterator liTable = aTableList.iterator();
311 while (liTable.hasNext()) {
312 emptyTable((String) liTable.next());
318 * @throws SQLException
320 public void emptyTable(String aTable) throws SQLException {
321 executeSql("delete from " + aTable);
326 * @throws SQLException
328 public void dropTable(String aTable) throws SQLException {
329 executeQuery("drop table " + aTable);
333 * Executes an SQL statement within a transaction.
337 * @return Return code of the corresponding JDBC call.
339 public int executeSql(final String aSql) {
340 return executeSql(aSql, new Object[0]);
344 * Executes an SQL statement within a transaction. See
345 * {@link #setPreparedParam(int, PreparedStatement, Object)}for details on
346 * supported argument types.
351 * Argument of the sql statement.
352 * @return Return code of the corresponding JDBC call.
354 public int executeSql(final String aSql, final Object aArg) {
355 return executeSql(aSql, new Object[] { aArg });
359 * Executes an sql statement. See
360 * {@link #setPreparedParam(int, PreparedStatement, Object)}for details on
361 * supported argument types.
364 * SQL query to execute.
367 * @return Number of rows updated.
369 public int executeSql(final String aSql, final Object[] aArgs) {
370 Map results = executeTransaction(new TestTransactionCallback() {
371 public Map execute() throws Exception {
372 JdbcTemplate template = new JdbcTemplate(getDataSource());
373 int result = template.update(aSql, aArgs);
375 Map<String, Integer> map = new TreeMap<String, Integer>();
376 map.put("result", new Integer(result));
382 return ((Integer) results.get("result")).intValue();
386 * Executes a transaction with a result.
389 * Callback to do your transactional work.
392 public Object executeTransaction(TransactionCallback aCallback) {
393 TransactionTemplate lTemplate = new TransactionTemplate(
394 getTransactionManager());
395 return lTemplate.execute(aCallback);
399 * Executes a transaction without a result.
402 * Callback to do your transactional work. .
404 protected void executeTransaction(TransactionCallbackWithoutResult aCallback) {
405 TransactionTemplate template = new TransactionTemplate(
406 getTransactionManager());
407 template.execute(aCallback);
411 * Executes a transaction with a result, causing the testcase to fail if any
412 * type of exception is thrown.
415 * Code to be executed within the transaction.
418 public Map executeTransaction(final TestTransactionCallback aCallback) {
419 return (Map) executeTransaction(new TransactionCallback() {
420 public Object doInTransaction(TransactionStatus aArg) {
422 return aCallback.execute();
423 } catch (Exception e) {
424 // test case must fail.
426 throw new RuntimeException(e);
433 * Executes a transaction with a result, causing the testcase to fail if any
434 * type of exception is thrown.
437 * Code to be executed within the transaction.
439 public void executeTransaction(
440 final TestTransactionCallbackWithoutResult aCallback) {
441 executeTransaction(new TransactionCallbackWithoutResult() {
442 public void doInTransactionWithoutResult(TransactionStatus aArg) {
445 } catch (Exception e) {
446 // test case must fail.
447 throw new RuntimeException(e.getMessage(), e);
454 * Executes an SQL query.
458 * @return Result set.
460 public ResultSet executeQuery(String aSql) {
461 return executeQuery(aSql, new Object[0]);
465 * Executes a query with a single argument. See
466 * {@link #setPreparedParam(int, PreparedStatement, Object)}for details on
467 * supported argument types.
473 * @return Result set.
475 public ResultSet executeQuery(String aSql, Object aArg) {
476 return executeQuery(aSql, new Object[] { aArg });
480 * Executes a query. See
481 * {@link #setPreparedParam(int, PreparedStatement, Object)}for details on
482 * supported argument types.
487 * Arguments to the query.
488 * @return Result set.
490 public ResultSet executeQuery(final String aSql, final Object[] aArgs) {
492 Connection connection = getConnection();
494 PreparedStatement statement = connection.prepareStatement(aSql);
495 setPreparedParams(aArgs, statement);
497 return statement.executeQuery();
498 } catch (SQLException e) {
499 throw new RuntimeException(e);
504 * Sets the values of a prepared statement. See
505 * {@link #setPreparedParam(int, PreparedStatement, Object)}for details on
506 * supported argument types.
509 * Arguments to the prepared statement.
512 * @throws SQLException
514 private void setPreparedParams(final Object[] aArgs,
515 PreparedStatement aStatement) throws SQLException {
516 for (int i = 1; i <= aArgs.length; i++) {
517 setPreparedParam(i, aStatement, aArgs[i - 1]);
522 * Sets a prepared statement parameter.
525 * Index of the parameter.
527 * Prepared statement.
529 * Value Must be of type Integer, Long, or String. TODO extend
530 * with more types of values.
531 * @throws SQLException
533 private void setPreparedParam(int aIndex, PreparedStatement aStatement,
534 Object aObject) throws SQLException {
535 if (aObject instanceof Integer) {
536 aStatement.setInt(aIndex, ((Integer) aObject).intValue());
537 } else if (aObject instanceof Long) {
538 aStatement.setLong(aIndex, ((Integer) aObject).longValue());
539 } else if (aObject instanceof String) {
540 aStatement.setString(aIndex, (String) aObject);
542 TestCase.fail("Unsupported object type for prepared statement: "
543 + aObject.getClass() + " value: " + aObject
544 + " statement: " + aStatement);
548 private boolean isDatabaseConfigured() {
551 } catch (NoSuchBeanDefinitionException e) {
558 * @return Returns the dataSource.
560 public DataSource getDataSource() {
561 DataSource ds = (DriverManagerDataSource) getSpringContext().getBean(
569 * @throws SQLException
571 protected int getTableSize(final String aTable) throws SQLException {
573 ResultSet resultSet = executeQuery("select * from " + aTable);
576 while (resultSet.next()) {
582 protected int countResultSet(ResultSet aResultSet) throws SQLException {
585 while (aResultSet.next()) {