日期:2014-05-16 浏览次数:20508 次
/**
 * 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);
			}
		}
	}
}
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