|
From: <fg...@us...> - 2008-04-27 09:57:03
|
Revision: 756
http://openutils.svn.sourceforge.net/openutils/?rev=756&view=rev
Author: fgiust
Date: 2008-04-27 02:56:43 -0700 (Sun, 27 Apr 2008)
Log Message:
-----------
[maven-release-plugin] copy for tag openutils-testing-2.1
Added Paths:
-----------
tags/openutils-testing-2.1/
tags/openutils-testing-2.1/pom.xml
tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitExecution.java
tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitTestContext.java
tags/openutils-testing-2.1/src/site/changes/changes.xml
Removed Paths:
-------------
tags/openutils-testing-2.1/pom.xml
tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitExecution.java
tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitTestContext.java
tags/openutils-testing-2.1/src/site/changes/changes.xml
Copied: tags/openutils-testing-2.1 (from rev 751, trunk/openutils-testing)
Deleted: tags/openutils-testing-2.1/pom.xml
===================================================================
--- trunk/openutils-testing/pom.xml 2008-03-20 17:39:12 UTC (rev 751)
+++ tags/openutils-testing-2.1/pom.xml 2008-04-27 09:56:43 UTC (rev 756)
@@ -1,78 +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>7</version>
- <relativePath>..</relativePath>
- </parent>
- <artifactId>openutils-testing</artifactId>
- <name>openutils test utils</name>
- <version>2.0.4-SNAPSHOT</version>
- <description>openutils test utils</description>
- <dependencies>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.4.1</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>jcl104-over-slf4j</artifactId>
- <version>1.4.1</version>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <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>
- <optional>true</optional>
- <exclusions>
- <exclusion>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- </exclusion>
- <exclusion>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- </exclusion>
- <exclusion>
- <groupId>junit-addons</groupId>
- <artifactId>junit-addons</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.testng</groupId>
- <artifactId>testng</artifactId>
- <classifier>jdk15</classifier>
- <version>5.7</version>
- <scope>test</scope>
- <exclusions>
- <exclusion>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
- <properties>
- <spring.version>2.5.1</spring.version>
- </properties>
-</project>
\ No newline at end of file
Copied: tags/openutils-testing-2.1/pom.xml (from rev 755, trunk/openutils-testing/pom.xml)
===================================================================
--- tags/openutils-testing-2.1/pom.xml (rev 0)
+++ tags/openutils-testing-2.1/pom.xml 2008-04-27 09:56:43 UTC (rev 756)
@@ -0,0 +1,83 @@
+<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>7</version>
+ <relativePath>..</relativePath>
+ </parent>
+ <artifactId>openutils-testing</artifactId>
+ <name>openutils test utils</name>
+ <version>2.1</version>
+ <description>openutils test utils</description>
+ <dependencies>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>1.5.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>jcl104-over-slf4j</artifactId>
+ <version>1.5.0</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.4</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>
+ <optional>true</optional>
+ <exclusions>
+ <exclusion>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>junit-addons</groupId>
+ <artifactId>junit-addons</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.testng</groupId>
+ <artifactId>testng</artifactId>
+ <classifier>jdk15</classifier>
+ <version>5.7</version>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ </dependencies>
+ <properties>
+ <spring.version>2.5.3</spring.version>
+ </properties>
+
+ <scm>
+ <connection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-testing-2.1</connection>
+ <developerConnection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-testing-2.1</developerConnection>
+ <url>http://openutils.svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-testing-2.1</url>
+ </scm>
+</project>
\ No newline at end of file
Deleted: tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitExecution.java
===================================================================
--- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java 2008-03-20 17:39:12 UTC (rev 751)
+++ tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitExecution.java 2008-04-27 09:56:43 UTC (rev 756)
@@ -1,83 +0,0 @@
-/*
- * 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;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Inherited;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-import org.dbunit.dataset.datatype.IDataTypeFactory;
-import org.dbunit.operation.DatabaseOperation;
-
-
-/**
- * @author fgiust
- * @version $Id$
- */
-@Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.TYPE)
-@Inherited
-@Documented
-public @interface DbUnitExecution {
-
- /**
- * The resource locations to use for loading dbunit dataset.
- */
- String[] datasets() default {
-
- };
-
- /**
- * The name of the datasource to use. If can be left unset only if there is only one DataSource available in the
- * Spring context.
- */
- String dataSource() default "";
-
- /**
- * Db schema name that will be given to dbUnit. Can be left unset if not needed (database specific)
- */
- String schema() default "";
-
- /**
- * The datatype factory (DatabaseConfig.PROPERTY_DATATYPE_FACTORY) to use. Can be left unset if not needed.
- */
- Class< ? extends IDataTypeFactory> dataTypeFactory();
-
- /**
- * A regexp that can match table names. Any table matching this regular expression will not be considered by DbUnit.
- * By default tables starting with BIN$ are ignored (oracle recycle bin).
- */
- String excludedTables() default "^BIN\\$(.*)"; // oracle recycle bin
-
- /**
- * If true, <strong>all</strong> the tables in the database will be truncated before loading any dataset.
- */
- boolean truncateAll() default true;
-
- /**
- * The database operation that will be used to truncate tables. Defaults to org.dbunit.operation.DeleteAllOperation
- */
- Class< ? extends DatabaseOperation> truncateOperation() default org.dbunit.operation.DeleteAllOperation.class;
-
- /**
- * The database operation that will be used to load datasets. Defaults to org.dbunit.operation.InsertOperation
- */
- Class< ? extends DatabaseOperation> insertOperation() default org.dbunit.operation.InsertOperation.class;
-
-}
\ No newline at end of file
Copied: tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitExecution.java (from rev 752, trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java)
===================================================================
--- tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitExecution.java (rev 0)
+++ tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitExecution.java 2008-04-27 09:56:43 UTC (rev 756)
@@ -0,0 +1,84 @@
+/*
+ * 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;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.dbunit.dataset.datatype.DefaultDataTypeFactory;
+import org.dbunit.dataset.datatype.IDataTypeFactory;
+import org.dbunit.operation.DatabaseOperation;
+
+
+/**
+ * @author fgiust
+ * @version $Id$
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+@Inherited
+@Documented
+public @interface DbUnitExecution {
+
+ /**
+ * The resource locations to use for loading dbunit dataset.
+ */
+ String[] datasets() default {
+
+ };
+
+ /**
+ * The name of the datasource to use. If can be left unset only if there is only one DataSource available in the
+ * Spring context.
+ */
+ String dataSource() default "";
+
+ /**
+ * Db schema name that will be given to dbUnit. Can be left unset if not needed (database specific)
+ */
+ String schema() default "";
+
+ /**
+ * The datatype factory (DatabaseConfig.PROPERTY_DATATYPE_FACTORY) to use. Can be left unset if not needed.
+ */
+ Class< ? extends IDataTypeFactory> dataTypeFactory() default DefaultDataTypeFactory.class;
+
+ /**
+ * A regexp that can match table names. Any table matching this regular expression will not be considered by DbUnit.
+ * By default tables starting with BIN$ are ignored (oracle recycle bin).
+ */
+ String excludedTables() default "^BIN\\$(.*)"; // oracle recycle bin
+
+ /**
+ * If true, <strong>all</strong> the tables in the database will be truncated before loading any dataset.
+ */
+ boolean truncateAll() default true;
+
+ /**
+ * The database operation that will be used to truncate tables. Defaults to org.dbunit.operation.DeleteAllOperation
+ */
+ Class< ? extends DatabaseOperation> truncateOperation() default org.dbunit.operation.DeleteAllOperation.class;
+
+ /**
+ * The database operation that will be used to load datasets. Defaults to org.dbunit.operation.InsertOperation
+ */
+ Class< ? extends DatabaseOperation> insertOperation() default org.dbunit.operation.InsertOperation.class;
+
+}
\ No newline at end of file
Deleted: tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitTestContext.java
===================================================================
--- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java 2008-03-20 17:39:12 UTC (rev 751)
+++ tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitTestContext.java 2008-04-27 09:56:43 UTC (rev 756)
@@ -1,336 +0,0 @@
-/*
- * 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.DatabaseConfig;
-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.datatype.IDataTypeFactory;
-import org.dbunit.dataset.filter.ITableFilter;
-import org.dbunit.dataset.filter.SequenceTableFilter;
-import org.dbunit.operation.DatabaseOperation;
-import org.dbunit.operation.DeleteAllOperation;
-import org.dbunit.operation.DeleteOperation;
-import org.dbunit.operation.InsertOperation;
-import org.dbunit.operation.RefreshOperation;
-import org.dbunit.operation.TruncateTableOperation;
-import org.dbunit.operation.UpdateOperation;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.context.ApplicationContext;
-
-
-/**
- * @author fgiust
- * @version $Id$
- */
-public class 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>();
-
- /**
- * Logger.
- */
- private static Logger log = LoggerFactory.getLogger(DbUnitTestContext.class);
-
- /**
- * The test instance.
- */
- private Object testcase;
-
- /**
- * Spring application context.
- */
- private ApplicationContext applicationContext;
-
- /**
- * Dataset cache.
- */
- private Map<String, IDataSet> datasetCache = new HashMap<String, IDataSet>();
-
- /**
- * Instantiates a new DbUnitTestContext
- * @param testcase test instance
- * @param applicationContext Spring application context
- */
- 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 = null;
- if (singleDbUnitExecution != null)
- {
- executions = new DbUnitExecution[]{singleDbUnitExecution };
- }
- else
- {
- DbUnitConfiguration dbUnitConfiguration = testcase.getClass().getAnnotation(DbUnitConfiguration.class);
- if (dbUnitConfiguration != null)
- {
- executions = dbUnitConfiguration.dbUnitExecutions();
- }
- }
- if (executions != null)
- {
- 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);
-
- if (dbUnitExecution.dataTypeFactory() != null)
- {
- IDataTypeFactory dataTypeFactory = dbUnitExecution.dataTypeFactory().newInstance();
- connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, dataTypeFactory);
- }
-
- try
- {
- ITableFilter tableFilter = new RegExpTableFilter(dbUnitExecution.excludedTables());
-
- if (dbUnitExecution.truncateAll())
- {
- truncateAll(connection, tableFilter, dataSourceName, getDatabaseOperation(dbUnitExecution
- .truncateOperation()));
- }
-
- DatabaseOperation dbOperation = getDatabaseOperation(dbUnitExecution.insertOperation());
- for (String datasetFile : datasets)
- {
- importDataSet(createDataset(datasetFile), connection, tableFilter, dataSourceName, dbOperation);
- }
- }
- finally
- {
- connection.close();
- }
- }
- }
- }
-
- /**
- * Instantiates the givec Database Operation. Standard operations in the <code>org.dbunit.operation</code> package
- * have protected constructors and needs to be accessed using the public static fields in
- * <code>org.dbunit.operation.DatabaseOperation</code>.
- * @param dboperationClass db operation class
- * @return db operation instance
- * @throws InstantiationException if the given db operation class cannot be instantiated
- * @throws IllegalAccessException if the given db operation class cannot be instantiated
- */
- private DatabaseOperation getDatabaseOperation(Class< ? extends DatabaseOperation> dboperationClass)
- throws InstantiationException, IllegalAccessException
- {
- if (UpdateOperation.class.equals(dboperationClass))
- {
- return DatabaseOperation.UPDATE;
- }
- else if (InsertOperation.class.equals(dboperationClass))
- {
- return DatabaseOperation.INSERT;
- }
- else if (RefreshOperation.class.equals(dboperationClass))
- {
- return DatabaseOperation.REFRESH;
- }
- else if (DeleteOperation.class.equals(dboperationClass))
- {
- return DatabaseOperation.DELETE;
- }
- else if (DeleteAllOperation.class.equals(dboperationClass))
- {
- return DatabaseOperation.DELETE_ALL;
- }
- else if (TruncateTableOperation.class.equals(dboperationClass))
- {
- return DatabaseOperation.TRUNCATE_TABLE;
- }
- else if (DeleteOperation.class.equals(dboperationClass))
- {
- return DatabaseOperation.UPDATE;
- }
-
- return dboperationClass.newInstance();
-
- }
-
- /**
- * Creates a dataset instance by fetching a file from the classpath
- * @param datasetFile name of the file, will be loaded from the classpath
- * @return IDataSet instance
- * @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();
-
- if (log.isDebugEnabled())
- {
- log.debug("Unfiltered truncateDataSet: {}", ArrayUtils.toString(unsortedDataSet.getTableNames()));
- }
-
- // excluded unwanted tables
- unsortedDataSet = new FilteredDataSet(tableFilter, unsortedDataSet);
-
- // sort tables
- ITableFilter sortingFilter = new DatabaseSequenceFilter(connection, unsortedDataSet.getTableNames());
- truncateDataSet = new FilteredDataSet(sortingFilter, unsortedDataSet);
-
- storeTruncateDataset(dataSourceName, connection.getSchema(), truncateDataSet);
- log.debug("Sorted dataset generated");
- }
-
- if (truncateDataSet != null)
- {
- if (log.isDebugEnabled())
- {
- log.debug("Tables truncateDataSet: {}", ArrayUtils.toString(truncateDataSet.getTableNames()));
- }
- 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);
- }
-
- /**
- * Loads a named datasource from the spring context.
- * @param name datasource name.
- * @return Datasource instance
- */
- @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);
- }
-
-}
Copied: tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitTestContext.java (from rev 753, trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java)
===================================================================
--- tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitTestContext.java (rev 0)
+++ tags/openutils-testing-2.1/src/main/java/it/openutils/testing/DbUnitTestContext.java 2008-04-27 09:56:43 UTC (rev 756)
@@ -0,0 +1,335 @@
+/*
+ * 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.DatabaseConfig;
+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.datatype.IDataTypeFactory;
+import org.dbunit.dataset.filter.ITableFilter;
+import org.dbunit.dataset.filter.SequenceTableFilter;
+import org.dbunit.operation.DatabaseOperation;
+import org.dbunit.operation.DeleteAllOperation;
+import org.dbunit.operation.DeleteOperation;
+import org.dbunit.operation.InsertOperation;
+import org.dbunit.operation.RefreshOperation;
+import org.dbunit.operation.TruncateTableOperation;
+import org.dbunit.operation.UpdateOperation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.context.ApplicationContext;
+
+
+/**
+ * @author fgiust
+ * @version $Id$
+ */
+public class 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>();
+
+ /**
+ * Logger.
+ */
+ private static Logger log = LoggerFactory.getLogger(DbUnitTestContext.class);
+
+ /**
+ * The test instance.
+ */
+ private Object testcase;
+
+ /**
+ * Spring application context.
+ */
+ private ApplicationContext applicationContext;
+
+ /**
+ * Dataset cache.
+ */
+ private Map<String, IDataSet> datasetCache = new HashMap<String, IDataSet>();
+
+ /**
+ * Instantiates a new DbUnitTestContext
+ * @param testcase test instance
+ * @param applicationContext Spring application context
+ */
+ public DbUnitTestContext(Object testcase, ApplicationContext applicationContext)
+ {
+ this.testcase = testcase;
+ this.applicationContext = applicationContext;
+ }
+
+ /**
+ * Setup the Database before running the test method.
+ * @throws Exception Any exception.
+ */
+ public void setUpDbUnit() throws Exception
+ {
+ DbUnitExecution singleDbUnitExecution = testcase.getClass().getAnnotation(DbUnitExecution.class);
+
+ DbUnitExecution[] executions = null;
+ if (singleDbUnitExecution != null)
+ {
+ executions = new DbUnitExecution[]{singleDbUnitExecution };
+ }
+ else
+ {
+ DbUnitConfiguration dbUnitConfiguration = testcase.getClass().getAnnotation(DbUnitConfiguration.class);
+ if (dbUnitConfiguration != null)
+ {
+ executions = dbUnitConfiguration.dbUnitExecutions();
+ }
+ }
+ if (executions != null)
+ {
+ 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);
+
+ if (dbUnitExecution.dataTypeFactory() != null)
+ {
+ IDataTypeFactory dataTypeFactory = dbUnitExecution.dataTypeFactory().newInstance();
+ connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, dataTypeFactory);
+ }
+
+ try
+ {
+ ITableFilter tableFilter = new RegExpTableFilter(dbUnitExecution.excludedTables());
+
+ if (dbUnitExecution.truncateAll())
+ {
+ truncateAll(connection, tableFilter, dataSourceName, getDatabaseOperation(dbUnitExecution
+ .truncateOperation()));
+ }
+
+ DatabaseOperation dbOperation = getDatabaseOperation(dbUnitExecution.insertOperation());
+ for (String datasetFile : datasets)
+ {
+ importDataSet(createDataset(datasetFile), connection, tableFilter, dataSourceName, dbOperation);
+ }
+ }
+ finally
+ {
+ connection.close();
+ }
+ }
+ }
+ }
+
+ /**
+ * Instantiates the givec Database Operation. Standard operations in the <code>org.dbunit.operation</code> package
+ * have protected constructors and needs to be accessed using the public static fields in
+ * <code>org.dbunit.operation.DatabaseOperation</code>.
+ * @param dboperationClass db operation class
+ * @return db operation instance
+ * @throws InstantiationException if the given db operation class cannot be instantiated
+ * @throws IllegalAccessException if the given db operation class cannot be instantiated
+ */
+ private DatabaseOperation getDatabaseOperation(Class< ? extends DatabaseOperation> dboperationClass)
+ throws InstantiationException, IllegalAccessException
+ {
+ if (UpdateOperation.class.equals(dboperationClass))
+ {
+ return DatabaseOperation.UPDATE;
+ }
+ else if (InsertOperation.class.equals(dboperationClass))
+ {
+ return DatabaseOperation.INSERT;
+ }
+ else if (RefreshOperation.class.equals(dboperationClass))
+ {
+ return DatabaseOperation.REFRESH;
+ }
+ else if (DeleteOperation.class.equals(dboperationClass))
+ {
+ return DatabaseOperation.DELETE;
+ }
+ else if (DeleteAllOperation.class.equals(dboperationClass))
+ {
+ return DatabaseOperation.DELETE_ALL;
+ }
+ else if (TruncateTableOperation.class.equals(dboperationClass))
+ {
+ return DatabaseOperation.TRUNCATE_TABLE;
+ }
+ else if (DeleteOperation.class.equals(dboperationClass))
+ {
+ return DatabaseOperation.UPDATE;
+ }
+
+ return dboperationClass.newInstance();
+
+ }
+
+ /**
+ * Creates a dataset instance by fetching a file from the classpath
+ * @param datasetFile name of the file, will be loaded from the classpath
+ * @return IDataSet instance
+ * @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();
+
+ if (log.isDebugEnabled())
+ {
+ log.debug("Unfiltered truncateDataSet: {}", ArrayUtils.toString(unsortedDataSet.getTableNames()));
+ }
+
+ // excluded unwanted tables
+ unsortedDataSet = new FilteredDataSet(tableFilter, unsortedDataSet);
+
+ // sort tables
+ ITableFilter sortingFilter = new DatabaseSequenceFilter(connection, unsortedDataSet.getTableNames());
+ truncateDataSet = new FilteredDataSet(sortingFilter, unsortedDataSet);
+
+ storeTruncateDataset(dataSourceName, connection.getSchema(), truncateDataSet);
+ log.debug("Sorted dataset generated");
+ }
+
+ if (truncateDataSet != null)
+ {
+ if (log.isDebugEnabled())
+ {
+ log.debug("Tables truncateDataSet: {}", ArrayUtils.toString(truncateDataSet.getTableNames()));
+ }
+ 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);
+ }
+
+ /**
+ * Loads a named datasource from the spring context.
+ * @param name datasource name.
+ * @return Datasource instance
+ */
+ @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);
+ }
+
+}
Deleted: tags/openutils-testing-2.1/src/site/changes/changes.xml
===================================================================
--- trunk/openutils-testing/src/site/changes/changes.xml 2008-03-20 17:39:12 UTC (rev 751)
+++ tags/openutils-testing-2.1/src/site/changes/changes.xml 2008-04-27 09:56:43 UTC (rev 756)
@@ -1,40 +0,0 @@
-<?xml version="1.0"?>
-<!--
- "type" attribute can be: add, remove, update or fix.
--->
-<document>
- <properties>
- <title>Changes</title>
- <author email="fgiust(at)users.sourceforge.net">Fabrizio Giustina</author>
- </properties>
- <body>
- <release version="2.0.3" date="in svn" description="2.0.3">
- <action type="fix" dev="fgiust"> excludedTables in DbUnitExecution now defaults to "^BIN\\$(.*)", properly excluding
- tables in oracle recycle bin.</action>
- </release>
- <release version="2.0.3" date="2008-02-19" description="2.0.3">
- <action type="update" dev="fgiust"> it.openutils.testing.DateAssert is now thread safe. Failure messages have been
- improved.</action>
- <action type="add" dev="fcarone"> New option for DbUnitExection to specify the
- DatabaseConfig.PROPERTY_DATATYPE_FACTORY added.</action>
- </release>
- <release version="2.0.2" date="2008-01-31" description="2.0.2">
- <action type="add" dev="fgiust"> New dbunit operation classes for mssql and mysql (extension of standard dbunit
- operation, but refactored so that they can be used in annotations). See the javadocs in the
- it.openutils.testing.dbunit package</action>
- <action type="update" dev="fgiust"> truncate operation now defaults to DbUnitOperation.DELETE_ALL instead of
- DbUnitOperation.TRUNCATE.</action>
- <action type="fix" dev="fgiust">DbUnitUtils.exportDataset() doesn't close anymore the
- provided connection</action>
- <action type="add" dev="fgiust">new DbUnitUtils.exportTablesToDir() utility method</action>
- </release>
- <release version="2.0.1" date="2008-01-30" description="2.0.1">
- <action type="fix" dev="fgiust"> (junit version only).
- it.openutils.testing.junit.AbstractDbUnitJunitSpringContextTests was totally broken due to the setup method
- being protected. Fixed + added tests</action>
- </release>
- <release version="2.0" date="2008-01-20" description="2.0 release">
- <action type="update" dev="fgiust">Totally reworked dbunit test framework.</action>
- </release>
- </body>
-</document>
\ No newline at end of file
Copied: tags/openutils-testing-2.1/src/site/changes/changes.xml (from rev 754, trunk/openutils-testing/src/site/changes/changes.xml)
===================================================================
--- tags/openutils-testing-2.1/src/site/changes/changes.xml (rev 0)
+++ tags/openutils-testing-2.1/src/site/changes/changes.xml 2008-04-27 09:56:43 UTC (rev 756)
@@ -0,0 +1,43 @@
+<?xml version="1.0"?>
+<!--
+ "type" attribute can be: add, remove, update or fix.
+-->
+<document>
+ <properties>
+ <title>Changes</title>
+ <author email="fgiust(at)users.sourceforge.net">Fabrizio Giustina</author>
+ </properties>
+ <body>
+ <release version="2.1" date="2008-04-27" description="2.1">
+ <action type="update" dev="fgiust">Legacy base test classes (DbUnitTestCase, GenericsDbUnitTestCase) have been
+ dropped. Stick with 2.0.x if you are still use them or upgrade to 2.1 for a forced migration</action>
+ <action type="update" dev="fgiust">Updated spring, slf4j and commons-lang dependencies</action>
+ <action type="fix" dev="fgiust"> excludedTables in DbUnitExecution now defaults to "^BIN\\$(.*)", properly excluding
+ tables in oracle recycle bin.</action>
+ <action type="add" dev="fcarone"> New option for DbUnitExection to specify the
+ DatabaseConfig.PROPERTY_DATATYPE_FACTORY added.</action>
+ </release>
+ <release version="2.0.3" date="2008-02-19" description="2.0.3">
+ <action type="update" dev="fgiust"> it.openutils.testing.DateAssert is now thread safe. Failure messages have been
+ improved.</action>
+ </release>
+ <release version="2.0.2" date="2008-01-31" description="2.0.2">
+ <action type="add" dev="fgiust"> New dbunit operation classes for mssql and mysql (extension of standard dbunit
+ operation, but refactored so that they can be used in annotations). See the javadocs in the
+ it.openutils.testing.dbunit package</action>
+ <action type="update" dev="fgiust"> truncate operation now defaults to DbUnitOperation.DELETE_ALL instead of
+ DbUnitOperation.TRUNCATE.</action>
+ <action type="fix" dev="fgiust">DbUnitUtils.exportDataset() doesn't close anymore the
+ provided connection</action>
+ <action type="add" dev="fgiust">new DbUnitUtils.exportTablesToDir() utility method</action>
+ </release>
+ <release version="2.0.1" date="2008-01-30" description="2.0.1">
+ <action type="fix" dev="fgiust"> (junit version only).
+ it.openutils.testing.junit.AbstractDbUnitJunitSpringContextTests was totally broken due to the setup method
+ being protected. Fixed + added tests</action>
+ </release>
+ <release version="2.0" date="2008-01-20" description="2.0 release">
+ <action type="update" dev="fgiust">Totally reworked dbunit test framework.</action>
+ </release>
+ </body>
+</document>
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|