Unitils默认的DataSet仅支持Flat格式的XML数据集,如果测试数据多的话,不仅看起来不够美观,维护更是难上加难。DBUnit内置还支持MultiRow格式的数据集,Column的定义仅需一次,多条记录仅需增加Row即可;然而Unitils并不支持这种格式的数据集,其自己实现的MultiSchemaXmlDataSetFactory单独实现了XML内容的解析。
好在我们可以通过实现DataSetFactory接口很容易的扩展数据集的读取,这里我们将自定义一个XML数据集读取的实现,源代码如下:
package com.litt.core.test.unitils.ext;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Properties;import org.dbunit.dataset.CompositeDataSet;import org.dbunit.dataset.DataSetException;import org.dbunit.dataset.xml.FlatXmlDataSet;import org.dbunit.dataset.xml.XmlDataSet;import org.unitils.core.UnitilsException;import org.unitils.dbunit.datasetfactory.DataSetFactory;import org.unitils.dbunit.util.MultiSchemaDataSet;/** * * XML数据集扩展. * *通过修改unitils.properties,将默认工厂类改为新的实现描述: * Unitils默认仅支持Flat格式的XML数据集,且XML解析是其内部实现的而不是FlatXmlDataSet。 * 通过扩展DataSetFactory实现同时支持Flat格式和MultiRow格式的XML数据集读取。 * 配置方法: * 修改unitils.properties * 1、DbUnitModule.DataSet.factory.default=com.litt.core.test.unitils.ext.MultiRowXmlDataSetFactory * 1.1、DbUnitModule.ExpectedDataSet.factory.default=com.litt.core.test.unitils.ext.MultiRowXmlDataSetFactory * 3、增加DbUnitModule.DataSet.xml.format属性,属性值可以是“MultiRow”或"Flat",默认为Flat格式 ** *修改记录: * ** * @author 蔡源 * @since 2013-7-30 * @version 1.0 * */public class MultiRowXmlDataSetFactory implements DataSetFactory{ private String defaultSchemaName; private String format; /* (non-Javadoc) * @see org.unitils.dbunit.datasetfactory.DataSetFactory#createDataSet(java.io.File[]) */ @Override public MultiSchemaDataSet createDataSet(File... dataSetFiles) { MultiSchemaDataSet multiSchemaDataSet = null; try { CompositeDataSet globalFlatXmlDataSet = buildUniqueDataSet(dataSetFiles); multiSchemaDataSet = new MultiSchemaDataSet(); multiSchemaDataSet.setDataSetForSchema(defaultSchemaName, globalFlatXmlDataSet); } catch (Exception e) { throw new UnitilsException("Unable to create DbUnit dataset for data set files: " + Arrays.toString(dataSetFiles), e); } return multiSchemaDataSet; } /* (non-Javadoc) * @see org.unitils.dbunit.datasetfactory.DataSetFactory#getDataSetFileExtension() */ @Override public String getDataSetFileExtension() { return "xml"; } /* (non-Javadoc) * @see org.unitils.dbunit.datasetfactory.DataSetFactory#init(java.util.Properties, java.lang.String) */ @Override public void init(Properties configuration, String defaultSchemaName) { this.defaultSchemaName = defaultSchemaName; this.format = configuration.getProperty("DbUnitModule.DataSet.xml.format", "Flat"); } /** * Builds the unique data set. * * @param dataSetFiles the data set files * @return the composite data set * @throws IOException Signals that an I/O exception has occurred. * @throws DataSetException the data set exception */ private CompositeDataSet buildUniqueDataSet(File... dataSetFiles) throws IOException, DataSetException { if("MultiRow".equals(format)) { ListxmlDataSets = buildXmlDataSets(dataSetFiles); CompositeDataSet globalXmlDataSet = new CompositeDataSet(xmlDataSets.toArray(new XmlDataSet[0])); return globalXmlDataSet; } else { List xmlDataSets = buildFlatXmlDataSets(dataSetFiles); CompositeDataSet globalXmlDataSet = new CompositeDataSet(xmlDataSets.toArray(new FlatXmlDataSet[0])); return globalXmlDataSet; } } /** * Builds the xml data sets. * * @param dataSetFiles the data set files * @return the list * @throws IOException Signals that an I/O exception has occurred. * @throws DataSetException the data set exception */ private List buildXmlDataSets(File... dataSetFiles) throws IOException, DataSetException { ArrayList xmlDataSets = new ArrayList (); for (File dataSetFile : dataSetFiles) { xmlDataSets.add(new XmlDataSet(new FileInputStream(dataSetFile))); } return xmlDataSets; } /** * Builds the flat xml data sets. * * @param dataSetFiles the data set files * @return the list * @throws IOException Signals that an I/O exception has occurred. * @throws DataSetException the data set exception */ private List buildFlatXmlDataSets(File... dataSetFiles) throws IOException, DataSetException { ArrayList flatXmlDataSets = new ArrayList (); for (File dataSetFile : dataSetFiles) { flatXmlDataSets.add(new FlatXmlDataSet(dataSetFile, true)); } return flatXmlDataSets; }}
DbUnitModule.DataSet.factory.default=com.litt.core.test.unitils.ext.MultiRowXmlDataSetFactoryDbUnitModule.ExpectedDataSet.factory.default=com.litt.core.test.unitils.ext.MultiRowXmlDataSetFactory#xml.format: MultiRow,FlatDbUnitModule.DataSet.xml.format=MultiRow
默认支持格式:
新增支持格式:
ROLE_NAME STATUS REMARK |
TEST 1 Integration Test |
TEST2 2 Integration Test2