• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package tests.sql;
18 
19 import dalvik.annotation.KnownFailure;
20 import dalvik.annotation.TestTargetClass;
21 import dalvik.annotation.TestTargets;
22 import dalvik.annotation.TestLevel;
23 import dalvik.annotation.TestTargetNew;
24 
25 import java.sql.BatchUpdateException;
26 import java.sql.PreparedStatement;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.sql.SQLFeatureNotSupportedException;
30 import java.sql.SQLWarning;
31 import java.sql.Statement;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.Vector;
35 import java.util.logging.Logger;
36 
37 @TestTargetClass(Statement.class)
38 public class StatementTest extends SQLTest {
39 
40     /**
41      * @test java.sql.Statement#addBatch(String)
42      */
43     @TestTargetNew(
44         level = TestLevel.COMPLETE,
45         notes = "",
46         method = "addBatch",
47         args = {java.lang.String.class}
48     )
testAddBatch()49     public void testAddBatch() throws SQLException {
50 
51         Statement st = null;
52         try {
53             st = conn.createStatement();
54             st.addBatch("INSERT INTO zoo VALUES (3,'Tuzik','dog')");
55             st.addBatch("INSERT INTO zoo VALUES (4,'Mashka','cat')");
56 
57             int[] updateCounts = st.executeBatch();
58             assertEquals(2, updateCounts.length);
59             assertEquals(1, updateCounts[0]);
60             assertEquals(1, updateCounts[1]);
61 
62         } catch (SQLException e) {
63             fail("SQLException is thrown");
64         } finally {
65             try {
66                 st.close();
67             } catch (SQLException ee) {
68             }
69         }
70 
71         try {
72             st = conn.createStatement();
73             st.addBatch("");
74             st.executeBatch();
75             fail("SQLException is not thrown");
76         } catch (SQLException e) {
77             // expected
78         } finally {
79             try {
80                 st.close();
81             } catch (SQLException ee) {
82             }
83         }
84 
85         try {
86             st = conn.createStatement();
87             st.addBatch(null);
88             st.executeBatch();
89         } catch (SQLException e) {
90             // expected
91         } finally {
92             try {
93                 st.close();
94             } catch (SQLException ee) {
95             }
96         }
97     }
98 
99     /**
100      * @test java.sql.Statement#clearWarnings()
101      */
102     @TestTargetNew(
103         level = TestLevel.COMPLETE,
104         notes = "",
105         method = "clearWarnings",
106         args = {}
107     )
testClearWarnings()108     public void testClearWarnings() {
109         Statement st = null;
110         try {
111             st = conn.createStatement();
112             st.execute("select animals from zoo");
113         } catch (SQLException e) {
114             // expected
115         } finally {
116             try {
117                 st.close();
118             } catch (SQLException ee) {
119             }
120         }
121         try {
122             st = conn.createStatement();
123             st.clearWarnings();
124             SQLWarning w = st.getWarnings();
125             assertNull(w);
126         } catch (Exception e) {
127             fail("Unexpected Exception: " + e.getMessage());
128         } finally {
129             try {
130                 st.close();
131             } catch (SQLException ee) {
132             }
133         }
134     }
135 
136     /**
137      * @test java.sql.Statement#getWarnings()
138      *
139      * TODO getWarnings is not supported
140      */
141     @TestTargetNew(
142         level = TestLevel.COMPLETE,
143         notes = "not supported. always returns null. ",
144         method = "getWarnings",
145         args = {}
146     )
testGetWarnings()147     public void testGetWarnings() {
148 
149         Statement st = null;
150         int errorCode1 = -1;
151         int errorCode2 = -1;
152 
153         try {
154             st = conn.createStatement();
155             st.execute("select animals from zoooo");
156             fail("SQLException was not thrown");
157         } catch (SQLException e) {
158             // expected
159             errorCode1 = e.getErrorCode();
160         }
161 
162         try {
163             SQLWarning wrs = st.getWarnings();
164             assertNull(wrs);
165         } catch (Exception e) {
166             fail("Change test implementation: get warnings is supported now");
167         }
168         /*
169         Statement st = null;
170         int errorCode1 = -1;
171         int errorCode2 = -1;
172 
173         try {
174             st = conn.createStatement();
175             st.execute("select animals from zoooo");
176         } catch (SQLException e) {
177             // expected
178             errorCode1 = e.getErrorCode();
179         }
180         try {
181             SQLWarning wrs = st.getWarnings();
182             assertNull(wrs);
183         } catch (Exception e) {
184             fail("Unexpected Exception: " + e.getMessage());
185         }
186         try {
187             st.execute("select horse from zoooooo");
188         } catch (SQLException e) {
189             // expected
190             errorCode2 = e.getErrorCode();
191         }
192 
193         try {
194             SQLWarning wrs = st.getWarnings();
195             assertEquals(errorCode1, wrs.getErrorCode());
196             assertNotNull(wrs.getNextWarning());
197             assertEquals(errorCode2, wrs.getErrorCode());
198         } catch (Exception e) {
199             fail("Unexpected Exception: " + e.getMessage());
200         }
201 
202         try {
203             st.close();
204         } catch (SQLException ee) {
205         }
206         */
207 
208     }
209 
210     /**
211      * @test {@link java.sql.Statement#clearBatch()}
212      */
213     @TestTargetNew(
214         level = TestLevel.COMPLETE,
215         notes = "",
216         method = "clearBatch",
217         args = {}
218     )
testClearBatch()219     public void testClearBatch() throws SQLException {
220 
221         Statement st = null;
222 
223         try {
224             st = conn.createStatement();
225             st.addBatch("INSERT INTO zoo VALUES (3,'Tuzik','dog'); ");
226             st.addBatch("INSERT INTO zoo VALUES (4,'Mashka','cat')");
227 
228             st.clearBatch();
229 
230             int[] updateCounts = st.executeBatch();
231 
232             for (int i = 0; i < updateCounts.length; i++) {
233                 assertEquals(0, updateCounts[i]);
234             }
235 
236         } catch (SQLException e) {
237             fail("SQLException is thrown");
238         } finally {
239             try {
240                 st.close();
241             } catch (SQLException ee) {
242             }
243         }
244 
245         try {
246             st = conn.createStatement();
247             st.addBatch("");
248             st.executeBatch();
249             fail("SQLException is not thrown");
250         } catch (SQLException e) {
251             // expected
252         } finally {
253             try {
254                 st.close();
255             } catch (SQLException ee) {
256             }
257         }
258 
259         try {
260             st = conn.createStatement();
261             st.addBatch(null);
262             st.executeBatch();
263         } catch (SQLException e) {
264             // expected
265         } finally {
266             try {
267                 st.close();
268             } catch (SQLException ee) {
269             }
270         }
271     }
272 
273     /**
274      * @test java.sql.Statement#execute(String sql)
275      *
276      * TODO not pass on SQLite and RI.
277      *
278      */
279     @TestTargetNew(
280         level = TestLevel.PARTIAL_COMPLETE,
281         notes = "",
282         method = "execute",
283         args = {java.lang.String.class}
284     )
285     @KnownFailure("Return value wrong for queries below.")
testExecute()286     public void testExecute() throws SQLException {
287 
288         String[] queries = {
289                 "update zoo set name='Masha', family='cat' where id=2;",
290                 "drop table if exists hutch",
291                 "create table hutch (id integer not null, animal_id integer, address char(20), primary key (id));",
292                 "insert into hutch (id, animal_id, address) values (1, 2, 'Birds-house, 1');",
293                 "insert into hutch (id, animal_id, address) values (2, 1, 'Horse-house, 5');",
294                 "select animal_id, address from hutch where animal_id=1;",
295                 "create view address as select address from hutch where animal_id=2",
296                 "drop view address;", "drop table hutch;" };
297         boolean[] results = {false, false, false, false, false, true, false,
298                 false, false};
299 
300         for (int i = 0; i < queries.length; i++) {
301             Statement st = null;
302             try {
303                 st = conn.createStatement();
304                 boolean res = st.execute(queries[i]);
305                 assertEquals("different result for statement no. "+i, results[i], res);
306             } catch (SQLException e) {
307                 fail("SQLException is thrown: " + e.getMessage());
308             } finally {
309                 try {
310                     st.close();
311                 } catch (Exception ee) {
312                 }
313             }
314         }
315 
316         String[] inc_queries = {
317                 "update zoo_zoo set name='Masha', family='cat' where id=5;",
318                 "drop table hutchNO",
319                 "insert into hutch (id, animal_id, address) values (1, 2, 10);",
320                 "select animal_id, from hutch where animal_id=1;",
321                 "drop view address;", "drop table hutch;", "", null };
322 
323         for (int i = 0; i < inc_queries.length; i++) {
324             Statement st = null;
325             try {
326                 st = conn.createStatement();
327                 st.execute(inc_queries[i]);
328                 fail("SQLException is not thrown for query: " + inc_queries[i]);
329             } catch (SQLException e) {
330                 // expected
331             } finally {
332                 try {
333                     st.close();
334                 } catch (SQLException ee) {
335                 }
336             }
337         }
338     }
339 
340     /**
341      * @test java.sql.Statement#execute(String sql, int autoGeneratedKeys)
342      * TODO not supported
343      */
344     @TestTargetNew(
345         level = TestLevel.COMPLETE,
346         notes = "Missing implementation for Statement.RETURN_GENERATED_KEYS: keys not yet supported",
347         method = "execute",
348         args = {java.lang.String.class, int.class}
349     )
testExecute_String_int()350    public void testExecute_String_int() {
351         String[] queries = {
352                 "update zoo set name='Masha', family='cat' where id=2;",
353                 "drop table if exists hutch",
354                 "create table hutch (id integer not null, animal_id integer, address char(20), primary key (id));",
355                 "insert into hutch (id, animal_id, address) values (1, 2, 'Birds-house, 1');",
356                 "insert into hutch (id, animal_id, address) values (2, 1, 'Horse-house, 5');",
357                 "select animal_id, address from hutch where animal_id=1;",
358                 "create view address as select address from hutch where animal_id=2",
359                 "drop view address;", "drop table hutch;" };
360 
361         for (int i = 0; i < queries.length; i++) {
362             Statement st = null;
363             try {
364                 st = conn.createStatement();
365                 st.execute(queries[i], Statement.NO_GENERATED_KEYS);
366 
367                 ResultSet rs = st.getGeneratedKeys();
368                 assertFalse(rs.next());
369 
370             } catch (SQLException e) {
371                 // ok
372             } finally {
373                 try {
374                     st.close();
375                 } catch (SQLException ee) {
376                 }
377             }
378         }
379 
380         for (int i = 0; i < queries.length; i++) {
381             Statement st = null;
382             try {
383                 st = conn.createStatement();
384                 st.execute(queries[i], Statement.RETURN_GENERATED_KEYS);
385                 fail("Exception expected: Not supported");
386                 /*
387                 ResultSet rs = st.getGeneratedKeys();
388                 fail("Revise test implemenation for feature impl. has changed");
389                 assertFalse(rs.next());
390                 */
391             } catch (SQLException e) {
392                 //ok
393             } finally {
394                 try {
395                     st.close();
396                 } catch (SQLException ee) {
397                 }
398             }
399         }
400     }
401 
402     /**
403      * @test java.sql.Statement#getConnection()
404      */
405     @TestTargetNew(
406         level = TestLevel.COMPLETE,
407         notes = "SQLException test fails",
408         method = "getConnection",
409         args = {}
410     )
411     @KnownFailure("statment.close() does not wrap up")
testGetConnection()412     public void testGetConnection() {
413         Statement st = null;
414         try {
415             st = conn.createStatement();
416             assertSame(conn, st.getConnection());
417         } catch (SQLException e) {
418             fail("SQLException is thrown: " + e.getMessage());
419         } finally {
420             try {
421                 st.close();
422             } catch (SQLException ee) {
423             }
424         }
425 
426         try {
427             st.close();
428             st.getConnection();
429             fail("Exception expected");
430         } catch (SQLException e) {
431             //ok
432         }
433 
434 
435     }
436 
437     /**
438      * @test java.sql.Statement#getFetchDirection()
439      */
440     @TestTargetNew(
441         level = TestLevel.SUFFICIENT,
442         notes = "SQLException test fails. Not all Fetch directions supported.",
443         method = "getFetchDirection",
444         args = {}
445     )
446     @KnownFailure("statment.close() does not wrap up")
testGetFetchDirection()447     public void testGetFetchDirection() {
448         Statement st = null;
449         try {
450             st = conn.createStatement();
451             assertEquals(ResultSet.FETCH_UNKNOWN, st.getFetchDirection());
452         } catch (SQLException e) {
453             fail("SQLException is thrown " + e.getMessage());
454         }  finally {
455             try {
456                 st.close();
457             } catch (SQLException ee) {
458             }
459         }
460 
461         try {
462             st = conn.createStatement();
463             st.setFetchDirection(ResultSet.FETCH_FORWARD);
464             assertEquals(ResultSet.FETCH_FORWARD, st.getFetchDirection());
465             fail("Exception expected: not supported");
466         } catch (SQLException e) {
467             // ok
468         }  finally {
469             try {
470                 st.close();
471             } catch (SQLException ee) {
472             }
473         }
474 
475         try {
476             st.getFetchDirection();
477             fail("Exception expected");
478         } catch (SQLException e) {
479             //ok
480         }
481     }
482 
483     /**
484      * @test java.sql.Statement#setFetchDirection(int)
485      * TODO not supported
486      */
487     @TestTargetNew(
488         level = TestLevel.COMPLETE,
489         notes = "not supported. ",
490         method = "setFetchDirection",
491         args = {int.class}
492     )
testSetFetchDirection()493     public void testSetFetchDirection() {
494         Statement st = null;
495         try {
496             st = conn.createStatement();
497             st.setFetchDirection(ResultSet.FETCH_FORWARD);
498             st.executeQuery("select * from zoo;");
499             fail("Revise test implemenation for feature impl. has changed");
500 //            assertEquals(ResultSet.FETCH_FORWARD, st.getFetchDirection());
501         } catch (SQLException e) {
502 //            fail("SQLException is thrown: " + e.getMessage());
503             //ok
504         } finally {
505             try {
506                 st.close();
507             } catch (SQLException ee) {
508             }
509         }
510 
511         /*
512         try {
513             st = conn.createStatement();
514             st.setFetchDirection(-1);
515             fail("SQLException is not thrown");
516         } catch (SQLException e) {
517             // expected
518         }  finally {
519             try {
520                 st.close();
521             } catch (SQLException ee) {
522             }
523         }
524 
525         try {
526             st = conn.createStatement();
527             st.setFetchDirection(100);
528             fail("SQLException is not thrown");
529         } catch (SQLException e) {
530             // expected
531         } finally {
532             try {
533                 st.close();
534             } catch (SQLException ee) {
535             }
536         }
537 
538         */
539     }
540 
541     /**
542      * @test java.sql.Statement#getFetchSize()
543      */
544     @TestTargetNew(
545         level = TestLevel.COMPLETE,
546         notes = "SQLException test fails",
547         method = "getFetchSize",
548         args = {}
549     )
550     @KnownFailure("statment.close() does not wrap up")
testGetFetchSize()551     public void testGetFetchSize() {
552         Statement st = null;
553         try {
554             st = conn.createStatement();
555             st.execute("select * from zoo;");
556             assertEquals(1, st.getFetchSize());
557         } catch (SQLException e) {
558             fail("SQLException is thrown");
559         } finally {
560             try {
561                 st.close();
562             } catch (SQLException ee) {
563             }
564         }
565 
566         try {
567             st.close();
568             st.getFetchSize();
569             fail("Exception expected");
570         } catch (SQLException e) {
571             //ok
572         }
573     }
574 
575     /**
576      * @test {@link java.sql.Statement#setFetchSize(int)}
577      * TODO not supported
578      */
579     @TestTargetNew(
580         level = TestLevel.COMPLETE,
581         notes = "not supported.",
582         method = "setFetchSize",
583         args = {int.class}
584     )
testSetFetchSize()585     public void testSetFetchSize() {
586         Statement st = null;
587         try {
588             st = conn.createStatement();
589             int rows = 100;
590             for (int i = 0; i < rows; i++) {
591                 try {
592                     st.setFetchSize(i);
593                     assertEquals(i, st.getFetchSize());
594                 } catch (SQLException sqle) {
595                     // getFetchSize() hardcoded to 1.
596                     assertEquals("fetch size not 1", sqle.getMessage());
597                 }
598             }
599             /*
600             try {
601                 st.setFetchSize(-1);
602                 fail("SQLException is not thrown");
603             } catch (SQLException sqle) {
604                 // expected
605             }
606             */
607 
608         } catch (SQLException e) {
609             fail("SQLException is thrown");
610         } finally {
611             try {
612                 st.close();
613             } catch (SQLException ee) {
614             }
615         }
616     }
617 
618     /**
619      * @test java.sql.Statement#setMaxFieldSize(int max)
620      * TODO not supported
621      */
622     @TestTargetNew(
623         level = TestLevel.COMPLETE,
624         notes = "not supported",
625         method = "setMaxFieldSize",
626         args = {int.class}
627     )
testSetMaxFieldSize()628     public void testSetMaxFieldSize() {
629         Statement st = null;
630         try {
631             st = conn.createStatement();
632             for (int i = 0; i < 300; i += 50) {
633                 try {
634                     st.setMaxFieldSize(i);
635                     assertEquals(i, st.getMaxFieldSize());
636                     fail("Revise test implemenation for feature impl. has changed");
637                 } catch (SQLException sqle) {
638                     assertEquals("not supported", sqle.getMessage());
639 //                    fail("SQLException is thrown: " + sqle.getMessage());
640                 }
641             }
642             /*
643             try {
644                 st.setMaxFieldSize(-1);
645                 fail("SQLException isn't thrown");
646             } catch (SQLException sqle) {
647                 // expecteds
648             }
649             */
650         } catch (SQLException e) {
651             fail("Can't create statement, SQLException is thrown: "
652                     + e.getMessage());
653         } finally {
654             try {
655                 st.close();
656             } catch (SQLException ee) {
657             }
658         }
659     }
660 
661     /**
662      * @test java.sql.Statement#getMaxFieldSize()
663      * TODO not supported
664      */
665     @TestTargetNew(
666         level = TestLevel.COMPLETE,
667         notes = "not supported",
668         method = "getMaxFieldSize",
669         args = {}
670     )
testGetMaxFieldSize()671     public void testGetMaxFieldSize() {
672         Statement st = null;
673         try {
674             st = conn.createStatement();
675             for (int i = 200; i < 500; i += 50) {
676                 try {
677                     st.setMaxFieldSize(i);
678                     fail("Revise test implemenation for feature impl. has changed");
679                 } catch (SQLException sqle) {
680                     assertEquals("not supported", sqle.getMessage());
681  //                   fail("SQLException is thrown: " + sqle.getMessage());
682                 }
683             }
684         } catch (SQLException e) {
685             fail("Can't create statement, SQLException is thrown: "
686                     + e.getMessage());
687         } finally {
688             try {
689                 st.close();
690             } catch (SQLException ee) {
691             }
692         }
693     }
694 
testMaxRows()695     public void testMaxRows() {
696         Statement st = null;
697         try {
698             st = conn.createStatement();
699             for (int i = 0; i < 300; i += 50) {
700                 try {
701                     st.setMaxRows(i);
702                     assertEquals(i, st.getMaxRows());
703                     ResultSet r = st.executeQuery("select * from zoo;");
704                     int rowCount = 0;
705                     while (r.next()) {
706                         ++rowCount;
707                     }
708                     if (i == 0) {
709                         // 0 means unlimited.
710                         assertTrue("rowCount=" + rowCount + " i=" + i, rowCount > i);
711                     } else {
712                         assertTrue("rowCount=" + rowCount + " i=" + i, rowCount <= i);
713                     }
714                     r.close();
715                 } catch (SQLException sqle) {
716                     fail("SQLException is thrown: " + sqle.getMessage());
717                 }
718             }
719             try {
720                 st.setMaxRows(-1);
721                 fail("SQLException isn't thrown");
722             } catch (SQLException sqle) {
723                 // expecteds
724             }
725         } catch (SQLException e) {
726             fail("Can't create statement, SQLException is thrown: "
727                     + e.getMessage());
728         } finally {
729             try {
730                 st.close();
731             } catch (SQLException ee) {
732             }
733         }
734     }
735 
736     /**
737      * @test java.sql.Statement#close()
738      * not passed according to Java Docs: should release all resources
739      * IMMEDIATELY
740      */
741     @TestTargetNew(
742         level = TestLevel.COMPLETE,
743         notes = "",
744         method = "close",
745         args = {}
746     )
747     @KnownFailure("statment.close() does not wrap up")
testClose()748     public void testClose() {
749         Statement st = null;
750         ResultSet res = null;
751         try {
752             String[] queries = {
753                     "update zoo set name='Masha', family='cat' where id=2;",
754                     "insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');",
755                     "insert into zoo (id, name, family) values (4, 'Slon', 'elephant');",
756                     "select * from zoo"};
757             st = conn.createStatement();
758             for (int i = 0; i < queries.length; i++) {
759                 st.execute(queries[i]);
760             }
761             res = st.getResultSet();
762             assertNotNull(res);
763             assertTrue(res.next());
764             st.close();
765         } catch (SQLException e) {
766             fail("SQLException is thrown: " + e.getMessage());
767         } finally {
768             try {
769                 st.close();
770             } catch (SQLException ee) {
771             }
772         }
773 
774         // test release of resources:
775         // this code should throw an exception as the db is not available
776         // anymore in fact every resource which is used afterwards should throw
777         // an SQLException.
778         try {
779             res.next();
780             fail("Exception expected");
781         } catch (SQLException e) {
782             // ok
783         }
784 
785     }
786 
787     /**
788      * @test java.sql.Statement#execute(String sql, int[] columnIndexes)
789      * TODO not supported
790      */
791     @TestTargetNew(
792         level = TestLevel.COMPLETE,
793         notes = "not supported",
794         method = "execute",
795         args = {java.lang.String.class, int[].class}
796     )
testExecute_String_intArray()797     public void testExecute_String_intArray() {
798         Statement st = null;
799         try {
800             String[] queries = {
801                     "update zoo set name='Masha', family='cat' where id=2;",
802                     "insert zoo(id, name, family) values (3, 'Vorobey', 'sparrow');",
803                     "insert zoo(id, name, family) values (4, 'Slon', 'elephant');",
804                     "select * from zoo" };
805             Vector<int[]> array = new Vector<int[]>();
806             array.addElement(null);
807             array.addElement(new int[] { 1, 2, 3 });
808             array.addElement(new int[] { 1, 2, 10, 100 });
809             array.addElement(new int[] {});
810 
811             st = conn.createStatement();
812             for (int i = 0; i < queries.length; i++) {
813                 st.execute(queries[i], (int[]) array.elementAt(i));
814                 fail("SQLException expected: not supported");
815             }
816             /*
817             fail("Revise test implemenation for feature impl. has changed");
818             assertNotNull(st.getResultSet());
819             st.close();
820             assertNull(st.getResultSet());
821             */
822         } catch (SQLException e) {
823             // ok: not supported
824 //            fail("SQLException is thrown: " + e.getMessage());
825         } finally {
826             try {
827                 st.close();
828             } catch (SQLException ee) {
829             }
830         }
831     }
832 
833     /**
834      * @test java.sql.Statement#execute(String sql, String[] columnNames)
835      */
836     @TestTargetNew(
837         level = TestLevel.COMPLETE,
838         notes = "not supported",
839         method = "execute",
840         args = {java.lang.String.class, java.lang.String[].class}
841     )
testExecute_String_StringArray()842     public void testExecute_String_StringArray() {
843         Statement st = null;
844         try {
845             String[] queries = {
846                     "update zoo set name='Masha', family='cat' where id=2;",
847                     "insert zoo(id, name, family) values (3, 'Vorobey', 'sparrow');",
848                     "insert zoo(id, name, family) values (4, 'Slon', 'elephant');",
849                     "select * from zoo" };
850             Vector<String[]> array = new Vector<String[]>();
851             array.addElement(null);
852             array.addElement(new String[] { "", "", "", "", "", "", "", "" });
853             array.addElement(new String[] { "field 1", "", "field2" });
854             array.addElement(new String[] { "id", "family", "name" });
855 
856             st = conn.createStatement();
857             for (int i = 0; i < queries.length; i++) {
858                 st.execute(queries[i], (String[]) array.elementAt(i));
859                 fail("Exception expected: not supported");
860             }
861             fail("Revise test implemenation for feature impl. has changed");
862             assertNotNull(st.getResultSet());
863             st.close();
864             assertNull(st.getResultSet());
865         } catch (SQLException e) {
866             // ok: not supported
867             try {
868                 st.close();
869             } catch (SQLException ee) {
870             }
871         }
872     }
873 
874     /**
875      * @test java.sql.Statement#executeBatch()
876      */
877     @TestTargetNew(
878         level = TestLevel.COMPLETE,
879         notes = "Test fails: dropping table hutch affects at least 2 rows.executeBatch() always returns same result: 1.",
880         method = "executeBatch",
881         args = {}
882     )
883     @KnownFailure("always returns 1 for no. of updates")
testExecuteBatch()884     public void testExecuteBatch() {
885 
886         String[] queries = {
887                 "update zoo set name='Masha', family='cat' where id=2;",
888                 "drop table if exists hutch",
889                 "create table hutch (id integer not null, animal_id integer, address char(20), primary key (id));",
890                 "insert into hutch (id, animal_id, address) values (1, 2, 'Birds-house, 1');",
891                 "insert into hutch (id, animal_id, address) values (2, 1, 'Horse-house, 5');",
892                 "create view address as select address from hutch where animal_id=2",
893                 "drop view address;", "drop table hutch;" };
894 
895         String[] wrongQueries = {
896                 "update zoo set name='Masha', family='cat' where;",
897                 "drop table if exists hutch;",
898                 "create view address as select address from hutch where animal_id=2;",
899                 "drop view address;", "drop table hutch;" };
900 
901         int[] result = { 1, 1, 1, 1, 1, 1, 1, 1 };
902         Statement st = null;
903 
904         //Exception test
905         try {
906             st = conn.createStatement();
907             assertEquals(0, st.executeBatch().length);
908             for (int i = 0; i < wrongQueries.length; i++) {
909                 st.addBatch(wrongQueries[i]);
910             }
911             st.executeBatch();
912             fail("BatchupdateException expected");
913         } catch (BatchUpdateException e) {
914             //ok
915         } catch (SQLException e) {
916             fail("BatchupdateException expected");
917         } finally {
918             try {
919                 st.close();
920             } catch (SQLException ee) {
921             }
922         }
923 
924         try {
925             st = conn.createStatement();
926             assertEquals(0, st.executeBatch().length);
927             for (int i = 0; i < queries.length; i++) {
928                 st.addBatch(queries[i]);
929             }
930             int[] resArray = st.executeBatch();
931             assertTrue(java.util.Arrays.equals(result, resArray));
932         } catch (SQLException e) {
933             fail("SQLException is thrown: " + e.getMessage());
934         } finally {
935             try {
936                 st.close();
937             } catch (SQLException ee) {
938             }
939         }
940 
941         try {
942             st = conn.createStatement();
943             st.addBatch("select * from zoo");
944             st.executeBatch();
945             fail("Exception expected");
946         } catch (BatchUpdateException bue) {
947             // ok select returns a resultSet
948         } catch (SQLException sqle) {
949             fail("Unknown SQLException is thrown: " + sqle.getMessage());
950         } finally {
951             try {
952                 st.close();
953             } catch (SQLException ee) {
954             }
955         }
956         //Exception test
957         try {
958             st.close();
959             st.executeBatch();
960             fail("SQLException not thrown");
961         } catch (SQLException e) {
962             //ok
963         }
964     }
965 
966     /**
967      * @test java.sql.Statement#executeQuery(String sql)
968      */
969     @TestTargetNew(
970         level = TestLevel.COMPLETE,
971         notes = "Not according to spec.",
972         method = "executeQuery",
973         args = {java.lang.String.class}
974     )
975     @KnownFailure("Does throw an exception on non select statment.")
testExecuteQuery_String()976     public void testExecuteQuery_String() {
977 
978         String[] queries1 = { "select * from zoo",
979                 "select name, family from zoo where id = 1" };
980 
981         String[] queries2 = {
982                 "update zoo set name='Masha', family='cat' where id=2;",
983                 "drop table if exists hutch",
984                 "create table hutch (id integer not null, animal_id integer, address char(20), primary key (id));",
985                 "insert into hutch (id, animal_id, address) values (1, 2, 'Birds-house, 1');",
986                 "insert into hutch (id, animal_id, address) values (2, 1, 'Horse-house, 5');",
987                 "create view address as select address from hutch where animal_id=2",
988                 "drop view address;", "drop table hutch;", "select from zoo" };
989 
990         Statement st = null;
991 
992         try {
993             st = conn.createStatement();
994             for (int i = 0; i < queries1.length; i++) {
995                 try {
996                     ResultSet rs = st.executeQuery(queries1[i]);
997                     assertNotNull(rs);
998                 } catch (SQLException sqle) {
999                     fail("SQLException is thrown for query: " + queries1[i]);
1000                 }
1001             }
1002         } catch (SQLException e) {
1003             fail("SQLException is thrown: " + e.getMessage());
1004         } finally {
1005             try {
1006                 st.close();
1007             } catch (Exception ee) {
1008             }
1009         }
1010 
1011         // queries which do not produce a ResultSet -> exception testing
1012 
1013         try {
1014             st = conn.createStatement();
1015             for (int i = 0; i < queries2.length; i++) {
1016                 try {
1017                     ResultSet rs = st.executeQuery(queries2[i]);
1018                     assertNotNull(rs);
1019                     fail("SQLException is not thrown for query: " + queries2[i]);
1020                 } catch (SQLException sqle) {
1021                     // expected
1022                 }
1023             }
1024         } catch (SQLException sqle) {
1025             fail("Unknown SQLException is thrown: " + sqle.getMessage());
1026         } finally {
1027             try {
1028                 st.close();
1029             } catch (Exception ee) {
1030             }
1031         }
1032 
1033     }
1034 
1035     /**
1036      * @throws SQLException
1037      * @test java.sql.Statement#executeUpdate(String sql)
1038      */
1039     @TestTargetNew(
1040         level = TestLevel.PARTIAL_COMPLETE,
1041         notes = "impl not according to spec.",
1042         method = "executeUpdate",
1043         args = {java.lang.String.class}
1044     )
1045     @KnownFailure("Spec is not precise enough: should be: number of rows affected."+
1046             " eg. to be consistent for deletes: 'delete from s1;' should be different from "+
1047             "'delete from s1 where c1 = 1;' ")
testExecuteUpdate_String()1048     public void testExecuteUpdate_String() throws SQLException {
1049 
1050         String[] queries1 = {
1051                 "update zoo set name='Masha', family='cat' where id=2;",
1052                 "drop table if exists hutch",
1053                 "create table hutch (id integer not null, animal_id integer, address char(20), primary key (id));",
1054                 "insert into hutch (id, animal_id, address) values (1, 2, 'Birds-house, 1');",
1055                 "insert into hutch (id, animal_id, address) values (2, 1, 'Horse-house, 5');",
1056                 "create view address as select address from hutch where animal_id=2;",
1057                 "drop view address;", "drop table hutch;"};
1058 
1059         String queries2 = "select * from zoo;";
1060 
1061         Statement st = null;
1062         try {
1063             st = conn.createStatement();
1064             for (int i = 0; i < queries1.length; i++) {
1065                 try {
1066                     int count = st.executeUpdate(queries1[i]);
1067                     assertTrue(count > 0);
1068                 } catch (SQLException e) {
1069                     fail("SQLException is thrown: " + e.getMessage());
1070                 }
1071             }
1072 
1073             try {
1074                assertEquals(0, st.executeUpdate(queries2));
1075             } catch (SQLException e) {
1076                fail("SQLException is thrown: " + e.getMessage());
1077             }
1078 
1079         } catch (SQLException e) {
1080             fail("SQLException is thrown: " + e.getMessage());
1081         } finally {
1082             try {
1083                 st.close();
1084             } catch (Exception ee) {
1085             }
1086         }
1087 
1088         // test return value for specific numbers
1089 
1090         Statement stat = conn.createStatement();
1091 
1092         // there are 0 rows created therefore 0 should be returned.
1093         assertEquals(0 ,stat.executeUpdate("create table s1 (c1);"));
1094 
1095         assertEquals(1, stat.executeUpdate("insert into s1 values (0);"));
1096         assertEquals(1, stat.executeUpdate("insert into s1 values (1);"));
1097         assertEquals(1, stat.executeUpdate("insert into s1 values (2);"));
1098         assertEquals(1,stat.executeUpdate("delete from s1 where c1 = 1;"));
1099         assertEquals(2, stat.executeUpdate("update s1 set c1 = 5;"));
1100 
1101         // analogous to statemente before, delete all should return 2
1102         assertEquals(2,stat.executeUpdate("delete from s1;"));
1103 
1104         // there are no rows in table: 0 should be returned
1105         assertEquals(0, stat.executeUpdate("drop table s1;"));
1106 
1107         stat.executeUpdate("create table s1 (c1);");
1108         stat.executeUpdate("insert into s1 values (0);");
1109         stat.executeUpdate("insert into s1 values (1);");
1110         stat.executeUpdate("insert into s1 values (2);");
1111 
1112         // there are 3 rows in table: 3 should be returned
1113         assertEquals(3, stat.executeUpdate("drop table s1;"));
1114 
1115         stat.close();
1116     }
1117 
1118     /**
1119      * @test java.sql.Statement#executeUpdate(String sql, int[] columnIndexes)
1120      *
1121      * TODO executeUpdate(String sql, int[] columnIndexes) is not supported
1122      */
1123     @TestTargetNew(
1124         level = TestLevel.COMPLETE,
1125         notes = "not supported",
1126         method = "executeUpdate",
1127         args = {java.lang.String.class, int[].class}
1128     )
testExecuteUpdate_String_intArray()1129     public void testExecuteUpdate_String_intArray() throws SQLException {
1130         Statement st = null;
1131         try {
1132             String[] queries1 = {
1133                     "update zoo set name='Masha', family='cat' where id=2;",
1134                     "drop table if exists hutch",
1135                     "create table hutch (id integer not null, animal_id integer, address char(20), primary key (id));",
1136                     "insert into hutch (id, animal_id, address) values (1, 2, 'Birds-house, 1');",
1137                     "insert into hutch (id, animal_id, address) values (2, 1, 'Horse-house, 5');",
1138                     "create view address as select address from hutch where animal_id=2",
1139                     "drop view address;", "drop table hutch;" };
1140 
1141             Vector<int[]> array = new Vector<int[]>();
1142             array.addElement(null);
1143             array.addElement(new int[] { 1, 2, 3 });
1144             array.addElement(new int[] { 1, 2, 10, 100 });
1145             array.addElement(new int[] {});
1146             array.addElement(new int[] { 100, 200 });
1147             array.addElement(new int[] { -1, 0 });
1148             array.addElement(new int[] { 0, 0, 0, 1, 2, 3 });
1149             array.addElement(new int[] { -100, -200 });
1150 
1151             st = conn.createStatement();
1152             for (int i = 0; i < queries1.length; i++) {
1153                 st.executeUpdate(queries1[i], (int[]) array.elementAt(i));
1154                 fail("Exception expected");
1155             }
1156         } catch (SQLFeatureNotSupportedException e) {
1157             // expected
1158         } finally {
1159             try {
1160                 st.close();
1161             } catch (SQLException ee) {
1162             }
1163         }
1164     }
1165 
1166     /**
1167      * @test java.sql.Statement#executeUpdate(String sql, int autoGeneratedKeys)
1168      *
1169      * TODO  executeUpdate(String sql, int autoGeneratedKeys) is not supported
1170      */
1171     @TestTargetNew(
1172         level = TestLevel.SUFFICIENT,
1173         notes = "not supported",
1174         method = "executeUpdate",
1175         args = {java.lang.String.class, int.class}
1176     )
testExecuteUpdate_String_int()1177     public void testExecuteUpdate_String_int() {
1178         String[] queries = {
1179                 "update zoo set name='Masha', family='cat' where id=2;",
1180                 "drop table if exists hutch",
1181                 "create table hutch (id integer not null, animal_id integer, address char(20), primary key (id));",
1182                 "insert into hutch (id, animal_id, address) values (1, 2, 'Birds-house, 1');",
1183                 "insert into hutch (id, animal_id, address) values (2, 1, 'Horse-house, 5');",
1184                 "select animal_id, address from hutch where animal_id=1;",
1185                 "create view address as select address from hutch where animal_id=2",
1186                 "drop view address;", "drop table hutch;" };
1187 
1188             Statement st = null;
1189             ResultSet rs = null;
1190             try {
1191                 st = conn.createStatement();
1192                 st.executeUpdate(queries[1], Statement.NO_GENERATED_KEYS);
1193                 rs = st.getGeneratedKeys();
1194                 assertFalse(rs.next());
1195                 fail("Exception expected: not supported");
1196             } catch (SQLException e) {
1197                 //ok
1198             } finally {
1199                 try {
1200                     rs.close();
1201                     st.close();
1202                 } catch (Exception ee) {
1203                 }
1204             }
1205 
1206             try {
1207                 st = conn.createStatement();
1208                 st.executeUpdate(queries[1], Statement.RETURN_GENERATED_KEYS);
1209                 rs = st.getGeneratedKeys();
1210                 assertTrue(rs.next());
1211                 fail("Exception expected: not supported");
1212             } catch (SQLException e) {
1213                 //ok
1214             } finally {
1215                 try {
1216                     rs.close();
1217                     st.close();
1218                 } catch (Exception ee) {
1219                 }
1220             }
1221     }
1222 
1223     /**
1224      * @test java.sql.Statement#executeUpdate(String sql, String[] columnNames)
1225      *
1226      * TODO executeUpdate(String sql, String[] columnNames) is not supported
1227      */
1228     @TestTargetNew(
1229         level = TestLevel.COMPLETE,
1230         notes = "not supported",
1231         method = "executeUpdate",
1232         args = {java.lang.String.class, java.lang.String[].class}
1233     )
testExecuteUpdate_String_StringArray()1234     public void testExecuteUpdate_String_StringArray() throws SQLException {
1235         Statement st = null;
1236         try {
1237             String[] queries = {
1238                     "update zoo set name='Masha', family='cat' where id=2;",
1239                     "drop table if exists hutch",
1240                     "create table hutch (id integer not null, animal_id integer, address char(20), primary key (id));",
1241                     "insert into hutch (id, animal_id, address) values (1, 2, 'Birds-house, 1');",
1242                     "insert into hutch (id, animal_id, address) values (2, 1, 'Horse-house, 5');",
1243                     "create view address as select address from hutch where animal_id=2",
1244                     "drop view address;", "drop table hutch;" };
1245 
1246             Vector<String[]> array = new Vector<String[]>();
1247             array.addElement(null);
1248             array.addElement(new String[] { "", "", "", "", "", "", "", "" });
1249             array.addElement(new String[] { "field 1", "", "field2" });
1250             array.addElement(new String[] { "id", "family", "name" });
1251             array
1252                     .addElement(new String[] { "id", null, "family", null,
1253                             "name" });
1254             array.addElement(new String[] { "id", " ", "name" });
1255             array.addElement(new String[] { null, null, null, null });
1256             array.addElement(new String[] { " ", "123 21", "~!@#$%^&*()_+ ",
1257                     null });
1258 
1259             st = conn.createStatement();
1260             for (int i = 0; i < queries.length; i++) {
1261                 st.executeUpdate(queries[i], (String[]) array.elementAt(i));
1262                 fail("Revise test implemenation for feature impl. has changed");
1263             }
1264         } catch (SQLFeatureNotSupportedException e) {
1265             // expected
1266         } finally {
1267             try {
1268                 st.close();
1269             } catch (SQLException ee) {
1270             }
1271         }
1272     }
1273 
1274     /**
1275      * @test java.sql.Statement#getUpdateCount()
1276      */
1277     @TestTargetNew(
1278         level = TestLevel.COMPLETE,
1279         notes = "SQLException test fails",
1280         method = "getUpdateCount",
1281         args = {}
1282     )
1283     @KnownFailure("statment.close() does not wrap up")
testGetUpdateCount()1284     public void testGetUpdateCount() {
1285         Statement st = null;
1286         try {
1287             String query = "update zoo set name='Masha', family='cat' where id=2;";
1288             st = conn.createStatement();
1289             st.executeUpdate(query);
1290             assertEquals(1, st.getUpdateCount());
1291             query = "update zoo set name='Masha', family='cat' where id=5;";
1292             st.executeUpdate(query);
1293             assertEquals(0, st.getUpdateCount());
1294         } catch (SQLException e) {
1295             fail("SQLException is thrown: " + e.getMessage());
1296         } finally {
1297             try {
1298                 st.close();
1299             } catch (SQLException ee) {
1300             }
1301         }
1302         // statment closed : Exception test
1303         try {
1304             st.getUpdateCount();
1305             fail("Exception expected");
1306         } catch (SQLException e) {
1307             //ok
1308         }
1309     }
1310 
1311     /**
1312      * @test {@link java.sql.Statement#getGeneratedKeys()}
1313      *
1314      * TODO getGeneratedKeys() is not supported
1315      */
1316     @TestTargetNew(
1317         level = TestLevel.SUFFICIENT,
1318         notes = "not supported",
1319         method = "getGeneratedKeys",
1320         args = {}
1321     )
testGeneratedKeys()1322     public void testGeneratedKeys() throws SQLException {
1323         Statement st = null;
1324         try {
1325             String insert = "insert into zoo (id, name, family) values (8, 'Tuzik', 'dog');";
1326             st = conn.createStatement();
1327             assertNull(st.getGeneratedKeys());
1328             fail("Fail: statement does not fail");
1329         } catch (SQLFeatureNotSupportedException e) {
1330             // expected
1331         }
1332     }
1333 
1334     /**
1335      * @test {@link java.sql.Statement#setCursorName(String)}
1336      *
1337      * TODO setCursorName() is not supported
1338      */
1339     @TestTargetNew(
1340         level = TestLevel.SUFFICIENT,
1341         notes = "not supported",
1342         method = "setCursorName",
1343         args = {java.lang.String.class}
1344     )
testSetCursorName()1345     public void testSetCursorName() throws SQLException {
1346         Statement st = null;
1347         try {
1348             String select = "select * from zoo";
1349             st = conn.createStatement();
1350             st.setCursorName("test");
1351             fail("Fail: statement does not fail");
1352         } catch (SQLFeatureNotSupportedException e) {
1353             // expected
1354         }
1355     }
1356 
1357     /**
1358      * @test {@link java.sql.Statement#setEscapeProcessing}
1359      *
1360      * TODO setExcapeProcessing() is not supported
1361      */
1362     @TestTargetNew(
1363         level = TestLevel.SUFFICIENT,
1364         notes = "not supported",
1365         method = "setEscapeProcessing",
1366         args = {boolean.class}
1367     )
testSetEscapeProcessing()1368     public void testSetEscapeProcessing() {
1369         Statement st = null;
1370         try {
1371             String select = "select * from zoo";
1372             st = conn.createStatement();
1373             st.setEscapeProcessing(true);
1374             fail("Fail: statement does not fail");
1375         } catch (SQLException e) {
1376           assertEquals("not supported", e.getMessage());
1377         }
1378 
1379     }
1380 
1381     /**
1382      * @test {@link java.sql.Statement#setQueryTimeout}
1383      *
1384      */
1385     @TestTargets({
1386     @TestTargetNew(
1387         level = TestLevel.PARTIAL_COMPLETE,
1388         notes = "Error in impl. default query timeout for sqlite dbs is 0.",
1389         method = "setQueryTimeout",
1390         args = {int.class}
1391     ),
1392     @TestTargetNew(
1393             level = TestLevel.PARTIAL_COMPLETE,
1394             notes = "Error in impl. default query timeout for sqlite dbs is 0.",
1395             method = "getQueryTimeout",
1396             args = {}
1397         )
1398     })
testSetQueryTimeout()1399     public void testSetQueryTimeout() {
1400         try {
1401             Statement st = conn.createStatement();
1402             st.setQueryTimeout(2);
1403             assertEquals(2, st.getQueryTimeout());
1404 
1405             try {
1406                 st = conn.createStatement();
1407                 st.setQueryTimeout(-1);
1408                 fail("SQLException not thrown");
1409             } catch (SQLException expected) {
1410                 // expected
1411             }
1412 
1413             try {
1414                 st = conn.createStatement();
1415                 st.close();
1416                 st.setQueryTimeout(3);
1417                 fail("SQLException not thrown");
1418             } catch (SQLException expected) {
1419                 // expected
1420             }
1421         } catch (SQLException e) {
1422             throw new RuntimeException(e);
1423         }
1424     }
1425 
1426     /**
1427      * @test {@link java.sql.Statement#getResultSetType()}
1428      *
1429      */
1430     @TestTargetNew(
1431         level = TestLevel.COMPLETE,
1432         notes = "Tests fail. not fully supported: returns only ResultSet.TYPE_SCROLL_INSENSITIVE. Either should throw an unsupported exception or behave according to spec.",
1433         method = "getResultSetType",
1434         args = {}
1435     )
1436     @KnownFailure("not fully supported")
testGetResultSetType()1437     public void testGetResultSetType() {
1438         Statement st = null;
1439         // test default value
1440         try {
1441             st = conn.createStatement();
1442             st.getResultSetType();
1443             assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, st
1444                     .getResultSetType());
1445         } catch (SQLException e) {
1446             assertEquals("not supported", e.getMessage());
1447         }
1448 
1449         // failing tests
1450         try {
1451             st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1452                     ResultSet.CONCUR_UPDATABLE);
1453             st.getResultSetType();
1454             assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, st.getResultSetType());
1455         } catch (SQLException e) {
1456             assertEquals("not supported", e.getMessage());
1457         }
1458 
1459         try {
1460             st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1461                     ResultSet.CONCUR_UPDATABLE);
1462             st.getResultSetType();
1463             assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, st.getResultSetType());
1464         } catch (SQLException e) {
1465             assertEquals("not supported", e.getMessage());
1466         }
1467 
1468         try {
1469             st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
1470                     ResultSet.CONCUR_UPDATABLE);
1471             st.getResultSetType();
1472             assertEquals(ResultSet.TYPE_FORWARD_ONLY, st.getResultSetType());
1473         } catch (SQLException e) {
1474             assertEquals("not supported", e.getMessage());
1475         }
1476     }
1477 
1478     /**
1479      * @test {@link java.sql.Statement#getResultSetHoldability()}
1480      *
1481      */
1482     @TestTargetNew(
1483         level = TestLevel.SUFFICIENT,
1484         notes = "not supported",
1485         method = "getResultSetHoldability",
1486         args = {}
1487     )
1488     @KnownFailure("Test for default value fails")
testGetResultSetHoldability()1489     public void testGetResultSetHoldability() {
1490 
1491         // test default value
1492         Statement st = null;
1493         try {
1494             st = conn.createStatement();
1495             assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, st
1496                     .getResultSetHoldability());
1497         } catch (SQLException e) {
1498             assertEquals("not supported", e.getMessage());
1499         }
1500 
1501         // failing tests
1502         try {
1503             st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1504                     ResultSet.CONCUR_READ_ONLY,
1505                     ResultSet.HOLD_CURSORS_OVER_COMMIT);
1506             fail("Exception expected: not supported");
1507             /*
1508             st.getResultSetHoldability();
1509             assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, st
1510                     .getResultSetHoldability());
1511             */
1512         } catch (SQLException e) {
1513             // ok: not supported
1514         }
1515 
1516         try {
1517             st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1518                     ResultSet.CONCUR_READ_ONLY,
1519                     ResultSet.CLOSE_CURSORS_AT_COMMIT);
1520             fail("Exception expected: not supported");
1521             /*
1522             st.getResultSetHoldability();
1523             assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, st
1524                     .getResultSetHoldability());
1525             */
1526         } catch (SQLException e) {
1527          // ok: not supported
1528         }
1529     }
1530 
1531     /**
1532      * @test {@link java.sql.Statement#getResultSetConcurrency()}
1533      *
1534      */
1535     @TestTargetNew(
1536         level = TestLevel.SUFFICIENT,
1537         notes = "Tests fail. returns only ResultSet.TYPE_SCROLL_INSENSITIVE. Either should throw an unsupported exception or behave according to spec.",
1538         method = "getResultSetConcurrency",
1539         args = {}
1540     )
1541     @KnownFailure("Not supported")
testGetResultSetConcurrency()1542     public void testGetResultSetConcurrency() {
1543         Statement st = null;
1544 
1545         // test default value
1546         try {
1547             st = conn.createStatement();
1548             st.getResultSetConcurrency();
1549             assertEquals(ResultSet.CONCUR_READ_ONLY, st
1550                     .getResultSetConcurrency());
1551         } catch (SQLException e) {
1552             assertEquals("not supported", e.getMessage());
1553         }
1554 
1555      // failing tests
1556 
1557         try {
1558             st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
1559                     ResultSet.CONCUR_UPDATABLE);
1560             st.getResultSetConcurrency();
1561             assertEquals(ResultSet.CONCUR_UPDATABLE, st.getResultSetConcurrency());
1562             fail("Exception expected: not supported");
1563         } catch (SQLException e) {
1564             //ok
1565 
1566         }
1567 
1568         try {
1569             st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
1570                     ResultSet.CONCUR_READ_ONLY);
1571             st.getResultSetConcurrency();
1572             assertEquals(ResultSet.CONCUR_READ_ONLY, st.getResultSetConcurrency());
1573             fail("Exception expected: not supported");
1574         } catch (SQLException e) {
1575             //ok;
1576         }
1577     }
1578 
1579     /**
1580      * @test {@link java.sql.Statement#getResultSet()}
1581      *
1582      */
1583     @TestTargetNew(
1584         level = TestLevel.COMPLETE,
1585         notes = "Error in implementation. Is not according to spec:if updateCount > 0 resultset must be null.",
1586         method = "getResultSet",
1587         args = {}
1588     )
1589     @KnownFailure("Does not return null on update count > 0 (not a select statement) ")
testGetResultSet()1590     public void testGetResultSet() {
1591         Statement st = null;
1592         ResultSet res = null;
1593 
1594         try {
1595             st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
1596                     ResultSet.CONCUR_READ_ONLY,
1597                     ResultSet.CLOSE_CURSORS_AT_COMMIT);
1598             st.execute("create table test (c1);");
1599             res = st.getResultSet();
1600             assertNull(res);
1601         } catch (SQLException e) {
1602             fail("Unexpected Exception "+e);
1603         }
1604 
1605         try {
1606             st = conn.createStatement();
1607             String select = "select * from zoo where id == 4;";
1608             String insert =  "insert into zoo (id, name, family) values (4, 'Vorobuy', 'bear');";
1609             st.execute(insert);
1610             st.execute(select);
1611             assertEquals(-1, st.getUpdateCount());
1612             res = st.getResultSet();
1613             assertNotNull(res);
1614             res.next();
1615             assertEquals(4,res.getInt(1));
1616             assertEquals("Vorobuy",res.getString(2));
1617             assertEquals("bear",res.getString(3));
1618 //            assertEquals(0, st.getUpdateCount()); not supported
1619             assertFalse(res.next());
1620         } catch (SQLException e) {
1621             fail("SQLException is thrown:"+e.getMessage());
1622         }
1623 
1624         try {
1625             st = conn.createStatement();
1626             String insert = "insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');";
1627             st
1628             .execute(insert);
1629             res = st.getResultSet();
1630             // statement is an update and should return null according to spec.
1631             if (st.getUpdateCount() > 0)  {
1632                 assertNull(res);
1633             }
1634 
1635         } catch (SQLException e) {
1636             fail("SQLException is thrown:"+e.getMessage());
1637         }
1638 
1639         try {
1640             st.close();
1641             st.getResultSet();
1642             fail("Exception expected");
1643         } catch (SQLException e) {
1644             //ok
1645         }
1646     }
1647 
1648     /**
1649      * @test {@link java.sql.Statement#setQueryTimeout}
1650      *
1651      */
1652     @TestTargetNew(
1653         level = TestLevel.COMPLETE,
1654         notes = "Errors in impl.An other value is returned than was set (X * 1000)Default query timeout for sqlite dbs is 0.",
1655         method = "getQueryTimeout",
1656         args = {}
1657     )
1658     @KnownFailure("An other value is returned than was set (X * 1000)")
testGetQueryTimeout()1659     public void testGetQueryTimeout() {
1660         Statement st = null;
1661         try {
1662             st = conn.createStatement();
1663             st.setQueryTimeout(2000);
1664             assertEquals(2000, st.getQueryTimeout());
1665         } catch (SQLException e) {
1666             fail("SQLException is thrown: " + e.getMessage());
1667         }
1668 
1669         try {
1670             st = conn.createStatement();
1671             assertEquals(0,st.getQueryTimeout());
1672         } catch (SQLException e) {
1673             fail("SQLException is thrown: " + e.getMessage());
1674         }
1675 
1676         try {
1677             st.close();
1678             st.getQueryTimeout();
1679             fail("Exception expected");
1680         } catch (SQLException e) {
1681             //ok
1682         }
1683     }
1684 
1685     /**
1686      * @test {@link java.sql.Statement#getMoreResults()}
1687      *
1688      */
1689     @TestTargetNew(
1690         level = TestLevel.SUFFICIENT,
1691         notes = "not fully supported",
1692         method = "getMoreResults",
1693         args = {}
1694     )
1695     @KnownFailure("not supported")
testGetMoreResults()1696     public void testGetMoreResults() {
1697         Statement st = null;
1698         ResultSet res1 = null;
1699         ResultSet res2 = null;
1700         String[] queries = {
1701                 "insert into zoo values (3,'John','bird');",
1702                 "update zoo set name='Masha', family='cat' where id=3;",
1703                 "update zoo set name='Masha', family='bear' where id=3;"};
1704 
1705        try {
1706             st = conn.createStatement();
1707             st.execute(queries[0]);
1708             assertFalse(st.getMoreResults());
1709 
1710             try {
1711                 st.getResultSet();
1712                 fail("Exception expected");
1713             } catch (SQLException e) {
1714                 //ok
1715             }
1716         } catch (SQLException e) {
1717             fail("SQLException is thrown: " + e.getMessage());
1718         } finally {
1719             try {
1720                 st.close();
1721             } catch (SQLException ee) {
1722             }
1723         }
1724 
1725         try {
1726             st.getMoreResults();
1727             fail("Exception expected");
1728         } catch (SQLException e) {
1729             //ok
1730         }
1731     }
1732 
1733     /**
1734      * @test {@link java.sql.Statement#getMoreResults(int)}
1735      *
1736      */
1737     @TestTargetNew(
1738         level = TestLevel.NOT_FEASIBLE,
1739         notes = "Callable Statements are not supported",
1740         method = "getMoreResults",
1741         args = {int.class}
1742     )
testGetMoreResultsInt()1743     public void testGetMoreResultsInt() {
1744         /*
1745         } catch (BatchUpdateException e) {
1746             fail("Unexpected Exception "+e.getMessage());
1747         } catch (SQLException e) {
1748             assertEquals("not supported",e.getMessage());
1749         } finally {
1750             try {
1751                 st.close();
1752             } catch (SQLException ee) {
1753             }
1754         }
1755 
1756         try {
1757             st.getMoreResults(Integer.MAX_VALUE);
1758             fail("Exception expected");
1759         } catch (SQLException e) {
1760             //ok
1761         }
1762         */
1763     }
1764 
1765     /**
1766      * @test {@link java.sql.Statement#cancel()}
1767      *
1768      */
1769     @TestTargetNew(
1770         level = TestLevel.COMPLETE,
1771         notes = "Test fails. See also SQLite.DatabaseTest test of interrupt().",
1772         method = "cancel",
1773         args = {}
1774     )
1775     @KnownFailure("Bug in implementation of cancel: Does not fulfill spec.")
testCancel()1776     public void testCancel() {
1777         Statement st = null;
1778         try {
1779             st = conn.prepareStatement("insert into zoo values (7,'Speedy Gonzales','Mouse');");
1780 
1781             CancelThread c = new CancelThread(st);
1782             InsertThread ins = new InsertThread((PreparedStatement)st);
1783 
1784             try {
1785                 ins.t.join();
1786                 c.t.join();
1787             } catch (InterruptedException e) {
1788                 fail("Error in test setup: ");
1789             } catch (Exception e){
1790                 // Insert thread may throw an exception
1791                 // that it could not complete statement
1792             }
1793 
1794             // both threads have terminated and cancel should have cancelled the insert statement.
1795             ResultSet res = st.executeQuery("select * from zoo where id=7");
1796             assertFalse(res.next());
1797 
1798         } catch (SQLException e) {
1799             fail("SQLException is thrown: " + e.getMessage());
1800         }
1801 
1802         try {
1803             st.close();
1804             st.cancel();
1805             fail("Exception expected");
1806         } catch (SQLException e) {
1807             //ok
1808         }
1809     }
1810 
1811     class CancelThread implements Runnable{
1812         Thread t;
1813         Statement stmt;
CancelThread(Statement aSt)1814         CancelThread (Statement aSt) {
1815            this.stmt = aSt;
1816            t = new Thread(this,"Cancel thread");
1817            t.start();
1818         }
run()1819         public void run() {
1820            Logger.global.info("*Cancel* thread started");
1821            try {
1822              Thread.sleep(1500);
1823          } catch (InterruptedException e1) {
1824              fail("Error in test setup");
1825              e1.printStackTrace();
1826          }
1827            try {
1828                Logger.global.info("*Cancel* thread, about to do stmt.cancel()");
1829                stmt.cancel();
1830                Logger.global.info("*Cancel* thread, stmt.cancel() done");
1831            } catch (SQLException e) {
1832                fail("Error in test setup");
1833                e.printStackTrace();
1834            }
1835            Logger.global.info("*Cancel* thread terminated");
1836         }
1837      }
1838 
1839     class InsertThread implements Runnable{
1840         Thread t;
1841         PreparedStatement stmt;
InsertThread(PreparedStatement aSt)1842         InsertThread (PreparedStatement aSt) {
1843            this.stmt = aSt;
1844            t = new Thread(this,"Insert thread");
1845            t.start();
1846         }
run()1847         public void run() {
1848           Logger.global.info("*Insert* thread started");
1849            try {
1850              Thread.sleep(1500);
1851          } catch (InterruptedException e1) {
1852              fail("Error in test setup");
1853              e1.printStackTrace();
1854          }
1855            try {
1856                Logger.global.info("*Insert* thread, about to do insertion");
1857                stmt.execute();
1858                stmt.execute();
1859                Logger.global.info("*Insert* thread inserted");
1860            } catch (SQLException e) {
1861                fail("Error in test setup");
1862                e.printStackTrace();
1863            }
1864           Logger.global.info("*Insert* thread terminated");
1865         }
1866      }
1867 
1868 }
1869