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.?}??