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