//DBUnit Skeletion
//简单的DBUnit例子
//经实例整理而成,数据层采用jdo。
public class CargoContainerDAOTest extends DatabaseTestCase
{
private IDatabaseConnection conn = null;
private IDataSet dataSet = null;
private DAOJDO dao = null;
private static String[] tableNames = new String[]
{"AIRSIDE_MOVEMENT",
"ULD_SERIAL_TARE_WEIGHT"};
private static String[] excludeColumns = new String[]
{"CREATED_BY",
"CREATED_BY_FUNCTION",
"CREATED_DATE_TIME",
"JDOVERSION",
"LAST_UPDATED_BY",
"LAST_UPDATED_BY_FUNCTION",
"LAST_UPDATED_DATE_TIME",
"VERSION"};
private static final String SEED =
"/testdata/seed1.xml";
//Setup 和 teardown 方法:在每个testXXX方法之前、之后调用。为了避免数据冲突,把表清空。
protected void setUp() throws Exception
{
conn = getConnection();
emptyTable();
dao = new DAOJDO();
//应用Transaction
TransactionManagerUtil.getTransactionManager().begin();
}
protected void tearDown() throws Exception
{
closeConnection(conn);
}
protected IDatabaseConnection getConnection() throws Exception
{
//装载驱动,通过用户名,密码连接数据库,创建jdbc连接
Class driverClass = Class.forName(System.getProperty("driverClassName"));
Connection jdbcConnection = DriverManager.getConnection(System.getProperty("dbunit.url"), System
.getProperty("dbunit.user"), System.getProperty("dbunit.password"));
return new DatabaseConnection(jdbcConnection);
}
protected IDataSet getDataSet() throws Exception
{
return null;
}
protected IDataSet getDataSet(String seed) throws Exception
{
URL url = CargoContainerDAOTest.class.getResource(seed);
if (url == null)
{
throw new Exception("could not find file for " + seed);
}
return new FlatXmlDataSet(url);
}
protected void emptyTable() throws Exception
{
IDataSet dataSet = conn.createDataSet(tableNames);
DatabaseOperation.DELETE_ALL.execute(conn, dataSet);
}
//***********************start test methods******************
public void testCreateAirsideMovement() throws Exception
{
emptyTable();
AirsideMovement am = new AirsideMovement();
am.setAirsideMovementId(100);
dao.createAirsideMovement(am, null);
TransactionManagerUtil.getTransactionManager().commit();
String[] nowTable = {"AIRSIDE_MOVEMENT"};
IDataSet databaseDataSet = conn.createDataSet(nowTable);
ITable actualTable = databaseDataSet.getTable("AIRSIDE_MOVEMENT");
IDataSet expectedDataSet = getDataSet(SEED1);
ITable expectedTable = expectedDataSet.getTable("AIRSIDE_MOVEMENT");
//创建表的test方法都类似这样。
//
actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, excludeColumns);
Assertion.assertEquals(expectedTable, actualTable);
}
public void testRetrieveEICbyULDItinerary() throws Exception
{
emptyTable();
DatabaseOperation.INSERT.execute(conn, getDataSet(SEED3));
isRetrieve = true;
EIC eic = dao.retrieveEICbyULDItinerary("ULDI1001", 123);
assertEquals(eic.getEicId(), 123);
}
}