This commit is contained in:
Calvin
2012-03-06 22:08:11 +08:00
parent a3e02f5df3
commit 1d66b229df
2 changed files with 63 additions and 79 deletions
@@ -1,78 +0,0 @@
package org.springside.modules.test.data;
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.operation.DatabaseOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
public abstract class Fixtures {
private static Logger logger = LoggerFactory.getLogger(Fixtures.class);
private static ResourceLoader resourceLoader = new DefaultResourceLoader();
/**
* 插入XML文件中的数据到数据库.
*
* @param xmlFilePaths 符合Spring Resource路径格式的文件路径列表.
*/
public static void loadData(DataSource dataSource, String... xmlFilePaths) throws Exception {
execute(DatabaseOperation.INSERT, dataSource, xmlFilePaths);
}
/**
* 先删除XML数据文件中涉及的表的数据, 再插入XML文件中的数据到数据库.
*
* 在更新全部表时速度没有reloadAllTable快且容易产生锁死, 适合只更新小部分表的数据的情况.
*
* @param xmlFilePaths 符合Spring Resource路径格式的文件路径列表.
*/
public static void reloadData(DataSource dataSource, String... xmlFilePaths) throws Exception {
execute(DatabaseOperation.CLEAN_INSERT, dataSource, xmlFilePaths);
}
/**
* 在数据库中删除XML文件中涉及的表的数据.
*
* @param xmlFilePaths 符合Spring Resource路径格式的文件路径列表.
*/
public static void deleteData(DataSource dataSource, String... xmlFilePaths) throws Exception {
execute(DatabaseOperation.DELETE_ALL, dataSource, xmlFilePaths);
}
/**
* 对XML文件中的数据在数据库中执行Operation.
*
* @param xmlFilePaths 符合Spring Resource路径格式的文件列表.
*/
private static void execute(DatabaseOperation operation, DataSource dataSource, String... xmlFilePaths)
throws DatabaseUnitException, SQLException {
IDatabaseConnection connection = getConnection(dataSource);
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);
}
}
}
protected static IDatabaseConnection getConnection(DataSource dataSource) throws DatabaseUnitException,
SQLException {
throw new UnsupportedOperationException("Need to override in subclass ");
}
}
@@ -5,6 +5,8 @@
*/
package org.springside.modules.test.data;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
@@ -14,18 +16,78 @@ import java.util.List;
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.springframework.jdbc.core.JdbcTemplate;
import org.springside.modules.utils.Exceptions;
/**
* 基于DBUnit初始化测试数据到H2数据库的工具类.
*/
public class H2Fixtures extends Fixtures {
public class H2Fixtures {
private H2Fixtures() {
}
private static Logger logger = LoggerFactory.getLogger(H2Fixtures.class);
private static ResourceLoader resourceLoader = new DefaultResourceLoader();
/**
* 插入XML文件中的数据到数据库.
*
* @param xmlFilePaths 符合Spring Resource路径格式的文件路径列表.
*/
public static void loadData(DataSource dataSource, String... xmlFilePaths) throws Exception {
execute(DatabaseOperation.INSERT, dataSource, xmlFilePaths);
}
/**
* 先删除XML数据文件中涉及的表的数据, 再插入XML文件中的数据到数据库.
*
* 在更新全部表时速度没有reloadAllTable快且容易产生锁死, 适合只更新小部分表的数据的情况.
*
* @param xmlFilePaths 符合Spring Resource路径格式的文件路径列表.
*/
public static void reloadData(DataSource dataSource, String... xmlFilePaths) throws Exception {
execute(DatabaseOperation.CLEAN_INSERT, dataSource, xmlFilePaths);
}
/**
* 在数据库中删除XML文件中涉及的表的数据.
*
* @param xmlFilePaths 符合Spring Resource路径格式的文件路径列表.
*/
public static void deleteData(DataSource dataSource, String... xmlFilePaths) throws Exception {
execute(DatabaseOperation.DELETE_ALL, dataSource, xmlFilePaths);
}
/**
* 对XML文件中的数据在数据库中执行Operation.
*
* @param xmlFilePaths 符合Spring Resource路径格式的文件列表.
*/
private static void execute(DatabaseOperation operation, DataSource dataSource, String... xmlFilePaths)
throws DatabaseUnitException, SQLException {
IDatabaseConnection connection = getConnection(dataSource);
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);
}
}
}
protected static H2Connection getConnection(DataSource dataSource) throws DatabaseUnitException, SQLException {
return new H2Connection(dataSource.getConnection(), null);
}