• 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 libcore.java.sql;
18 
19 import java.sql.CallableStatement;
20 import java.sql.Connection;
21 import java.sql.DatabaseMetaData;
22 import java.sql.DriverManager;
23 import java.sql.PreparedStatement;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.sql.SQLWarning;
27 import java.sql.Savepoint;
28 import java.sql.Statement;
29 
30 public final class OldConnectionTest extends OldSQLTest {
31 
testCreateStatement()32     public void testCreateStatement() throws SQLException {
33         Statement statement = conn.createStatement();
34         assertNotNull(statement);
35         //check default values
36         assertEquals(ResultSet.FETCH_UNKNOWN, statement.getFetchDirection());
37         assertNull(statement.getWarnings());
38         assertTrue(statement.getQueryTimeout() > 0);
39         try {
40             conn.close();
41             statement.executeQuery("select * from zoo");
42             fail("SQLException is not thrown after close");
43         } catch (SQLException e) {
44             // expected
45         }
46     }
47 
48     // Scrolling on a forward only RS not allowed. conn.close() does not wrap up
testCreateStatement_int_int()49     public void testCreateStatement_int_int() throws SQLException {
50         Statement st = null;
51         ResultSet rs = null;
52 
53         // test read only
54         try {
55             st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
56                     ResultSet.CONCUR_READ_ONLY);
57             st.execute("select id, name from zoo");
58             rs = st.getResultSet();
59             try {
60                 rs.deleteRow();
61                 fail("Could delete row for READ_ONLY ResultSet");
62             } catch (SQLException sqle) {
63                 // expected
64             }
65         } finally {
66             try {
67                 rs.close();
68                 st.close();
69             } catch (Exception ee) {
70             }
71         }
72 
73         // test forward only: scrolling not allowed
74         try {
75             st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
76                     ResultSet.CONCUR_READ_ONLY);
77             st.execute("select id, name from zoo");
78             rs = st.getResultSet();
79             try {
80                 rs.absolute(1);
81                 rs.previous();
82                 fail("Could scroll backwards");
83             } catch (SQLException sqle) {
84                 // expected
85             }
86         } finally {
87             try {
88                 rs.close();
89                 st.close();
90             } catch (Exception ee) {
91             }
92         }
93 
94         // test forward only
95         try {
96             st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
97                     ResultSet.CONCUR_READ_ONLY);
98             st.execute("select id, name from zoo");
99             rs = st.getResultSet();
100             try {
101                 rs.last();
102                 rs.first();
103                 fail("Could scroll backwards");
104             } catch (SQLException sqle) {
105                 // expected
106             }
107         } finally {
108             try {
109                 rs.close();
110                 st.close();
111             } catch (Exception ee) {
112             }
113         }
114 
115 
116         // test updating ResultSets
117         try {
118             st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
119                     ResultSet.CONCUR_UPDATABLE);
120             st.execute("select name, family from zoo");
121             rs = st.getResultSet();
122             try {
123                 rs.insertRow();
124                 rs.updateObject("family", "bird");
125                 rs.next();
126                 rs.previous();
127                 assertEquals("parrot", (rs.getString(1)));
128                 fail("SQLException was not thrown");
129             } catch (SQLException sqle) {
130                 // expected
131             }
132         } finally {
133             try {
134                 rs.close();
135                 st.close();
136             } catch (Exception ee) {
137             }
138         }
139 
140         try {
141             st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
142                     ResultSet.CONCUR_UPDATABLE);
143             st.execute("select name, family from zoo");
144             rs = st.getResultSet();
145             try {
146                 rs.insertRow();
147                 rs.updateObject("family", "bird");
148                 rs.next();
149                 rs.previous();
150                 assertEquals("bird", (rs.getString(1)));
151                 fail("SQLException was not thrown");
152             } catch (SQLException sqle) {
153                 // expected
154             }
155         } finally {
156             try {
157                 rs.close();
158                 st.close();
159             } catch (Exception ee) {
160             }
161         }
162 
163         conn.close();
164 
165         try {
166             // exception is not specified for this case
167             conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, -1);
168             fail("Illigal arguments: should return exception.");
169         } catch (SQLException sqle) {
170             // expected
171         }
172 
173         try {
174            // exception is not specified for this case
175             conn.createStatement(Integer.MIN_VALUE, ResultSet.CONCUR_READ_ONLY);
176             fail("Illigal arguments: should return exception.");
177         } catch (SQLException sqle) {
178             // expected
179         }
180     }
181 
testCreateStatement_int_int_int()182     public void testCreateStatement_int_int_int() throws SQLException {
183         Statement st = null;
184         try {
185             assertNotNull(conn);
186             st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
187                     ResultSet.CONCUR_READ_ONLY,
188                     ResultSet.HOLD_CURSORS_OVER_COMMIT);
189             assertNotNull(st);
190             st.execute("select id, name from zoo");
191             ResultSet rs = st.getResultSet();
192             rs.next();
193             int pos = rs.getRow();
194             conn.commit();
195             assertEquals("ResultSet cursor position has changed",pos, rs.getRow());
196             rs.close();
197         } finally {
198             try {
199                 if (st != null) st.close();
200             } catch (SQLException ee) {
201             }
202         }
203 
204         try {
205             conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
206                     ResultSet.CONCUR_READ_ONLY, -100);
207             fail("SQLException was not thrown");
208         } catch (SQLException sqle) {
209             //ok
210         }
211     }
212 
213     // known failure: not supported
testCreateStatementIntIntIntNotSupported()214     public void testCreateStatementIntIntIntNotSupported() throws SQLException {
215         Statement st = null;
216         try {
217             st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
218                     ResultSet.CONCUR_READ_ONLY,
219                     ResultSet.CLOSE_CURSORS_AT_COMMIT);
220             assertNotNull(st);
221             st.execute("select id, name from zoo");
222             ResultSet rs = st.getResultSet();
223 
224             try {
225                 rs.close();
226                 fail("SQLException was not thrown");
227             } catch (SQLException sqle) {
228                 // expected
229             }
230         } finally {
231             if (st != null) {
232                 try {
233                     st.close();
234                 } catch (SQLException ee) {
235                 }
236             }
237         }
238     }
239 
240     // conn.close() does not wrap up
testGetMetaData()241     public void testGetMetaData() throws SQLException {
242         DatabaseMetaData md = conn.getMetaData();
243         Connection con = md.getConnection();
244         assertEquals(conn, con);
245 
246         conn.close();
247         try {
248             conn.getMetaData();
249             fail("Exception expected");
250         } catch (SQLException e) {
251             //ok
252         }
253     }
254 
255     // TODO clearWarnings is not supported
testClearWarnings()256     public void testClearWarnings() throws Exception {
257         SQLWarning w = conn.getWarnings();
258         assertNull(w);
259 
260         Statement st = null;
261         try {
262             st = conn.createStatement();
263             st.execute("select animals from zoo");
264             fail("SQLException was not thrown");
265         } catch (SQLException e) {
266             assertNotNull(conn.getWarnings());
267         } finally {
268             try {
269                 st.close();
270             } catch (SQLException ee) {
271             }
272         }
273 
274         conn.clearWarnings();
275         w = conn.getWarnings();
276         assertNull(w);
277 
278         try {
279             st = conn.createStatement();
280             st.execute("select monkey from zoo");
281             fail("SQLException was not thrown");
282         } catch (SQLException e) {
283             assertEquals("SQLite.Exception: error in prepare/compile",e.getMessage());
284         } finally {
285             try {
286                 st.close();
287             } catch (SQLException ee) {
288             }
289         }
290 
291         //Test for correct functionality
292         w = conn.getWarnings();
293         assertNotNull(w);
294 
295         conn.close();
296         try {
297             conn.clearWarnings();
298             fail("Exception expected");
299         } catch (SQLException e) {
300             //ok
301         }
302 
303     }
304 
305 
306     // TODO GetWarnings is not supported: returns null
testGetWarnings()307     public void testGetWarnings() throws Exception {
308         Statement st = null;
309         int errorCode1 = -1;
310         int errorCode2 = -1;
311 
312         try {
313             st = conn.createStatement();
314             st.execute("select animals from zoooo");
315             fail("SQLException was not thrown");
316         } catch (SQLException e) {
317             // expected
318             errorCode1 = e.getErrorCode();
319         }
320 
321         SQLWarning wrs = conn.getWarnings();
322         assertNull(wrs);
323 
324         // tests implementation: but errorcodes need to change too -> change impl.
325         /*
326         Statement st = null;
327         int errorCode1 = -1;
328         int errorCode2 = -1;
329 
330         try {
331             st = conn.createStatement();
332             st.execute("select animals from zoooo");
333             fail("SQLException was not thrown");
334         } catch (SQLException e) {
335             // expected
336             errorCode1 = e.getErrorCode();
337         }
338 
339         try {
340             SQLWarning wrs = conn.getWarnings();
341             assertNotNull(wrs);
342             assertEquals(errorCode1, wrs.getErrorCode());
343             assertNull(wrs.getNextWarning());
344         } catch (Exception e) {
345             fail("Unexpected Exception: " + e.getMessage());
346         }
347         try {
348             st.execute("select horse from zoooooo");
349         } catch (SQLException e) {
350             // expected
351             errorCode2 = e.getErrorCode();
352         }
353 
354         try {
355             SQLWarning wrs = conn.getWarnings();
356             assertEquals(errorCode1, wrs.getErrorCode());
357             assertNotNull(wrs.getNextWarning());
358             assertEquals(errorCode2, wrs.getErrorCode());
359         } catch (Exception e) {
360             fail("Unexpected Exception: " + e.getMessage());
361         }
362 
363         try {
364             st.close();
365         } catch (SQLException ee) {
366         }
367 
368         */
369 
370         conn.close();
371         try {
372             conn.getWarnings();
373             fail("Exception expected");
374         } catch (SQLException e) {
375             //ok
376         }
377     }
378 
testGetAutoCommit()379     public void testGetAutoCommit() throws SQLException {
380         conn.setAutoCommit(true);
381         assertTrue(conn.getAutoCommit());
382         conn.setAutoCommit(false);
383         assertFalse(conn.getAutoCommit());
384         conn.setAutoCommit(true);
385         assertTrue(conn.getAutoCommit());
386     }
387 
388     // conn.close() does not wrap up
testSetAutoCommit()389     public void testSetAutoCommit() throws SQLException {
390         Statement st = null;
391         ResultSet rs = null;
392         ResultSet rs1 = null;
393         try {
394             conn.setAutoCommit(true);
395             st = conn.createStatement();
396             st.execute("insert into zoo (id, name, family) values (3, 'Chichichi', 'monkey');");
397             conn.commit();
398         } catch (SQLException e) {
399             //ok
400         } finally {
401             try {
402                 st.close();
403             } catch (SQLException ee) {
404             }
405         }
406          // even though exception was thrown value is committed
407         try {
408             st = conn.createStatement();
409             st.execute("select * from zoo");
410             rs = st.getResultSet();
411             assertEquals(3, getCount(rs));
412         } finally {
413             try {
414                 st.close();
415             } catch (SQLException ee) {
416             }
417         }
418 
419 
420         try {
421             conn.setAutoCommit(false);
422             st = conn.createStatement();
423             st.execute("insert into zoo (id, name, family) values (4, 'Burenka', 'cow');");
424             st.execute("select * from zoo");
425             rs = st.getResultSet();
426             assertEquals(4, getCount(rs));
427             conn.commit();
428             // Check cursors closed after commit
429             rs1 = st.getResultSet();
430             assertEquals(0, getCount(rs1));
431         } finally {
432             try {
433                 rs.close();
434                 rs1.close();
435                 st.close();
436             } catch (SQLException ee) {
437             }
438         }
439 
440         conn.close();
441 
442         try {
443             conn.setAutoCommit(true);
444             fail("Exception expected");
445         } catch (SQLException e) {
446             //ok
447         }
448     }
449 
450     // conn.close() does not wrap up
testIsReadOnly()451     public void testIsReadOnly() throws SQLException {
452         conn.setReadOnly(true);
453         assertTrue(conn.isReadOnly());
454         conn.setReadOnly(false);
455         assertFalse(conn.isReadOnly());
456 
457         conn.close();
458         try {
459             conn.isReadOnly();
460             fail("Exception expected");
461         } catch (SQLException e) {
462             //ok
463         }
464     }
465 
466     // not supported
testSetReadOnly()467     public void testSetReadOnly() throws SQLException {
468 
469         // Pseudo test: not supported test
470         Statement st = null;
471         try {
472             conn.setReadOnly(true);
473             st = conn.createStatement();
474             st.execute("insert into zoo (id, name, family) values (3, 'ChiChiChi', 'monkey');");
475            // fail("SQLException is not thrown");
476         } finally {
477             try {
478                 st.close();
479             } catch (SQLException ee) {
480             }
481         }
482 
483         // test for correct implementation
484         st = null;
485         try {
486             conn.setReadOnly(true);
487             st = conn.createStatement();
488             st.execute("insert into zoo (id, name, family) values (3, 'ChiChiChi', 'monkey');");
489             fail("SQLException is not thrown");
490         } catch (SQLException sqle) {
491             // expected
492         } finally {
493             try {
494                 st.close();
495             } catch (SQLException ee) {
496             }
497         }
498 
499         try {
500             conn.setReadOnly(true);
501             st = conn.createStatement();
502             st.executeUpdate("insert into zoo (id, name, family) values (4, 'ChaChaCha', 'monkey');");
503             fail("SQLException is not thrown");
504         } catch (SQLException sqle) {
505             // expected
506         } finally {
507             try {
508                 st.close();
509             } catch (SQLException ee) {
510             }
511         }
512 
513         try {
514             conn.setReadOnly(false);
515             st = conn.createStatement();
516             st.execute("insert into zoo (id, name, family) values (4, 'ChiChiChi', 'monkey');");
517         } finally {
518             try {
519                 st.close();
520             } catch (SQLException ee) {
521             }
522         }
523 
524         conn.close();
525         try {
526             conn.setReadOnly(true);
527             fail("Exception expected");
528         } catch (SQLException e) {
529             //ok
530         }
531     }
532 
533     // TODO ResultSet.CLOSE_CURSORS_AT_COMMIT is not supported
testGetHoldability()534     public void testGetHoldability() throws SQLException {
535         conn.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
536         assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, conn.getHoldability());
537 
538         try {
539             conn.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
540             assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, conn
541                     .getHoldability());
542         } catch (SQLException e) {
543             assertEquals("not supported", e.getMessage());
544         }
545 
546         // Exception checking
547 
548         conn.close();
549 
550         try {
551             conn.getHoldability();
552             fail("Could execute statement on closed connection.");
553         } catch (SQLException e) {
554             //ok
555         }
556     }
557 
558     // TODO ResultSet.CLOSE_CURSORS_AT_COMMIT is not supported
testSetHoldability()559     public void testSetHoldability() throws SQLException {
560         Statement st = null;
561         try {
562             conn.setAutoCommit(false);
563             conn.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
564             assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, conn
565                   .getHoldability());
566             st = conn.createStatement();
567             st.execute("insert into zoo (id, name, family) values (4, 'ChiChiChi', 'monkey');");
568             ResultSet rs = st.getResultSet();
569             conn.commit();
570             try {
571                 rs.next();
572             } catch (SQLException sqle) {
573                 //ok
574             }
575             conn.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
576             assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, conn
577                     .getHoldability());
578             st = conn.createStatement();
579             st.execute("insert into zoo (id, name, family) values (4, 'ChiChiChi', 'monkey');");
580             rs = st.getResultSet();
581             conn.commit();
582             rs.next();
583         } finally {
584             try {
585                 st.close();
586             } catch (Exception ee) {
587             }
588         }
589 
590         try {
591             conn.setHoldability(-1);
592             fail("SQLException is not thrown");
593         } catch (SQLException sqle) {
594             // expected
595         }
596     }
597 
598     // TODO only Connection.TRANSACTION_SERIALIZABLE is supported
testGetTransactionIsolation()599     public void testGetTransactionIsolation() throws SQLException {
600         conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
601         assertEquals(Connection.TRANSACTION_READ_UNCOMMITTED, conn
602                 .getTransactionIsolation());
603         conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
604         assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn
605                 .getTransactionIsolation());
606         conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
607        assertEquals(Connection.TRANSACTION_REPEATABLE_READ, conn
608                 .getTransactionIsolation());
609         conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
610         assertEquals(Connection.TRANSACTION_SERIALIZABLE, conn
611                 .getTransactionIsolation());
612 
613         // Exception checking
614 
615         conn.close();
616 
617         try {
618             conn.getTransactionIsolation();
619             fail("Could execute statement on closed connection.");
620         } catch (SQLException e) {
621             //ok
622         }
623     }
624 
625     // TODO only Connection.TRANSACTION_SERIALIZABLE is supported
testGetTransactionIsolationNotSupported()626     public void testGetTransactionIsolationNotSupported() throws SQLException {
627       /*
628       try {
629           conn
630                   .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
631           assertEquals(Connection.TRANSACTION_READ_UNCOMMITTED, conn
632                   .getTransactionIsolation());
633           conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
634           assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn
635                   .getTransactionIsolation());
636           conn
637                   .setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
638          assertEquals(Connection.TRANSACTION_REPEATABLE_READ, conn
639                   .getTransactionIsolation());
640       } catch (SQLException sqle) {
641           fail("SQLException is thrown: " + sqle.toString());
642       }
643       */
644     }
645 
646     // TODO only Connection.TRANSACTION_SERIALIZABLE is supported
testSetTransactionIsolation()647     public void testSetTransactionIsolation() throws SQLException {
648 //            conn
649 //                  .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
650 //            assertEquals(Connection.TRANSACTION_READ_UNCOMMITTED, conn
651 //                    .getTransactionIsolation());
652 //            conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
653 //            assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn
654 //                    .getTransactionIsolation());
655 //            conn
656 //                    .setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
657 //            assertEquals(Connection.TRANSACTION_REPEATABLE_READ, conn
658 //                    .getTransactionIsolation());
659         conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
660         assertEquals(Connection.TRANSACTION_SERIALIZABLE, conn.getTransactionIsolation());
661 
662         try {
663             conn.setTransactionIsolation(0);
664             fail("SQLException is not thrown");
665         } catch (SQLException sqle) {
666             // expected
667         }
668     }
669 
670     // TODO setCatalog method does nothing: Hint default catalog sqlite_master.
testSetCatalog()671     public void testSetCatalog() throws SQLException {
672         String[] catalogs = { "test", "test1", "test" };
673         Statement st = null;
674         try {
675             for (int i = 0; i < catalogs.length; i++) {
676                 conn.setCatalog(catalogs[i]);
677                 assertNull(catalogs[i], conn.getCatalog());
678                 st = conn.createStatement();
679                 st
680                         .equals("create table test_table (id integer not null, name varchar(20), primary key(id));");
681                 st.equals("drop table test_table;");
682 
683             }
684         } finally {
685             try {
686                 st.close();
687             } catch (Exception ee) {
688             }
689         }
690 
691         /*
692         String[] catalogs = { "test"};
693         Statement st = null;
694         try {
695             for (int i = 0; i < catalogs.length; i++) {
696                 conn.setCatalog(catalogs[i]);
697                 fail("illegal catalog name");
698                 assertEquals(catalogs[i], conn.getCatalog());
699                 st = conn.createStatement();
700                 st
701                         .equals("create table test_table (id integer not null, name varchar(20), primary key(id));");
702                 st.equals("drop table test_table;");
703             }
704         } catch (SQLException sqle) {
705             System.out.println("TODO: Test for correct error message: name with ,\"sqlite_\" prefix expected");
706         } finally {
707             try {
708                 st.close();
709             } catch (Exception ee) {
710             }
711         }
712 
713         String[] catalogs = { "sqlite_test", "sqlite_test1", "sqlite_test" };
714         Statement st = null;
715         try {
716             for (int i = 0; i < catalogs.length; i++) {
717                 conn.setCatalog(catalogs[i]);
718                 assertEquals(catalogs[i], conn.getCatalog());
719                 st = conn.createStatement();
720                 st
721                         .equals("create table test_table (id integer not null, name varchar(20), primary key(id));");
722                 st.equals("drop table test_table;");
723 
724             }
725         } catch (SQLException sqle) {
726             fail("SQLException is thrown");
727         } finally {
728             try {
729                 st.close();
730             } catch (Exception ee) {
731             }
732         }
733 
734         try {
735             conn.setCatalog(null);
736             fail("SQLException is not thrown");
737         } catch (SQLException e) {
738             // expected
739         }
740 
741         try {
742             conn.setCatalog("not_exist");
743             fail("SQLException is not thrown");
744         } catch (SQLException e) {
745             // expected
746         }
747         */
748     }
749 
750     // not supported
testGetCatalog()751     public void testGetCatalog() throws SQLException {
752         // test default catalog
753         assertEquals("sqlite_master", conn.getCatalog());
754 
755         String[] catalogs = { "sqlite_test", "sqlite_test1", "sqlite_test" };
756         Statement st = null;
757         for (int i = 0; i < catalogs.length; i++) {
758             conn.setCatalog(catalogs[i]);
759             assertNull(conn.getCatalog());
760         }
761 
762         // Exception checking
763 
764         conn.close();
765 
766         try {
767             conn.getCatalog();
768             fail("Could execute statement on closed connection.");
769         } catch (SQLException e) {
770             //ok
771         }
772     }
773 
774     // TODO setTypeMap is not supported
testSetTypeMap()775     public void testSetTypeMap() {
776         /*
777         try {
778             java.util.Map map = conn.getTypeMap();
779             map
780                     .put(
781                             "org.apache.harmony.sql.tests.java.sql.TestHelper_Connection1",
782                             Class.forName("TestHelper_Connection1"));
783             conn.setTypeMap(map);
784             assertEquals(map, conn.getTypeMap());
785         } catch (SQLException sqle) {
786             //ok
787         } catch (Exception e) {
788             fail("Unexpected Exception " + e.getMessage());
789         }
790 
791         try {
792             conn.setTypeMap(null);
793             fail("SQLException is not thrown");
794         } catch (SQLException e) {
795             // expected
796         }
797         */
798     }
799 
800     // TODO getTypeMap is not supported
testGetTypeMap()801     public void testGetTypeMap() throws SQLException {
802         /*
803         try {
804             java.util.Map map = conn.getTypeMap();
805             map
806                     .put(
807                             "org.apache.harmony.sql.tests.java.sql.TestHelper_Connection1",
808                             Class.forName("TestHelper_Connection1"));
809             conn.setTypeMap(map);
810             assertEquals(map, conn.getTypeMap());
811         } catch (SQLException sqle) {
812             //ok
813         } catch (Exception e) {
814             fail("Unexpected Exception " + e.getMessage());
815         }
816 
817 // Exception checking
818 
819         conn.close();
820 
821         try {
822             conn.setTypeMap(null);
823             fail("Could execute statement on closed connection.");
824         } catch (SQLException e) {
825             //ok
826         }
827         */
828     }
829 
830     // TODO nativeSQL is not supported
testNativeSQL()831     public void testNativeSQL() throws SQLException{
832         String[] queries = {
833                 "select * from zoo;",
834                 "insert into zoo (id, name, family) values (3, 'Chichichi', 'monkey');",
835                 "create table zoo_office(id integer not null, name varchar(20), primary key(id));",
836                 "drop table zoo_office;" };
837         String[] native_queries = {
838                 "select * from zoo;",
839                 "insert into zoo (id, name, family) values (3, 'Chichichi', 'monkey');",
840                 "create table zoo_office(id integer not null, name varchar(20), primary key(id));",
841                 "drop table zoo_office;" };
842         Statement st = null;
843         String nativeQuery = "";
844         try {
845             for (int i = 0; i < queries.length; i++) {
846                 nativeQuery = conn.nativeSQL(queries[i]);
847                 assertEquals(native_queries[i], nativeQuery);
848                 st = conn.createStatement();
849                 st.execute(nativeQuery);
850             }
851         } catch (SQLException sqle) {
852             //ok
853         } finally {
854             try {
855                 st.close();
856             } catch (Exception ee) {
857             }
858         }
859 
860         String[] inc_queries = { "", "  ", "not query" };
861         for (int i = 0; i < inc_queries.length; i++) {
862             try {
863                 nativeQuery = conn.nativeSQL(inc_queries[i]);
864                 assertEquals(inc_queries[i], nativeQuery);
865             } catch (SQLException e) {
866                 assertEquals("not supported",e.getMessage());
867             }
868         }
869 
870         // Exception checking
871 
872         conn.close();
873 
874         try {
875             conn.nativeSQL(inc_queries[0]);
876             fail("Could execute statement on closed connection.");
877         } catch (SQLException e) {
878             //ok
879         }
880     }
881 
882     // TODO prepareCall is not supported
testPrepareCall()883     public void testPrepareCall() throws SQLException {
884         CallableStatement cstmt = null;
885         ResultSet rs = null;
886         ResultSet rs1 = null;
887         Statement st = null;
888         Statement st1 = null;
889         try {
890             cstmt = conn.prepareCall("call welcomeAnimal(3, 'Petya', 'Cock')");
891             st = conn.createStatement();
892             st.execute("select * from zoo");
893             rs = st.getResultSet();
894             assertEquals(2, getCount(rs));
895             cstmt.execute();
896             st1 = conn.createStatement();
897             st1.execute("select * from zoo");
898             rs1 = st1.getResultSet();
899             assertEquals(3, getCount(rs1));
900         } catch (SQLException e) {
901             //ok not supported
902         } finally {
903             try {
904                 st.close();
905                 st1.close();
906                 rs.close();
907                 rs1.close();
908                 cstmt.close();
909             } catch (Exception ee) {
910             }
911         }
912 
913 
914         try {
915             conn.prepareCall("welcomeAnimal(4, 'Petya', 'Cock')");
916             fail("SQL Exception is not thrown");
917         } catch (SQLException e) {
918             // expected
919         }
920 
921         try {
922             conn.prepareCall(null);
923             fail("SQL Exception is not thrown");
924         } catch (SQLException e) {
925             // expected
926         }
927 
928         // Exception checking
929 
930         conn.close();
931 
932         try {
933             conn.prepareCall("");
934             fail("Could execute statement on closed connection.");
935         } catch (SQLException e) {
936             //ok
937         }
938 
939     }
940 
941     // TODO prepareCall is not supported
testPrepareCall_String_int_int()942     public void testPrepareCall_String_int_int() {
943         CallableStatement cstmt = null;
944         ResultSet rs = null;
945 
946         try {
947             String query = "call welcomeAnimal(3, 'Petya', 'Cock')";
948             cstmt = conn.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY,
949                     ResultSet.CONCUR_READ_ONLY);
950         } catch (SQLException e) {
951             //ok
952         }
953 
954         /*
955         try {
956             String query = "call welcomeAnimal(3, 'Petya', 'Dino')";
957             cstmt = conn.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY,
958                     ResultSet.CONCUR_READ_ONLY);
959             cstmt.execute("select id, name from zoo");
960             rs = cstmt.getResultSet();
961             try {
962                 rs.deleteRow();
963                 fail("Can delete row for READ_ONLY ResultSet");
964             } catch (SQLException sqle) {
965                 // expected
966             }
967 
968             try {
969                 rs.absolute(0);
970                 fail("Can move cursor to the last position for TYPE_FORWARD_ONLY ResultSet");
971             } catch (SQLException sqle) {
972                 // expected
973             }
974 
975         } catch (SQLException e) {
976             fail("SQLException was thrown: " + e.getMessage());
977         } finally {
978             try {
979                 rs.close();
980                 cstmt.close();
981             } catch (Exception ee) {
982             }
983         }
984         Statement st = null;
985         try {
986             st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
987                     ResultSet.CONCUR_UPDATABLE);
988             st.execute("select name, family from zoo");
989             rs = st.getResultSet();
990             try {
991                 rs.insertRow();
992                 rs.updateObject("family", "bird");
993                 rs.next();
994                 rs.previous();
995                 assertEquals("parrot", (rs.getString(1)));
996                 fail("SQLException was not thrown");
997             } catch (SQLException sqle) {
998                 // expected
999             }
1000 
1001         } catch (SQLException e) {
1002             fail("SQLException was thrown: " + e.getMessage());
1003         } finally {
1004             try {
1005                 rs.close();
1006                 st.close();
1007             } catch (SQLException ee) {
1008             }
1009         }
1010 
1011         try {
1012             st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1013                     ResultSet.CONCUR_UPDATABLE);
1014             st.execute("select name, family from zoo");
1015             rs = st.getResultSet();
1016             try {
1017                 rs.insertRow();
1018                 rs.updateObject("family", "bird");
1019                 rs.next();
1020                 rs.previous();
1021                 assertEquals("bird", (rs.getString(1)));
1022                 fail("SQLException was not thrown");
1023             } catch (SQLException sqle) {
1024                 // expected
1025             }
1026 
1027         } catch (SQLException e) {
1028             fail("SQLException was thrown: " + e.getMessage());
1029         } finally {
1030             try {
1031                 rs.close();
1032                 st.close();
1033             } catch (SQLException ee) {
1034             }
1035         }
1036 
1037         try {
1038             conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, -1);
1039             fail("SQLException was not thrown");
1040         } catch (SQLException sqle) {
1041             // expected
1042         }
1043 
1044         try {
1045             conn.createStatement(Integer.MIN_VALUE, ResultSet.CONCUR_READ_ONLY);
1046             fail("SQLException was not thrown");
1047         } catch (SQLException sqle) {
1048             // expected
1049         }
1050 
1051         */
1052     }
1053 
1054     // TODO prepareCall is not supported
testPrepareCall_String_int_int_int()1055     public void testPrepareCall_String_int_int_int() {
1056         CallableStatement cstmt = null;
1057         ResultSet rs = null;
1058 
1059         try {
1060             String query = "call welcomeAnimal(?, ?, ?)";
1061             cstmt = conn.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY,
1062                     ResultSet.CONCUR_READ_ONLY,
1063                     ResultSet.HOLD_CURSORS_OVER_COMMIT);
1064         } catch (SQLException e) {
1065             //ok
1066         }
1067         /*
1068         try {
1069             String query = "call welcomeAnimal(?, ?, ?)";
1070             cstmt = conn.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY,
1071                     ResultSet.CONCUR_READ_ONLY,
1072                     ResultSet.HOLD_CURSORS_OVER_COMMIT);
1073             cstmt.setInt(1, 3);
1074             cstmt.setString(2, "Petya");
1075             cstmt.setString(3, "Cock");
1076             cstmt.execute("select id, name from zoo");
1077             rs = cstmt.getResultSet();
1078             try {
1079                 rs.close();
1080                 fail("SQLException was not thrown");
1081             } catch (SQLException sqle) {
1082                 fail("Unexpected exception was thrown during closing ResultSet");
1083             }
1084         } catch (SQLException e) {
1085             fail("SQLException was thrown: " + e.getMessage());
1086         } finally {
1087             try {
1088                 rs.close();
1089                 cstmt.close();
1090             } catch (Exception ee) {
1091             }
1092         }
1093 
1094         Statement st = null;
1095 
1096         try {
1097             st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
1098                     ResultSet.CONCUR_READ_ONLY,
1099                     ResultSet.CLOSE_CURSORS_AT_COMMIT);
1100             st.execute("select id, name from zoo");
1101             rs = st.getResultSet();
1102             try {
1103                 rs.close();
1104                 fail("SQLException was not thrown");
1105             } catch (SQLException sqle) {
1106                 // expected
1107             }
1108         } catch (SQLException e) {
1109             fail("SQLException was thrown: " + e.getMessage());
1110         }
1111 
1112         try {
1113             conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
1114                     ResultSet.CONCUR_READ_ONLY, -100);
1115             fail("SQLException was not thrown");
1116         } catch (SQLException sqle) {
1117             // expected
1118         }
1119         */
1120 
1121     }
1122 
testPrepareStatement()1123     public void testPrepareStatement() throws SQLException {
1124         PreparedStatement prst = null;
1125         Statement st = null;
1126         ResultSet rs = null;
1127         ResultSet rs1 = null;
1128         try {
1129             String update = "update zoo set family = ? where name = ?;";
1130             prst = conn.prepareStatement(update);
1131             prst.setString(1, "cat");
1132             prst.setString(2, "Yasha");
1133             st = conn.createStatement();
1134             st.execute("select * from zoo where family = 'cat'");
1135             rs = st.getResultSet();
1136             assertEquals(0, getCount(rs));
1137             prst.executeUpdate();
1138             st.execute("select * from zoo where family = 'cat'");
1139             rs1 = st.getResultSet();
1140             assertEquals(1, getCount(rs1));
1141         } finally {
1142             try {
1143                 rs.close();
1144                 rs1.close();
1145                 prst.close();
1146                 st.close();
1147             } catch (SQLException ee) {
1148             }
1149         }
1150 
1151         try {
1152             prst = conn.prepareStatement("");
1153             prst.execute();
1154             fail("SQLException is not thrown");
1155         } catch (SQLException e) {
1156             //ok
1157         }
1158 
1159         try {
1160             conn.prepareStatement(null);
1161             fail("SQLException is not thrown");
1162         } catch (Exception e) {
1163             //ok
1164         }
1165     }
1166 
1167     // TODO Crashes VM. Fix later.
testPrepareStatement_String_int()1168     public void testPrepareStatement_String_int() throws SQLException {
1169         PreparedStatement prst = null;
1170         PreparedStatement prst1 = null;
1171         Statement st = null;
1172         ResultSet rs = null;
1173         ResultSet rs1 = null;
1174         ResultSet rs4 = null;
1175         ResultSet rs5 = null;
1176 
1177 
1178         try {
1179             String insert = "insert into zoo (id, name, family) values (?, ?, ?);";
1180             prst = conn.prepareStatement(insert,
1181                     Statement.RETURN_GENERATED_KEYS);
1182             fail("Fail: prepareStatement does not fail");
1183         } catch (SQLException e) {
1184           //ok not supported
1185         }
1186 
1187         try {
1188             String insert = "insert into zoo (id, name, family) values (?, ?, ?);";
1189 
1190             prst = conn.prepareStatement(insert,
1191                     Statement.NO_GENERATED_KEYS);
1192             prst.setInt(1, 8);
1193             prst.setString(2, "Tuzik");
1194             prst.setString(3, "dog");
1195             st = conn.createStatement();
1196             st.execute("select * from zoo");
1197             rs = st.getResultSet();
1198             assertEquals(2, getCount(rs));
1199             prst.execute();
1200             st.execute("select * from zoo where family = 'dog'");
1201             rs1 = st.getResultSet();
1202             assertEquals(1, getCount(rs1));
1203 //          TODO getGeneratedKeys is not supported
1204             rs4 = prst.getGeneratedKeys();
1205             assertEquals(0, getCount(rs4));
1206 
1207             prst1 = conn.prepareStatement(insert, Statement.RETURN_GENERATED_KEYS);
1208             prst1.setInt(1, 5);
1209             prst1.setString(2, "Layka");
1210             prst1.setString(3, "dog");
1211 
1212             prst1.execute();
1213 
1214             rs5 = prst1.getGeneratedKeys();
1215             assertEquals(0, getCount(rs5));
1216         } finally {
1217             try {
1218                 rs.close();
1219                 rs1.close();
1220                 prst.close();
1221                 st.close();
1222             } catch (Exception ee) {
1223             }
1224         }
1225     }
1226 
testCommit()1227     public void testCommit() throws SQLException {
1228         Statement st = null;
1229         Statement st1 = null;
1230         Statement st2 = null;
1231         Statement st3 = null;
1232         Statement st4 = null;
1233         ResultSet rs1 = null;
1234         ResultSet rs2 = null;
1235         ResultSet rs3 = null;
1236         ResultSet rs4 = null;
1237         try {
1238             conn.setAutoCommit(false);
1239 
1240             st = conn.createStatement();
1241             st.execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');");
1242             st.execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');");
1243 
1244             st1 = conn.createStatement();
1245             st1.execute("select * from zoo");
1246             rs1 = st1.getResultSet();
1247             assertEquals(4, getCount(rs1));
1248             try {
1249                 conn.commit();
1250                 st2 = conn.createStatement();
1251                 st2.execute("select * from zoo");
1252                 rs2 = st2.getResultSet();
1253                 assertEquals(4, getCount(rs2));
1254             } finally {
1255                 try {
1256                     rs2.close();
1257                     st2.close();
1258                 } catch (SQLException ee) {
1259                 }
1260             }
1261 
1262             try {
1263                 st3 = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
1264                         ResultSet.CONCUR_READ_ONLY,
1265                         ResultSet.HOLD_CURSORS_OVER_COMMIT);
1266                 st3.execute("select * from zoo");
1267                 rs3 = st3.getResultSet();
1268                 conn.commit();
1269                 assertEquals(4, getCount(rs3));
1270             } finally {
1271                 try {
1272                     if (rs3 != null) rs3.close();
1273                     if (st3 != null) st3.close();
1274                 } catch (SQLException ee) {
1275                 }
1276             }
1277         } finally {
1278             try {
1279                 rs1.close();
1280                 st.close();
1281                 st1.close();
1282             } catch (Exception ee) {
1283             }
1284         }
1285     }
1286 
testRollback()1287     public void testRollback() throws SQLException {
1288         Statement st = null;
1289         Statement st1 = null;
1290         ResultSet rs1 = null;
1291         ResultSet rs2 = null;
1292         ResultSet rs3 = null;
1293 
1294         try {
1295             conn.setAutoCommit(false);
1296             st = conn.createStatement();
1297             st.execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');");
1298             st.execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');");
1299             conn.rollback();
1300             st1 = conn.createStatement();
1301             st1.execute("select * from zoo");
1302             rs1 = st1.getResultSet();
1303             assertEquals("Rollback was ineffective",2, getCount(rs1));
1304         } finally {
1305             conn.setAutoCommit(true);
1306             try {
1307                 st.close();
1308                 st1.close();
1309                 rs1.close();
1310             } catch (SQLException ee) {
1311             }
1312         }
1313         try {
1314             conn.setAutoCommit(false);
1315             st = conn.createStatement();
1316             st.execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');");
1317             st.execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');");
1318 
1319             if (!conn.getAutoCommit()) {
1320                 st1 = conn.createStatement();
1321                 st1.execute("select * from zoo");
1322                 rs1 = st1.getResultSet();
1323                 assertEquals(4, getCount(rs1));
1324                 Statement st2 = null;
1325                 Statement st3 = null;
1326                 try {
1327                     conn.commit();
1328                     st2 = conn.createStatement();
1329                     st2.execute("select * from zoo");
1330                     rs2 = st2.getResultSet();
1331                     assertEquals(4, getCount(rs2));
1332                     // rollback after commit ineffective
1333                     conn.rollback();
1334                     st3 = conn.createStatement();
1335                     st3.execute("select * from zoo");
1336                     rs3 = st3.getResultSet();
1337                     assertEquals(4, getCount(rs3));
1338                 } finally {
1339                     conn.setAutoCommit(true);
1340                     try {
1341                         rs2.close();
1342                         rs3.close();
1343                         st2.close();
1344                         st3.close();
1345                     } catch (SQLException ee) {
1346                     }
1347                 }
1348             } else {
1349                 fail("Error in test setup: cannot turn autocommit off.");
1350             }
1351         } finally {
1352             try {
1353                 st.close();
1354                 st1.close();
1355                 rs1.close();
1356             } catch (SQLException ee) {
1357             }
1358         }
1359 
1360         conn.close();
1361         try {
1362             conn.rollback();
1363             fail("SQLException expected");
1364         } catch (SQLException e) {
1365             // ok
1366         }
1367     }
1368 
1369     // TODO setSavepoint is not supported
testSetSavepoint()1370     public void testSetSavepoint() throws SQLException {
1371         conn.setAutoCommit(false);
1372 
1373         try {
1374             Savepoint sp = conn.setSavepoint();
1375         } catch (SQLException e) {
1376             // ok not supported
1377         }
1378 
1379 
1380         //Complete test but: not supported exception is thrown
1381         /*
1382         try {
1383             conn.setAutoCommit(false);
1384 
1385             st = conn.createStatement();
1386             st
1387                     .execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');");
1388             st
1389                     .execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');");
1390 
1391             if (!conn.getAutoCommit()) {
1392                 st1 = conn.createStatement();
1393                 st1.execute("select * from zoo");
1394                 rs1 = st1.getResultSet();
1395                 assertEquals(4, getCount(rs1));
1396                 Statement st2 = null;
1397                 ResultSet rs2 = null;
1398                 try {
1399                     Savepoint sp = conn.setSavepoint();
1400                     st
1401                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1402                     conn.rollback(sp);
1403                     st2 = conn.createStatement();
1404                     st2.execute("select * from zoo");
1405                     rs2 = st2.getResultSet();
1406                     assertEquals(4, getCount(rs2));
1407                 } catch (SQLException e) {
1408                     fail("SQLException is thrown: " + e.toString());
1409                 } finally {
1410                     try {
1411                         rs2.close();
1412                         st2.close();
1413                     } catch (Exception ee) {
1414                     }
1415                 }
1416 
1417                 try {
1418                     Savepoint sp1 = conn.setSavepoint();
1419                     assertNotNull(sp1);
1420                     st
1421                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1422                     Savepoint sp2 = conn.setSavepoint();
1423                     assertNotNull(sp2);
1424                     st
1425                             .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');");
1426                     conn.rollback(sp1);
1427                     st2 = conn.createStatement();
1428                     st2.execute("select * from zoo");
1429                     rs2 = st2.getResultSet();
1430                     assertEquals(4, getCount(rs2));
1431                 } catch (SQLException e) {
1432                     fail("SQLException is thrown: " + e.toString());
1433                 } finally {
1434                     try {
1435                         rs2.close();
1436                         st2.close();
1437                     } catch (SQLException ee) {
1438                     }
1439                 }
1440 
1441                 try {
1442                     Savepoint sp1 = conn.setSavepoint();
1443                     assertNotNull(sp1);
1444                     st
1445                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1446                     Savepoint sp2 = conn.setSavepoint();
1447                     assertNotNull(sp2);
1448                     st
1449                             .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');");
1450                     conn.rollback();
1451                     st2 = conn.createStatement();
1452                     st2.execute("select * from zoo");
1453                     rs2 = st2.getResultSet();
1454                     assertEquals(4, getCount(rs2));
1455                 } catch (SQLException e) {
1456                     fail("SQLException is thrown: " + e.toString());
1457                 } finally {
1458                     try {
1459                         rs2.close();
1460                         st2.close();
1461                     } catch (SQLException ee) {
1462                     }
1463                 }
1464 
1465             } else {
1466                 st1 = conn.createStatement();
1467                 st1.execute("select * from zoo");
1468                 rs1 = st1.getResultSet();
1469                 assertEquals(4, getCount(rs1));
1470                 try {
1471                     Savepoint sp = conn.setSavepoint();
1472                     st
1473                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1474                     conn.rollback(sp);
1475                     fail("SQLException is not thrown");
1476                 } catch (SQLException sqle) {
1477                     // expected
1478                 }
1479             }
1480         } catch (SQLException sqle) {
1481             fail("SQLException is thrown: " + sqle.toString());
1482         } finally {
1483             try {
1484                 rs1.close();
1485                 st.close();
1486                 st1.close();
1487             } catch (SQLException ee) {
1488             }
1489         }
1490         */
1491     }
1492 
1493     // TODO setSavepoint is not supported
testSetSavepoint_String()1494     public void testSetSavepoint_String() throws SQLException {
1495         String testSavepoint = "testSavepoint";
1496         conn.setAutoCommit(false);
1497 
1498         try {
1499             Savepoint sp = conn.setSavepoint(testSavepoint);
1500         } catch (SQLException e) {
1501             // ok not supported
1502         }
1503 
1504     /*
1505         Statement st = null;
1506         Statement st1 = null;
1507         ResultSet rs1 = null;
1508         try {
1509             conn.setAutoCommit(false);
1510 
1511             st = conn.createStatement();
1512             st
1513                     .execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');");
1514             st
1515                     .execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');");
1516 
1517             if (!conn.getAutoCommit()) {
1518                 st1 = conn.createStatement();
1519                 st1.execute("select * from zoo");
1520                 rs1 = st1.getResultSet();
1521                 assertEquals(4, getCount(rs1));
1522                 Statement st2 = null;
1523                 ResultSet rs2 = null;
1524                 try {
1525                     Savepoint sp = conn.setSavepoint("one");
1526                     st
1527                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1528                     conn.rollback(sp);
1529                     st2 = conn.createStatement();
1530                     st2.execute("select * from zoo");
1531                     rs2 = st2.getResultSet();
1532                     assertEquals(4, getCount(rs2));
1533                 } catch (SQLException e) {
1534                     fail("SQLException is thrown: " + e.toString());
1535                 } finally {
1536                     try {
1537                         rs2.close();
1538                         st2.close();
1539                     } catch (Exception ee) {
1540                     }
1541                 }
1542 
1543                 try {
1544                     Savepoint sp1 = conn.setSavepoint("one");
1545                     st
1546                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1547                     Savepoint sp2 = conn.setSavepoint("two");
1548                     st
1549                             .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');");
1550                     conn.rollback(sp1);
1551                     st2 = conn.createStatement();
1552                     st2.execute("select * from zoo");
1553                     rs2 = st2.getResultSet();
1554                     assertEquals(4, getCount(rs2));
1555                 } catch (SQLException e) {
1556                     fail("SQLException is thrown: " + e.toString());
1557                 } finally {
1558                     try {
1559                         rs2.close();
1560                         st2.close();
1561                     } catch (SQLException ee) {
1562                     }
1563                 }
1564 
1565                 try {
1566                     Savepoint sp1 = conn.setSavepoint("three");
1567                     st
1568                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1569                     Savepoint sp2 = conn.setSavepoint("four");
1570                     st
1571                             .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');");
1572                     conn.rollback();
1573                     st2 = conn.createStatement();
1574                     st2.execute("select * from zoo");
1575                     rs2 = st2.getResultSet();
1576                     assertEquals(4, getCount(rs2));
1577                 } catch (SQLException e) {
1578                     fail("SQLException is thrown: " + e.toString());
1579                 } finally {
1580                     try {
1581                         rs2.close();
1582                         st2.close();
1583                     } catch (SQLException ee) {
1584                     }
1585                 }
1586 
1587             } else {
1588                 st1 = conn.createStatement();
1589                 st1.execute("select * from zoo");
1590                 rs1 = st1.getResultSet();
1591                 assertEquals(4, getCount(rs1));
1592                 try {
1593                     Savepoint sp = conn.setSavepoint("five");
1594                     st
1595                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1596                     conn.rollback(sp);
1597                     fail("SQLException is not thrown");
1598                 } catch (SQLException sqle) {
1599                     // expected
1600                 }
1601             }
1602         } catch (SQLException sqle) {
1603             fail("SQLException is thrown: " + sqle.toString());
1604         } finally {
1605             try {
1606                 rs1.close();
1607                 st.close();
1608                 st1.close();
1609             } catch (SQLException ee) {
1610             }
1611         }
1612         */
1613     }
1614 
1615     // TODO Savepoint is not supported
testRollback_Savepoint()1616     public void testRollback_Savepoint() throws SQLException {
1617         Savepoint sp = new DummySavePoint();
1618         conn.setAutoCommit(false);
1619 
1620         try {
1621             conn.rollback(sp);
1622         } catch (SQLException e) {
1623             //ok
1624         }
1625         /*
1626         Statement st = null;
1627         Statement st1 = null;
1628         ResultSet rs1 = null;
1629         try {
1630             conn.setAutoCommit(false);
1631 
1632             st = conn.createStatement();
1633             st
1634                     .execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');");
1635             st
1636                     .execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');");
1637 
1638             if (!conn.getAutoCommit()) {
1639                 st1 = conn.createStatement();
1640                 st1.execute("select * from zoo");
1641                 rs1 = st1.getResultSet();
1642                 assertEquals(4, getCount(rs1));
1643                 Statement st2 = null;
1644                 ResultSet rs2 = null;
1645                 try {
1646                     Savepoint sp = conn.setSavepoint("one");
1647                     st
1648                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1649                     conn.rollback(sp);
1650                     st2 = conn.createStatement();
1651                     st2.execute("select * from zoo");
1652                     rs2 = st2.getResultSet();
1653                     assertEquals(4, getCount(rs2));
1654                 } catch (SQLException e) {
1655                     fail("SQLException is thrown: " + e.toString());
1656                 } finally {
1657                     try {
1658                         rs2.close();
1659                         st2.close();
1660                     } catch (Exception ee) {
1661                     }
1662                 }
1663 
1664                 try {
1665                     Savepoint sp1 = conn.setSavepoint("one");
1666                     st
1667                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1668                     Savepoint sp2 = conn.setSavepoint("two");
1669                     st
1670                             .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');");
1671                     conn.rollback(sp1);
1672                     st2 = conn.createStatement();
1673                     st2.execute("select * from zoo");
1674                     rs2 = st2.getResultSet();
1675                     assertEquals(4, getCount(rs2));
1676                 } catch (SQLException e) {
1677                     fail("SQLException is thrown: " + e.toString());
1678                 } finally {
1679                     try {
1680                         rs2.close();
1681                         st2.close();
1682                     } catch (SQLException ee) {
1683                     }
1684                 }
1685 
1686                 try {
1687                     Savepoint sp1 = conn.setSavepoint("three");
1688                     st
1689                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1690                     Savepoint sp2 = conn.setSavepoint("four");
1691                     st
1692                             .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');");
1693                     conn.rollback();
1694                     st2 = conn.createStatement();
1695                     st2.execute("select * from zoo");
1696                     rs2 = st2.getResultSet();
1697                     assertEquals(4, getCount(rs2));
1698                 } catch (SQLException e) {
1699                     fail("SQLException is thrown: " + e.toString());
1700                 } finally {
1701                     try {
1702                         rs2.close();
1703                         st2.close();
1704                     } catch (SQLException ee) {
1705                     }
1706                 }
1707 
1708             } else {
1709                 st1 = conn.createStatement();
1710                 st1.execute("select * from zoo");
1711                 rs1 = st1.getResultSet();
1712                 assertEquals(4, getCount(rs1));
1713                 try {
1714                     Savepoint sp = conn.setSavepoint("five");
1715                     st
1716                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1717                     conn.rollback(sp);
1718                     fail("SQLException is not thrown");
1719                 } catch (SQLException sqle) {
1720                     // expected
1721                 }
1722             }
1723         } catch (SQLException sqle) {
1724             fail("SQLException is thrown: " + sqle.toString());
1725         } finally {
1726             try {
1727                 rs1.close();
1728                 st.close();
1729                 st1.close();
1730             } catch (SQLException ee) {
1731             }
1732         }
1733         */
1734     }
1735 
1736     // TODO Savepoint is not supported
testReleaseSavepoint_Savepoint()1737     public void testReleaseSavepoint_Savepoint() throws SQLException {
1738         Savepoint sp = new DummySavePoint();
1739         conn.setAutoCommit(false);
1740 
1741         try {
1742             conn.releaseSavepoint(sp);
1743         } catch (SQLException e) {
1744             //ok
1745         }
1746         /*
1747 
1748         Statement st = null;
1749         Statement st1 = null;
1750         ResultSet rs1 = null;
1751         try {
1752             conn.setAutoCommit(false);
1753 
1754             st = conn.createStatement();
1755             st
1756                     .execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');");
1757             st
1758                     .execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');");
1759 
1760             if (!conn.getAutoCommit()) {
1761                 st1 = conn.createStatement();
1762                 st1.execute("select * from zoo");
1763                 rs1 = st1.getResultSet();
1764                 assertEquals(4, getCount(rs1));
1765                 Statement st2 = null;
1766                 ResultSet rs2 = null;
1767                 try {
1768                     Savepoint sp = conn.setSavepoint("one");
1769                     st
1770                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1771                     conn.rollback(sp);
1772                     st2 = conn.createStatement();
1773                     st2.execute("select * from zoo");
1774                     rs2 = st2.getResultSet();
1775                     assertEquals(4, getCount(rs2));
1776                     st
1777                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1778                     conn.releaseSavepoint(sp);
1779                     try {
1780                         conn.rollback(sp);
1781                         fail("SQLException is not thrown");
1782                     } catch (SQLException sqle) {
1783                         // expected
1784                     }
1785                     conn.rollback();
1786                 } catch (SQLException e) {
1787                     fail("SQLException is thrown: " + e.toString());
1788                 } finally {
1789                     try {
1790                         rs2.close();
1791                         st2.close();
1792                     } catch (Exception ee) {
1793                     }
1794                 }
1795 
1796                 try {
1797                     Savepoint sp1 = conn.setSavepoint("one");
1798                     st
1799                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1800                     Savepoint sp2 = conn.setSavepoint("two");
1801                     st
1802                             .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');");
1803                     conn.releaseSavepoint(sp1);
1804                     try {
1805                         conn.rollback(sp1);
1806                         fail("SQLException is not thrown");
1807                     } catch (SQLException sqle) {
1808                         // expected
1809                     }
1810                     conn.commit();
1811                     conn.rollback(sp2);
1812                     st2 = conn.createStatement();
1813                     st2.execute("select * from zoo");
1814                     rs2 = st2.getResultSet();
1815                     assertEquals(4, getCount(rs2));
1816                 } catch (SQLException e) {
1817                     fail("SQLException is thrown: " + e.toString());
1818                 } finally {
1819                     try {
1820                         rs2.close();
1821                         st2.close();
1822                     } catch (SQLException ee) {
1823                     }
1824                 }
1825 
1826             } else {
1827                 st1 = conn.createStatement();
1828                 st1.execute("select * from zoo");
1829                 rs1 = st1.getResultSet();
1830                 assertEquals(4, getCount(rs1));
1831                 try {
1832                     Savepoint sp = conn.setSavepoint("five");
1833                     st
1834                             .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');");
1835                     conn.releaseSavepoint(sp);
1836                     fail("SQLException is not thrown");
1837                 } catch (SQLException sqle) {
1838                     // expected
1839                 }
1840             }
1841         } catch (SQLException sqle) {
1842             fail("SQLException is thrown: " + sqle.toString());
1843         } finally {
1844             try {
1845                 rs1.close();
1846                 st.close();
1847                 st1.close();
1848             } catch (SQLException ee) {
1849             }
1850         }
1851         */
1852     }
1853 
1854     // TODO prepareStatement(String sql, int[] columnIndexes) is not supported
testPrepareStatement_String_intArray()1855     public void testPrepareStatement_String_intArray() {
1856         PreparedStatement prst = null;
1857         try {
1858             String insert = "insert into zoo (id, name, family) values (?, ?, ?);";
1859             prst = conn.prepareStatement(insert, new int[] { 0, 1, 2 });
1860         } catch (SQLException e) {
1861             //ok not supported
1862         } finally {
1863             try {
1864                 prst.close();
1865             } catch (Exception ee) {
1866             }
1867         }
1868         /*
1869 
1870         Statement st = null;
1871         PreparedStatement prst1 = null;
1872         PreparedStatement prst = null;
1873         ResultSet rs = null;
1874         ResultSet rs1 = null;
1875         ResultSet rs4 = null;
1876         ResultSet rs5 = null;
1877         try {
1878             String insert = "insert into zoo (id, name, family) values (?, ?, ?);";
1879             prst = conn.prepareStatement(insert, new int[] { 0, 1, 2 });
1880             prst.setInt(1, 8);
1881             prst.setString(2, "Tuzik");
1882             prst.setString(3, "dog");
1883 
1884             st = conn.createStatement();
1885             st.execute("select * from zoo");
1886             rs = st.getResultSet();
1887             assertEquals(2, getCount(rs));
1888             prst.execute();
1889             st.execute("select * from zoo where family = 'dog'");
1890             rs1 = st.getResultSet();
1891             assertEquals(1, getCount(rs1));
1892 
1893             rs4 = prst.getGeneratedKeys();
1894             assertEquals(0, getCount(rs4));
1895 
1896             prst1 = conn.prepareStatement(insert, new int[] { 0, 1, 2, 10 });
1897             prst1.setInt(1, 5);
1898             prst1.setString(2, "Layka");
1899             prst1.setString(3, "dog");
1900 
1901             prst1.execute();
1902 
1903             rs5 = prst1.getGeneratedKeys();
1904             assertEquals(0, getCount(rs5));
1905 
1906         } catch (SQLException e) {
1907             fail("SQLException is thrown: " + e.getMessage());
1908         } finally {
1909             try {
1910                 rs.close();
1911                 rs1.close();
1912                 rs4.close();
1913                 rs5.close();
1914                 st.close();
1915                 prst1.close();
1916                 prst.close();
1917             } catch (Exception ee) {
1918             }
1919         }
1920 
1921         try {
1922             String insert = "insert into zoo (id, name, family) values (?, ?, ?);";
1923             conn.prepareStatement(insert, new int[] {});
1924         } catch (SQLException e) {
1925             fail("SQLException is thrown");
1926         }
1927 
1928         try {
1929             String insert = "insert into zoo (id, name, family) values (?, ?, ?);";
1930             conn.prepareStatement(insert, (int[]) null);
1931         } catch (SQLException e) {
1932             fail("SQLException is thrown");
1933         }
1934         */
1935     }
1936 
testPrepareStatement_String_int_int()1937     public void testPrepareStatement_String_int_int() throws SQLException {
1938         String query = "insert into zoo (id, name, family) values (?, ?, ?);";
1939         PreparedStatement st = null;
1940         ResultSet rs = null;
1941         try {
1942 
1943             st = conn.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY,
1944                     ResultSet.CONCUR_READ_ONLY);
1945             st.execute("select id, name from zoo");
1946             rs = st.getResultSet();
1947             try {
1948                 rs.deleteRow();
1949                 fail("Can delete row for READ_ONLY ResultSet");
1950             } catch (SQLException sqle) {
1951                 // expected
1952             }
1953         } finally {
1954             try {
1955                 if (rs != null) rs.close();
1956                 if (st != null) st.close();
1957             } catch (SQLException ee) {
1958             }
1959         }
1960 
1961         try {
1962             st = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE,
1963                     ResultSet.CONCUR_UPDATABLE);
1964             st.execute("select name, family from zoo");
1965             rs = st.getResultSet();
1966             try {
1967                 rs.insertRow();
1968                 rs.updateObject("family", "bird");
1969                 rs.next();
1970                 rs.previous();
1971                 assertEquals("bird", (rs.getString(1)));
1972             } catch (SQLException sqle) {
1973                 // expected
1974             }
1975         } finally {
1976             try {
1977                 rs.close();
1978                 st.close();
1979             } catch (SQLException ee) {
1980             }
1981         }
1982 
1983         try {
1984             conn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, -1);
1985         } catch (SQLException sqle) {
1986             // expected
1987         }
1988 
1989         try {
1990             conn.prepareStatement(query, Integer.MIN_VALUE,
1991                     ResultSet.CONCUR_READ_ONLY);
1992         } catch (SQLException sqle) {
1993             // expected
1994         }
1995     }
1996 
1997     // not supported
testPrepareStatementNotSupported()1998     public void testPrepareStatementNotSupported() throws SQLException {
1999         String query = "insert into zoo (id, name, family) values (?, ?, ?);";
2000         PreparedStatement st = null;
2001         ResultSet rs = null;
2002         try {
2003             st = conn.prepareStatement(query,
2004                     ResultSet.TYPE_SCROLL_INSENSITIVE,
2005                     ResultSet.CONCUR_UPDATABLE);
2006             st.execute("select name, family from zoo");
2007             rs = st.getResultSet();
2008             rs.insertRow();
2009             rs.updateObject("family", "bird");
2010             rs.next();
2011             rs.previous();
2012             assertEquals("parrot", (rs.getString(1)));
2013         } finally {
2014             try {
2015                 if (rs != null) rs.close();
2016                 if (st != null) st.close();
2017             } catch (SQLException ee) {
2018             }
2019         }
2020     }
2021 
2022     //  TODO Crashes VM. Fix later.
testPrepareStatement_String_int_int_int()2023     public void testPrepareStatement_String_int_int_int() throws SQLException {
2024         String query = "insert into zoo (id, name, family) values (?, ?, ?);";
2025         PreparedStatement st = null;
2026         ResultSet rs = null;
2027         try {
2028             st = conn.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY,
2029                     ResultSet.CONCUR_READ_ONLY,
2030                     ResultSet.HOLD_CURSORS_OVER_COMMIT);
2031             st.setInt(1, 3);
2032             st.setString(2, "Petya");
2033             st.setString(3, "Cock");
2034             st.execute("select id, name from zoo");
2035             rs = st.getResultSet();
2036             rs.close();
2037         } finally {
2038             try {
2039                 if (rs != null) rs.close();
2040                 if (st != null) st.close();
2041             } catch (SQLException ee) {
2042             }
2043         }
2044         /*
2045         //TODO ResultSet.CLOSE_CURSORS_AT_COMMIT is not supported
2046         try {
2047             st = conn.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY,
2048                     ResultSet.CONCUR_READ_ONLY,
2049                     ResultSet.CLOSE_CURSORS_AT_COMMIT);
2050             st.execute("select id, name from zoo");
2051             rs = st.getResultSet();
2052             try {
2053                 rs.close();
2054                 fail("SQLException was not thrown");
2055             } catch (SQLException sqle) {
2056                 // expected
2057             }
2058         } catch (SQLException e) {
2059             fail("SQLException was thrown: " + e.getMessage());
2060         } finally {
2061             try {
2062                 st.close();
2063                 rs.close();
2064             } catch (SQLException ee) {
2065             }
2066         }
2067         */
2068 
2069         try {
2070             conn.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY,
2071                     ResultSet.CONCUR_READ_ONLY, -100);
2072             fail("SQLException was not thrown");
2073         } catch (SQLException sqle) {
2074             // expected
2075         }
2076 
2077     }
2078 
2079     // TODO prepareStatement(String sql, String[] columnNames) method is not supported
testPrepareStatement_String_StringArray()2080     public void testPrepareStatement_String_StringArray() {
2081         PreparedStatement prst = null;
2082         PreparedStatement prst1 = null;
2083         ResultSet rs = null;
2084         ResultSet rs1 = null;
2085         ResultSet rs4 = null;
2086         ResultSet rs5 = null;
2087 
2088         try {
2089             String insert = "insert into zoo (id, name, family) values (?, ?, ?);";
2090             conn.prepareStatement(insert, new String[] { "id", "name",
2091             "family" });
2092         } catch (SQLException e) {
2093             //ok not supported
2094         }
2095 
2096         /*
2097         try {
2098             String insert = "insert into zoo (id, name, family) values (?, ?, ?);";
2099             conn.prepareStatement(insert, new String[] {});
2100         } catch (SQLException e) {
2101             fail("SQLException is thrown");
2102         }
2103 
2104         try {
2105             String insert = "insert into zoo (id, name, family) values (?, ?, ?);";
2106             conn.prepareStatement(insert, (String[]) null);
2107         } catch (SQLException e) {
2108             fail("SQLException is thrown");
2109         }
2110 
2111         try {
2112             String insert = "insert into zoo (id, name, family) values (?, ?, ?);";
2113             prst = conn.prepareStatement(insert, new String[] { "id", "name",
2114                     "family" });
2115             prst.setInt(1, 8);
2116             prst.setString(2, "Tuzik");
2117             prst.setString(3, "dog");
2118 
2119             Statement st = conn.createStatement();
2120             st.execute("select * from zoo");
2121             rs = st.getResultSet();
2122             assertEquals(2, getCount(rs));
2123             prst.execute();
2124             st.execute("select * from zoo where family = 'dog'");
2125             rs1 = st.getResultSet();
2126             assertEquals(1, getCount(rs1));
2127 
2128             rs4 = prst.getGeneratedKeys();
2129             assertEquals(0, getCount(rs4));
2130 
2131             prst1 = conn.prepareStatement(insert, new String[] { "id", "name", "" });
2132             prst1.setInt(1, 5);
2133             prst1.setString(2, "Layka");
2134             prst1.setString(3, "dog");
2135 
2136             prst1.execute();
2137 
2138             rs5 = prst1.getGeneratedKeys();
2139             assertEquals(0, getCount(rs5));
2140 
2141         } catch (SQLException e) {
2142             fail("SQLException is thrown: " + e.getMessage());
2143         } finally {
2144             try {
2145                 rs.close();
2146                 rs1.close();
2147                 rs4.close();
2148                 rs5.close();
2149                 prst.close();
2150                 prst1.close();
2151             } catch (Exception ee) {
2152             }
2153         }
2154         */
2155 
2156 
2157     }
2158 
testClose()2159     public void testClose() throws SQLException {
2160         if (! conn.isClosed()) {
2161         conn.close();
2162         }
2163         assertTrue(conn.isClosed());
2164 
2165         try {
2166         conn.prepareCall("select * from zoo");
2167         fail("Should not be able to prepare query closed connection");
2168         } catch (SQLException e) {
2169             //ok
2170         }
2171     }
2172 
testIsClosed()2173     public void testIsClosed() throws Exception {
2174         assertFalse(conn.isClosed());
2175         conn.close();
2176         assertTrue(conn.isClosed());
2177 
2178         conn = DriverManager.getConnection("jdbc:sqlite:/" + dbFile.getPath());
2179         assertFalse(conn.isClosed());
2180         Statement st = conn.createStatement();
2181         st.execute("select * from zoo");
2182     }
2183 
2184     private static class DummySavePoint implements Savepoint{
getSavepointId()2185         public int getSavepointId()  {
2186             return 0;
2187         }
2188 
getSavepointName()2189         public String getSavepointName() {
2190             return "NoName";
2191         }
2192     }
2193 }
2194