日期:2014-05-16  浏览次数:20440 次

关于SpringSide的DbUnit测试的思考
关于SpringSide的DbUnit测试的思考

------》springSide的DbUnitUtils
/**
 * Copyright (c) 2005-2010 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: DbUnitUtils.java 1186 2010-08-29 16:34:10Z calvinxiu $
 */
package com.xx.unit.dao.account;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.dbunit.DatabaseUnitException;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.ext.h2.H2Connection;
import org.dbunit.operation.DatabaseOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springside.modules.utils.PropertiesUtils;

/**
 * 使用DBUnit初始化测试用H2嵌入式数据库数据的工具类.
 */
public class DbUnitUtils {

	private static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);
	private static ResourceLoader resourceLoader = new DefaultResourceLoader();

	/**
	 * 清除并插入XML数据文件到H2数据库.
	 * 
	 * XML数据文件中涉及的表在插入数据前会先进行清除. 
	 * 
	 * @param xmlFilePaths 符合Spring Resource路径格式的文件列表.
	 */
	public static void loadData(DataSource h2DataSource, String... xmlFilePaths) throws Exception {
		execute(DatabaseOperation.CLEAN_INSERT, h2DataSource, xmlFilePaths);
	}

	/**
	 * 插入XML数据文件到H2数据库. 
	 */
	public static void appendData(DataSource h2DataSource, String... xmlFilePaths) throws Exception {
		execute(DatabaseOperation.INSERT, h2DataSource, xmlFilePaths);
	}

	/**
	 * 在H2数据库中删除XML数据文件中涉及的表的数据. 
	 */
	public static void removeData(DataSource h2DataSource, String... xmlFilePaths) throws Exception {
		execute(DatabaseOperation.DELETE_ALL, h2DataSource, xmlFilePaths);
	}
	
	/**
	 * 在H2数据库中删除XML表示的数据. 
	 */
	public static void removeDataFromXML(DataSource h2DataSource, String... xmlFilePaths) throws Exception {
		execute(DatabaseOperation.DELETE, h2DataSource, xmlFilePaths);
	}

	/**
	 * 按DBUnit Operation执行XML数据文件的数据.
	 * 
	 * @param xmlFilePaths 符合Spring Resource路径格式的文件列表.
	 */
	private static void execute(DatabaseOperation operation, DataSource h2DataSource, String... xmlFilePaths)
			throws DatabaseUnitException, SQLException {
		IDatabaseConnection connection = new H2Connection(h2DataSource.getConnection(), "");
		for (String xmlPath : xmlFilePaths) {
			try {
				InputStream input = resourceLoader.getResource(xmlPath).getInputStream();
				IDataSet dataSet = new FlatXmlDataSetBuilder().setColumnSensing(true).build(input);
				operation.execute(connection, dataSet);
			} catch (IOException e) {
				logger.warn(xmlPath + " file not found", e);
			}
		}
	}
}


springSide的测试用的是 :

DbUnitUtils.loadData(dataSource, "/data/default-data.xml");// 加载xml数据前先清空涉及的表
DbUnitUtils.removeData(dataSourceHolder, "/data/default-data.xml");//删除xml涉及到的表的数据

如果是测试数据库,这么写可以,但如果要在应用数据库上测试(当然实际不太可能)应该用以下方式:

DbUnitUtils.appendData(dataSource, "/data/dbunit.xml"); //在原基础上增加数据
DbUnitUtils.removeDataFromXML(dataSourceHolder, "/data/dbunit.xml");//删除xml中涉及的数据

总结 :DbUnit结合spring测试框架,前者负责准备数据,后者进行测试控制。

------》附 DbUnit导出数据库到XML
package com.test.dbunit;

import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;

import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.database.QueryDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;

public class TestApp {
	
	public static void main(String[] args) throws Exception{
		Class.forName("org.h2.Driver");

		Connection conn = DriverManager.getConnection(
				"jdbc:h2:tcp://localhost/~/mySpringSide1", "sa", "");
		
		IDatabaseConnection connection = new DatabaseConnection(conn);
		QueryDataSet dataSet = new QueryDataS