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

https://bitbucket.org/aways/android_libcore · Java · 285 lines · 196 code · 25 blank · 64 comment · 13 complexity · af1c38496d5c6618d9213251e2d42e06 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 dalvik.annotation.KnownFailure;
  18. import java.sql.Connection;
  19. import java.sql.DatabaseMetaData;
  20. import java.sql.PreparedStatement;
  21. import java.sql.ResultSet;
  22. import java.sql.SQLException;
  23. import java.sql.Statement;
  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 UpdateFunctionalityTest2 extends TestCase {
  31. private static Connection conn = null;
  32. private static Statement statement = null;
  33. public void setUp() throws Exception {
  34. super.setUp();
  35. Support_SQL.loadDriver();
  36. try {
  37. conn = Support_SQL.getConnection();
  38. statement = conn.createStatement();
  39. createTestTables();
  40. } catch (SQLException e) {
  41. fail("Unexpected SQLException " + e.toString());
  42. }
  43. DatabaseCreator.fillParentTable(conn);
  44. DatabaseCreator.fillSimpleTable3(conn);
  45. DatabaseCreator.fillSimpleTable1(conn);
  46. }
  47. public void tearDown() throws Exception {
  48. deleteTestTables();
  49. statement.close();
  50. conn.close();
  51. super.tearDown();
  52. }
  53. private 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.PARENT_TABLE)) {
  60. statement
  61. .execute(DatabaseCreator.DROP_TABLE_PARENT);
  62. } else if (tableName
  63. .equals(DatabaseCreator.FKCASCADE_TABLE)) {
  64. statement
  65. .execute(DatabaseCreator.DROP_TABLE_FKCASCADE);
  66. } else if (tableName
  67. .equals(DatabaseCreator.FKSTRICT_TABLE)) {
  68. statement
  69. .execute(DatabaseCreator.DROP_TABLE_FKSTRICT);
  70. } else if (tableName
  71. .equals(DatabaseCreator.SIMPLE_TABLE1)) {
  72. statement
  73. .execute(DatabaseCreator.DROP_TABLE_SIMPLE1);
  74. } else if (tableName
  75. .equals(DatabaseCreator.SIMPLE_TABLE3)) {
  76. statement
  77. .execute(DatabaseCreator.DROP_TABLE_SIMPLE3);
  78. } else if (tableName
  79. .equals(DatabaseCreator.TEST_TABLE5)) {
  80. statement.execute(DatabaseCreator.DROP_TABLE5);
  81. }
  82. }
  83. userTab.close();
  84. statement.execute(DatabaseCreator.CREATE_TABLE_PARENT);
  85. statement.execute(DatabaseCreator.CREATE_TABLE_FKSTRICT);
  86. statement.execute(DatabaseCreator.CREATE_TABLE_FKCASCADE);
  87. statement.execute(DatabaseCreator.CREATE_TABLE_SIMPLE3);
  88. statement.execute(DatabaseCreator.CREATE_TABLE_SIMPLE1);
  89. statement.execute(DatabaseCreator.CREATE_TABLE5);
  90. } catch (SQLException e) {
  91. fail("Unexpected SQLException " + e.toString());
  92. }
  93. }
  94. private void deleteTestTables() {
  95. try {
  96. statement.execute(DatabaseCreator.DROP_TABLE_FKCASCADE);
  97. statement.execute(DatabaseCreator.DROP_TABLE_FKSTRICT);
  98. statement.execute(DatabaseCreator.DROP_TABLE_PARENT);
  99. statement.execute(DatabaseCreator.DROP_TABLE_SIMPLE3);
  100. statement.execute(DatabaseCreator.DROP_TABLE_SIMPLE1);
  101. statement.execute(DatabaseCreator.DROP_TABLE5);
  102. } catch (SQLException e) {
  103. fail("Unexpected SQLException " + e.toString());
  104. }
  105. }
  106. /**
  107. * UpdateFunctionalityTest2#testUpdate1(). Updates row with no
  108. * referencing ones and RESTRICT action
  109. */
  110. public void testUpdate1() throws SQLException {
  111. DatabaseCreator.fillFKStrictTable(conn);
  112. statement.execute("UPDATE " + DatabaseCreator.PARENT_TABLE
  113. + " SET id = 4 WHERE id = 3");
  114. }
  115. /**
  116. * UpdateFunctionalityTest2#testUpdate2(). Attempts to update row
  117. * with referencing ones and RESTRICT action - expecting SQLException
  118. *
  119. * TODO not supported
  120. */
  121. @KnownFailure("not supported")
  122. public void testUpdate2() throws SQLException {
  123. DatabaseCreator.fillFKStrictTable(conn);
  124. try {
  125. statement.executeUpdate("UPDATE " + DatabaseCreator.PARENT_TABLE
  126. + " SET id = 5 WHERE id = 1;");
  127. fail("expecting SQLException");
  128. } catch (SQLException ex) {
  129. // expected
  130. }
  131. }
  132. /**
  133. * UpdateFunctionalityTest2#testUpdate3(). Deletes all referencing
  134. * rows and then updates referenced one
  135. */
  136. public void testUpdate3() throws SQLException {
  137. DatabaseCreator.fillFKStrictTable(conn);
  138. statement.execute("DELETE FROM " + DatabaseCreator.FKSTRICT_TABLE
  139. + " WHERE name_id = 1;");
  140. statement.execute("UPDATE " + DatabaseCreator.PARENT_TABLE
  141. + " SET id = 5 WHERE id = 1;");
  142. }
  143. /**
  144. * UpdateFunctionalityTest2#testUpdate4(). Attempts to set incorrect
  145. * foreign key value - expecting SQLException
  146. *
  147. * TODO foreign key functionality is not supported
  148. */
  149. @KnownFailure("not supported")
  150. public void testUpdate4() throws SQLException {
  151. DatabaseCreator.fillFKStrictTable(conn);
  152. try {
  153. statement.executeUpdate("UPDATE " + DatabaseCreator.FKSTRICT_TABLE
  154. + " SET name_id = 6 WHERE name_id = 2");
  155. fail("expecting SQLException");
  156. } catch (SQLException ex) {
  157. // expected
  158. }
  159. }
  160. /**
  161. * UpdateFunctionalityTest2#testUpdate5(). Updates row with
  162. * referencing ones and CASCADE action - expecting that all
  163. * referencing rows will also be updated
  164. */
  165. public void testUpdate5() throws SQLException {
  166. DatabaseCreator.fillFKCascadeTable(conn);
  167. statement.executeUpdate("UPDATE " + DatabaseCreator.PARENT_TABLE
  168. + " SET id = 5 WHERE id = 1;");
  169. ResultSet r = statement.executeQuery("SELECT COUNT(*) " + "FROM "
  170. + DatabaseCreator.FKCASCADE_TABLE + " WHERE name_id = 1;");
  171. r.next();
  172. assertEquals("Should be 2 rows", 2, r.getInt(1));
  173. r = statement.executeQuery("SELECT COUNT(*) " + "FROM "
  174. + DatabaseCreator.FKCASCADE_TABLE + " WHERE name_id = 5;");
  175. r.next();
  176. assertEquals("Should be 0 rows", 0, r.getInt(1));
  177. r.close();
  178. }
  179. /**
  180. * UpdateFunctionalityTest2#testUpdate6(). Attempts to set incorrect
  181. * foreign key value to row with CASCADE action - expecting
  182. * SQLException
  183. *
  184. * TODO Foreign key functionality is not supported
  185. */
  186. @KnownFailure("not supported")
  187. public void testUpdate6() throws SQLException {
  188. DatabaseCreator.fillFKCascadeTable(conn);
  189. try {
  190. statement.executeUpdate("UPDATE " + DatabaseCreator.FKCASCADE_TABLE
  191. + " SET name_id = 6 WHERE name_id = 2");
  192. fail("expecting SQLException");
  193. } catch (SQLException ex) {
  194. // expected
  195. }
  196. }
  197. /**
  198. * UpdateFunctionalityTest2#testUpdate7(). Updates table using
  199. * subquery in WHERE clause
  200. *
  201. * TODO Foreign key functionality is not supported
  202. */
  203. @KnownFailure("not supported")
  204. public void testUpdate7() throws SQLException {
  205. DatabaseCreator.fillFKStrictTable(conn);
  206. statement.executeUpdate("UPDATE " + DatabaseCreator.FKSTRICT_TABLE
  207. + " SET value = 'updated' WHERE name_id = ANY (SELECT id FROM "
  208. + DatabaseCreator.PARENT_TABLE + " WHERE id > 1)");
  209. ResultSet r = statement.executeQuery("SELECT COUNT(*) FROM "
  210. + DatabaseCreator.FKSTRICT_TABLE + " WHERE value = 'updated';");
  211. r.next();
  212. assertEquals("Should be 1 row", 1, r.getInt(1));
  213. r.close();
  214. }
  215. /**
  216. * UpdateFunctionalityTest2#testUpdate8(). Updates table using scalar
  217. * subquery as new field value
  218. */
  219. public void testUpdate8() throws SQLException {
  220. statement.executeUpdate("UPDATE " + DatabaseCreator.SIMPLE_TABLE3
  221. + " SET speed = (SELECT MAX(speed) FROM "
  222. + DatabaseCreator.SIMPLE_TABLE1
  223. + ") WHERE id = (SELECT id FROM "
  224. + DatabaseCreator.SIMPLE_TABLE1
  225. + " WHERE speed = (SELECT MAX(speed) FROM "
  226. + DatabaseCreator.SIMPLE_TABLE1 + "))");
  227. ResultSet r = statement.executeQuery("SELECT id FROM "
  228. + DatabaseCreator.SIMPLE_TABLE3
  229. + " WHERE speed = (SELECT MAX(speed) FROM "
  230. + DatabaseCreator.SIMPLE_TABLE1 + ");");
  231. r.next();
  232. assertEquals("Incorrect id updated", 1, r.getInt(1));
  233. r.close();
  234. }
  235. /**
  236. * UpdateFunctionalityTest2#testUpdate9(). Updates table using
  237. * PreparedStatement
  238. */
  239. public void testUpdate9() throws SQLException {
  240. DatabaseCreator.fillTestTable5(conn);
  241. PreparedStatement stat = conn.prepareStatement("UPDATE "
  242. + DatabaseCreator.TEST_TABLE5
  243. + " SET testValue = ? WHERE testID = ?");
  244. stat.setString(1, "1");
  245. stat.setInt(2, 1);
  246. stat.execute();
  247. stat.setString(1, "2");
  248. stat.setInt(2, 2);
  249. stat.execute();
  250. ResultSet r = statement.executeQuery("SELECT testId, testValue FROM "
  251. + DatabaseCreator.TEST_TABLE5
  252. + " WHERE testID < 3 ORDER BY testID");
  253. while (r.next()) {
  254. assertEquals("Incorrect value was returned", new Integer(r
  255. .getInt(1)).toString(), r.getString(2));
  256. }
  257. r.close();
  258. stat.close();
  259. }
  260. }