From: <fg...@us...> - 2007-05-23 18:05:56
|
Revision: 333 http://svn.sourceforge.net/openutils/?rev=333&view=rev Author: fgiust Date: 2007-05-23 11:05:56 -0700 (Wed, 23 May 2007) Log Message: ----------- [maven-release-plugin] copy for tag openutils-testing-junit-1.1.1 Added Paths: ----------- tags/openutils-testing-junit-1.1.1/ tags/openutils-testing-junit-1.1.1/pom.xml tags/openutils-testing-junit-1.1.1/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java Removed Paths: ------------- tags/openutils-testing-junit-1.1.1/pom.xml tags/openutils-testing-junit-1.1.1/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java Copied: tags/openutils-testing-junit-1.1.1 (from rev 327, trunk/openutils-testing-junit) Deleted: tags/openutils-testing-junit-1.1.1/pom.xml =================================================================== --- trunk/openutils-testing-junit/pom.xml 2007-05-04 16:49:46 UTC (rev 327) +++ tags/openutils-testing-junit-1.1.1/pom.xml 2007-05-23 18:05:56 UTC (rev 333) @@ -1,58 +0,0 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils</artifactId> - <version>3</version> - <relativePath>..</relativePath> - </parent> - <artifactId>openutils-testing-junit</artifactId> - <name>openutils test utils (junit)</name> - <version>1.1.1-SNAPSHOT</version> - <description>openutils test utils</description> - <dependencies> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <version>1.2</version> - </dependency> - <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <version>1.2.13</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-core</artifactId> - <version>2.0.1</version> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-context</artifactId> - <version>2.0.1</version> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-hibernate3</artifactId> - <version>2.0.1</version> - <optional>true</optional> - </dependency> - <dependency> - <groupId>commons-lang</groupId> - <artifactId>commons-lang</artifactId> - <version>2.3</version> - </dependency> - <dependency> - <groupId>org.dbunit</groupId> - <artifactId>dbunit</artifactId> - <version>2.2</version> - <optional>true</optional> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>3.8.1</version> - </dependency> - </dependencies> -</project> \ No newline at end of file Copied: tags/openutils-testing-junit-1.1.1/pom.xml (from rev 332, trunk/openutils-testing-junit/pom.xml) =================================================================== --- tags/openutils-testing-junit-1.1.1/pom.xml (rev 0) +++ tags/openutils-testing-junit-1.1.1/pom.xml 2007-05-23 18:05:56 UTC (rev 333) @@ -0,0 +1,64 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils</artifactId> + <version>3</version> + <relativePath>..</relativePath> + </parent> + <artifactId>openutils-testing-junit</artifactId> + <name>openutils test utils (junit)</name> + <version>1.1.1</version> + <description>openutils test utils</description> + <dependencies> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.2</version> + </dependency> + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <version>1.2.13</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-core</artifactId> + <version>2.0.1</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + <version>2.0.1</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-hibernate3</artifactId> + <version>2.0.1</version> + <optional>true</optional> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.3</version> + </dependency> + <dependency> + <groupId>org.dbunit</groupId> + <artifactId>dbunit</artifactId> + <version>2.2</version> + <optional>true</optional> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>3.8.1</version> + </dependency> + </dependencies> + + <scm> + <connection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-testing-junit-1.1.1</connection> + <developerConnection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-testing-junit-1.1.1</developerConnection> + <url>http://openutils.svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-testing-junit-1.1.1</url> + </scm> +</project> \ No newline at end of file Deleted: tags/openutils-testing-junit-1.1.1/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java =================================================================== --- trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java 2007-05-04 16:49:46 UTC (rev 327) +++ tags/openutils-testing-junit-1.1.1/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java 2007-05-23 18:05:56 UTC (rev 333) @@ -1,223 +0,0 @@ -/* - * Copyright 2005 Fabrizio Giustina. - * - * 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 java.io.InputStream; -import java.util.Map; - -import javax.sql.DataSource; - -import org.apache.commons.lang.ClassUtils; -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.excel.XlsDataSet; -import org.dbunit.dataset.filter.ITableFilter; -import org.dbunit.dataset.filter.SequenceTableFilter; -import org.dbunit.dataset.xml.XmlDataSet; -import org.dbunit.ext.mssql.InsertIdentityOperation; -import org.dbunit.operation.DatabaseOperation; -import org.hibernate.SessionFactory; -import org.springframework.orm.hibernate3.SessionFactoryUtils; - - -/** - * Base class for running DAO tests. - * @author fgiust - * @version $Revision $ ($Author $) - */ -public abstract class DbUnitTestCase extends SpringTestCase -{ - - /** - * Hibernate session factory. - */ - private SessionFactory sessionFactory; - - private static final String BASETEST_DELETE = "/_BaseDAOTest-delete.xml"; - - protected static IDataSet truncateDataSet; - - /** - * Should use deferred close emulating the spring OpenSessionInView filter? Default is <code>true</code> - * @return <code>true</code> if deferred close should be used - */ - protected boolean mimicSessionFilter() - { - return true; - } - - /** - * {@inheritDoc} - */ - @SuppressWarnings("unchecked") - @Override - protected void setUp() throws Exception - { - super.setUp(); - - // insert values - IDataSet dataSet = null; - - String datesetFileName = "/" + ClassUtils.getShortClassName(getClass()) + "-load.xml"; - InputStream testData = getClass().getResourceAsStream(datesetFileName); - - if (testData != null) - { - if (log.isDebugEnabled()) - { - log.debug("loading dataset {}", datesetFileName); - } - - dataSet = new XmlDataSet(testData); - } - else - { - // check for excel - datesetFileName = "/" + ClassUtils.getShortClassName(getClass()) + "-load.xls"; - testData = getClass().getResourceAsStream(datesetFileName); - - if (testData != null) - { - if (log.isDebugEnabled()) - { - log.debug("loading dataset {}", datesetFileName); - } - - dataSet = new XlsDataSet(testData); - } - } - - if (dataSet == null) - { - log.debug("No test data found with name [{}]", datesetFileName); - } - else - { - - DataSource dataSource = (DataSource) ctx.getBean("dataSource"); - IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection()); - - // truncate common tables - if (truncateDataSet == null) - { - log.debug("Generating sorted dataset for initial cleanup"); - IDataSet unsortedTruncateDataSet = connection.createDataSet(); - ITableFilter filter = new DatabaseSequenceFilter(connection); - truncateDataSet = new FilteredDataSet(filter, unsortedTruncateDataSet); - log.debug("Sorted dataset generated"); - } - - IDataSet orderedDataset = dataSet; - - // if a sorted dataset is available, use table sequence for sorting - if (truncateDataSet == null) - { - ITableFilter filter = new SequenceTableFilter(truncateDataSet.getTableNames()); - orderedDataset = new FilteredDataSet(filter, dataSet); - } - - try - { - if (truncateDataSet != null) - { - DatabaseOperation.DELETE_ALL.execute(connection, truncateDataSet); - } - if (dataSet != null) - { - InsertIdentityOperation.INSERT.execute(connection, orderedDataset); - } - } - finally - { - connection.close(); - } - } - - // mimic the Spring OpenSessionInViewFilter - if (mimicSessionFilter()) - { - Map<String, SessionFactory> sfbeans = ctx.getBeansOfType(SessionFactory.class); - if (sfbeans.isEmpty()) - { - fail("No bean of type org.hibernate.SessionFactory found in spring context"); - } - this.sessionFactory = sfbeans.get(sfbeans.keySet().iterator().next()); - - SessionFactoryUtils.initDeferredClose(this.sessionFactory); - } - - } - - /** - * {@inheritDoc} - */ - @Override - protected void tearDown() throws Exception - { - if (mimicSessionFilter()) - { - // close open hibernate sessions, mimic the OpenSessionInViewFilter - SessionFactoryUtils.processDeferredClose(this.sessionFactory); - } - - // regenerate db initial state - String datesetFileName = "/initial-load.xml"; - InputStream testData = getClass().getResourceAsStream(datesetFileName); - - if (testData != null) - { - log.debug("Restoring db state"); - - IDataSet dataSet = new XmlDataSet(testData); - - DataSource dataSource = (DataSource) ctx.getBean("dataSource"); - IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection()); - - try - { - DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet); - } - finally - { - connection.close(); - } - } - - super.tearDown(); - } - - /** - * Returns the full test name. - * @see junit.framework.TestCase#getName() - */ - @Override - public String getName() - { - return ClassUtils.getShortClassName(this.getClass()) + "::" + super.getName(); - } - - /** - * return the current Hibernate SessionFactory - * @return SessionFactory object - */ - protected SessionFactory getSessionFactory() - { - return sessionFactory; - } -} \ No newline at end of file Copied: tags/openutils-testing-junit-1.1.1/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java (from rev 328, trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java) =================================================================== --- tags/openutils-testing-junit-1.1.1/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java (rev 0) +++ tags/openutils-testing-junit-1.1.1/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java 2007-05-23 18:05:56 UTC (rev 333) @@ -0,0 +1,251 @@ +/* + * Copyright 2005 Fabrizio Giustina. + * + * 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 java.io.InputStream; +import java.util.Map; + +import javax.sql.DataSource; + +import org.apache.commons.lang.ClassUtils; +import org.apache.commons.lang.StringUtils; +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.excel.XlsDataSet; +import org.dbunit.dataset.filter.AbstractTableFilter; +import org.dbunit.dataset.filter.ITableFilter; +import org.dbunit.dataset.filter.SequenceTableFilter; +import org.dbunit.dataset.xml.XmlDataSet; +import org.dbunit.ext.mssql.InsertIdentityOperation; +import org.dbunit.operation.DatabaseOperation; +import org.hibernate.SessionFactory; +import org.springframework.orm.hibernate3.SessionFactoryUtils; + + +/** + * Base class for running DAO tests. + * @author fgiust + * @version $Revision $ ($Author $) + */ +public abstract class DbUnitTestCase extends SpringTestCase +{ + + /** + * Hibernate session factory. + */ + private SessionFactory sessionFactory; + + private static final String BASETEST_DELETE = "/_BaseDAOTest-delete.xml"; + + protected static IDataSet truncateDataSet; + + /** + * Should use deferred close emulating the spring OpenSessionInView filter? Default is <code>true</code> + * @return <code>true</code> if deferred close should be used + */ + protected boolean mimicSessionFilter() + { + return true; + } + + /** + * Returns the table filter that will be used to exclude certain tables from sort/deletion. This may be overridden + * by subclasses, and is needed when tables have circular references (not handled by dbunit DatabaseSequenceFilter) + * @return an <code>ITableFilter</code> + */ + protected ITableFilter getTableFilter() + { + return new AbstractTableFilter() + { + + @Override + public boolean isValidName(String tableName) throws DataSetException + { + // default excludes: + // $ = oracle recycle bin tables + // JBPM = jbpm tables, with circular references + return !StringUtils.contains(tableName, "$") && !StringUtils.contains(tableName, "JBPM"); + } + }; + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + @Override + protected void setUp() throws Exception + { + super.setUp(); + + // insert values + IDataSet dataSet = null; + + String datesetFileName = "/" + ClassUtils.getShortClassName(getClass()) + "-load.xml"; + InputStream testData = getClass().getResourceAsStream(datesetFileName); + + if (testData != null) + { + if (log.isDebugEnabled()) + { + log.debug("loading dataset {}", datesetFileName); + } + + dataSet = new XmlDataSet(testData); + } + else + { + // check for excel + datesetFileName = "/" + ClassUtils.getShortClassName(getClass()) + "-load.xls"; + testData = getClass().getResourceAsStream(datesetFileName); + + if (testData != null) + { + if (log.isDebugEnabled()) + { + log.debug("loading dataset {}", datesetFileName); + } + + dataSet = new XlsDataSet(testData); + } + } + + if (dataSet == null) + { + log.debug("No test data found with name [{}]", datesetFileName); + } + else + { + + DataSource dataSource = (DataSource) ctx.getBean("dataSource"); + IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection()); + + // truncate common tables + if (truncateDataSet == null) + { + log.debug("Generating sorted dataset for initial cleanup"); + IDataSet unsortedTruncateDataSet = connection.createDataSet(); + + ITableFilter filter = new DatabaseSequenceFilter(connection, new FilteredDataSet( + getTableFilter(), + unsortedTruncateDataSet).getTableNames()); + truncateDataSet = new FilteredDataSet(filter, unsortedTruncateDataSet); + truncateDataSet = new FilteredDataSet(getTableFilter(), truncateDataSet); + log.debug("Sorted dataset generated"); + } + + IDataSet orderedDataset = dataSet; + + // if a sorted dataset is available, use table sequence for sorting + if (truncateDataSet == null) + { + ITableFilter filter = new SequenceTableFilter(truncateDataSet.getTableNames()); + orderedDataset = new FilteredDataSet(filter, dataSet); + } + + try + { + if (truncateDataSet != null) + { + DatabaseOperation.DELETE_ALL.execute(connection, truncateDataSet); + } + if (dataSet != null) + { + InsertIdentityOperation.INSERT.execute(connection, orderedDataset); + } + } + finally + { + connection.close(); + } + } + + // mimic the Spring OpenSessionInViewFilter + if (mimicSessionFilter()) + { + Map<String, SessionFactory> sfbeans = ctx.getBeansOfType(SessionFactory.class); + if (sfbeans.isEmpty()) + { + fail("No bean of type org.hibernate.SessionFactory found in spring context"); + } + this.sessionFactory = sfbeans.get(sfbeans.keySet().iterator().next()); + + SessionFactoryUtils.initDeferredClose(this.sessionFactory); + } + + } + + /** + * {@inheritDoc} + */ + @Override + protected void tearDown() throws Exception + { + if (mimicSessionFilter()) + { + // close open hibernate sessions, mimic the OpenSessionInViewFilter + SessionFactoryUtils.processDeferredClose(this.sessionFactory); + } + + // regenerate db initial state + String datesetFileName = "/initial-load.xml"; + InputStream testData = getClass().getResourceAsStream(datesetFileName); + + if (testData != null) + { + log.debug("Restoring db state"); + + IDataSet dataSet = new XmlDataSet(testData); + + DataSource dataSource = (DataSource) ctx.getBean("dataSource"); + IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection()); + + try + { + DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet); + } + finally + { + connection.close(); + } + } + + super.tearDown(); + } + + /** + * Returns the full test name. + * @see junit.framework.TestCase#getName() + */ + @Override + public String getName() + { + return ClassUtils.getShortClassName(this.getClass()) + "::" + super.getName(); + } + + /** + * return the current Hibernate SessionFactory + * @return SessionFactory object + */ + protected SessionFactory getSessionFactory() + { + return sessionFactory; + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |