From: <fg...@us...> - 2008-01-12 10:46:19
|
Revision: 525 http://openutils.svn.sourceforge.net/openutils/?rev=525&view=rev Author: fgiust Date: 2008-01-12 02:46:12 -0800 (Sat, 12 Jan 2008) Log Message: ----------- cleaning up Modified Paths: -------------- trunk/openutils-testing/pom.xml trunk/openutils-testing/src/main/java/it/openutils/testing/DateAssert.java trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitConfiguration.java trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitUtils.java trunk/openutils-testing/src/main/java/it/openutils/testing/RegExpTableFilter.java trunk/openutils-testing-junit/pom.xml trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/GenericsDbUnitTestCase.java trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/SpringTestCase.java trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/AbstractDbUnitTestNGSpringContextTests.java trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/DbUnitTestCase.java trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/GenericsDbUnitTestCase.java trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/SpringTestCase.java trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/utils/DateAssert.java trunk/openutils-testing-testng/src/test/java/it/openutils/testing/testng/MultipleDatasourceDbUnitTest.java trunk/openutils-testing-testng/src/test/java/it/openutils/testing/testng/SingleDatasourceDbUnitTest.java Added Paths: ----------- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/AbstractDbUnitJunitSpringContextTests.java Modified: trunk/openutils-testing/pom.xml =================================================================== --- trunk/openutils-testing/pom.xml 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing/pom.xml 2008-01-12 10:46:12 UTC (rev 525) @@ -28,6 +28,17 @@ <version>2.3</version> </dependency> <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> <groupId>org.dbunit</groupId> <artifactId>dbunit</artifactId> <version>2.2</version> Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DateAssert.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DateAssert.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DateAssert.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.testing; import java.text.DateFormat; Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitConfiguration.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitConfiguration.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitConfiguration.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.testing; import java.lang.annotation.Documented; Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.testing; import java.lang.annotation.Documented; @@ -7,7 +22,9 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.dbunit.operation.DatabaseOperation; + /** * @author fgiust * @version $Id: $ @@ -31,4 +48,8 @@ boolean truncateAll() default true; + Class< ? extends DatabaseOperation> truncateOperation() default org.dbunit.operation.TruncateTableOperation.class; + + Class< ? extends DatabaseOperation> insertOperation() default org.dbunit.operation.InsertOperation.class; + } \ No newline at end of file Added: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java (rev 0) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java 2008-01-12 10:46:12 UTC (rev 525) @@ -0,0 +1,246 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.testing; + +import java.io.IOException; +import java.net.URL; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +import javax.sql.DataSource; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; +import org.dbunit.DatabaseUnitException; +import org.dbunit.database.DatabaseConnection; +import org.dbunit.database.DatabaseSequenceFilter; +import org.dbunit.database.IDatabaseConnection; +import org.dbunit.dataset.DataSetException; +import org.dbunit.dataset.FilteredDataSet; +import org.dbunit.dataset.IDataSet; +import org.dbunit.dataset.filter.ITableFilter; +import org.dbunit.dataset.filter.SequenceTableFilter; +import org.dbunit.operation.DatabaseOperation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; + + +/** + * @author fgiust + * @version $Id: $ + */ +public class DbUnitTestContext +{ + + /** + * Logger. + */ + private static Logger log = LoggerFactory.getLogger(DbUnitTestContext.class); + + private Object testcase; + + private ApplicationContext applicationContext; + + /** + * Dataset cache. + */ + private Map<String, IDataSet> datasetCache = new HashMap<String, IDataSet>(); + + /** + * Truncate dataset cache. This is kept as a static attribute since the creation of the dataset is very expensive + * and it doesn't change across tests. + */ + protected static Map<String, IDataSet> truncateDataSetCache = new HashMap<String, IDataSet>(); + + public DbUnitTestContext(Object testcase, ApplicationContext applicationContext) + { + this.testcase = testcase; + this.applicationContext = applicationContext; + } + + /** + * Setup the Database before running the test method. + * @throws Exception Any exception. + */ + @SuppressWarnings("unchecked") + public void setUpDbUnit() throws Exception + { + DbUnitExecution singleDbUnitExecution = testcase.getClass().getAnnotation(DbUnitExecution.class); + + DbUnitExecution[] executions; + if (singleDbUnitExecution != null) + { + executions = new DbUnitExecution[]{singleDbUnitExecution }; + } + else + { + DbUnitConfiguration dbUnitConfiguration = testcase.getClass().getAnnotation(DbUnitConfiguration.class); + executions = dbUnitConfiguration.dbUnitExecutions(); + } + + for (DbUnitExecution dbUnitExecution : executions) + { + String[] datasets = dbUnitExecution.datasets(); + String dataSourceName = dbUnitExecution.dataSource(); + String schema = dbUnitExecution.schema(); + if (StringUtils.isEmpty(schema)) + { + schema = null; + } + + IDatabaseConnection connection = new DatabaseConnection( + getDatasource(dataSourceName).getConnection(), + schema); + + try + { + ITableFilter tableFilter = new RegExpTableFilter(dbUnitExecution.excludedTables()); + + if (dbUnitExecution.truncateAll()) + { + truncateAll(connection, tableFilter, dataSourceName, dbUnitExecution + .truncateOperation() + .newInstance()); + } + + DatabaseOperation dbOperation = dbUnitExecution.insertOperation().newInstance(); + for (String datasetFile : datasets) + { + importDataSet(createDataset(datasetFile), connection, tableFilter, dataSourceName, dbOperation); + } + } + finally + { + connection.close(); + } + } + } + + /** + * @param datasetFile + * @return + * @throws IOException + * @throws DataSetException + */ + private IDataSet createDataset(String datasetFile) throws IOException, DataSetException + { + IDataSet dataSet = datasetCache.get(datasetFile); + if (dataSet == null) + { + URL datasetUrl = getClass().getResource(datasetFile); + if (datasetUrl == null) + { + throw new IllegalArgumentException("Dataset " + datasetFile + " not found"); + } + dataSet = DbUnitUtils.loadDataset(datasetUrl); + datasetCache.put(datasetFile, dataSet); + } + return dataSet; + } + + private void importDataSet(IDataSet dataSet, IDatabaseConnection connection, ITableFilter tableFilter, + String dataSourceName, DatabaseOperation databaseOperation) throws SQLException, DataSetException, + DatabaseUnitException + { + + if (dataSet == null) + { + throw new IllegalArgumentException("dataSet is null"); + } + + IDataSet orderedDataset = new FilteredDataSet(tableFilter, dataSet); + if (log.isDebugEnabled()) + { + log.debug("Tables: {}", ArrayUtils.toString(orderedDataset.getTableNames())); + } + + // if a sorted dataset is available, use table sequence for sorting + IDataSet truncateDataSet = getTruncateDataset(dataSourceName, connection.getSchema()); + if (truncateDataSet != null) + { + ITableFilter filter = new SequenceTableFilter(truncateDataSet.getTableNames()); + orderedDataset = new FilteredDataSet(filter, dataSet); + } + + if (dataSet != null) + { + databaseOperation.execute(connection, orderedDataset); + } + + } + + /** + * @param connection + * @param tableFilter + * @throws SQLException + * @throws DataSetException + * @throws DatabaseUnitException + */ + private void truncateAll(IDatabaseConnection connection, ITableFilter tableFilter, String dataSourceName, + DatabaseOperation databaseOperation) throws SQLException, DataSetException, DatabaseUnitException + { + IDataSet truncateDataSet = getTruncateDataset(dataSourceName, connection.getSchema()); + if (truncateDataSet == null) + { + log.debug("Generating sorted dataset for initial cleanup"); + IDataSet unsortedDataSet = connection.createDataSet(); + + log.debug("Unfiltered truncateDataSet: {}", ArrayUtils.toString(unsortedDataSet.getTableNames())); + + ITableFilter filter = new DatabaseSequenceFilter(connection, new FilteredDataSet( + tableFilter, + unsortedDataSet).getTableNames()); + truncateDataSet = new FilteredDataSet(tableFilter, new FilteredDataSet(filter, unsortedDataSet)); + storeTruncateDataset(dataSourceName, connection.getSchema(), truncateDataSet); + log.debug("Sorted dataset generated"); + } + + log.debug("Tables truncateDataSet: {}", ArrayUtils.toString(truncateDataSet.getTableNames())); + if (truncateDataSet != null) + { + databaseOperation.execute(connection, truncateDataSet); + } + } + + private IDataSet getTruncateDataset(String datasourceName, String schema) + { + return truncateDataSetCache.get(datasourceName + "_" + StringUtils.defaultString(schema)); + } + + private IDataSet storeTruncateDataset(String datasourceName, String schema, IDataSet dataset) + { + return truncateDataSetCache.put(datasourceName + "_" + StringUtils.defaultString(schema), dataset); + } + + @SuppressWarnings("unchecked") + protected DataSource getDatasource(String name) + { + if (StringUtils.isEmpty(name)) + { + Map<String, DataSource> dsMap = applicationContext.getBeansOfType(DataSource.class); + if (dsMap == null || dsMap.size() != 1) + { + throw new RuntimeException( + "Unable to find a datasource in spring applicationContext, please specify the datasource bean name " + + "using the \"datasource\" attribute of @DbUnitConfiguration"); + } + return dsMap.values().iterator().next(); + } + return (DataSource) applicationContext.getBean(name); + } +} Property changes on: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitUtils.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitUtils.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitUtils.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.testing; import java.io.File; Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/RegExpTableFilter.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/RegExpTableFilter.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/RegExpTableFilter.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.testing; import java.util.regex.Pattern; Modified: trunk/openutils-testing-junit/pom.xml =================================================================== --- trunk/openutils-testing-junit/pom.xml 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing-junit/pom.xml 2008-01-12 10:46:12 UTC (rev 525) @@ -97,7 +97,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.4</version> </dependency> <dependency> <groupId>org.hibernate</groupId> Added: trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/AbstractDbUnitJunitSpringContextTests.java =================================================================== --- trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/AbstractDbUnitJunitSpringContextTests.java (rev 0) +++ trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/AbstractDbUnitJunitSpringContextTests.java 2008-01-12 10:46:12 UTC (rev 525) @@ -0,0 +1,54 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.testing.junit; + +import it.openutils.testing.DbUnitTestContext; + +import org.junit.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + + +/** + * @author fgiust + * @version $Id: $ + */ +public class AbstractDbUnitJunitSpringContextTests extends AbstractJUnit4SpringContextTests +{ + + /** + * Slf4j logger that can be reused in subclasses. + */ + protected Logger log = LoggerFactory.getLogger(getClass()); + + /** + * DBUnit text context. + */ + private DbUnitTestContext dbUnitTestContext; + + /** + * Setup the Database before running the test method. + * @throws Exception Any exception. + */ + @SuppressWarnings("unchecked") + @Before + protected void setUpDbUnit() throws Exception + { + dbUnitTestContext = new DbUnitTestContext(this, applicationContext); + dbUnitTestContext.setUpDbUnit(); + } +} Property changes on: trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/AbstractDbUnitJunitSpringContextTests.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Modified: trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java =================================================================== --- trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,5 +1,5 @@ /* - * Copyright 2005 Fabrizio Giustina. + * Copyright Openmind http://www.openmindonline.it * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,7 +45,9 @@ * Base class for running DAO tests. * @author fgiust * @version $Revision $ ($Author $) + * @deprecated use {@link AbstractDbUnitJunitSpringContextTests} */ +@Deprecated public abstract class DbUnitTestCase extends SpringTestCase { Modified: trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/GenericsDbUnitTestCase.java =================================================================== --- trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/GenericsDbUnitTestCase.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/GenericsDbUnitTestCase.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,5 +1,5 @@ /* - * Copyright 2005 Fabrizio Giustina. + * Copyright Openmind http://www.openmindonline.it * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,9 @@ * from Spring is made available through the <code>instance</code> protected field. * @author fgiust * @version $Id$ + * @deprecated use {@link AbstractDbUnitJunitSpringContextTests} */ +@Deprecated public class GenericsDbUnitTestCase<D> extends DbUnitTestCase { Modified: trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/SpringTestCase.java =================================================================== --- trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/SpringTestCase.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/SpringTestCase.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,5 +1,5 @@ /* - * Copyright 2005 Fabrizio Giustina. + * Copyright Openmind http://www.openmindonline.it * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,7 +27,9 @@ * Base class for running spring-based tests. * @author fgiust * @version $Revision $ ($Author $) + * @deprecated {@link org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests} */ +@Deprecated public abstract class SpringTestCase extends TestCase { @@ -56,7 +58,7 @@ if (ctx == null) { // load Spring's BeanFactory - String[] paths = {DEFAULT_TEST_CONTEXT_FILE}; + String[] paths = {DEFAULT_TEST_CONTEXT_FILE }; ctx = new ClassPathXmlApplicationContext(paths); } Modified: trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/AbstractDbUnitTestNGSpringContextTests.java =================================================================== --- trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/AbstractDbUnitTestNGSpringContextTests.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/AbstractDbUnitTestNGSpringContextTests.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,30 +1,22 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.testing.testng; -import it.openutils.testing.DbUnitConfiguration; -import it.openutils.testing.DbUnitExecution; -import it.openutils.testing.DbUnitUtils; -import it.openutils.testing.RegExpTableFilter; +import it.openutils.testing.DbUnitTestContext; -import java.io.IOException; -import java.net.URL; -import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; - -import javax.sql.DataSource; - -import org.apache.commons.lang.ArrayUtils; -import org.apache.commons.lang.StringUtils; -import org.dbunit.DatabaseUnitException; -import org.dbunit.database.DatabaseConnection; -import org.dbunit.database.DatabaseSequenceFilter; -import org.dbunit.database.IDatabaseConnection; -import org.dbunit.dataset.DataSetException; -import org.dbunit.dataset.FilteredDataSet; -import org.dbunit.dataset.IDataSet; -import org.dbunit.dataset.filter.ITableFilter; -import org.dbunit.dataset.filter.SequenceTableFilter; -import org.dbunit.operation.DatabaseOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; @@ -39,22 +31,16 @@ { /** - * Logger. + * Slf4j logger that can be reused in subclasses. */ - private Logger log = LoggerFactory.getLogger(getClass()); + protected Logger log = LoggerFactory.getLogger(getClass()); /** - * Dataset cache. + * DBUnit text context. */ - private Map<String, IDataSet> datasetCache = new HashMap<String, IDataSet>(); + private DbUnitTestContext dbUnitTestContext; /** - * Truncate dataset cache. This is kept as a static attribute since the creation of the dataset is very expensive - * and it doesn't change across tests. - */ - protected static Map<String, IDataSet> truncateDataSetCache = new HashMap<String, IDataSet>(); - - /** * Setup the Database before running the test method. * @throws Exception Any exception. */ @@ -62,161 +48,8 @@ @BeforeMethod protected void setUpDbUnit() throws Exception { - DbUnitExecution singleDbUnitExecution = getClass().getAnnotation(DbUnitExecution.class); - - DbUnitExecution[] executions; - if (singleDbUnitExecution != null) - { - executions = new DbUnitExecution[]{singleDbUnitExecution }; - } - else - { - DbUnitConfiguration dbUnitConfiguration = getClass().getAnnotation(DbUnitConfiguration.class); - executions = dbUnitConfiguration.dbUnitExecutions(); - } - - for (DbUnitExecution dbUnitExecution : executions) - { - String[] datasets = dbUnitExecution.datasets(); - String dataSourceName = dbUnitExecution.dataSource(); - String schema = dbUnitExecution.schema(); - if (StringUtils.isEmpty(schema)) - { - schema = null; - } - - IDatabaseConnection connection = new DatabaseConnection( - getDatasource(dataSourceName).getConnection(), - schema); - - try - { - ITableFilter tableFilter = new RegExpTableFilter(dbUnitExecution.excludedTables()); - - if (dbUnitExecution.truncateAll()) - { - truncateAll(connection, tableFilter, dataSourceName); - } - - for (String datasetFile : datasets) - { - importDataSet(createDataset(datasetFile), connection, tableFilter, dataSourceName); - } - } - finally - { - connection.close(); - } - } + dbUnitTestContext = new DbUnitTestContext(this, applicationContext); + dbUnitTestContext.setUpDbUnit(); } - /** - * @param datasetFile - * @return - * @throws IOException - * @throws DataSetException - */ - private IDataSet createDataset(String datasetFile) throws IOException, DataSetException - { - IDataSet dataSet = datasetCache.get(datasetFile); - if (dataSet == null) - { - URL datasetUrl = getClass().getResource(datasetFile); - if (datasetUrl == null) - { - throw new IllegalArgumentException("Dataset " + datasetFile + " not found"); - } - dataSet = DbUnitUtils.loadDataset(datasetUrl); - datasetCache.put(datasetFile, dataSet); - } - return dataSet; - } - - private void importDataSet(IDataSet dataSet, IDatabaseConnection connection, ITableFilter tableFilter, - String dataSourceName) throws SQLException, DataSetException, DatabaseUnitException - { - - if (dataSet == null) - { - throw new IllegalArgumentException("dataSet is null"); - } - IDataSet orderedDataset = new FilteredDataSet(tableFilter, dataSet); - - log.debug("Tables: {}", ArrayUtils.toString(orderedDataset.getTableNames())); - - // if a sorted dataset is available, use table sequence for sorting - IDataSet truncateDataSet = getTruncateDataset(dataSourceName, connection.getSchema()); - if (truncateDataSet != null) - { - ITableFilter filter = new SequenceTableFilter(truncateDataSet.getTableNames()); - orderedDataset = new FilteredDataSet(filter, dataSet); - } - - if (dataSet != null) - { - DatabaseOperation.INSERT.execute(connection, orderedDataset); - } - - } - - /** - * @param connection - * @param tableFilter - * @throws SQLException - * @throws DataSetException - * @throws DatabaseUnitException - */ - private void truncateAll(IDatabaseConnection connection, ITableFilter tableFilter, String dataSourceName) - throws SQLException, DataSetException, DatabaseUnitException - { - IDataSet truncateDataSet = getTruncateDataset(dataSourceName, connection.getSchema()); - if (truncateDataSet == null) - { - log.debug("Generating sorted dataset for initial cleanup"); - IDataSet unsortedDataSet = connection.createDataSet(); - - log.debug("Unfiltered truncateDataSet: {}", ArrayUtils.toString(unsortedDataSet.getTableNames())); - - ITableFilter filter = new DatabaseSequenceFilter(connection, new FilteredDataSet( - tableFilter, - unsortedDataSet).getTableNames()); - truncateDataSet = new FilteredDataSet(tableFilter, new FilteredDataSet(filter, unsortedDataSet)); - storeTruncateDataset(dataSourceName, connection.getSchema(), truncateDataSet); - log.debug("Sorted dataset generated"); - } - - log.debug("Tables truncateDataSet: {}", ArrayUtils.toString(truncateDataSet.getTableNames())); - if (truncateDataSet != null) - { - DatabaseOperation.DELETE_ALL.execute(connection, truncateDataSet); - } - } - - private IDataSet getTruncateDataset(String datasourceName, String schema) - { - return truncateDataSetCache.get(datasourceName + "_" + StringUtils.defaultString(schema)); - } - - private IDataSet storeTruncateDataset(String datasourceName, String schema, IDataSet dataset) - { - return truncateDataSetCache.put(datasourceName + "_" + StringUtils.defaultString(schema), dataset); - } - - @SuppressWarnings("unchecked") - protected DataSource getDatasource(String name) - { - if (StringUtils.isEmpty(name)) - { - Map<String, DataSource> dsMap = applicationContext.getBeansOfType(DataSource.class); - if (dsMap == null || dsMap.size() != 1) - { - throw new RuntimeException( - "Unable to find a datasource in spring applicationContext, please specify the datasource bean name " - + "using the \"datasource\" attribute of @DbUnitConfiguration"); - } - return dsMap.values().iterator().next(); - } - return (DataSource) applicationContext.getBean(name); - } - } Modified: trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/DbUnitTestCase.java =================================================================== --- trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/DbUnitTestCase.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/DbUnitTestCase.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,5 +1,5 @@ /* - * Copyright 2005 Fabrizio Giustina. + * Copyright Openmind http://www.openmindonline.it * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. Modified: trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/GenericsDbUnitTestCase.java =================================================================== --- trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/GenericsDbUnitTestCase.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/GenericsDbUnitTestCase.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,5 +1,5 @@ /* - * Copyright 2005 Fabrizio Giustina. + * Copyright Openmind http://www.openmindonline.it * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. Modified: trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/SpringTestCase.java =================================================================== --- trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/SpringTestCase.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/SpringTestCase.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,5 +1,5 @@ /* - * Copyright 2005 Fabrizio Giustina. + * Copyright Openmind http://www.openmindonline.it * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. Modified: trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/utils/DateAssert.java =================================================================== --- trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/utils/DateAssert.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/utils/DateAssert.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,5 +1,17 @@ /* - * Copyright (c) Openmind. All rights reserved. http://www.openmindonline.it + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.testing.testng.utils; Modified: trunk/openutils-testing-testng/src/test/java/it/openutils/testing/testng/MultipleDatasourceDbUnitTest.java =================================================================== --- trunk/openutils-testing-testng/src/test/java/it/openutils/testing/testng/MultipleDatasourceDbUnitTest.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing-testng/src/test/java/it/openutils/testing/testng/MultipleDatasourceDbUnitTest.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.testing.testng; import it.openutils.testing.DbUnitConfiguration; Modified: trunk/openutils-testing-testng/src/test/java/it/openutils/testing/testng/SingleDatasourceDbUnitTest.java =================================================================== --- trunk/openutils-testing-testng/src/test/java/it/openutils/testing/testng/SingleDatasourceDbUnitTest.java 2008-01-09 23:06:51 UTC (rev 524) +++ trunk/openutils-testing-testng/src/test/java/it/openutils/testing/testng/SingleDatasourceDbUnitTest.java 2008-01-12 10:46:12 UTC (rev 525) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.testing.testng; import it.openutils.testing.DbUnitExecution; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |