DBUnitインスタンスSCテスト概要(二)--データベーステストDbunitDemo

8238 ワード

テスト構造は次のとおりです.
1テストデータの定義
2テストデータをデータベースにインポートする
3予想結果と実績を比較する
目的:
selectAllDemoメソッドをテストし、クエリー結果が有効かどうかを検出します.
Dbuintフレームワークを使用してテストを完了
テスト用のデータベース接続オブジェクトを返します
protected abstract IDatabaseConnection getConnection() throws Exception;
テスト用XMLデータセットオブジェクトを返す
protected abstract IDataSet getDataSet() throws Exception;
部分的にcsdn記事を参照
package baor.src.dbunit;

import java.sql.ResultSet;
import java.sql.SQLException;
import baor.test.sql.ConnPropInfo;
import baor.test.sql.ConnUtil;
import java.sql.Statement;

public interface CRUDDemo {
	public abstract ResultSet selectAllDemo(Statement stmt, String SQLString)
			throws SQLException;
	public abstract Statement setSTMT(ConnUtil connUtil, ConnPropInfo demo)
			throws SQLException;
}
package baor.src.dbunit;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import baor.test.sql.ConnPropInfo;
import baor.test.sql.ConnUtil;

public class SelectAllDemo implements CRUDDemo {

	@Override
	public ResultSet selectAllDemo(Statement stmt, String SQLString)
			throws SQLException {
		// TODO Auto-generated method stub
		//  
		ResultSet rs = stmt.executeQuery(SQLString);
		// System.out.println("TestNo" + "\t" + "Description" + "\t" +
		// "Author");
		return rs;
	}

	@Override
	public Statement setSTMT(ConnUtil connUtil, ConnPropInfo demo)
			throws SQLException {
		// TODO Auto-generated method stub
		//  stmt 
		Statement stmt = connUtil.getConnection(demo).createStatement();
		return stmt;
	}

}
package baor.test.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnUtil {
	Connection connection = null;

	public Connection getConnection(ConnPropInfo prop) {
		if (connection == null) {
			try {
				//  Class.forName() 
				Class.forName(prop.getDriver());
				System.out.println(" MySQL !");
				connection = DriverManager.getConnection(prop.getUrl(),
						prop.getUser(), prop.getPwd());
				System.out.print(" !");
			} catch (ClassNotFoundException e1) {
				System.out.println(" MySQL !");
				e1.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return connection;

	}

	public void closeConnection(Connection conn) {
		try {
			if (conn != null) {
				conn.close();
				conn = null;
				System.out.println(" !!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
package baor.src.dbunit;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

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

public class DataUtil {

	public void simgleTableExport(Connection conn, String tableName,
			String xmlFile) {
		IDatabaseConnection connection = null;
		try {
			connection = new DatabaseConnection(conn);
			QueryDataSet dataSet = new QueryDataSet(connection);
			dataSet.addTable(tableName);
			FlatXmlDataSet.write(dataSet, new FileOutputStream(xmlFile));
		} catch (DatabaseUnitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void allTableExport(Connection conn, String fileName) {
		IDatabaseConnection connection = null;
		try {
			connection = new DatabaseConnection(conn);
			IDataSet dataSet = connection.createDataSet();
			FlatXmlDataSet.write(dataSet, new FileOutputStream(fileName));
		} catch (DatabaseUnitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
package baor.src.dbunit;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;

import org.dbunit.DatabaseTestCase;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.dataset.xml.FlatXmlProducer;
import org.dbunit.ext.mysql.MySqlConnection;
import org.dbunit.operation.DatabaseOperation;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.InputSource;
import java.sql.Statement;
import baor.test.sql.ConnPropInfo;
import baor.test.sql.ConnUtil;
import baor.test.sql.PropInfoFactory;
import baor.test.sql.PropMySQLFactory;

public class DemoTest extends DatabaseTestCase {
	Connection conn;
	ResultSet rs;
	Statement stmt;
	FlatXmlProducer fxp;
	SelectAllDemo testDemo;
	ConnPropInfo demo;
	ConnUtil testConnUtil;
	String sql = "select * from testdemo where TestNo = 1";

	@Before
	public void setUp() throws Exception {
		//  
		PropInfoFactory test = new PropMySQLFactory();
		demo = test.createPropInfo();
		testConnUtil = new ConnUtil();
		conn = testConnUtil.getConnection(demo);
		// xml 
		InputStream fis = new FileInputStream("C://testdemo1.xml");
		InputSource is = new InputSource(fis);
		fxp = new FlatXmlProducer(is);

	}

	@After
	public void tearDown() throws Exception {
		testConnUtil.closeConnection(conn);
	}

	@Test
	public void testGet() throws Exception {
		testDemo = new SelectAllDemo();
		stmt = testDemo.setSTMT(testConnUtil, demo);
		rs = testDemo.selectAllDemo(stmt, sql);
		while (rs.next()) {
			assertEquals(1, rs.getInt(1));
			assertEquals("test", rs.getString(2));
			assertEquals("paur", rs.getString(3));
		}
		stmt.close();
		rs.close();

	}

	@Override
	protected IDatabaseConnection getConnection() throws Exception {
		// TODO Auto-generated method stub
		return new MySqlConnection(conn, "test");
	}

	@Override
	protected IDataSet getDataSet() throws Exception {
		// TODO Auto-generated method stub
		return new FlatXmlDataSet(fxp);

	}

	/**
	 *  xml db , 。
	 */
	protected DatabaseOperation getSetUpOperation() throws Exception {
		System.out.println("setup...");
		return DatabaseOperation.CLEAN_INSERT;
	}

	/**
	 *  , 
	 */
	protected DatabaseOperation getTearDownOperation() throws Exception {
		System.out.println("teardown...");
		return DatabaseOperation.NONE;
	}

}