본문 바로가기

기술관련/개발환경

오늘 만든 DBUnit BaseClass

package xxx.framework.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import org.dbunit.Assertion;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.database.QueryDataSet;
import org.dbunit.dataset.CompositeTable;
import org.dbunit.dataset.FilteredTableMetaData;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.filter.DefaultColumnFilter;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;

import junit.framework.TestCase;
public class DBTestCase extends TestCase {

private DBinfo info = DBinfo.getInstance();

/**
* コンストラクタ。
*
*/
public DBTestCase() {
super();

}


/**
* 指定したテーブルのデータでXMLFileを作成します。
* @param arrayTables 対象テーブル名
* @param sFileName xmlファイル名
*/
protected void exportOp(String[] arrayTables, String sFileName)throws Exception {
info.init();
exportTable(arrayTables,new File(info.sExportDir_, sFileName));
}

/**
* XMLFileをDBに書き込みます。
* @param sFileName xmlファイル名
*/
protected void importOp(String sFileName)throws Exception {
info.init();
importTable(new File(info.sExportDir_, sFileName));
}

/**
* 指定したテーブルの現在データとXMLFileを比較します。
* @param arrayTables 対象テーブル名
* @param sFileName xmlファイル名
*/
protected void compareOp(String[] arrayTables, String sFileName) throws Exception{
info.init();
compareTable(arrayTables,new File(info.sExportDir_,sFileName),null);
}
/**
* 指定したテーブルの現在データとXMLFileを比較します。
* @param arrayTables 対象テーブル名
* @param sFileName xmlファイル名
* @param columns 比較対象外とするカラム名
*/
protected void compareOp(String[] arrayTables, String sFileName,String[] columns) throws Exception{
info.init();
compareTable(arrayTables,new File(info.sExportDir_,sFileName),columns);
}

private void compareTable(String[] arrayTables, File file,String[] columns) throws Exception {
IDatabaseConnection connection = null;

try {
connection = getConnection();
QueryDataSet partialDataSet = new QueryDataSet(connection);

for (int i = 0; i < arrayTables.length; i++) {
IDataSet databaseDataSet = getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable(arrayTables[i]);
IDataSet expectedDataSet = new FlatXmlDataSet(file);
ITable expectedTable = expectedDataSet.getTable(arrayTables[i]);
if(columns == null){
Assertion.assertEquals(actualTable, expectedTable);
}else{
DefaultColumnFilter columnFilter = new DefaultColumnFilter();
for(int x = 0; x < columns.length; x++) {
columnFilter.excludeColumn(columns[x]);
}
FilteredTableMetaData metaData = new FilteredTableMetaData(actualTable.getTableMetaData(), columnFilter);
ITable filteredTable = new CompositeTable(metaData, actualTable);
Assertion.assertEquals(filteredTable, expectedTable);
}
}
}finally {
if (connection != null) {
connection.close();
}
}
}


private void importTable(File file) throws Exception {

IDatabaseConnection connection = null;
try {
connection = getConnection();
FlatXmlDataSet dataSet = new FlatXmlDataSet(file);
DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
} finally {
if (connection != null) {
connection.close();
}
}
}

private void exportTable(String[] arrayTables,File file) throws Exception{
IDatabaseConnection connection = null;
try {
connection = getConnection();
QueryDataSet partialDataSet = new QueryDataSet(connection);
for (int i = 0; i < arrayTables.length; i++) {
partialDataSet.addTable(arrayTables[i]);
}
FlatXmlDataSet.write(partialDataSet, new FileOutputStream(file));
} finally {
if (connection != null) {
connection.close();
}
}
}

private IDatabaseConnection getConnection()throws Exception {
Class driverClass =
Class.forName(info.sDriver_);

Connection jdbcConnection =
DriverManager.getConnection(
info.sUrl_, info.sUser_, info.sPassword_);

return new DatabaseConnection(jdbcConnection);
}

private void operation(DatabaseOperation operation,IDataSet dataSet)throws Exception {
IDatabaseConnection connection = null;
try {
connection = getConnection();
operation.execute(connection, dataSet);
} finally {
if (connection != null) {
connection.close();
}
}
}

private static class DBinfo{
private static final DBinfo INSTANCE = new DBinfo();
private boolean initialrized;
private Properties prop;
public String sDriver_;
public String sUrl_;
public String sUser_;
public String sPassword_;
public String sSchema_;
public String sExportDir_;


public void init() throws IOException, ClassNotFoundException {
if (initialrized) {
return;
}
synchronized (this) {
prop = new Properties();
InputStream resourceStream =
ClassLoader.getSystemResourceAsStream("dbunit.properties");

if (resourceStream == null) {
System.err.println("クラスパスにdbunit.propertiesがありません!!");
}
prop.load(resourceStream);
sDriver_ = prop.getProperty("driver");
sUrl_ = prop.getProperty("url");
sUser_ = prop.getProperty("user");
sPassword_ = prop.getProperty("password");
sSchema_ = prop.getProperty("schema");
sExportDir_ = prop.getProperty("exportDir");
Class.forName(sDriver_);
initialrized = true;
}
}

public static DBinfo getInstance() {
return INSTANCE;
}

}
}

'기술관련 > 개발환경' 카테고리의 다른 글

DB 변경사항 반영  (0) 2005.03.31
TDD  (0) 2005.02.17
코더 10명  (0) 2005.02.02
vi command  (0) 2005.01.20
j2ee개발 환경(웹로직 개발시 디렉토리)  (1) 2004.12.21