Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
DBUnit Skeleton

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

//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);
 }
}




相关文章

相关软件