/luni/src/test/java/tests/java/sql/UpdateFunctionalityTest.java

https://bitbucket.org/aways/android_libcore · Java · 349 lines · 231 code · 27 blank · 91 comment · 15 complexity · 09e9dd4cf5b2c6e8231ae4625d1c60bc MD5 · raw file

  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package tests.java.sql;
  17. import java.math.BigDecimal;
  18. import java.sql.Connection;
  19. import java.sql.DatabaseMetaData;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.sql.Statement;
  23. import java.util.HashSet;
  24. import tests.support.DatabaseCreator;
  25. import tests.support.Support_SQL;
  26. import junit.extensions.TestSetup;
  27. import junit.framework.Test;
  28. import junit.framework.TestCase;
  29. import junit.framework.TestSuite;
  30. public class UpdateFunctionalityTest extends TestCase {
  31. private static final int numberOfRecords = 20;
  32. private static Connection conn;
  33. private static Statement statement;
  34. public void setUp() throws Exception {
  35. super.setUp();
  36. Support_SQL.loadDriver();
  37. try {
  38. conn = Support_SQL.getConnection();
  39. statement = conn.createStatement();
  40. createTestTables();
  41. } catch (SQLException e) {
  42. fail("Unexpected SQLException " + e.toString());
  43. }
  44. DatabaseCreator.fillTestTable1(conn, numberOfRecords);
  45. DatabaseCreator.fillTestTable2(conn, numberOfRecords);
  46. }
  47. public void tearDown() throws Exception {
  48. deleteTestTables();
  49. statement.close();
  50. conn.close();
  51. super.tearDown();
  52. }
  53. protected void createTestTables() {
  54. try {
  55. DatabaseMetaData meta = conn.getMetaData();
  56. ResultSet userTab = meta.getTables(null, null, null, null);
  57. while (userTab.next()) {
  58. String tableName = userTab.getString("TABLE_NAME");
  59. if (tableName.equals(DatabaseCreator.TEST_TABLE1)) {
  60. statement.execute(DatabaseCreator.DROP_TABLE1);
  61. } else if (tableName
  62. .equals(DatabaseCreator.TEST_TABLE2)) {
  63. statement.execute(DatabaseCreator.DROP_TABLE2);
  64. } else if (tableName
  65. .equals(DatabaseCreator.TEST_TABLE3)) {
  66. statement.execute(DatabaseCreator.DROP_TABLE3);
  67. }
  68. }
  69. userTab.close();
  70. statement.execute(DatabaseCreator.CREATE_TABLE3);
  71. statement.execute(DatabaseCreator.CREATE_TABLE2);
  72. statement.execute(DatabaseCreator.CREATE_TABLE1);
  73. } catch (SQLException e) {
  74. fail("Unexpected SQLException " + e.toString());
  75. }
  76. }
  77. protected void deleteTestTables() {
  78. try {
  79. statement.execute(DatabaseCreator.DROP_TABLE1);
  80. statement.execute(DatabaseCreator.DROP_TABLE2);
  81. statement.execute(DatabaseCreator.DROP_TABLE3);
  82. } catch (SQLException e) {
  83. fail("Unexpected SQLException " + e.toString());
  84. }
  85. }
  86. /**
  87. * UpdateFunctionalityTest#testUpdate1(). Updates all values in one
  88. * column in the table
  89. */
  90. public void testUpdate1() {
  91. String newValue = "newValue";
  92. String updateQuery = "UPDATE " + DatabaseCreator.TEST_TABLE1
  93. + " SET field1='" + newValue + "'";
  94. try {
  95. int num = statement.executeUpdate(updateQuery);
  96. assertEquals("Not all records in the database were updated",
  97. numberOfRecords, num);
  98. String selectQuery = "SELECT field1 FROM "
  99. + DatabaseCreator.TEST_TABLE1;
  100. ResultSet result = statement.executeQuery(selectQuery);
  101. while (result.next()) {
  102. assertEquals("The field field1 was not updated", newValue,
  103. result.getString("field1"));
  104. }
  105. result.close();
  106. } catch (SQLException e) {
  107. fail("Unexpected exception" + e.getMessage());
  108. }
  109. }
  110. /**
  111. * UpdateFunctionalityTest#testUpdate2(). Updates values in one
  112. * column in the table using where condition in update command
  113. */
  114. public void testUpdate2() {
  115. String newValue = "newValue";
  116. String updateQuery = "UPDATE " + DatabaseCreator.TEST_TABLE1
  117. + " SET field1='" + newValue + "' WHERE (id > 2) and (id < 10)";
  118. try {
  119. int num = statement.executeUpdate(updateQuery);
  120. int expectedUpdated = 7;
  121. assertEquals("Not all records in the database were updated",
  122. expectedUpdated, num);
  123. String selectQuery = "SELECT * FROM " + DatabaseCreator.TEST_TABLE1;
  124. ResultSet result = statement.executeQuery(selectQuery);
  125. while (result.next()) {
  126. int id = result.getInt("id");
  127. String field1 = result.getString("field1");
  128. if ((id > 2) && (id < 10)) {
  129. assertEquals("The field field1 was not updated", newValue,
  130. field1);
  131. } else {
  132. assertEquals("The field field1 was not updated",
  133. DatabaseCreator.defaultString + id, field1);
  134. }
  135. }
  136. result.close();
  137. } catch (SQLException e) {
  138. fail("Unexpected exception" + e.getMessage());
  139. }
  140. }
  141. /**
  142. * UpdateFunctionalityTest#testUpdate3(). Updates values in a several
  143. * columns in the table
  144. */
  145. public void testUpdate3() {
  146. int newValue1 = -1;
  147. int newValue2 = -2;
  148. String updateQuery = "UPDATE " + DatabaseCreator.TEST_TABLE1
  149. + " SET field2=" + newValue1 + ", field3=" + newValue2;
  150. try {
  151. int num = statement.executeUpdate(updateQuery);
  152. assertEquals("Not all records in the database were updated",
  153. numberOfRecords, num);
  154. String selectQuery = "SELECT field2, field3 FROM "
  155. + DatabaseCreator.TEST_TABLE1;
  156. ResultSet result = statement.executeQuery(selectQuery);
  157. while (result.next()) {
  158. // TODO getBigDecimal is not supported
  159. // assertEquals("The field field2 was not updated", newValue1,
  160. // result.getBigDecimal("field2").intValue());
  161. // assertEquals("The field field3 was not updated", newValue2,
  162. // result.getBigDecimal("field3").intValue());
  163. }
  164. result.close();
  165. } catch (SQLException e) {
  166. fail("Unexpected exception" + e.getMessage());
  167. }
  168. }
  169. /**
  170. * UpdateFunctionalityTest#testUpdate4(). Updates values in a several
  171. * columns in the table using where condition in update command
  172. */
  173. public void testUpdate4() {
  174. int newValue1 = -1;
  175. int newValue2 = -2;
  176. String updateQuery = "UPDATE " + DatabaseCreator.TEST_TABLE1
  177. + " SET field2=" + newValue1 + ", field3=" + newValue2
  178. + " WHERE id > 10";
  179. try {
  180. int num = statement.executeUpdate(updateQuery);
  181. int expectedUpdated = 9;
  182. assertEquals("Not all records in the database were updated",
  183. expectedUpdated, num);
  184. String selectQuery = "SELECT id, field2, field3 FROM "
  185. + DatabaseCreator.TEST_TABLE1;
  186. ResultSet result = statement.executeQuery(selectQuery);
  187. while (result.next()) {
  188. int id = result.getInt("id");
  189. // TODO getBigDecimal is not supported
  190. // int value2 = result.getBigDecimal("field2").intValue();
  191. // int value3 = result.getBigDecimal("field3").intValue();
  192. // if (id > expectedUpdated + 1) {
  193. // assertEquals("The field field2 was not updated", newValue1,
  194. // value2);
  195. // assertEquals("The field field3 was not updated", newValue2,
  196. // value3);
  197. // } else {
  198. // assertEquals("The field field2 was not updated", id, value2);
  199. // assertEquals("The field field3 was not updated", id, value3);
  200. // }
  201. }
  202. result.close();
  203. } catch (SQLException e) {
  204. fail("Unexpected exception" + e.getMessage());
  205. }
  206. }
  207. /**
  208. * UpdateFunctionalityTest#testUpdate5(). Updates values in one
  209. * columns in the table using condition
  210. */
  211. public void testUpdate5() {
  212. int factor = 3;
  213. String updateQuery = "UPDATE " + DatabaseCreator.TEST_TABLE1
  214. + " SET field2=field2 *" + factor;
  215. try {
  216. String selectQuery = "SELECT field2 FROM "
  217. + DatabaseCreator.TEST_TABLE1;
  218. ResultSet result = statement.executeQuery(selectQuery);
  219. HashSet<BigDecimal> values = new HashSet<BigDecimal>();
  220. // TODO getBigDecimal is not supported
  221. // while (result.next()) {
  222. // values.add(BigDecimal.valueOf(result.getBigDecimal("field2")
  223. // .intValue()
  224. // * factor));
  225. // }
  226. int num = statement.executeUpdate(updateQuery);
  227. assertEquals("Not all records in the database were updated",
  228. numberOfRecords, num);
  229. result = statement.executeQuery(selectQuery);
  230. // TODO getBigDecimal is not supported
  231. // while (result.next()) {
  232. // BigDecimal value = result.getBigDecimal("field2");
  233. // assertTrue("Wrong value of field2 after update"
  234. // + value.intValue(), values.remove(value));
  235. // }
  236. assertTrue("Not all records were updated", values.isEmpty());
  237. result.close();
  238. } catch (SQLException e) {
  239. fail("Unexpected exception" + e.getMessage());
  240. }
  241. }
  242. /**
  243. * UpdateFunctionalityTest#testUpdate6(). Sets value of field2 to
  244. * default
  245. */
  246. public void testUpdate6() {
  247. String updateQuery = "UPDATE " + DatabaseCreator.TEST_TABLE1
  248. + " SET field2='1'";
  249. try {
  250. int num = statement.executeUpdate(updateQuery);
  251. assertEquals("Not all records in the database were updated",
  252. numberOfRecords, num);
  253. String selectQuery = "SELECT field2 FROM "
  254. + DatabaseCreator.TEST_TABLE1;
  255. ResultSet result = statement.executeQuery(selectQuery);
  256. // TODO getBigDecimal is not supported
  257. // while (result.next()) {
  258. // assertEquals("value of field2 should be default ",
  259. // DatabaseCreator.defaultInt, result.getBigDecimal(
  260. // "field2").intValue());
  261. // }
  262. result.close();
  263. } catch (SQLException e) {
  264. fail("Unexpected exception" + e.getMessage());
  265. }
  266. }
  267. /**
  268. * UpdateFunctionalityTest#testUpdate7(). Updates records in the
  269. * table using subquery in update command
  270. */
  271. public void testUpdate7() {
  272. String updateQuery = "UPDATE " + DatabaseCreator.TEST_TABLE1
  273. + " SET field2='1' WHERE id < ( SELECT COUNT(*) FROM "
  274. + DatabaseCreator.TEST_TABLE2 + " WHERE finteger > 15)";
  275. try {
  276. int num = statement.executeUpdate(updateQuery);
  277. int expectedUpdated = 4;
  278. assertEquals("Not all records in the database were updated",
  279. expectedUpdated, num);
  280. String selectQuery = "SELECT id, field2 FROM "
  281. + DatabaseCreator.TEST_TABLE1;
  282. ResultSet result = statement.executeQuery(selectQuery);
  283. while (result.next()) {
  284. // TODO getBigDecimal is not supported
  285. // int value = result.getBigDecimal("field2").intValue();
  286. // int id = result.getInt("id");
  287. // if (id < expectedUpdated) {
  288. // assertEquals("value of field2 should be default ",
  289. // DatabaseCreator.defaultInt, value);
  290. // } else {
  291. // assertEquals("wrong value of field2", id, value);
  292. // }
  293. }
  294. result.close();
  295. } catch (SQLException e) {
  296. fail("Unexpected exception" + e.getMessage());
  297. }
  298. }
  299. /**
  300. * UpdateFunctionalityTest#testUpdate8(). Sets value of field2 to
  301. * NULL
  302. */
  303. public void testUpdate8() {
  304. String updateQuery = "UPDATE " + DatabaseCreator.TEST_TABLE1
  305. + " SET field2=NULL";
  306. try {
  307. int num = statement.executeUpdate(updateQuery);
  308. assertEquals("Not all records in the database were updated",
  309. numberOfRecords, num);
  310. String selectQuery = "SELECT field2 FROM "
  311. + DatabaseCreator.TEST_TABLE1;
  312. ResultSet result = statement.executeQuery(selectQuery);
  313. while (result.next()) {
  314. assertNull("value of field2 should be NULL", result
  315. .getObject("field2"));
  316. }
  317. result.close();
  318. } catch (SQLException e) {
  319. fail("Unexpected exception" + e.getMessage());
  320. }
  321. }
  322. }