From: <fg...@us...> - 2009-03-28 18:00:25
|
Revision: 1108 http://openutils.svn.sourceforge.net/openutils/?rev=1108&view=rev Author: fgiust Date: 2009-03-28 18:00:22 +0000 (Sat, 28 Mar 2009) Log Message: ----------- added support for FlatXmlFormat, added features() and properties() attributes Modified Paths: -------------- trunk/openutils-testing/pom.xml trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitUtils.java trunk/openutils-testing/src/site/changes/changes.xml trunk/openutils-testing-junit/src/test/java/it/openutils/testing/junit/SingleDatasourceDbUnitTest.java Added Paths: ----------- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitFeature.java trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitProperty.java trunk/openutils-testing/src/main/java/it/openutils/testing/dbunit/DatasetType.java Modified: trunk/openutils-testing/pom.xml =================================================================== --- trunk/openutils-testing/pom.xml 2009-03-25 14:29:46 UTC (rev 1107) +++ trunk/openutils-testing/pom.xml 2009-03-28 18:00:22 UTC (rev 1108) @@ -1,4 +1,5 @@ -<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"> +<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> @@ -70,7 +71,7 @@ <dependency> <groupId>org.dbunit</groupId> <artifactId>dbunit</artifactId> - <version>2.2</version> + <version>2.4.4</version> <exclusions> <exclusion> <groupId>junit</groupId> Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java 2009-03-25 14:29:46 UTC (rev 1107) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java 2009-03-28 18:00:22 UTC (rev 1108) @@ -15,6 +15,8 @@ */ package it.openutils.testing; +import it.openutils.testing.dbunit.DatasetType; + import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; @@ -83,4 +85,21 @@ */ Class< ? extends DatabaseOperation> insertOperation() default org.dbunit.operation.InsertOperation.class; + /** + * The type of dataset to load. Defautils to XML, but automatically switches to EXCEL if you are using a file with + * the ".xml" extension. You need to explicitely set this only if you are using the "flat xml" format. + */ + DatasetType datasetType() default DatasetType.XML; + + /** + * A list of dbunit features that should be enabled or disabled on the connection. See + * http://www.dbunit.org/properties.html. + */ + DbUnitFeature[] features() default {}; + + /** + * A list of dbunit properties that should be set on the connection. See http://www.dbunit.org/properties.html. + */ + DbUnitProperty[] properties() default {}; + } \ No newline at end of file Added: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitFeature.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitFeature.java (rev 0) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitFeature.java 2009-03-28 18:00:22 UTC (rev 1108) @@ -0,0 +1,35 @@ +/* + * 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; + +/** + * A DbUnit feature. See http://www.dbunit.org/properties.html + * @author fgiust + * @version $Id$ + */ +public @interface DbUnitFeature { + + /** + * Property key. + */ + String key() default ""; + + /** + * Property value. + */ + boolean value() default true; + +} Property changes on: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitFeature.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitProperty.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitProperty.java (rev 0) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitProperty.java 2009-03-28 18:00:22 UTC (rev 1108) @@ -0,0 +1,35 @@ +/* + * 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; + +/** + * A DbUnit property. See http://www.dbunit.org/properties.html + * @author fgiust + * @version $Id$ + */ +public @interface DbUnitProperty { + + /** + * Property key. + */ + String key() default ""; + + /** + * Property value. + */ + String value() default ""; + +} Property changes on: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitProperty.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java 2009-03-25 14:29:46 UTC (rev 1107) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java 2009-03-28 18:00:22 UTC (rev 1108) @@ -16,6 +16,7 @@ package it.openutils.testing; import it.openutils.testing.dbunit.CleanInsertOperation; +import it.openutils.testing.dbunit.DatasetType; import it.openutils.testing.dbunit.MsSqlIndentityCleanInsertOperation; import it.openutils.testing.dbunit.MsSqlIndentityInsertOperation; @@ -29,11 +30,13 @@ import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.math.NumberUtils; 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.database.DatabaseConfig.ConfigProperty; import org.dbunit.dataset.DataSetException; import org.dbunit.dataset.FilteredDataSet; import org.dbunit.dataset.IDataSet; @@ -133,12 +136,24 @@ getDatasource(dataSourceName).getConnection(), schema); + DatabaseConfig config = connection.getConfig(); + if (dbUnitExecution.dataTypeFactory() != null) { IDataTypeFactory dataTypeFactory = dbUnitExecution.dataTypeFactory().newInstance(); - connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, dataTypeFactory); + config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, dataTypeFactory); } + for (DbUnitFeature feature : dbUnitExecution.features()) + { + config.setFeature(feature.key(), feature.value()); + } + + for (DbUnitProperty property : dbUnitExecution.properties()) + { + setupProperty(config, property); + } + try { ITableFilter tableFilter = new RegExpTableFilter(dbUnitExecution.excludedTables()); @@ -169,7 +184,12 @@ } for (String datasetFile : datasets) { - importDataSet(createDataset(datasetFile), connection, tableFilter, dataSourceName, dbOperation); + importDataSet( + createDataset(datasetFile, dbUnitExecution.datasetType()), + connection, + tableFilter, + dataSourceName, + dbOperation); } } finally @@ -181,6 +201,53 @@ } /** + * Setup a dbunit property, handling type conversion if needed. + * @param config + * @param property + */ + private void setupProperty(DatabaseConfig config, DbUnitProperty property) + { + for (ConfigProperty configProperty : DatabaseConfig.ALL_PROPERTIES) + { + if (property.key().equals(configProperty.getProperty())) + { + Class< ? > propertyType = configProperty.getPropertyType(); + + Object propertyValue = property.value(); + + if (String.class.equals(propertyType)) + { + propertyValue = property.value(); + } + else if (Integer.class.equals(propertyType)) + { + propertyValue = NumberUtils.toInt(property.value()); + } + else + { + // a class name? + try + { + Class< ? > classProperty = Class.forName(property.value()); + propertyValue = classProperty.newInstance(); + } + catch (Throwable e) + { + // no, not a class name, ignore. + log.debug("Got a {} while trying to convert the {} property", e.getClass().getName(), property + .key()); + } + } + + config.setProperty(property.key(), propertyValue); + return; + } + } + + log.warn("Unknown property {}, skipping", property.key()); + } + + /** * 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>. @@ -228,11 +295,12 @@ /** * Creates a dataset instance by fetching a file from the classpath * @param datasetFile name of the file, will be loaded from the classpath + * @param datasetType Dataset Type * @return IDataSet instance * @throws IOException * @throws DataSetException */ - private IDataSet createDataset(String datasetFile) throws IOException, DataSetException + private IDataSet createDataset(String datasetFile, DatasetType datasetType) throws IOException, DataSetException { IDataSet dataSet = datasetCache.get(datasetFile); if (dataSet == null) @@ -242,7 +310,7 @@ { throw new IllegalArgumentException("Dataset " + datasetFile + " not found"); } - dataSet = DbUnitUtils.loadDataset(datasetUrl); + dataSet = DbUnitUtils.loadDataset(datasetUrl, datasetType); datasetCache.put(datasetFile, dataSet); } return dataSet; Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitUtils.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitUtils.java 2009-03-25 14:29:46 UTC (rev 1107) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitUtils.java 2009-03-28 18:00:22 UTC (rev 1108) @@ -15,6 +15,8 @@ */ package it.openutils.testing; +import it.openutils.testing.dbunit.DatasetType; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -30,6 +32,7 @@ import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.excel.XlsDataSet; import org.dbunit.dataset.filter.ITableFilter; +import org.dbunit.dataset.xml.FlatXmlDataSet; import org.dbunit.dataset.xml.XmlDataSet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,10 +66,28 @@ * @throws IOException An IO Exception. * @throws DataSetException A dataset exception. * @throws SQLException A SQL Exception. + * @deprecated use {@link #exportTablesToDir(IDatabaseConnection, File, DatasetType, ITableFilter)} */ + @Deprecated public static void exportDataset(IDatabaseConnection connection, String[] tableNames, File file) throws SQLException, IOException, DataSetException { + exportDataset(connection, tableNames, file, DatasetType.XML); + } + + /** + * Exports a list of table to an xml/xsl file + * @param file The file name we save the dataset to + * @param connection connection to use + * @param tableNames optional list of table names + * @param datasetType dataset Type + * @throws IOException An IO Exception. + * @throws DataSetException A dataset exception. + * @throws SQLException A SQL Exception. + */ + public static void exportDataset(IDatabaseConnection connection, String[] tableNames, File file, + DatasetType datasetType) throws SQLException, IOException, DataSetException + { OutputStream fos = null; try @@ -79,14 +100,19 @@ } fos = new FileOutputStream(file); - if (StringUtils.equalsIgnoreCase(StringUtils.substringAfterLast(file.getName(), "."), "xls")) + if (datasetType == DatasetType.EXCEL + || StringUtils.equalsIgnoreCase(StringUtils.substringAfterLast(file.getName(), "."), "xls")) { XlsDataSet.write(fullDataSet, fos); } - else + else if (datasetType == DatasetType.XML) { XmlDataSet.write(fullDataSet, fos); } + else if (datasetType == DatasetType.FLATXML) + { + FlatXmlDataSet.write(fullDataSet, fos); + } } finally { @@ -105,10 +131,28 @@ * @throws SQLException sql exception * @throws DataSetException dbunit exception * @throws IOException IO exception + * @deprecated use {@link #exportTablesToDir(IDatabaseConnection, File, DatasetType, ITableFilter)} */ + @Deprecated public void exportTablesToDir(IDatabaseConnection connection, File directory, boolean xls, ITableFilter tableFilter) throws SQLException, DataSetException, IOException + { + exportTablesToDir(connection, directory, xls ? DatasetType.EXCEL : DatasetType.XML, tableFilter); + } + /** + * Exports a full db saving a dataset for each table. + * @param connection dbunit connection + * @param directory export directory + * @param datasetType dbunit dataset type + * @param tableFilter optional table filter + * @throws SQLException sql exception + * @throws DataSetException dbunit exception + * @throws IOException IO exception + */ + public void exportTablesToDir(IDatabaseConnection connection, File directory, DatasetType datasetType, + ITableFilter tableFilter) throws SQLException, DataSetException, IOException + { directory.mkdirs(); @@ -122,9 +166,10 @@ for (String table : tablenames) { - File exportFile = new File(directory, table + (xls ? ".xls" : ".xml")); + File exportFile = new File(directory, table + (datasetType == DatasetType.EXCEL ? ".xls" : ".xml")); + log.info("Exporting {}", table); - DbUnitUtils.exportDataset(connection, new String[]{table }, exportFile); + DbUnitUtils.exportDataset(connection, new String[]{table }, exportFile, datasetType); } } @@ -133,13 +178,32 @@ * @return loaded dataset * @throws IOException error reading the file * @throws DataSetException error loading the dataset + * @deprecated use {@link #loadDataset(URL, DatasetType)} */ + @Deprecated public static IDataSet loadDataset(URL url) throws IOException, DataSetException { if (url == null) { throw new IllegalArgumentException("URL is null"); } + + return loadDataset(url, url.getFile().endsWith(".xls") ? DatasetType.EXCEL : DatasetType.XML); + } + + /** + * @param url dataset url (can be an xml or excel file) + * @param datasetType dataset Type + * @return loaded dataset + * @throws IOException error reading the file + * @throws DataSetException error loading the dataset + */ + public static IDataSet loadDataset(URL url, DatasetType datasetType) throws IOException, DataSetException + { + if (url == null) + { + throw new IllegalArgumentException("URL is null"); + } InputStream is = url.openStream(); IDataSet dataSet = null; Added: trunk/openutils-testing/src/main/java/it/openutils/testing/dbunit/DatasetType.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/dbunit/DatasetType.java (rev 0) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/dbunit/DatasetType.java 2009-03-28 18:00:22 UTC (rev 1108) @@ -0,0 +1,38 @@ +/* + * 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.dbunit; + +/** + * @author fgiust + * @version $Id$ + */ +public enum DatasetType { + + /** + * Default Xml format. + */ + XML, + + /** + * Flat Xml format. + */ + FLATXML, + + /** + * Excel format. + */ + EXCEL; +} Property changes on: trunk/openutils-testing/src/main/java/it/openutils/testing/dbunit/DatasetType.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/openutils-testing/src/site/changes/changes.xml =================================================================== --- trunk/openutils-testing/src/site/changes/changes.xml 2009-03-25 14:29:46 UTC (rev 1107) +++ trunk/openutils-testing/src/site/changes/changes.xml 2009-03-28 18:00:22 UTC (rev 1108) @@ -1,5 +1,5 @@ <?xml version="1.0"?> -<!-- + <!-- "type" attribute can be: add, remove, update or fix. --> <document> @@ -8,10 +8,19 @@ <author email="fgiust(at)users.sourceforge.net">Fabrizio Giustina</author> </properties> <body> + <release version="2.2" date="2009-03-28" description="2.1.2"> + <action type="add" dev="fgiust">Added support for flat xml format.</action> + <action type="update" dev="fgiust">Updated slf4j dependencies.</action> + <action type="update" dev="fgiust">Updated dbunit version to 2.4.4</action> + <action type="add" dev="fgiust">Added support for setting DbUnit features and properties through the "features" and + "properties" attributes in DbUnitExecution.</action> + </release> <release version="2.1.1" date="2009-02-17" description="2.1.1"> - <action type="add" dev="fgiust"> Detection of ms sqlserver databases and automatic disabling of identities - (switch from CleanInsertOperation to MsSqlIndentityCleanInsertOperation and from InsertOperation to IndentityCleanInsertOperation).</action> - </release> <release version="2.1" date="2008-04-27" description="2.1"> + <action type="add" dev="fgiust"> Detection of ms sqlserver databases and automatic disabling of identities (switch + from CleanInsertOperation to MsSqlIndentityCleanInsertOperation and from InsertOperation to + IndentityCleanInsertOperation).</action> + </release> + <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> @@ -30,8 +39,7 @@ 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="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"> Modified: trunk/openutils-testing-junit/src/test/java/it/openutils/testing/junit/SingleDatasourceDbUnitTest.java =================================================================== --- trunk/openutils-testing-junit/src/test/java/it/openutils/testing/junit/SingleDatasourceDbUnitTest.java 2009-03-25 14:29:46 UTC (rev 1107) +++ trunk/openutils-testing-junit/src/test/java/it/openutils/testing/junit/SingleDatasourceDbUnitTest.java 2009-03-28 18:00:22 UTC (rev 1108) @@ -16,6 +16,8 @@ package it.openutils.testing.junit; import it.openutils.testing.DbUnitExecution; +import it.openutils.testing.DbUnitFeature; +import it.openutils.testing.DbUnitProperty; import org.dbunit.operation.InsertOperation; import org.junit.Test; @@ -31,7 +33,11 @@ * @version $Id$ */ @ContextConfiguration(locations = {"/spring-tests.xml" }) -@DbUnitExecution(datasets = {"/db1-load.xml" }, dataSource = "dataSource1", insertOperation = InsertOperation.class) +@DbUnitExecution(datasets = {"/db1-load.xml" }, dataSource = "dataSource1", insertOperation = InsertOperation.class, // +properties = { + @DbUnitProperty(key = "http://www.dbunit.org/properties/fetchSize", value = "10"), + @DbUnitProperty(key = "http://www.dbunit.org/properties/statementFactory", value = "org.dbunit.database.statement.PreparedStatementFactory") }, // +features = {@DbUnitFeature(key = "http://www.dbunit.org/features/batchedStatements") }) @TestExecutionListeners({TransactionalTestExecutionListener.class }) @Transactional @TransactionConfiguration(transactionManager = "transactionManager1") This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |