a67c9735d5b790f7776bfec10a37051bf66f2222.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /***************************************************
  2. * Copyright 2010-5-24, -sun,All rights reserved.
  3. * Create date : 2010-5-24
  4. *
  5. * Author : sun
  6. * JDBCHelper JDBCHelper = new JDBCHelper()测试方法
  7. *
  8. * JDBCHelper:是为了解决业务操作存在多条,需要在一个事务中处理的情况
  9. **************************************************/
  10. package test.junitTest.sysmodel;
  11. import java.sql.Connection;
  12. import java.sql.SQLException;
  13. import java.util.ArrayList;
  14. import junit.framework.TestCase;
  15. import org.apache.log4j.Logger;
  16. import test.junitTest.JunitInit;
  17. import com.persistence.service.JDBCHelper;
  18. import com.persistence.service.assitant.DataObject;
  19. import com.persistence.service.assitant.Field;
  20. import com.persistence.service.exception.PersistenceException;
  21. import com.persistence.service.exception.TransformTypeException;
  22. /**
  23. * The Class TestJDBCHelper.
  24. */
  25. public class TestJDBCHelper extends TestCase{
  26. private final static Logger log = Logger.getLogger(TestJDBCHelper.class);
  27. @SuppressWarnings("unused")
  28. private JunitInit unit = null;
  29. protected void setUp() throws Exception {
  30. unit = new JunitInit();
  31. super.setUp();
  32. }
  33. protected void tearDown() throws Exception {
  34. super.tearDown();
  35. }
  36. public void testGetConnection() {
  37. Connection conn = null;
  38. try {
  39. JDBCHelper JDBCHelper = new JDBCHelper(); // 默认调用 classid=99
  40. // 指向的数据库链接
  41. // JDBCHelper JDBCHelper = new JDBCHelper(880);//调用 classid=880
  42. // 指向的数据库链接
  43. // 创建数据库链接
  44. JDBCHelper.begin();
  45. conn = JDBCHelper.getConnection();
  46. conn.close();
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. } finally {
  50. try {
  51. conn.close();
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57. public void testAddObjectData() {
  58. JDBCHelper JDBCHelper = new JDBCHelper();
  59. try {
  60. // 创建数据库链接
  61. JDBCHelper.begin();
  62. DataObject dataObject = new DataObject();
  63. dataObject.setClassid(801);
  64. Field attribute = new Field();
  65. attribute.setFieldname("User_ID");
  66. attribute.setFieldvalue("User_ID");
  67. dataObject.addAttribute(attribute);
  68. attribute = new Field();
  69. attribute.setFieldname("Option_Time");
  70. attribute.setFieldvalue("2010-05-10 11:01:12");
  71. dataObject.addAttribute(attribute);
  72. attribute = new Field();
  73. attribute.setFieldname("Description");
  74. attribute.setFieldvalue("Description");
  75. dataObject.addAttribute(attribute);
  76. String fdk = JDBCHelper.addObjectData(dataObject);
  77. log.info("插入数据库中的记录 对应 fd_objectid ::" + fdk);
  78. super.assertTrue(fdk.length() >= 30);
  79. // 忘了执行这个数据不会提交到数据库
  80. JDBCHelper.commit();
  81. } catch (TransformTypeException e) {
  82. JDBCHelper.rollback();
  83. e.printStackTrace();
  84. } catch (PersistenceException e) {
  85. JDBCHelper.rollback();
  86. e.printStackTrace();
  87. }
  88. }
  89. @SuppressWarnings("unused")
  90. public void testUpdateObjectData() {
  91. JDBCHelper JDBCHelper = new JDBCHelper();
  92. try {
  93. // 创建数据库链接
  94. JDBCHelper.begin();
  95. DataObject dataObject = new DataObject();
  96. dataObject.setClassid(801);
  97. Field attribute = new Field();
  98. attribute.setFieldname("User_ID");
  99. attribute.setFieldvalue("User_ID");
  100. dataObject.addAttribute(attribute);
  101. attribute = new Field();
  102. attribute.setFieldname("Option_Time");
  103. attribute.setFieldvalue("2010-05-10 11:01:12");
  104. dataObject.addAttribute(attribute);
  105. attribute = new Field();
  106. attribute.setFieldname("Description");
  107. attribute.setFieldvalue("Description");
  108. dataObject.addAttribute(attribute);
  109. String objectID = JDBCHelper.addObjectData(dataObject);
  110. log.info("插入数据库中的记录 对应 fd_objectid ::" + objectID);
  111. super.assertTrue(objectID.length() >= 30);
  112. DataObject dataobject = JDBCHelper.searchObjectData(objectID, 801);
  113. // 获得对象中指定属性的值
  114. String User_ID = (String) dataobject.getValue("USER_ID");// 传入参数不区分大小写,单必须在DataModel.xml中配置
  115. JDBCHelper.updateObjectData(dataobject);
  116. // 忘了执行这个数据不会提交到数据库
  117. JDBCHelper.commit();
  118. } catch (TransformTypeException e) {
  119. JDBCHelper.rollback();
  120. e.printStackTrace();
  121. } catch (PersistenceException e) {
  122. JDBCHelper.rollback();
  123. e.printStackTrace();
  124. }
  125. }
  126. public void testDeleteDataStringIntBoolean() {
  127. JDBCHelper JDBCHelper = new JDBCHelper();
  128. try {
  129. // 创建数据库链接
  130. JDBCHelper.begin();
  131. DataObject dataObject = new DataObject();
  132. dataObject.setClassid(801);
  133. Field attribute = new Field();
  134. attribute.setFieldname("User_ID");
  135. attribute.setFieldvalue("User_ID");
  136. dataObject.addAttribute(attribute);
  137. attribute = new Field();
  138. attribute.setFieldname("Option_Time");
  139. attribute.setFieldvalue("2010-05-10 11:01:12");
  140. dataObject.addAttribute(attribute);
  141. attribute = new Field();
  142. attribute.setFieldname("Description");
  143. attribute.setFieldvalue("Description");
  144. dataObject.addAttribute(attribute);
  145. String objectID = JDBCHelper.addObjectData(dataObject);
  146. log.info("插入数据库中的记录 对应 fd_objectid ::" + objectID);
  147. super.assertTrue(objectID.length() >= 30);
  148. // 假删除:调用这个方法时,数据库中必须有列字段is_Del
  149. // super.assertTrue(JDBCHelper.deleteData(objectID, 801, false));
  150. // 真删除
  151. super.assertTrue(JDBCHelper.deleteData(objectID, 801, true));
  152. // 忘了执行这个数据不会提交到数据库
  153. JDBCHelper.commit();
  154. } catch (TransformTypeException e) {
  155. JDBCHelper.rollback();
  156. e.printStackTrace();
  157. } catch (PersistenceException e) {
  158. JDBCHelper.rollback();
  159. e.printStackTrace();
  160. }
  161. }
  162. public void testDeleteDataStringInt() {
  163. JDBCHelper JDBCHelper = new JDBCHelper();
  164. try {
  165. // 创建数据库链接
  166. JDBCHelper.begin();
  167. DataObject dataObject = new DataObject();
  168. dataObject.setClassid(801);
  169. Field attribute = new Field();
  170. attribute.setFieldname("User_ID");
  171. attribute.setFieldvalue("User_ID");
  172. dataObject.addAttribute(attribute);
  173. attribute = new Field();
  174. attribute.setFieldname("Option_Time");
  175. attribute.setFieldvalue("2010-05-10 11:01:12");
  176. dataObject.addAttribute(attribute);
  177. attribute = new Field();
  178. attribute.setFieldname("Description");
  179. attribute.setFieldvalue("Description");
  180. dataObject.addAttribute(attribute);
  181. String objectID = JDBCHelper.addObjectData(dataObject);
  182. log.info("插入数据库中的记录 对应 fd_objectid ::" + objectID);
  183. super.assertTrue(objectID.length() >= 30);
  184. // 假删除:调用这个方法时,数据库中必须有列字段is_Del
  185. super.assertTrue(JDBCHelper.deleteData(objectID, 801));
  186. // 忘了执行这个数据不会提交到数据库
  187. JDBCHelper.commit();
  188. } catch (TransformTypeException e) {
  189. JDBCHelper.rollback();
  190. e.printStackTrace();
  191. } catch (PersistenceException e) {
  192. JDBCHelper.rollback();
  193. e.printStackTrace();
  194. }
  195. }
  196. /**
  197. * 返回执行sql语句结果
  198. *
  199. * @param sql
  200. * sql语句
  201. * @return 执行结果 if >0 success -1 fail public int executeUpdateSQL(String
  202. * sql) throws PersistenceException
  203. */
  204. public void testExecuteUpdateSQLString() {
  205. JDBCHelper JDBCHelper = new JDBCHelper();
  206. try {
  207. // 事务操作开始
  208. JDBCHelper.begin();
  209. int insert = JDBCHelper
  210. .executeUpdateSQL("INSERT INTO sys_log (FD_OBJECTID, User_ID, Display_Name,"
  211. + " Dept_Id, Option_Time, Action, Description, IP, URL) "
  212. + "VALUES ('-1','a','','','2010-05-10 11:01:12','5','登录时,用户名不存在',"
  213. + "'127.0.0.1','http://localhost:8080/nbgl/ws/sys/LoginService/Login')");
  214. super.assertEquals(1, insert);
  215. int idel = JDBCHelper.executeUpdateSQL("delete from sys_log where FD_OBJECTID = '-1'");
  216. super.assertEquals(1, idel);
  217. // 事务操作提交
  218. JDBCHelper.commit();
  219. } catch (PersistenceException e) {
  220. log.error("数据操作失败");
  221. e.printStackTrace();
  222. JDBCHelper.rollback();// 捕获异常事务回滚
  223. }
  224. }
  225. /**
  226. * 返回执行多条sql语句结果
  227. *
  228. * @param sql
  229. * sql集合
  230. * @return 执行结果
  231. * @throws PersistenceException
  232. * public int[] executeUpdateSQL(ArrayList<String> sql) throws
  233. * PersistenceException
  234. */
  235. public void testExecuteUpdateSQLArrayListOfString() {
  236. JDBCHelper JDBCHelper = new JDBCHelper();
  237. try {
  238. // 事务操作开始
  239. JDBCHelper.begin();
  240. ArrayList<String> sql = new ArrayList<String>();
  241. sql.add("INSERT INTO sys_log (FD_OBJECTID, User_ID, Display_Name, Dept_Id, Option_Time, Action, Description, IP, URL) VALUES ('-1','a','','','2010-05-10 11:01:12','5','登录时,用户名不存在','127.0.0.1','http://localhost:8080/nbgl/ws/sys/LoginService/Login')");
  242. sql.add("INSERT INTO sys_log (FD_OBJECTID, User_ID, Display_Name, Dept_Id, Option_Time, Action, Description, IP, URL) VALUES ('-2','a','','','2010-05-10 11:01:12','5','登录时,用户名不存在','127.0.0.1','http://localhost:8080/nbgl/ws/sys/LoginService/Login')");
  243. sql.add("INSERT INTO sys_log (FD_OBJECTID, User_ID, Display_Name, Dept_Id, Option_Time, Action, Description, IP, URL) VALUES ('-3','a','','','2010-05-10 11:01:12','5','登录时,用户名不存在','127.0.0.1','http://localhost:8080/nbgl/ws/sys/LoginService/Login')");
  244. int[] insert = JDBCHelper.executeUpdateSQL(sql);
  245. super.assertTrue(insert.length == 3);
  246. super.assertEquals(1, insert[0]);
  247. super.assertEquals(1, insert[1]);
  248. super.assertEquals(1, insert[2]);
  249. sql.clear();
  250. sql.add("delete from sys_log where FD_OBJECTID in ('-1','-3')");
  251. sql.add("delete from sys_log where FD_OBJECTID = '-2'");
  252. int[] idel = JDBCHelper.executeUpdateSQL(sql);
  253. super.assertTrue(idel.length == 2);
  254. super.assertEquals(2, idel[0]);
  255. super.assertEquals(1, idel[1]);
  256. // 事务操作提交
  257. JDBCHelper.commit();
  258. } catch (PersistenceException e) {
  259. log.error("数据操作失败");
  260. e.printStackTrace();
  261. JDBCHelper.rollback();// 捕获异常事务回滚
  262. }
  263. }
  264. /*
  265. * public void testStoreBlob() { fail("Not yet implemented"); }
  266. *
  267. * public void testStoreClob() { fail("Not yet implemented"); }
  268. */
  269. // ---提供的查询方法------------------------------------------------------
  270. /**
  271. * 查询返回数据对象<DataObject>,不包括lob数据
  272. *
  273. * @param objectID
  274. * 数据库表记录唯一主键 fd_objectid
  275. * @param classid
  276. * 数据库表记录唯一标示,对应DataModel.xml配置
  277. *
  278. * public DataObject searchObjectData(String objectID, int
  279. * classid)
  280. */
  281. @SuppressWarnings("unused")
  282. public void testSearchObjectData() throws PersistenceException {
  283. JDBCHelper JDBCHelper = new JDBCHelper(); // 默认调用 classid=99 指向的数据库链接
  284. // JDBCHelper JDBCHelper = new JDBCHelper(880);//调用 classid=880 指向的数据库链接
  285. // 创建数据库链接
  286. JDBCHelper.begin();
  287. // 查找DataModel.xml 中一个classid,到数据库中查找一个FD_OBJECTID
  288. DataObject DataObject = JDBCHelper.searchObjectData("880121076688331200000039110112", 880);
  289. // 打印获得的对象
  290. DataObject.PrintDataObject();
  291. // 获得对象中指定属性的值
  292. String User_ID = (String) DataObject.getValue("USER_ID");// 传入参数不区分大小写,单必须在DataModel.xml中配置
  293. super.assertEquals("880121076688331200000039110112", DataObject.getObjectID());
  294. // 关闭数据库链接
  295. JDBCHelper.release();
  296. }
  297. /**
  298. * 根据条件查询数据,只查询该表数据
  299. *
  300. * @param SQLcondition
  301. * where 查询条件
  302. * @return 返回数据集合,每个对象为DataObject
  303. *
  304. * public ArrayList<DataObject> searchAllData(int classid, String
  305. * SQLcondition)
  306. */
  307. @SuppressWarnings("unused")
  308. public void testSearchAllData() throws TransformTypeException, PersistenceException {
  309. JDBCHelper JDBCHelper = new JDBCHelper();
  310. JDBCHelper.begin();
  311. ArrayList<DataObject> objlis = JDBCHelper.searchAllData(880, " where type='2'");
  312. // 注意循环中不要调用objlis.size() 这样可以提高性能
  313. int objlislen = objlis.size();
  314. for (int i = 0; i < objlislen; i++) {
  315. DataObject Obj = objlis.get(i);
  316. // 获得对象中指定属性的值
  317. String User_ID = (String) Obj.getValue("USER_ID");// 传入参数不区分大小写,单必须在DataModel.xml中配置
  318. }
  319. super.assertTrue(objlis.size() > 0);
  320. JDBCHelper.release();
  321. }
  322. /**
  323. * 返回数值运算函数结果 public int getFunctionNumber(int classid, String sql)
  324. */
  325. public void testGetFunctionNumber() throws TransformTypeException, PersistenceException {
  326. JDBCHelper JDBCHelper = new JDBCHelper();
  327. JDBCHelper.begin();
  328. super.assertTrue(JDBCHelper.getFunctionNumber(880, "select count(*) from SysUser_Info") > 0);
  329. JDBCHelper.release();
  330. }
  331. /**
  332. * 结果集中每个对象是字符串数组,内容是sql语句的查询字段 public ArrayList<String[]>
  333. * getSearchResult(int classid, String sql) throws PersistenceException
  334. */
  335. public void testGetSearchResult() {
  336. JDBCHelper JDBCHelper = new JDBCHelper();
  337. try {
  338. JDBCHelper.begin();
  339. ArrayList<String[]> lst = JDBCHelper.getSearchResult(880,
  340. "select fd_objectid,Name from SysUser_Info");
  341. int lstlen = lst.size();
  342. // 返回对象取值方法
  343. for (int i = 0; i < lstlen; i++) {
  344. String[] temp = lst.get(i);
  345. log.info("fd_objectid=" + temp[0] + " Name=" + temp[1]);
  346. }
  347. super.assertTrue(lst.size() > 0);
  348. } catch (PersistenceException e) {
  349. e.printStackTrace();
  350. } finally {
  351. JDBCHelper.release();
  352. }
  353. }
  354. /*
  355. * <mdpClass classid="801" description="日志信息表" name="Sys_Log" oldName=""
  356. * type="Sys" catche="false" exist="true" dataSource="1" validate="true">
  357. * <mdpAttribute description="操作者登录名称" name="User_ID" oldName="" unit=""
  358. * dataType="string" precision="24" scale="" referenceType="0"
  359. * defaultValue="" logicPrimaryKey="false" notNull="false" validate="true"
  360. * index="false" indexType=""/> <mdpAttribute description="操作者名称"
  361. * name="Display_Name" oldName="" unit="" dataType="string" precision="24"
  362. * scale="" referenceType="0" defaultValue="" logicPrimaryKey="false"
  363. * notNull="false" validate="true" index="false" indexType=""/>
  364. * <mdpAttribute description="操作者所属部门" name="Dept_Id" oldName="" unit=""
  365. * dataType="string" precision="15" scale="0" referenceType="2"
  366. * defaultValue="" logicPrimaryKey="false" notNull="false" validate="true"
  367. * index="false" indexType=""> <reference displayName=""
  368. * referenceTable="BM$_UserArea" storeName=""/> </mdpAttribute>
  369. * <mdpAttribute description="操作时间" name="Option_Time" oldName="" unit=""
  370. * dataType="string" precision="20" scale="" referenceType="0"
  371. * defaultValue="" logicPrimaryKey="false" notNull="false" validate="true"
  372. * index="false" indexType=""/> <mdpAttribute description="动作类型"
  373. * name="Action" oldName="" unit="" dataType="string" precision="15"
  374. * scale="" referenceType="2" defaultValue="" logicPrimaryKey="false"
  375. * notNull="false" validate="true" index="false" indexType=""> <reference
  376. * displayName="" referenceTable="BM$_LogAction" storeName=""/>
  377. * </mdpAttribute> <mdpAttribute description="动作描述" name="Description"
  378. * oldName="" unit="" dataType="string" precision="200" scale=""
  379. * referenceType="0" defaultValue="" logicPrimaryKey="false" notNull="false"
  380. * validate="true" index="false" indexType=""/> <mdpAttribute
  381. * description="操作者的IP" name="IP" oldName="" unit="" dataType="string"
  382. * precision="24" scale="" referenceType="0" defaultValue=""
  383. * logicPrimaryKey="false" notNull="false" validate="true" index="false"
  384. * indexType=""/> <mdpAttribute description="网络地址" name="URL" oldName=""
  385. * unit="" dataType="string" precision="400" scale="" referenceType="0"
  386. * defaultValue="" logicPrimaryKey="false" notNull="false" validate="true"
  387. * index="false" indexType=""/> </mdpClass>
  388. */
  389. }