【讓開發自動化】Unitils對XML數據集擴展支持

    Unitils默認的DataSet僅支持Flat格式的XML數據集,若是測試數據多的話,不只看起來不夠美觀,維護更是難上加難。DBUnit內置還支持MultiRow格式的數據集,Column的定義僅需一次,多條記錄僅需增長Row便可;然而Unitils並不支持這種格式的數據集,其本身實現的MultiSchemaXmlDataSetFactory單獨實現了XML內容的解析。 java

    好在咱們能夠經過實現DataSetFactory接口很容易的擴展數據集的讀取,這裏咱們將自定義一個XML數據集讀取的實現,源代碼以下: ide

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數據集擴展.
 * 
 * <pre><b>描述:</b>
 *     Unitils默認僅支持Flat格式的XML數據集,且XML解析是其內部實現的而不是FlatXmlDataSet。
 * 經過擴展DataSetFactory實現同時支持Flat格式和MultiRow格式的XML數據集讀取。
 * 配置方法:
 * 修改unitils.properties
 * 一、DbUnitModule.DataSet.factory.default=com.litt.core.test.unitils.ext.MultiRowXmlDataSetFactory
 * 	1.一、DbUnitModule.ExpectedDataSet.factory.default=com.litt.core.test.unitils.ext.MultiRowXmlDataSetFactory
 * 三、增長DbUnitModule.DataSet.xml.format屬性,屬性值能夠是「MultiRow」或"Flat",默認爲Flat格式
 * </pre>
 * 
 * <pre><b>修改記錄:</b>
 *    
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">蔡源</a>
 * @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))
		{
			List<XmlDataSet> xmlDataSets = buildXmlDataSets(dataSetFiles);
			CompositeDataSet globalXmlDataSet = new CompositeDataSet(xmlDataSets.toArray(new XmlDataSet[0]));
			return globalXmlDataSet;
		}
		else 
		{
			List<FlatXmlDataSet> 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<XmlDataSet> buildXmlDataSets(File... dataSetFiles) throws IOException, DataSetException
	{
		ArrayList<XmlDataSet> xmlDataSets = new ArrayList<XmlDataSet>();
		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<FlatXmlDataSet> buildFlatXmlDataSets(File... dataSetFiles) throws IOException, DataSetException
	{
		ArrayList<FlatXmlDataSet> flatXmlDataSets = new ArrayList<FlatXmlDataSet>();
		for (File dataSetFile : dataSetFiles)
		{
			flatXmlDataSets.add(new FlatXmlDataSet(dataSetFile, true));
		}
		return flatXmlDataSets;
	}

}
經過修改unitils.properties,將默認工廠類改成新的實現
DbUnitModule.DataSet.factory.default=com.litt.core.test.unitils.ext.MultiRowXmlDataSetFactory
DbUnitModule.ExpectedDataSet.factory.default=com.litt.core.test.unitils.ext.MultiRowXmlDataSetFactory
#xml.format: MultiRow,Flat
DbUnitModule.DataSet.xml.format=MultiRow

默認支持格式: 測試

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <ROLE ROLE_ID="1" ROLE_NAME="superadmin" STATUS="9" REMARK="Default super role"/>
  <ROLE ROLE_ID="2" ROLE_NAME="admin" STATUS="1" REMARK="Default admin role"/>
</dataset>
新增支持格式:

<?xml version="1.0" encoding="UTF-8"?>
<dataset xmlns="cidp_test">  
	<table name="ROLE" >
		<column>ROLE_NAME</column>
		<column>STATUS</column>
		<column>REMARK</column>
		<row>
			<value>TEST</value>
			<value>1</value>
			<value>Integration Test</value>
		</row>
		<row>
			<value>TEST2</value>
			<value>2</value>
			<value>Integration Test2</value>
		</row>
	</table>
</dataset>
相關文章
相關標籤/搜索