KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > cursor


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.cursor
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.lang;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.ResultSetMetaData JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.sql.SQLWarning JavaDoc;
32
33 import org.apache.derby.tools.ij;
34 import org.apache.derby.tools.JDBCDisplayUtil;
35
36 /**
37   This tests cursor handling
38
39   Not done in ij since the cursor names may not be stable,
40   and we want to control navigation through the cursor rows.
41
42   This could be more complete, but since this is SQL92 Entry
43   standard, we are assuming that some future purchase of the
44   NIST suite or some equivalent will suffice.
45  */

46
47 public class cursor {
48
49     private static Connection JavaDoc conn;
50     private static boolean passed = false;
51
52     public static void main(String JavaDoc[] args) {
53         System.out.println("Test cursor starting");
54
55         try {
56             // use the ij utility to read the property file and
57
// make the initial connection.
58
ij.getPropertyArg(args);
59             conn = ij.startJBMS();
60
61             conn.setAutoCommit(false);
62
63             setup(true);
64             testCursor();
65             testCursorParam();
66             testgetCursorName();
67             teardown();
68
69             conn.commit();
70             conn.close();
71
72             passed = true;
73
74         } catch (Throwable JavaDoc e) {
75             System.out.println("FAIL: exception thrown:");
76             passed = false;
77             JDBCDisplayUtil.ShowException(System.out,e);
78         }
79
80         if (passed)
81             System.out.println("PASS");
82         System.out.println("Test cursor finished");
83     }
84
85     static void setup(boolean first) throws SQLException JavaDoc {
86         Statement JavaDoc stmt = conn.createStatement();
87
88         if (first) {
89             verifyCount(
90                 stmt.executeUpdate("create table t (i int, c char(50))"),
91                 0);
92
93             verifyCount(
94                 stmt.executeUpdate("create table s (i int, c char(50))"),
95                 0);
96         } else {
97             verifyBoolean(
98                 stmt.execute("delete from t"),
99                 false);
100         }
101
102         verifyCount(
103             stmt.executeUpdate("insert into t values (1956, 'hello world')"),
104             1);
105
106         verifyCount(
107             stmt.executeUpdate("insert into t values (456, 'hi yourself')"),
108             1);
109
110         verifyCount(
111             stmt.executeUpdate("insert into t values (180, 'rubber ducky')"),
112             1);
113
114         verifyCount(
115             stmt.executeUpdate("insert into t values (3, 'you are the one')"),
116             1);
117
118         stmt.close();
119
120         System.out.println("PASS: setup complete");
121     }
122
123
124     static void teardown() throws SQLException JavaDoc {
125         Statement JavaDoc stmt = conn.createStatement();
126
127         verifyCount(
128             stmt.executeUpdate("drop table t"),
129             0);
130
131         verifyCount(
132             stmt.executeUpdate("drop table s"),
133             0);
134
135         stmt.close();
136
137         System.out.println("PASS: teardown complete");
138     }
139
140     static void verifyCount(int count, int expect) throws SQLException JavaDoc {
141         if (count!=expect) {
142             System.out.println("FAIL: Expected "+expect+" got "+count+" rows");
143             throw new SQLException JavaDoc("Wrong number of rows returned");
144         }
145         else
146             System.out.println("PASS: expected and got "+count+
147                                (count == 1? " row":" rows"));
148     }
149
150     static void verifyBoolean(boolean got, boolean expect) throws SQLException JavaDoc {
151         if (got!=expect) {
152             System.out.println("FAIL: Expected "+expect+" got "+got);
153             throw new SQLException JavaDoc("Wrong boolean returned");
154         }
155         else
156             System.out.println("PASS: expected and got "+got);
157     }
158
159     static int countRows(String JavaDoc query) throws SQLException JavaDoc {
160         Statement JavaDoc select = conn.createStatement();
161         ResultSet JavaDoc counter = select.executeQuery(query);
162         int count = 0;
163
164         while (counter.next()) {
165             count++;
166             System.out.println("Row: "+counter.getInt(1)+","+counter.getString(2));
167         }
168         select.close();
169
170         return count;
171     }
172
173     static void nextRow(ResultSet JavaDoc r) throws SQLException JavaDoc {
174         verifyBoolean(r.next(), true);
175         System.out.println("Row: "+r.getInt(1)+","+r.getString(2));
176     }
177
178     static boolean ifRow(ResultSet JavaDoc r) throws SQLException JavaDoc {
179         boolean b = r.next();
180
181         if (b)
182             System.out.println("Row: "+r.getInt(1)+","+r.getString(2));
183
184         return b;
185     }
186
187     static void testCursor() throws SQLException JavaDoc {
188         PreparedStatement JavaDoc select, delete;
189         Statement JavaDoc select2, delete2;
190         ResultSet JavaDoc cursor;
191         boolean caught;
192
193         // because there is no order by (nor can there be)
194
// the fact that this test prints out rows may someday
195
// be a problem. When that day comes, the row printing
196
// can (should) be removed from this test.
197

198         select = conn.prepareStatement("select i, c from t for update");
199         cursor = select.executeQuery(); // cursor is now open
200

201         // TEST: fetch of a row works
202
nextRow(cursor);
203
204         // TEST: close and then fetch gets error on fetch.
205
cursor.close();
206
207         // bang away on the nexts for a little while,
208
// see what the error quiets out to...
209
for (int i=0;i<5;i++) {
210             caught = false;
211             try {
212                 ifRow(cursor); // no current row / closed
213
} catch (SQLException JavaDoc se) {
214                 JDBCDisplayUtil.ShowSQLException(System.out,se);
215                 caught = true;
216                 System.out.println("PASS: Attempt to get next on closed cursor caught");
217             }
218             if (! caught)
219                 System.out.println("FAIL: No error from next on closed cursor");
220         }
221
222         // restart the query for another test.
223
cursor = select.executeQuery();
224
225         // TEST: next past the end of the table.
226
while (ifRow(cursor)); // keep going to the last row
227
caught = false;
228         try {
229             boolean b = ifRow(cursor); // no current row / closed (past the last row)
230
if (!b) {
231                 System.out.println("No current row");
232                 caught = true;
233             }
234         } catch (SQLException JavaDoc se) {
235             JDBCDisplayUtil.ShowSQLException(System.out,se);
236             caught = true;
237             System.out.println("PASS: Attempt to get next after end of cursor caught");
238         }
239         if (! caught)
240             System.out.println("FAIL: No error from next past end of cursor");
241
242         System.out.println("PASS: cursor test complete");
243
244         cursor.close();
245     }
246
247     static void testCursorParam() throws SQLException JavaDoc {
248         PreparedStatement JavaDoc select, delete;
249         Statement JavaDoc select2, delete2;
250         ResultSet JavaDoc cursor;
251         boolean caught;
252
253         // because there is no order by (nor can there be)
254
// the fact that this test prints out rows may someday
255
// be a problem. When that day comes, the row printing
256
// can (should) be removed from this test.
257

258         select = conn.prepareStatement("select i, c from t where ?=1 for update");
259         select.setInt(1,1);
260         cursor = select.executeQuery(); // cursor is now open
261

262         // TEST: fetch of a row works
263
nextRow(cursor);
264
265         // TEST: close and then fetch gets error on fetch.
266
cursor.close();
267
268         // bang away on the nexts for a little while,
269
// see what the error quiets out to...
270
for (int i=0;i<5;i++) {
271             caught = false;
272             try {
273                 ifRow(cursor); // no current row / closed
274
} catch (SQLException JavaDoc se) {
275                 JDBCDisplayUtil.ShowSQLException(System.out,se);
276                 caught = true;
277                 System.out.println("PASS: Attempt to get next on closed cursor caught");
278             }
279             if (! caught)
280                 System.out.println("FAIL: No error from next on closed cursor");
281         }
282
283         // restart the query for another test.
284
select.setBoolean(1,false);
285         select.setCursorName("ForCoverageSake");
286         cursor = select.executeQuery();
287
288         if (cursor.getCursorName().equals("ForCoverageSake")) {
289             System.out.println("PASS: cursor name set");
290         }
291         else
292         {
293             System.out.println("FAIL: cursor name not set, is still: "+cursor.getCursorName());
294         }
295
296         // TEST: next past the end of the table -- expect no rows at all.
297
caught = false;
298         try {
299             boolean b = ifRow(cursor); // no current row / closed (past the last row)
300
if (!b) {
301                 System.out.println("No current row");
302                 caught = true;
303             }
304         } catch (SQLException JavaDoc se) {
305             JDBCDisplayUtil.ShowSQLException(System.out,se);
306             caught = true;
307             System.out.println("PASS: Attempt to get next after end of cursor caught");
308         }
309         if (! caught)
310             System.out.println("FAIL: No error from next past end of cursor");
311
312         System.out.println("PASS: cursor test complete");
313
314         cursor.close();
315     }
316     static void testgetCursorName() throws SQLException JavaDoc {
317             // test cursor name
318
testCursorName("select * from t", (String JavaDoc)null);
319             testCursorName("select * from t for update", (String JavaDoc)null);
320             testCursorName("select * from t for update of i", (String JavaDoc)null);
321             testCursorName("select * from t", "myselect");
322             testCursorName("select * from t for update", "myselect");
323             testCursorName("select * from t for update of i", "myselect");
324     }
325     static private void testCursorName(String JavaDoc statement, String JavaDoc cursorname) throws SQLException JavaDoc{
326         System.out.println("Test cursor name for " + statement +
327             " Cursor name = " + cursorname);
328         Statement JavaDoc s = conn.createStatement();
329         if (cursorname != null)
330             s.setCursorName(cursorname);
331         ResultSet JavaDoc rs = s.executeQuery(statement);
332         if (rs != null)
333         {
334             String JavaDoc cursorName = rs.getCursorName();
335             System.out.println("cursor name = " + cursorName);
336         }
337         rs.close();
338         s.close();
339
340     }
341
342 }
343
Popular Tags