TestDBConnection父类 所有test类继承该类
1. package test.sample.service.util;
2.
3. import java.io.FileInputStream;
4. import java.io.FileOutputStream;
5. import java.sql.Connection;
6. import java.sql.DriverManager;
7.
8. import org.dbunit.DatabaseTestCase;
9. import org.dbunit.database.DatabaseConnection;
10. import org.dbunit.database.IDatabaseConnection;
11. import org.dbunit.dataset.IDataSet;
12. import org.dbunit.dataset.xml.FlatXmlDataSet;
13. import org.dbunit.operation.DatabaseOperation;
14. /**
15. * DbUnit 可以有不同的数据库操作,我使用了其中的两种:
16. *DELETE_ALL ,它删除表中所有行。
17. *CLEAN_INSERT ,它删除表中所有行并插入数据集提供的行。
18. *This method inserts the contents of a FlatXmlDataSet file
19. *into the connection
20. * @author donganlei
21. *
22. */
23. public class TestDBConnection extends DatabaseTestCase {
24. private IDatabaseConnection conn;
25.
26. private String driverName = "oracle.jdbc.OracleDriver";
27.
28. private String dburl = "jdbc:oracle:thin:@192.168.0.2:1521:PORTAL";
29.
30. private String username = "portal";
31.
32. private String pwd = "portal";
33.
34. private String schema = "PORTAL";
35.
36. private String xmlUrl = "e:\\test.xml";//从数据库中取值到XML中(路径)
37.
38. private String dbxmlurl = "e:\\test.xml";//从XML中还原数据库中的记录(路径)
39. /**
40. * 初始化TestDBConnection
41. *
42. */
43. public TestDBConnection() {
44. try {
45. Class.forName(driverName);
46.
47. Connection jdbcConnection = DriverManager.getConnection(dburl,
48. username, pwd);
49. conn = new DatabaseConnection(jdbcConnection, schema);
50. } catch (Exception e) {
51. e.printStackTrace();
52. }
53. }
54. /**
55. * 建立连接
56. * 返回IDatabaseConnection
57. */
58. public synchronized IDatabaseConnection getConnection() throws Exception {
59. Class.forName(driverName);
60. Connection jdbcConnection = DriverManager.getConnection(dburl,
61. username, pwd);
62. conn = new DatabaseConnection(jdbcConnection, schema);
63. return conn;
64. }
65. /**
66. * 方法:它删除表中所有行并插入数据集提供的行
67. * @throws Exception
68. */
69. protected void insertFileIntoDb() throws Exception
70. {
71. DatabaseOperation.CLEAN_INSERT.execute(getConnection(), getDataSet());
72. }
73. /**
74. * 方法:删除数据库中的所有数据
75. * @throws Exception
76. */
77. protected void deleteFileDb()throws Exception{
78. DatabaseOperation.DELETE_ALL.execute(conn, getDataSet());
79. }
80. /**
81. * 方法 :从XML中读取数据
82. */
83. protected IDataSet getDataSet() throws Exception {
84. return new FlatXmlDataSet(new FileInputStream(dbxmlurl));
85. }
86. /**
87. * 方法:读取数据库中的内容到xmlUrl中
88. * @param tableNames
89. * @throws Exception
90. */
91. public void backUp(String[] tableNames) throws Exception {
92. IDataSet fullDataSet = conn.createDataSet(tableNames);
93. FlatXmlDataSet.write(fullDataSet, new FileOutputStream(xmlUrl));
94. }
95. /**
96. * 方法:还原数据库中的数据
97. *
98. */
99. public void overRead() {
100.
101. }
102. /**
103. * 方法关闭连接
104. * @throws Exception
105. */
106. public void closeConn()throws Exception{
107. conn.close();
108. }
109. }
Test类
1. package test.sample.service.manage;
2. import java.util.List;
3.
4. import org.dbunit.database.IDatabaseConnection;
5. import org.dbunit.dataset.IDataSet;
6. import org.dbunit.dataset.ITable;
7.
8. import pub.tools.PageList;
9. import test.sample.service.util.TestDBConnection;
10.
11. import com.huawei.service.manage.WordManage;
12. import com.huawei.service.object.LeaveWordObject;
13. /**
14. * insertFileIntoDb(): Inserts a file in the database
15. emptyTable(): Cleans a database table
16. insertAllFilesIntoDb(): Inserts all the files for your project
17. emptyAllTables(): Cleans all the tables for your project
18.
19. * @author donganlei
20. *
21. */
22. public class WordManageTest extends TestDBConnection {
23. private IDataSet expectedDataSet;//XML中数据源设置
24. private ITable expectedTable;//XML中的数据
25. private IDataSet databaseDataSet;//数据库中的数据源设置
26. private ITable actualTable;//数据库中的数据
27. LeaveWordObject word;
28. String[] args={"ser_leaveword"};//所有要操作的表
29. private IDatabaseConnection conn;
30. public WordManageTest()throws Exception{
31. conn=this.getConnection();
32. this.backUp(args);//得到数据
33. expectedDataSet = getDataSet();
34. expectedTable = expectedDataSet.getTable("ser_leaveword");
35. databaseDataSet = getConnection().createDataSet();
36. actualTable = databaseDataSet.getTable("ser_leaveword");
37. }
38. /**
39. * 测 getList 方法
40. * @throws Exception
41. */
42. public void testGetWordList()throws Exception {
43. PageList pagination=new PageList();
44. WordManage wm=new WordManage();
45. List list=wm.getWordList(1, pagination);
46. String result=((LeaveWordObject)list.get(0)).getWordtitle();
47. assertEquals("testGetWordList",expectedTable.getValue(15, "WORDTITLE"),actualTable.getValue(15, "WORDTITLE"));
48. assertEquals("testGetWordList",expectedTable.getValue(15, "WORDTITLE"),result);
49. }
50. /**
51. * 测试Insert方法
52. * @throws Exception
53. */
54. public void testInsertWord()throws Exception{
55. WordManage wm=new WordManage();
56. word=new LeaveWordObject();
57. word.setClientid("1");
58. word.setLeaveword("测试方法");
59. word.setLeavetime("2008-01-01 11:11:11");
60. word.setFlag("0");
61. word.setAnswerman("3");
62. word.setWordtitle("标题测试");
63. wm.insertWord(word);
64. conn=this.getConnection();
65. this.backUp(args);
66. assertEquals("testGetWordList",expectedTable.getValue(16, "WORDTITLE"),actualTable.getValue(16, "WORDTITLE"));
67. assertEquals("testGetWordList",expectedTable.getValue(16, "WORDTITLE"),"标题测试");
68. }
69. /**
70. * 测试根据ID查找留言对象
71. * @throws Exception
72. */
73. public void testGetWordById()throws Exception{
74. WordManage wm=new WordManage();
75. conn=this.getConnection();
76. this.backUp(args);
77. assertEquals("testGetWordList",expectedTable.getValue(16, "WORDTITLE"),actualTable.getValue(16, "WORDTITLE"));
78. assertEquals("testGetWordList",expectedTable.getValue(16, "WORDTITLE"), wm.getWordById(180).getWordtitle());
79. }
80. /**
81. * 根据用户ID和时间查询 测试用户留言列表
82. * @throws Exception
83. */
84. public void testGetUserListByTime()throws Exception{
85. WordManage wm=new WordManage();
86. PageList pagination=new PageList();
87. conn=this.getConnection();
88. this.backUp(args);
89. List list=wm.getUserListByTime(1, "2008-01-01 11:11:11", "2008-04-04 11:11:11", pagination);
90. String result=((LeaveWordObject)list.get(0)).getWordtitle();
91.
92. assertEquals("testGetWordList",expectedTable.getValue(2, "WORDTITLE"),actualTable.getValue(2, "WORDTITLE"));
93. assertEquals("testGetWordList",expectedTable.getValue(2, "WORDTITLE"),result);
94. }
95. /**
96. * 根据坐席ID和时间查询 测试用户留言列表
97. * @throws Exception
98. */
99. public void testGetSeatListByTime()throws Exception{
100. WordManage wm=new WordManage();
101. PageList pagination=new PageList();
102. conn=this.getConnection();
103. this.backUp(args);
104. List list=wm.getSeatListByTime(3, "2008-01-01 11:11:11", "2008-04-04 11:11:11", pagination);
105. String result=((LeaveWordObject)list.get(0)).getWordtitle();
106. assertEquals("testGetWordList",expectedTable.getValue(8, "WORDTITLE"),actualTable.getValue(8, "WORDTITLE"));
107. assertEquals("testGetWordList",expectedTable.getValue(8, "WORDTITLE"),result);
108. }
109. /**
110. * 测试用户回复留言
111. * @throws Exception
112. */
113. public void testUpdateWord()throws Exception{
114. WordManage wm=new WordManage();
115. word=new LeaveWordObject();
116. word.setId("1");
117. word.setClientid("1");
118. word.setLeaveword("测试方法");
119. word.setLeavetime("2008-01-01 11:11:11");
120. word.setFlag("0");
121. word.setAnswerman("3");
122. word.setWordtitle("标题测试");
123. word.setAnswercontent("测试回复");
124. wm.updateWord(word);
125. conn=this.getConnection();
126. this.backUp(args);
127. assertEquals("testGetWordList",expectedTable.getValue(0, "ANSWERCONTENT"),actualTable.getValue(0, "ANSWERCONTENT"));
128. assertEquals("testGetWordList",expectedTable.getValue(0, "ANSWERCONTENT"),"测试回复");
129. }
130.
131. public static void main(String[] args){
132. junit.textui.TestRunner.run(WordManageTest.class);
133. }
134. }