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

https://github.com/MIPS/libcore · Java · 249 lines · 174 code · 19 blank · 56 comment · 8 complexity · 4a396b873261207edf5228c0560282d6 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 dalvik.annotation.TestTargetClass;
  19. import dalvik.annotation.TestTargets;
  20. import dalvik.annotation.TestLevel;
  21. import dalvik.annotation.TestTargetNew;
  22. import java.sql.Connection;
  23. import java.sql.DatabaseMetaData;
  24. import java.sql.PreparedStatement;
  25. import java.sql.ResultSet;
  26. import java.sql.SQLException;
  27. import java.sql.Statement;
  28. import tests.support.DatabaseCreator;
  29. import tests.support.Support_SQL;
  30. import junit.extensions.TestSetup;
  31. import junit.framework.Test;
  32. import junit.framework.TestCase;
  33. import junit.framework.TestSuite;
  34. @TestTargetClass(Statement.class)
  35. public class DeleteFunctionalityTest extends TestCase {
  36. private static Connection conn = null;
  37. private static Statement statement = null;
  38. public void setUp() throws Exception {
  39. super.setUp();
  40. Support_SQL.loadDriver();
  41. conn = Support_SQL.getConnection();
  42. statement = conn.createStatement();
  43. createTestTables();
  44. DatabaseCreator.fillParentTable(conn);
  45. }
  46. public void tearDown() throws Exception {
  47. deleteTestTables();
  48. statement.close();
  49. conn.close();
  50. super.tearDown();
  51. }
  52. public void createTestTables() {
  53. try {
  54. DatabaseMetaData meta = conn.getMetaData();
  55. ResultSet userTab = meta.getTables(null, null, null, null);
  56. while (userTab.next()) {
  57. String tableName = userTab.getString("TABLE_NAME");
  58. if (tableName.equals(DatabaseCreator.PARENT_TABLE)) {
  59. statement
  60. .execute(DatabaseCreator.DROP_TABLE_PARENT);
  61. } else if (tableName
  62. .equals(DatabaseCreator.FKCASCADE_TABLE)) {
  63. statement
  64. .execute(DatabaseCreator.DROP_TABLE_FKCASCADE);
  65. } else if (tableName
  66. .equals(DatabaseCreator.FKSTRICT_TABLE)) {
  67. statement
  68. .execute(DatabaseCreator.DROP_TABLE_FKSTRICT);
  69. } else if (tableName
  70. .equals(DatabaseCreator.TEST_TABLE5)) {
  71. statement.execute(DatabaseCreator.DROP_TABLE5);
  72. }
  73. }
  74. userTab.close();
  75. statement.execute(DatabaseCreator.CREATE_TABLE_PARENT);
  76. statement.execute(DatabaseCreator.CREATE_TABLE_FKSTRICT);
  77. statement.execute(DatabaseCreator.CREATE_TABLE_FKCASCADE);
  78. statement.execute(DatabaseCreator.CREATE_TABLE5);
  79. } catch (SQLException e) {
  80. fail("Unexpected SQLException " + e.toString());
  81. }
  82. }
  83. public void deleteTestTables() {
  84. try {
  85. statement.execute(DatabaseCreator.DROP_TABLE_FKCASCADE);
  86. statement.execute(DatabaseCreator.DROP_TABLE_FKSTRICT);
  87. statement.execute(DatabaseCreator.DROP_TABLE_PARENT);
  88. statement.execute(DatabaseCreator.DROP_TABLE5);
  89. } catch (SQLException e) {
  90. fail("Unexpected SQLException " + e.toString());
  91. }
  92. }
  93. /**
  94. * @tests DeleteFunctionalityTest#testDelete1(). Deletes row with no
  95. * referencing ones and RESTRICT action
  96. */
  97. @TestTargetNew(
  98. level = TestLevel.PARTIAL_COMPLETE,
  99. notes = "Functionality test: Deletes row with no referencing ones and RESTRICT action",
  100. method = "execute",
  101. args = {java.lang.String.class}
  102. )
  103. public void testDelete1() throws SQLException {
  104. DatabaseCreator.fillFKStrictTable(conn);
  105. statement.execute("DELETE FROM " + DatabaseCreator.PARENT_TABLE
  106. + " WHERE id = 3;");
  107. }
  108. /**
  109. * @tests DeleteFunctionalityTest#testDelete2(). Attempts to delete row with
  110. * referencing ones and RESTRICT action - expecting SQLException
  111. * TODO foreign key functionality is not supported
  112. */
  113. /* public void testDelete2() throws SQLException {
  114. DatabaseCreator.fillFKStrictTable(conn);
  115. try {
  116. statement.execute("DELETE FROM " + DatabaseCreator.PARENT_TABLE
  117. + " WHERE id = 1;");
  118. fail("expecting SQLException");
  119. } catch (SQLException ex) {
  120. // expected
  121. }
  122. }
  123. */
  124. /**
  125. * @tests DeleteFunctionalityTest#testDelete3(). Deletes all referencing
  126. * rows and then deletes referenced one
  127. */
  128. @TestTargetNew(
  129. level = TestLevel.PARTIAL_COMPLETE,
  130. notes = "Functionality test: Deletes all referencing rows and then deletes referenced one",
  131. method = "execute",
  132. args = {java.lang.String.class}
  133. )
  134. public void testDelete3() throws SQLException {
  135. statement.execute("DELETE FROM " + DatabaseCreator.FKSTRICT_TABLE
  136. + " WHERE name_id = 1;");
  137. statement.execute("DELETE FROM " + DatabaseCreator.FKSTRICT_TABLE
  138. + " WHERE id = 1;");
  139. }
  140. /**
  141. * @tests DeleteFunctionalityTest#testDelete4(). Deletes row with no
  142. * referencing ones and CASCADE action
  143. */
  144. @TestTargetNew(
  145. level = TestLevel.PARTIAL_COMPLETE,
  146. notes = "Functionality test: Deletes row with no referencing ones and CASCADE action",
  147. method = "execute",
  148. args = {java.lang.String.class}
  149. )
  150. public void testDelete4() throws SQLException {
  151. DatabaseCreator.fillFKCascadeTable(conn);
  152. statement.execute("DELETE FROM " + DatabaseCreator.PARENT_TABLE
  153. + " WHERE id = 3;");
  154. }
  155. /**
  156. * @tests DeleteFunctionalityTest#testDelete5(). Attempts to delete row with
  157. * referencing ones and CASCADE action - expecting all referencing
  158. * rows will also be deleted
  159. */
  160. @TestTargets({
  161. @TestTargetNew(
  162. level = TestLevel.PARTIAL_COMPLETE,
  163. notes = "Functionality test: Attempts to delete row with referencing ones and CASCADE action - expecting all referencing rows will also be deleted",
  164. method = "execute",
  165. args = {java.lang.String.class}
  166. ),
  167. @TestTargetNew(
  168. level = TestLevel.PARTIAL_COMPLETE,
  169. notes = "Functionality test: Attempts to delete row with referencing ones and CASCADE action - expecting all referencing rows will also be deleted",
  170. method = "executeQuery",
  171. args = {java.lang.String.class}
  172. )
  173. })
  174. public void testDelete5() throws SQLException {
  175. statement.execute("DELETE FROM " + DatabaseCreator.PARENT_TABLE
  176. + " WHERE id = 1;");
  177. ResultSet r = statement.executeQuery("SELECT COUNT(*) FROM "
  178. + DatabaseCreator.FKCASCADE_TABLE + " WHERE name_id = 1;");
  179. r.next();
  180. assertEquals("Should be no rows", 0, r.getInt(1));
  181. r.close();
  182. }
  183. /**
  184. * @tests DeleteFunctionalityTest#testDelete6().
  185. * TODO Foreign key functionality is not supported
  186. */
  187. @TestTargetNew(
  188. level = TestLevel.PARTIAL_COMPLETE,
  189. notes = "Deletes rows using subquery in WHERE clause with foreign keys. Foreign keys not supported.",
  190. method = "execute",
  191. args = {String.class}
  192. )
  193. @KnownFailure("not supported")
  194. public void testDelete6() throws SQLException {
  195. DatabaseCreator.fillFKStrictTable(conn);
  196. statement.execute("DELETE FROM " + DatabaseCreator.FKSTRICT_TABLE
  197. + " WHERE name_id = ANY (SELECT id FROM "
  198. + DatabaseCreator.PARENT_TABLE + " WHERE id > 1)");
  199. ResultSet r = statement.executeQuery("SELECT COUNT(*) FROM "
  200. + DatabaseCreator.FKSTRICT_TABLE + " WHERE name_id = 1;");
  201. r.next();
  202. assertEquals("Should be 2 rows", 2, r.getInt(1));
  203. r.close();
  204. }
  205. /**
  206. * @tests DeleteFunctionalityTest#testDelete7(). Deletes rows using
  207. * PreparedStatement
  208. */
  209. @TestTargetNew(
  210. level = TestLevel.PARTIAL_COMPLETE,
  211. notes = "Functionality test: Deletes rows using PreparedStatement",
  212. method = "executeQuery",
  213. args = {java.lang.String.class}
  214. )
  215. public void testDelete7() throws SQLException {
  216. DatabaseCreator.fillTestTable5(conn);
  217. PreparedStatement stat = conn.prepareStatement("DELETE FROM "
  218. + DatabaseCreator.TEST_TABLE5 + " WHERE testID = ?");
  219. stat.setInt(1, 1);
  220. stat.execute();
  221. stat.setInt(1, 2);
  222. stat.execute();
  223. ResultSet r = statement.executeQuery("SELECT COUNT(*) FROM "
  224. + DatabaseCreator.TEST_TABLE5 + " WHERE testID < 3 ");
  225. r.next();
  226. assertEquals(0, r.getInt(1));
  227. r.close();
  228. stat.close();
  229. }
  230. }