1/*
2 * Copyright (C) 2007 Google Inc.
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
17package libcore.java.sql;
18
19import java.io.InputStream;
20import java.io.InputStreamReader;
21import java.io.OutputStream;
22import java.io.Reader;
23import java.io.Writer;
24import java.math.BigDecimal;
25import java.net.URL;
26import java.sql.Array;
27import java.sql.Blob;
28import java.sql.Clob;
29import java.sql.Date;
30import java.sql.PreparedStatement;
31import java.sql.Ref;
32import java.sql.ResultSet;
33import java.sql.ResultSetMetaData;
34import java.sql.SQLException;
35import java.sql.Statement;
36import java.sql.Time;
37import java.sql.Timestamp;
38import java.sql.Types;
39import java.util.Calendar;
40import java.util.GregorianCalendar;
41import java.util.Locale;
42import java.util.Map;
43import java.util.TimeZone;
44
45public final class OldPreparedStatementTest extends OldSQLTest {
46
47    String queryAllSelect = "select * from type";
48
49    String[] queries = {
50            "create table type (" +
51
52            " BoolVal BOOLEAN," + " IntVal INT," + " LongVal LONG,"
53                    + " Bint BIGINT," + " Tint TINYINT," + " Sint SMALLINT,"
54                    + " Mint MEDIUMINT, " +
55
56                    " IntegerVal INTEGER, " + " RealVal REAL, "
57                    + " DoubleVal DOUBLE, " + " FloatVal FLOAT, "
58                    + " DecVal DECIMAL, " +
59
60                    " NumVal NUMERIC, " + " charStr CHAR(20), "
61                    + " dateVal DATE, " + " timeVal TIME, " + " TS TIMESTAMP, "
62                    +
63
64                    " DT DATETIME, " + " TBlob TINYBLOB, " + " BlobVal BLOB, "
65                    + " MBlob MEDIUMBLOB, " + " LBlob LONGBLOB, " +
66
67                    " TText TINYTEXT, " + " TextVal TEXT, "
68                    + " MText MEDIUMTEXT, " + " LText LONGTEXT " + ");",
69
70            "insert into type (BoolVal, IntVal, LongVal, Bint, Tint, Sint, Mint,"
71                    + "IntegerVal, RealVal, DoubleVal, FloatVal, DecVal,"
72                    + "NumVal, charStr, dateVal, timeVal, TS,"
73                    + "DT, TBlob, BlobVal, MBlob, LBlob,"
74                    + "TText, TextVal, MText, LText"
75                    + ") "
76                    + "values (1, -1, 22, 2, 33,"
77                    + "3, 1, 2, 3.9, 23.2, 33.3, 44,"
78                    + "5, 'test string', '1799-05-26', '12:35:45', '2007-10-09 14:28:02.0',"
79                    + "'1221-09-22 10:11:55', 1, 2, 3, 4,"
80                    + "'Test text message tiny', 'Test text message', 'Test text message medium', 'Test text message long');" };
81
82    public void setUp() throws Exception {
83        super.setUp();
84        Statement st = null;
85        try {
86            st = conn.createStatement();
87            for (int i = 0; i < queries.length; i++) {
88                st.execute(queries[i]);
89            }
90        } finally {
91            try {
92                st.close();
93            } catch (Exception ee) {
94            }
95        }
96    }
97
98    public void tearDown() throws SQLException {
99        Statement st = null;
100        try {
101            st = conn.createStatement();
102            st.execute("drop table if exists type");
103        } finally {
104            try {
105                st.close();
106            } catch (SQLException ee) {
107            }
108        }
109        super.tearDown();
110    }
111
112    public void testAddBatch() throws SQLException {
113        PreparedStatement ps = null;
114        try {
115            ps = conn
116                    .prepareStatement("INSERT INTO zoo VALUES (3,'Tuzik', ?);");
117            ps.addBatch("INSERT INTO zoo VALUES (?,'Burenka', ?); ");
118            ps.addBatch("INSERT INTO zoo VALUES (?,'Mashka','cat')");
119            try {
120                ps.executeBatch();
121            } catch (SQLException sqle) {
122                fail("SQLException is thrown for executeBatch()");
123            }
124            ps.setString(1, "dog");
125            Statement st = null;
126            try {
127                ps.executeBatch();
128                st = conn.createStatement();
129                st.execute("select * from zoo");
130                ResultSet rs = st.getResultSet();
131                assertEquals(2, getCount(rs));
132            } catch (SQLException sqle) {
133                fail("SQLException is thrown for executeBatch()");
134            } finally {
135                try {
136                    st.close();
137                } catch (SQLException ee) {
138                }
139            }
140        } finally {
141            try {
142                ps.close();
143            } catch (SQLException ee) {
144            }
145        }
146
147        try {
148            ps = conn
149                    .prepareStatement("INSERT INTO zoo VALUES (3,'Tuzik', ?);");
150            ps.addBatch("");
151        } catch (SQLException e) {
152            // expected
153        } finally {
154            try {
155                ps.close();
156            } catch (SQLException ee) {
157            }
158        }
159
160        try {
161            ps = conn
162                    .prepareStatement("INSERT INTO zoo VALUES (3,'Tuzik', ?);");
163            ps.addBatch(null);
164        } catch (SQLException e) {
165            // expected
166        } finally {
167            try {
168                ps.close();
169            } catch (SQLException ee) {
170            }
171        }
172    }
173
174
175    // preparedStatement.execute() does not return false on update.
176    public void testExecute() throws SQLException {
177        Statement st = null;
178        PreparedStatement ps = null;
179        try {
180            //update
181            String query = "insert into zoo(id, family, name) values(?, ?, 'unknown animal')";
182            ps = conn.prepareStatement(query);
183            ps.setInt(1, 3);
184            ps.setString(2, "No name");
185            assertFalse(ps.execute());
186            assertEquals(1,ps.getUpdateCount());
187
188            // select
189            ps = conn.prepareStatement("select * from zoo");
190            assertTrue(ps.execute());
191            assertEquals(3, getCount(ps.getResultSet()));
192        } finally {
193            try {
194                ps.close();
195            } catch (Exception ee) {
196            }
197        }
198
199        try {
200            String query = "update zoo set name='Masha', family=? where id=?;";
201            ps = conn.prepareStatement(query);
202            ps.setString(1, "cat");
203            ps.setInt(2, 2);
204            assertFalse(ps.execute());
205            assertEquals(1, ps.getUpdateCount());
206            st = conn.createStatement();
207            st.execute("select family from zoo where id=2");
208            ResultSet rs = st.getResultSet();
209            rs.next();
210            assertEquals("cat", rs.getString(1));
211        } finally {
212            try {
213                ps.close();
214                st.close();
215            } catch (Exception ee) {
216            }
217        }
218
219        try {
220            conn.createStatement().execute("drop table if exists hutch");
221            String query = "create table hutch (id integer not null, animal_id integer, address char(20), primary key (id));";
222            ps = conn.prepareStatement(query);
223            assertFalse(ps.execute());
224            assertTrue(ps.getUpdateCount() > 0);
225        } finally {
226            try {
227                ps.close();
228            } catch (Exception ee) {
229            }
230        }
231
232        try {
233            String query = "select name, family from zoo where id = ?";
234            ps = conn.prepareStatement(query);
235            ps.setInt(1, 1);
236            assertTrue(ps.execute());
237        } finally {
238            try {
239                ps.close();
240            } catch (Exception ee) {
241            }
242        }
243
244        try {
245            String query = "select name, family from zoo where id = ?";
246            ps = conn.prepareStatement(query);
247            ps.execute();
248        } finally {
249            try {
250                ps.close();
251            } catch (Exception ee) {
252            }
253        }
254        //Exception test
255        try {
256            String query = "update zoo set name='Masha', family=? where id=?;";
257            ps = conn.prepareStatement(query);
258            ps.setString(1, "cat");
259            ps.setInt(2, 2);
260            assertTrue(ps.execute("update zoo set name='Masha', family='cat' where id=2;"));
261        } catch (SQLException e) {
262            // ok Should not provide string argument for a prepared Statement
263        } finally {
264            try {
265                ps.close();
266            } catch (Exception ee) {
267            }
268        }
269    }
270
271    public void testExecuteQuery() throws SQLException {
272        String[] queries2 = {
273                "update zoo set name='Masha', family='cat' where id=;",
274                "insert into hutch (id, animal_id, address) values (1, ?,'Birds-house, 1');",
275                "insert into hutch (id, animal_id, address) values (?, 1, 'Horse-house, 5');",
276                "create view address as select address from hutch where animal_id=?"};
277
278        for (int i = 0; i < queries2.length; i++) {
279            PreparedStatement ps = null;
280            try {
281                ps = conn.prepareStatement(queries2[i]);
282                ps.executeQuery();
283                fail("SQLException is not thrown for query: " + queries2[i]);
284            } catch (SQLException sqle) {
285                // expected
286            } finally {
287                try {
288                    ps.close();
289                } catch (Exception ee) {
290                }
291            }
292        }
293
294        String query = "select * from zoo where id = ?";
295        PreparedStatement ps = null;
296        try {
297            ps = conn.prepareStatement(query);
298            ps.setInt(1, 1);
299            ResultSet rs = ps.executeQuery();
300            rs.next();
301            assertEquals(1, rs.getInt(1));
302            assertEquals("Kesha", rs.getString(2));
303            assertEquals("parrot", rs.getString(3));
304        } finally {
305            try {
306                ps.close();
307            } catch (Exception ee) {
308            }
309        }
310
311        try {
312            ps = conn.prepareStatement(query);
313            ps.setInt(1, 5);
314            ResultSet rs = ps.executeQuery();
315            assertNotNull(rs);
316            assertFalse(rs.next());
317        } finally {
318            try {
319                ps.close();
320            } catch (Exception ee) {
321            }
322        }
323    }
324
325    // TODO Crashes VM. Fix later.
326    public void testExecuteUpdate() throws SQLException {
327          String[] queries1 = { "insert into hutch (id, animal_id, address) values (1, ?, 'Birds-house, 1');",
328                              "insert into hutch (id, animal_id, address) values (?, 1, 'Horse-house, 5');",
329                              "create view address as select address from hutch where animal_id=2" };
330
331          for (int i = 0; i < queries1.length; i++) {
332              PreparedStatement ps = null;
333              try {
334                  ps = conn.prepareStatement(queries1[i]);
335                  ps.executeUpdate();
336                  fail("SQLException is not thrown for query: " + queries1[i]);
337          } catch(SQLException sqle) {
338              // expected
339          } finally {
340              try {
341                  ps.close();
342              } catch(Exception ee) {}
343          }
344      }
345
346          String query = "update zoo set name='Masha', family='cat' where id=?;";
347          PreparedStatement ps = null;
348          try {
349              ps = conn.prepareStatement(query);
350              ps.setInt(1, 2);
351              int updateCount = ps.executeUpdate();
352              assertEquals(1, updateCount);
353              ps.setInt(1, 1);
354              int updateCount1 = ps.executeUpdate();
355              assertEquals(1, updateCount1);
356          } finally {
357              try {
358                  ps.close();
359              } catch(Exception ee) {}
360          }
361      }
362
363    /**
364     * TODO Doesn't pass. according to spec, it is possible to invoke the
365     * method getMetaData on a PreparedStatement object before it is executed.
366     */
367    public void testGetMetaData() throws SQLException {
368        PreparedStatement ps = null;
369
370        // Specification testing
371
372        try {
373            String query = "update zoo set name='Masha', family='cat' where id=?;";
374            ps = conn.prepareStatement(query);
375            assertNotNull(ps);
376            ResultSetMetaData meta = ps.getMetaData();
377            assertNotNull(meta);
378        } finally {
379            try {
380                ps.close();
381            } catch (SQLException ee) {
382            }
383        }
384
385        try {
386            String query = "select * from zoo where id = ?";
387            ps = conn.prepareStatement(query);
388            ResultSetMetaData rsmd = ps.getMetaData();
389            assertNotNull(rsmd);
390            assertEquals(3, rsmd.getColumnCount());
391            assertEquals("id", rsmd.getColumnName(1));
392        } finally {
393            try {
394                ps.close();
395            } catch (SQLException ee) {
396            }
397        }
398
399        // ps closed
400        try {
401            ps.getMetaData();
402            fail("SQLException expected");
403        } catch (SQLException e) {
404            // ok
405        }
406    }
407
408    public void testGetParameterMetaData() throws SQLException {
409        PreparedStatement ps = null;
410        String query = "select * from zoo where id = ?";
411        ps = conn.prepareStatement(query);
412
413        try {
414            ps.getParameterMetaData();
415            fail();
416        } catch (SQLException e) {
417            assertEquals("not supported",e.getMessage());
418        } finally {
419            try {
420                ps.close();
421            } catch (SQLException ee) {
422            }
423        }
424
425        ps.close();
426
427        try {
428            ps.getParameterMetaData();
429            fail("SQLException expected");
430        } catch (SQLException e) {
431            // ok
432        }
433    }
434
435
436    /**
437     * Test fails: clearparameters should be implemented with Stmt.reset()
438     */
439    public void testClearParameters() throws SQLException {
440        PreparedStatement ps = null;
441        try {
442            String query = "select * from zoo where id = ? and family=?";
443            ps = conn.prepareStatement(query);
444            ps.clearParameters();
445            try {
446                ps.execute();
447                fail("SQLException is not thrown during execute method after calling clearParameters()");
448            } catch (SQLException sql) {
449            }
450            ps.setInt(1, 2);
451            ps.setString(2, "dog");
452            ps.clearParameters();
453            try {
454                ps.execute();
455                fail("SQLException is not thrown during execute method after calling clearParameters()");
456            } catch (SQLException sqle) {
457                // expected
458            }
459            ps.setInt(1, 2);
460            ps.clearParameters();
461            try {
462                ps.execute();
463                fail("SQLException is not thrown during execute method after calling clearParameters()");
464            } catch (SQLException sqle) {
465                // expected
466            }
467            ps.setInt(1, 2);
468            ps.setString(2, "cat");
469
470            try {
471                ps.execute();
472            } catch (SQLException sqle) {
473                fail("SQLException is thrown during execute method after calling clearParameters() twice");
474            }
475        } finally {
476            try {
477                ps.close();
478            } catch (SQLException ee) {
479            }
480        }
481    }
482
483    // exception test fails
484    public void testSetInt() throws SQLException {
485
486        PreparedStatement ps = null;
487        Statement st = null;
488        try {
489            String query = "insert into type (IntVal) values (?);";
490            ps = conn.prepareStatement(query);
491            try {
492                ps.setInt(1, Integer.MAX_VALUE);
493                ps.execute();
494                st = conn.createStatement();
495                st.execute("select * from type where IntVal="
496                        + Integer.MAX_VALUE);
497                ResultSet rs = st.getResultSet();
498                assertEquals(1, getCount(rs));
499            } finally {
500                try {
501                    ps.close();
502                    st.close();
503                } catch (Exception ee) {
504                }
505            }
506            ps = conn.prepareStatement(query);
507            try {
508                ps.setInt(1, Integer.MIN_VALUE);
509                ps.execute();
510                st = conn.createStatement();
511                st.execute("select * from type where IntVal="
512                        + Integer.MAX_VALUE);
513                ResultSet rs = st.getResultSet();
514                assertEquals(1, getCount(rs));
515            } finally {
516                try {
517                    ps.close();
518                    st.close();
519                } catch (SQLException ee) {
520                }
521            }
522            ps = conn.prepareStatement(query);
523            ps.close();
524            try {
525                ps.setInt(1, Integer.MIN_VALUE);
526                fail("SQLException is not thrown");
527            } catch (SQLException sqle) {
528                // expected
529            }
530        } finally {
531            try {
532
533                ps.close();
534            } catch (SQLException ee) {
535            }
536        }
537    }
538
539    // exception test fails
540    public void testSetLong() throws SQLException {
541
542        PreparedStatement ps = null;
543        try {
544            String query = "insert into type (LongVal) values (?);";
545            ps = conn.prepareStatement(query);
546            Statement st = null;
547            try {
548                ps.setLong(1, Long.MAX_VALUE);
549                ps.execute();
550                st = conn.createStatement();
551                st.execute("select * from type where LongVal=" + Long.MAX_VALUE);
552                ResultSet rs = st.getResultSet();
553                assertEquals(1, getCount(rs));
554            } finally {
555                try {
556                    st.close();
557                } catch (Exception ee) {
558                }
559            }
560
561            try {
562                ps.setLong(1, Long.MIN_VALUE);
563                ps.execute();
564                st = conn.createStatement();
565                st.execute("select * from type where LongVal=" + Long.MAX_VALUE);
566                ResultSet rs = st.getResultSet();
567                assertEquals(1, getCount(rs));
568            } finally {
569                try {
570                    st.close();
571                } catch (SQLException ee) {
572                }
573            }
574
575            ps.close();
576            try {
577                ps.setLong(1, Long.MIN_VALUE);
578                fail("SQLException is not thrown");
579            } catch (SQLException sqle) {
580                // expected
581            }
582        } finally {
583            try {
584                ps.close();
585            } catch (SQLException ee) {
586            }
587        }
588
589    }
590
591    // exception test fails
592    public void testSetFloat() throws SQLException {
593        float value1 = 12345678.12345689f;
594        float value2 = -12345678.12345689f;
595
596        PreparedStatement ps = null;
597        String query = "insert into type (FloatVal) values (?);";
598        ps = conn.prepareStatement(query);
599
600        try {
601
602            Statement st = null;
603            try {
604                ps.setFloat(1, value1);
605                ps.execute();
606                st = conn.createStatement();
607                st.execute("select * from type where FloatVal=" + value1);
608                ResultSet rs = st.getResultSet();
609                assertEquals(1, getCount(rs));
610            } finally {
611                try {
612                    st.close();
613                } catch (Exception ee) {
614                }
615            }
616
617            try {
618                ps.setFloat(1, value2);
619                ps.execute();
620                st = conn.createStatement();
621                st.execute("select * from type where FloatVal=" + value2);
622                ResultSet rs = st.getResultSet();
623                assertEquals(1, getCount(rs));
624            } finally {
625                try {
626                    st.close();
627                } catch (SQLException ee) {
628
629                }
630            }
631            ps.close();
632            try {
633                ps.setFloat(1, Float.MIN_VALUE);
634                fail("SQLException is not thrown");
635            } catch (SQLException sqle) {
636                // expected
637            }
638        } finally {
639            try {
640
641                ps.close();
642            } catch (SQLException ee) {
643            }
644        }
645    }
646
647    // exception test fails
648    public void testSetDouble() throws SQLException {
649        PreparedStatement ps = null;
650        String query = "insert into type (DoubleVal) values (?);";
651        ps = conn.prepareStatement(query);
652
653        try {
654
655            Statement st = null;
656            try {
657                ps.setDouble(1, Double.MAX_VALUE);
658                ps.execute();
659                st = conn.createStatement();
660                st.execute("select * from type where DoubleVal="
661                        + Double.MAX_VALUE);
662                ResultSet rs = st.getResultSet();
663                assertEquals(1, getCount(rs));
664            } finally {
665                try {
666                    st.close();
667                } catch (Exception ee) {
668                }
669            }
670
671            try {
672                ps.setDouble(1, Double.MIN_VALUE);
673                ps.execute();
674                st = conn.createStatement();
675                st.execute("select * from type where DoubleVal="
676                        + Double.MIN_VALUE);
677                ResultSet rs = st.getResultSet();
678                assertEquals(1, getCount(rs));
679            } finally {
680                try {
681                    st.close();
682                } catch (SQLException ee) {
683                }
684            }
685
686            ps.close();
687            try {
688                ps.setDouble(1, 2.0);
689                fail("SQLException is not thrown");
690            } catch (SQLException sqle) {
691                // expected
692            }
693        } finally {
694            try {
695
696                ps.close();
697            } catch (SQLException ee) {
698            }
699        }
700    }
701
702    // exception test fails
703    public void testSetString_charField() throws SQLException {
704        PreparedStatement ps = null;
705        try {
706            String query = "insert into type (charStr) values (?);";
707            ps = conn.prepareStatement(query);
708
709            String str = "test^text$test%";
710            Statement st = null;
711            try {
712                ps.setString(1, str);
713                ps.execute();
714                st = conn.createStatement();
715                st.execute("select * from type where charStr='" + str + "'");
716                ResultSet rs = st.getResultSet();
717                assertEquals(1, getCount(rs));
718            } finally {
719                try {
720                    st.close();
721                } catch (Exception ee) {
722                }
723            }
724
725            try {
726                ps.setString(1, "");
727                ps.execute();
728                st = conn.createStatement();
729                st.execute("select * from type where charStr=''");
730                ResultSet rs = st.getResultSet();
731                assertEquals(1, getCount(rs));
732            } finally {
733                try {
734                    st.close();
735                } catch (SQLException ee) {
736                }
737            }
738
739            try {
740                ps.setString(1, "                   ");
741                ps.execute();
742                st = conn.createStatement();
743                st.execute("select * from type where charStr='                   '");
744                ResultSet rs = st.getResultSet();
745                assertEquals(1, getCount(rs));
746            } finally {
747                try {
748                    st.close();
749                } catch (SQLException ee) {
750                }
751            }
752
753            ps.setString(1, " test & text * test % text * test ^ text ");
754            ps.execute();
755            ps.setString(1, null);
756            ps.execute();
757            ps.close();
758
759            try {
760                ps.setString(1, "test text");
761                fail("SQLException is not thrown");
762            } catch (SQLException sqle) {
763                // expected
764            }
765        } finally {
766            try {
767                ps.close();
768            } catch (SQLException ee) {
769            }
770        }
771    }
772
773    // statment.close() does not wrap up
774    public void testSetString_tinyTextField() throws SQLException {
775        PreparedStatement ps = null;
776        try {
777            String str = "test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test/test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test";
778            String query = "insert into type (TText) values (?);";
779            ps = conn.prepareStatement(query);
780            Statement st = null;
781            try {
782                ps.setString(1, str);
783                ps.execute();
784                st = conn.createStatement();
785                st.execute("select * from type where TText='" + str + "'");
786                ResultSet rs = st.getResultSet();
787                assertEquals(1, getCount(rs));
788            } finally {
789                try {
790                    st.close();
791                } catch (Exception ee) {
792                }
793            }
794
795            try {
796                ps.setString(1, "");
797                ps.execute();
798                st = conn.createStatement();
799                st.execute("select * from type where TText=''");
800                ResultSet rs = st.getResultSet();
801                assertEquals(1, getCount(rs));
802            } finally {
803                try {
804                    st.close();
805                } catch (SQLException ee) {
806                }
807            }
808
809            try {
810                ps.setString(1, "                   ");
811                ps.execute();
812                st = conn.createStatement();
813                st.execute("select * from type where TText='                   '");
814                ResultSet rs = st.getResultSet();
815                assertEquals(1, getCount(rs));
816            } finally {
817                try {
818                    st.close();
819                } catch (SQLException ee) {
820                }
821            }
822
823            try {
824                ps.setString(1,
825                        "test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test/test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test*test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test/test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test-test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test/test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test/test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test+test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test/test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test?test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test/test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test#test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test/test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test ");
826                ps.execute();
827            } catch (SQLException sqle) {
828                fail("SQLException is thrown");
829            }
830
831            try {
832                ps.setString(1, null);
833                ps.execute();
834            } catch (SQLException sqle) {
835                fail("SQLException is thrown: " + sqle.getMessage());
836            }
837
838            ps.close();
839
840            try {
841                ps.setString(1, "test text");
842                fail("SQLException is not thrown");
843            } catch (SQLException sqle) {
844                // expected
845            }
846        } finally {
847            try {
848                ps.close();
849            } catch (SQLException ee) {
850            }
851        }
852    }
853
854    public void testSetString_textField() throws SQLException {
855        PreparedStatement ps = null;
856        try {
857            String str = "test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test/test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test";
858            String query = "insert into type (TextVal) values (?);";
859            ps = conn.prepareStatement(query);
860            Statement st = null;
861            try {
862                ps.setString(1, str);
863                ps.execute();
864                st = conn.createStatement();
865                st.execute("select * from type where TextVal='" + str + "'");
866                ResultSet rs = st.getResultSet();
867                assertEquals(1, getCount(rs));
868            } finally {
869                try {
870                    st.close();
871                } catch (Exception ee) {
872                }
873            }
874
875            try {
876                ps.setString(1, "");
877                ps.execute();
878                st = conn.createStatement();
879                st.execute("select * from type where TextVal=''");
880                ResultSet rs = st.getResultSet();
881                assertEquals(1, getCount(rs));
882            } finally {
883                try {
884                    st.close();
885                } catch (SQLException ee) {
886                }
887            }
888
889            try {
890                ps.setString(1, "                   ");
891                ps.execute();
892                st = conn.createStatement();
893                st.execute("select * from type where TextVal='                   '");
894                ResultSet rs = st.getResultSet();
895                assertEquals(1, getCount(rs));
896            } finally {
897                try {
898                    st.close();
899                } catch (SQLException ee) {
900                }
901            }
902
903            String longString = " test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test/";
904            for (int i = 0; i < 10; i++) {
905                longString += longString;
906            }
907            ps.setString(1, longString);
908            ps.execute();
909
910            ps.setString(1, null);
911            ps.execute();
912
913            ps.close();
914
915            try {
916                ps.setString(2, "test text");
917                fail("SQLException is not thrown");
918            } catch (SQLException sqle) {
919                // expected
920            }
921        } finally {
922            try {
923                ps.close();
924            } catch (SQLException ee) {
925            }
926        }
927    }
928
929    public void testSetString_mediumTextField() throws SQLException {
930        PreparedStatement ps = null;
931        try {
932            String str = "test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test/test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test";
933            String query = "insert into type (MText) values (?);";
934            ps = conn.prepareStatement(query);
935            Statement st = null;
936            try {
937                ps.setString(1, str);
938                ps.execute();
939                st = conn.createStatement();
940                st.execute("select * from type where MText='" + str + "'");
941                ResultSet rs = st.getResultSet();
942                assertEquals(1, getCount(rs));
943            } finally {
944                try {
945                    st.close();
946                } catch (Exception ee) {
947                }
948            }
949
950            try {
951                ps.setString(1, "");
952                ps.execute();
953                st = conn.createStatement();
954                st.execute("select * from type where MText=''");
955                ResultSet rs = st.getResultSet();
956                assertEquals(1, getCount(rs));
957            } finally {
958                try {
959                    st.close();
960                } catch (Exception ee) {
961                }
962            }
963
964            try {
965                ps.setString(1, "                   ");
966                ps.execute();
967                st = conn.createStatement();
968                st
969                        .execute("select * from type where MText='                   '");
970                ResultSet rs = st.getResultSet();
971                assertEquals(1, getCount(rs));
972            } finally {
973                try {
974                    st.close();
975                } catch (Exception ee) {
976                }
977            }
978
979            ps.setString(1, null);
980            ps.execute();
981            ps.close();
982
983            try {
984                ps.setString(2, "test text");
985                fail("SQLException is not thrown");
986            } catch (SQLException sqle) {
987                // expected
988            }
989        } finally {
990            try {
991                ps.close();
992            } catch (Exception ee) {
993            }
994        }
995    }
996
997    // exception test fails
998    public void testSetString_longTextField() throws SQLException {
999        PreparedStatement ps = null;
1000        try {
1001            String str = "test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test/test^text$test%test(text)test@text5test~test^text$test%test(text)test@text5test";
1002            String query = "insert into type (LText) values (?);";
1003            ps = conn.prepareStatement(query);
1004            Statement st = null;
1005            try {
1006                ps.setString(1, str);
1007                ps.execute();
1008                st = conn.createStatement();
1009                st.execute("select * from type where LText='" + str + "'");
1010                ResultSet rs = st.getResultSet();
1011                assertEquals(1, getCount(rs));
1012            } finally {
1013                try {
1014                    st.close();
1015                } catch (Exception ee) {
1016                }
1017            }
1018
1019            try {
1020                ps.setString(1, "");
1021                ps.execute();
1022                st = conn.createStatement();
1023                st.execute("select * from type where LText=''");
1024                ResultSet rs = st.getResultSet();
1025                assertEquals(1, getCount(rs));
1026            } finally {
1027                try {
1028                    st.close();
1029                } catch (Exception ee) {
1030                }
1031            }
1032
1033            try {
1034                ps.setString(1, "                   ");
1035                ps.execute();
1036                st = conn.createStatement();
1037                st.execute("select * from type where LText='                   '");
1038                ResultSet rs = st.getResultSet();
1039                assertEquals(1, getCount(rs));
1040            } finally {
1041                try {
1042                    st.close();
1043                } catch (Exception ee) {
1044                }
1045            }
1046
1047            ps.setString(1, null);
1048            ps.execute();
1049            ps.close();
1050
1051            try {
1052                ps.setString(1, "test text");
1053                fail("SQLException is not thrown");
1054            } catch (SQLException sqle) {
1055                // expected
1056            }
1057        } finally {
1058            try {
1059                ps.close();
1060            } catch (Exception ee) {
1061            }
1062        }
1063    }
1064
1065    // exception test fails
1066    public void testSetShort() throws SQLException {
1067        PreparedStatement ps = null;
1068        PreparedStatement ps1 = null;
1069        PreparedStatement ps2 = null;
1070        try {
1071            String query = "insert into type (Sint) values (?);";
1072            ps = conn.prepareStatement(query);
1073            Statement st = null;
1074            try {
1075                ps.setShort(1, Short.MAX_VALUE);
1076                ps.execute();
1077                st = conn.createStatement();
1078                st.execute("select * from type where Sint=" + Short.MAX_VALUE);
1079                ResultSet rs = st.getResultSet();
1080                assertEquals(1, getCount(rs));
1081            } finally {
1082                try {
1083                    st.close();
1084                } catch (Exception ee) {
1085                }
1086            }
1087
1088            try {
1089                ps.setShort(1, Short.MIN_VALUE);
1090                ps.execute();
1091                st = conn.createStatement();
1092                st.execute("select * from type where Sint=" + Short.MIN_VALUE);
1093                ResultSet rs = st.getResultSet();
1094                assertEquals(1, getCount(rs));
1095            } finally {
1096                try {
1097                    st.close();
1098                } catch (Exception ee) {
1099                }
1100            }
1101
1102            ps.close();
1103
1104            try {
1105                ps.setShort(1, Short.MIN_VALUE);
1106                fail("SQLException is not thrown");
1107            } catch (SQLException sqle) {
1108                // expected
1109            }
1110
1111            String query1 = "insert into type (Tint) values (?);";
1112            ps1 = conn.prepareStatement(query1);
1113            ps1.setShort(1, Short.MAX_VALUE);
1114
1115            String query2 = "insert into type (IntVal) values (?);";
1116            ps2 = conn.prepareStatement(query2);
1117            ps2.setShort(1, Short.MAX_VALUE);
1118            ps2.execute();
1119            st = conn.createStatement();
1120            st.execute("select * from type where IntVal=" + Short.MAX_VALUE);
1121            ResultSet rs = st.getResultSet();
1122            assertEquals(1, getCount(rs));
1123        } finally {
1124            try {
1125
1126                ps.close();
1127                ps1.close();
1128                ps2.close();
1129            } catch (Exception ee) {
1130            }
1131        }
1132    }
1133
1134    // exception test fails
1135    public void testSetBoolean() throws SQLException {
1136        PreparedStatement ps = null;
1137        PreparedStatement ps1 = null;
1138        try {
1139            String query = "insert into type (BoolVal) values (?);";
1140            ps = conn.prepareStatement(query);
1141            Statement st = null;
1142            try {
1143                ps.setBoolean(1, false);
1144                ps.execute();
1145                st = conn.createStatement();
1146                st.execute("select * from type where BoolVal = 0");
1147                ResultSet rs = st.getResultSet();
1148                assertEquals(1, getCount(rs));
1149            } finally {
1150                try {
1151                    st.close();
1152                } catch (Exception ee) {
1153                }
1154            }
1155
1156            try {
1157                ps.setBoolean(1, true);
1158                ps.execute();
1159                st = conn.createStatement();
1160                st.execute("select * from type where BoolVal= 1");
1161                ResultSet rs = st.getResultSet();
1162                assertEquals(2, getCount(rs));
1163            } finally {
1164                try {
1165                    st.close();
1166                } catch (Exception ee) {
1167                }
1168            }
1169
1170            ps.close();
1171
1172            try {
1173                ps.setBoolean(1, false);
1174                fail("SQLException is not thrown");
1175            } catch (SQLException sqle) {
1176                // expected
1177            }
1178
1179            String query1 = "insert into type (Tint) values (?);";
1180            ps1 = conn.prepareStatement(query1);
1181            ps1.setBoolean(1, true);
1182            ps1.execute();
1183        } finally {
1184            try {
1185
1186                ps.close();
1187                ps1.close();
1188            } catch (Exception ee) {
1189            }
1190        }
1191    }
1192
1193    // exception test fails
1194    public void testSetByte() throws SQLException {
1195        PreparedStatement ps = null;
1196        PreparedStatement ps1 = null;
1197        try {
1198            String query = "insert into type (Tint) values (?);";
1199            ps = conn.prepareStatement(query);
1200            Statement st = null;
1201            try {
1202                ps.setByte(1, Byte.MAX_VALUE);
1203                ps.execute();
1204                st = conn.createStatement();
1205                st.execute("select * from type where Tint=" + Byte.MAX_VALUE);
1206                ResultSet rs = st.getResultSet();
1207                assertEquals(1, getCount(rs));
1208            } finally {
1209                try {
1210                    st.close();
1211                } catch (SQLException ee) {
1212                }
1213            }
1214
1215            try {
1216                ps.setByte(1, Byte.MIN_VALUE);
1217                ps.execute();
1218                st = conn.createStatement();
1219                st.execute("select * from type where Tint=" + Byte.MIN_VALUE);
1220                ResultSet rs = st.getResultSet();
1221                assertEquals(1, getCount(rs));
1222            } finally {
1223                try {
1224                    st.close();
1225                } catch (SQLException ee) {
1226                }
1227            }
1228
1229            try {
1230                ps.setByte(2, Byte.MAX_VALUE);
1231                fail("SQLException is not thrown");
1232            } catch (Exception sqle) {
1233                // expected
1234            }
1235
1236            ps.close();
1237
1238            try {
1239                ps.setByte(1, Byte.MIN_VALUE);
1240                fail("SQLException is not thrown");
1241            } catch (SQLException sqle) {
1242                // expected
1243            }
1244
1245            String query1 = "insert into type (IntVal) values (?);";
1246            ps1 = conn.prepareStatement(query1);
1247            ps1.setByte(1, Byte.MAX_VALUE);
1248            ps1.execute();
1249        } finally {
1250            try {
1251
1252                ps.close();
1253                ps1.close();
1254            } catch (Exception ee) {
1255            }
1256        }
1257    }
1258
1259    // preparedStatement.execute() does not return false on update.
1260    public void testSetBytes() throws SQLException {
1261        byte[] bytesArray = {1, 0};
1262        PreparedStatement ps = null;
1263        PreparedStatement ps1 = null;
1264        try {
1265            String query = "insert into type (LBlob) values (?);";
1266            ps = conn.prepareStatement(query);
1267
1268            try {
1269                ps.setBytes(1, bytesArray);
1270                assertFalse(ps.execute());
1271                assertTrue(ps.getUpdateCount() > 0);
1272            } catch (SQLException sqle) {
1273                fail("SQLException is thrown: " + sqle.getMessage());
1274            }
1275
1276            try {
1277                ps.setBytes(2, bytesArray);
1278                fail("SQLException is not thrown");
1279            } catch (Exception sqle) {
1280                // expected RuntimeException or SQLException
1281            }
1282
1283            ps.close();
1284
1285            try {
1286                ps.setBytes(1, bytesArray);
1287                fail("SQLException is not thrown");
1288            } catch (SQLException sqle) {
1289                // expected
1290            }
1291            String query1 = "insert into type (TBlob) values (?);";
1292            ps1 = conn.prepareStatement(query1);
1293
1294            ps.setBytes(1, bytesArray);
1295            assertFalse(ps.execute());
1296            assertTrue(ps.getUpdateCount() > 0);
1297        } finally {
1298            try {
1299
1300                if (ps != null) ps.close();
1301                if (ps1 != null) ps1.close();
1302            } catch (Exception ee) {
1303            }
1304        }
1305    }
1306
1307    // preparedStatement.execute() does not return false on update.
1308    public void testSetBigDecimal() throws SQLException {
1309        BigDecimal bd = new BigDecimal("50");
1310        PreparedStatement ps = null;
1311        PreparedStatement ps1 = null;
1312        try {
1313            String query = "insert into type (DecVal) values (?);";
1314            ps = conn.prepareStatement(query);
1315            Statement st = null;
1316            ps.setBigDecimal(1, bd);
1317            assertFalse(ps.execute());
1318            assertTrue(ps.getUpdateCount() > 0);
1319
1320            try {
1321                ps.setBigDecimal(2, bd);
1322                fail("SQLException is not thrown");
1323            } catch (SQLException sqle) {
1324                // expected
1325                assertEquals("bad parameter index", sqle.getMessage());
1326            }
1327
1328            try {
1329                ps.setBigDecimal(-2, bd);
1330                fail("SQLException is not thrown");
1331            } catch (SQLException sqle) {
1332                // expected
1333                assertEquals("bad parameter index", sqle.getMessage());
1334            }
1335            String query1 = "insert into type (Tint) values (?);";
1336            ps1 = conn.prepareStatement(query1);
1337
1338            ps1.setBigDecimal(1, bd);
1339        } finally {
1340            try {
1341                if (ps != null) ps.close();
1342                if (ps1 != null) ps1.close();
1343            } catch (SQLException ee) {
1344            }
1345        }
1346    }
1347
1348    /**
1349     * preparedStatement.execute() does not return false on update. Setting a
1350     * data for a declared INTEGER should throw Exception
1351     */
1352    public void testSetDate_int_Date() throws SQLException {
1353        Calendar cal = new GregorianCalendar(1799, 5, 26);
1354        Date[] dates = {
1355                new Date(cal.getTimeInMillis()), new Date(Integer.MAX_VALUE),
1356                new Date(123456789)};
1357
1358        PreparedStatement ps = null;
1359        PreparedStatement ps1 = null;
1360        try {
1361            String query = "insert into type (dateVal) values (?);";
1362            ps = conn.prepareStatement(query);
1363
1364            for (int i = 0; i < dates.length; i++) {
1365                ps.setDate(1, dates[i]);
1366                assertFalse(ps.execute());
1367                assertTrue(ps.getUpdateCount() > 0);
1368            }
1369
1370            try {
1371                ps.setDate(2, dates[0]);
1372                fail("SQLException is not thrown");
1373            } catch (Exception sqle) {
1374                // expected
1375            }
1376
1377            ps.close();
1378
1379            try {
1380                ps.setDate(1, dates[0]);
1381                fail("SQLException is not thrown");
1382            } catch (SQLException sqle) {
1383                // expected
1384            }
1385
1386            String query1 = "insert into type (Tint) values (?);";
1387            ps1 = conn.prepareStatement(query1);
1388
1389            try {
1390                ps1.setDate(1, dates[0]);
1391                fail("SQLException is not thrown");
1392            } catch (SQLException sqle) {
1393                // expected
1394                assertEquals("SQLite.Exception: error in prepare", sqle
1395                        .getMessage());
1396            }
1397        } finally {
1398            try {
1399                if (ps != null) ps.close();
1400                if (ps1 != null) ps1.close();
1401            } catch (SQLException ee) {
1402            }
1403        }
1404    }
1405
1406    // preparedStatement.execute() does not return false on update.
1407    public void testSetDate_int_Date_Calendar() throws SQLException {
1408        Calendar[] cals = { Calendar.getInstance(),
1409                Calendar.getInstance(Locale.GERMANY),
1410                Calendar.getInstance(TimeZone.getDefault()) };
1411        Calendar cal = new GregorianCalendar(1799,5,26);
1412
1413        Date[] dates = { new Date(cal.getTimeInMillis()), new Date(Integer.MAX_VALUE),
1414                new Date(123456789) };
1415
1416        PreparedStatement ps = null;
1417        PreparedStatement ps1 = null;
1418        try {
1419            String query = "insert into type (dateVal) values (?);";
1420            ps = conn.prepareStatement(query);
1421
1422            for (int i = 0; i < dates.length; i++) {
1423                ps.setDate(1, dates[i], cals[i]);
1424                assertFalse(ps.execute());
1425                assertTrue(ps.getUpdateCount() > 0);
1426            }
1427
1428            try {
1429                ps.setDate(2, dates[0], cals[0]);
1430                ps.execute();
1431                fail("SQLException is not thrown");
1432            } catch (Exception sqle) {
1433                // expected
1434            }
1435
1436            ps.close();
1437
1438            try {
1439                ps.setDate(1, dates[0], cals[1]);
1440                fail("SQLException is not thrown");
1441            } catch (Exception sqle) {
1442                // expected
1443            }
1444            String query1 = "insert into type (Tint) values (?);";
1445            ps1 = conn.prepareStatement(query1);
1446
1447            ps1.setDate(1, dates[0], cals[2]);
1448            ps1.execute();
1449        } finally {
1450            try {
1451                if (ps != null) ps.close();
1452                if (ps1 != null) ps1.close();
1453            } catch (SQLException ee) {
1454            }
1455        }
1456    }
1457
1458    /**
1459     * This test doesn't pass on RI
1460     */
1461    public void testSetNull_int_int() throws SQLException {
1462        PreparedStatement ps = null;
1463        try {
1464            String query = "insert into type (BoolVal, IntVal) values ('true', ?);";
1465            ps = conn.prepareStatement(query);
1466            Statement st = null;
1467            try {
1468                ps.setNull(1, Types.INTEGER);
1469                ps.execute();
1470            } finally {
1471                try {
1472                    ps.close();
1473                } catch (Exception ee) {
1474                }
1475            }
1476
1477            query = "insert into type (BoolVal, LongVal) values ('true', ?);";
1478            ps = conn.prepareStatement(query);
1479
1480            try {
1481                ps.setNull(1, Types.BIGINT);
1482                ps.execute();
1483            } finally {
1484                try {
1485                    ps.close();
1486                } catch (Exception ee) {
1487                }
1488            }
1489
1490            query = "insert into type (BoolVal, DecVal) values ('true', ?)";
1491            ps = conn.prepareStatement(query);
1492
1493            try {
1494                ps.setNull(1, Types.DECIMAL);
1495                ps.execute();
1496            } finally {
1497                try {
1498                    ps.close();
1499                } catch (Exception ee) {
1500                }
1501            }
1502
1503            query = "insert into type (BoolVal, dateVal) values ('true', ?);";
1504            ps = conn.prepareStatement(query);
1505
1506            try {
1507                ps.setNull(1, Types.DATE);
1508                ps.execute();
1509            } finally {
1510                try {
1511                    ps.close();
1512                } catch (Exception ee) {
1513                }
1514            }
1515
1516            query = "insert into type (BoolVal, BlobVal) values ('true', ?);";
1517            ps = conn.prepareStatement(query);
1518
1519            try {
1520                ps.setNull(1, Types.BLOB);
1521                ps.execute();
1522            } finally {
1523                try {
1524                    ps.close();
1525                } catch (Exception ee) {
1526                }
1527            }
1528
1529            query = "insert into type (BoolVal, TextVal) values ('true', ?);";
1530            ps = conn.prepareStatement(query);
1531            ps.setNull(1, Types.CHAR);
1532            ps.execute();
1533        } finally {
1534            try {
1535                ps.close();
1536            } catch (Exception ee) {
1537            }
1538        }
1539    }
1540
1541    /**
1542     * UDTs and Ref types not supported in SQLite v 3
1543     */
1544    public void testSetNullIntintString() throws SQLException {
1545        // test UDT
1546        String typeCreationStmtUDT = "CREATE TYPE addressType AS "
1547                + "( street INTEGER, zip TEXT);";
1548        String personTableCreateUDT = "CREATE TABLE person (name TEXT, address addressType);";
1549        Statement st = null;
1550        PreparedStatement ps = null;
1551        try {
1552            st = conn.createStatement();
1553            st.execute(typeCreationStmtUDT);
1554            st.execute(personTableCreateUDT);
1555            fail("UDTs and Ref Types not supported");
1556            String query = "insert into person (name, address) values ('Hans', ?);";
1557            ps = conn.prepareStatement(query);
1558            try {
1559                ps.setNull(1, Types.DATALINK);
1560                ps.execute();
1561            } catch (SQLException sqle) {
1562                fail("SQLException is thrown: " + sqle.getMessage());
1563                sqle.printStackTrace();
1564            } finally {
1565                try {
1566                    st.close();
1567                } catch (Exception ee) {
1568                }
1569            }
1570        } catch (SQLException e) {
1571            // UDTs or Ref types not supported
1572            // ok
1573        } finally {
1574            try {
1575                st.execute("drop table if exists person");
1576                ps.close();
1577            } catch (Exception ee) {
1578            }
1579        }
1580
1581        // test non UDT REF type Exception checking
1582        String personTableCreate = "create table person (name TEXT, Address TEXT)";
1583        try {
1584            st = conn.createStatement();
1585            st.execute(personTableCreate);
1586            String insert
1587                    = "insert into person (name, address) values (?, '1600 Amphitheatre Mountain View');";
1588            ps = conn.prepareStatement(insert);
1589            try {
1590                ps.setNull(1, 1, "");
1591                ps.execute();
1592            } catch (SQLException sqle) {
1593                assertEquals("SQLite.Exception: error in step", sqle.getMessage());
1594            } finally {
1595                try {
1596                    st.close();
1597                } catch (Exception ee) {
1598                }
1599            }
1600        } finally {
1601            try {
1602                st.execute("drop table if exists person");
1603                ps.close();
1604            } catch (Exception ee) {
1605            }
1606        }
1607
1608        // test non UDT REF type OK
1609
1610        personTableCreate = "create table person (name TEXT, Address TEXT)";
1611        try {
1612            st = conn.createStatement();
1613            st.execute("drop table if exists person");
1614            st.execute(personTableCreate);
1615            String insert
1616                    = "insert into person (name, address) values (?, '1600 Amphitheatre Mountain View');";
1617            ps = conn.prepareStatement(insert);
1618            try {
1619                ps.setNull(1, 1, "");
1620                ps.execute();
1621            } finally {
1622                try {
1623                    st.close();
1624                } catch (Exception ee) {
1625                }
1626            }
1627        } finally {
1628            try {
1629                st.execute("drop table if exists person");
1630                ps.close();
1631            } catch (Exception ee) {
1632            }
1633        }
1634    }
1635
1636    // exception test fails
1637    public void testSetObject_int_Object() throws SQLException {
1638        PreparedStatement ps = null;
1639        try {
1640            String query = "insert into type (IntVal) values (?);";
1641            ps = conn.prepareStatement(query);
1642            Statement st = null;
1643            try {
1644                ps.setObject(1, Integer.MAX_VALUE);
1645                ps.execute();
1646                st = conn.createStatement();
1647                st.execute("select * from type where IntVal="
1648                        + Integer.MAX_VALUE);
1649                ResultSet rs = st.getResultSet();
1650                assertEquals(1, getCount(rs));
1651            } finally {
1652                try {
1653                    st.close();
1654                } catch (Exception ee) {
1655                }
1656            }
1657
1658            query = "insert into type (LongVal) values (?);";
1659            ps = conn.prepareStatement(query);
1660
1661            try {
1662                ps.setObject(1, "test text");
1663                ps.execute();
1664                st = conn.createStatement();
1665                st.execute("select * from type where LongVal='test text';");
1666                ResultSet rs = st.getResultSet();
1667                assertEquals(1, getCount(rs));
1668            } finally {
1669                try {
1670                    st.close();
1671                } catch (SQLException ee) {
1672                }
1673            }
1674
1675            query = "insert into type (DecVal) values (?);";
1676            ps = conn.prepareStatement(query);
1677
1678            try {
1679                ps.setObject(1, new Object());
1680                ps.execute();
1681            } catch (SQLException sqle) {
1682                fail("SQLException is thrown");
1683            }
1684
1685            query = "insert into type (dateVal) values (?);";
1686            ps = conn.prepareStatement(query);
1687            Date d = new Date(123456789);
1688
1689            try {
1690                ps.setObject(1, d);
1691                ps.execute();
1692                st = conn.createStatement();
1693                st.execute("select * from type where dateVal='"
1694                        + d.getTime() + "';");
1695                ResultSet rs = st.getResultSet();
1696                assertEquals(1, getCount(rs));
1697            } finally {
1698                try {
1699                    st.close();
1700                } catch (Exception ee) {
1701                }
1702            }
1703
1704            // this sub test doesn't pass on RI
1705            query = "insert into type (BlobVal) values (?);";
1706            ps = conn.prepareStatement(query);
1707
1708            try {
1709                ps.setObject(1, null);
1710                ps.execute();
1711            } finally {
1712                try {
1713                    st.close();
1714                } catch (SQLException ee) {
1715                }
1716            }
1717
1718        } finally {
1719            try {
1720                ps.close();
1721            } catch (Exception ee) {
1722            }
1723        }
1724        try {
1725            ps.setObject(1, "test text");
1726            fail("Exception not thrown");
1727        } catch (SQLException e) {
1728            // ok
1729        }
1730
1731    }
1732
1733     /**
1734      * This test doesn't pass on RI
1735      */
1736    public void testSetObject_int_Object_int() throws SQLException {
1737        PreparedStatement ps = null;
1738        try {
1739            String query = "insert into type (IntVal) values (?);";
1740            ps = conn.prepareStatement(query);
1741            Statement st = null;
1742            try {
1743                ps.setObject(1, Integer.MAX_VALUE, Types.INTEGER);
1744                ps.execute();
1745                st = conn.createStatement();
1746                st.execute("select * from type where IntVal="
1747                        + Integer.MAX_VALUE);
1748                ResultSet rs = st.getResultSet();
1749                assertEquals(1, getCount(rs));
1750            } finally {
1751                try {
1752                    st.close();
1753                } catch (Exception ee) {
1754                }
1755            }
1756
1757            query = "insert into type (LongVal) values (?);";
1758            ps = conn.prepareStatement(query);
1759
1760            try {
1761                ps.setObject(1, "test text", Types.CHAR);
1762                ps.execute();
1763                st = conn.createStatement();
1764                st.execute("select * from type where LongVal='test text';");
1765                ResultSet rs = st.getResultSet();
1766                assertEquals(1, getCount(rs));
1767            } finally {
1768                try {
1769                    st.close();
1770                } catch (Exception ee) {
1771                }
1772            }
1773
1774            query = "insert into type (DecVal) values (?);";
1775            ps = conn.prepareStatement(query);
1776            ps.setObject(1, new Object(), Types.DECIMAL);
1777            ps.execute();
1778
1779            query = "insert into type (dateVal) values (?);";
1780            ps = conn.prepareStatement(query);
1781            Date d = new Date(123456789);
1782
1783            try {
1784                ps.setObject(1, d, Types.DATE);
1785                ps.execute();
1786                st = conn.createStatement();
1787                st.execute("select * from type where dateVal='"
1788                        + d.getTime() + "';");
1789                ResultSet rs = st.getResultSet();
1790                assertEquals(1, getCount(rs));
1791            } finally {
1792                try {
1793                    st.close();
1794                } catch (Exception ee) {
1795                }
1796            }
1797
1798            // this sub test doesn't pass on RI
1799            query = "insert into type (BlobVal) values (?);";
1800            ps = conn.prepareStatement(query);
1801
1802            try {
1803                ps.setObject(1, "", Types.BLOB);
1804                ps.execute();
1805            } finally {
1806                try {
1807                    st.close();
1808                } catch (Exception ee) {
1809                }
1810            }
1811        } finally {
1812            try {
1813                ps.close();
1814            } catch (Exception ee) {
1815            }
1816        }
1817
1818        try {
1819            ps.setObject(1, Integer.MAX_VALUE, Types.INTEGER);
1820            fail("Exception not thrown");
1821        } catch (SQLException e) {
1822            // ok
1823        }
1824    }
1825
1826    /**
1827     * This test doesn't pass on RI; Fails for Types.DATE
1828     */
1829    public void testSetObject_int_Object_int_int() throws SQLException {
1830        PreparedStatement ps = null;
1831        try {
1832            String query = "insert into type (IntVal) values (?);";
1833            ps = conn.prepareStatement(query);
1834            Statement st = null;
1835            try {
1836                ps.setObject(1, Integer.MAX_VALUE, Types.INTEGER,
1837                        Integer.MAX_VALUE);
1838                ps.execute();
1839                st = conn.createStatement();
1840                st.execute("select * from type where IntVal="
1841                        + Integer.MAX_VALUE);
1842                ResultSet rs = st.getResultSet();
1843                assertEquals(1, getCount(rs));
1844            } finally {
1845                try {
1846                    st.close();
1847                } catch (Exception ee) {
1848                }
1849            }
1850
1851            query = "insert into type (LongVal) values (?);";
1852            ps = conn.prepareStatement(query);
1853
1854            try {
1855                ps.setObject(1, "test text", Types.CHAR, Integer.MIN_VALUE);
1856                ps.execute();
1857                st = conn.createStatement();
1858                st.execute("select * from type where LongVal='test text';");
1859                ResultSet rs = st.getResultSet();
1860                assertEquals(1, getCount(rs));
1861            } finally {
1862                try {
1863                    st.close();
1864                } catch (Exception ee) {
1865                }
1866            }
1867
1868            query = "insert into type (DecVal) values (?);";
1869            ps = conn.prepareStatement(query);
1870            BigDecimal bd2 = new BigDecimal("12.21");
1871            ps.setObject(1, bd2, Types.DECIMAL, 2);
1872            ps.execute();
1873
1874            query = "insert into type (dateVal) values (?);";
1875            ps = conn.prepareStatement(query);
1876            Date d = new Date(123456789);
1877            try {
1878                ps.setObject(1, d , Types.DATE, -1);
1879                ps.execute();
1880                st = conn.createStatement();
1881                st.execute("select * from type where dateVal='"
1882                        + d.getTime() + "';");
1883                ResultSet rs = st.getResultSet();
1884                assertEquals(1, getCount(rs));
1885            } finally {
1886                try {
1887                    st.close();
1888                } catch (Exception ee) {
1889                }
1890            }
1891
1892            // this sub test doesn't pass on RI
1893            query = "insert into type(BlobVal) values (?);";
1894            ps = conn.prepareStatement(query);
1895
1896            try {
1897                ps.setObject(1, "", Types.BLOB, 0);
1898                ps.execute();
1899            } finally {
1900                try {
1901                    st.close();
1902                } catch (Exception ee) {
1903                }
1904            }
1905        } finally {
1906            try {
1907                ps.close();
1908            } catch (Exception ee) {
1909            }
1910        }
1911
1912        try {
1913            ps.setObject(1, "test text", Types.CHAR, Integer.MIN_VALUE);
1914            fail("Exception not thrown");
1915        } catch (SQLException e) {
1916            // ok
1917        }
1918    }
1919
1920    // statement.close() does not wrap up
1921    public void testSetTimeint_Time() throws SQLException {
1922        Time[] times = { new Time(24, 25, 26), new Time(Integer.MAX_VALUE),
1923                new Time(123456789) };
1924
1925        PreparedStatement ps = null;
1926        PreparedStatement ps1 = null;
1927        try {
1928            String query = "insert into type (timeVal) values (?);";
1929            ps = conn.prepareStatement(query);
1930            Statement st = null;
1931            for (int i = 0; i < times.length; i++) {
1932                try {
1933                    ps.setTime(1, times[i]);
1934                    ps.execute();
1935                    st = conn.createStatement();
1936                    st.execute("select * from type where timeVal='"
1937                            + times[i].getTime() + "'");
1938                    ResultSet rs = st.getResultSet();
1939                    assertEquals(1, getCount(rs));
1940                } finally {
1941                    try {
1942                        st.close();
1943                    } catch (Exception ee) {
1944                    }
1945                }
1946            }
1947
1948            try {
1949                ps.setTime(2, times[0]);
1950                fail("SQLException is not thrown");
1951            } catch (Exception sqle) {
1952                // expected index out of bounds
1953            }
1954
1955            ps.close();
1956
1957            try {
1958                ps.setTime(1, times[0]);
1959                fail("SQLException is not thrown");
1960            } catch (SQLException sqle) {
1961                // expected
1962            }
1963            String query1 = "insert into type (Tint) values (?)";
1964            ps1 = conn.prepareStatement(query1);
1965            ps1.setTime(1, times[0]);
1966            ps1.execute();
1967        } finally {
1968            try {
1969                ps.close();
1970                ps1.close();
1971            } catch (Exception ee) {
1972            }
1973        }
1974    }
1975
1976    // preparedStatement.execute() does not return False on update.
1977    public void testSetTime_int_Time_Calendar() throws SQLException {
1978        Calendar[] cals = { Calendar.getInstance(),
1979                Calendar.getInstance(Locale.GERMANY),
1980                Calendar.getInstance(TimeZone.getDefault()) };
1981
1982        Time[] times = { new Time(24, 25, 26), new Time(Integer.MAX_VALUE),
1983                new Time(123456789) };
1984
1985        PreparedStatement ps = null;
1986        PreparedStatement ps1 = null;
1987        try {
1988            String query = "insert into type (timeVal) values (?);";
1989            ps = conn.prepareStatement(query);
1990            Statement st = null;
1991            for (int i = 0; i < times.length; i++) {
1992                try {
1993                    ps.setTime(1, times[i], cals[i]);
1994                    assertFalse(ps.execute());
1995                    assertTrue(ps.getUpdateCount() > 0);
1996                } finally {
1997                    try {
1998                        st.close();
1999                    } catch (Exception ee) {
2000                    }
2001                }
2002            }
2003
2004            try {
2005                ps.setTime(2, times[0], cals[0]);
2006                fail("SQLException is not thrown");
2007            } catch (Exception sqle) {
2008                // expected
2009            }
2010
2011            ps.close();
2012
2013            try {
2014                ps.setTime(-2, times[0], cals[1]);
2015                fail("SQLException is not thrown");
2016            } catch (Exception sqle) {
2017                // expected
2018            }
2019            String query1 = "insert into type (Tint) values (?);";
2020            ps1 = conn.prepareStatement(query1);
2021            ps1.setTime(1, times[0], cals[2]);
2022            ps1.execute();
2023        } finally {
2024            try {
2025                ps.close();
2026                ps1.close();
2027            } catch (Exception ee) {
2028            }
2029        }
2030    }
2031
2032    // preparedStatement.execute() does not return false on update.
2033    public void testSetTimestamp_int_Timestamp() throws SQLException {
2034        Timestamp[] timestamps = { new Timestamp(2007, 10, 17, 19, 06, 50, 23),
2035                new Timestamp(123) };
2036
2037        PreparedStatement ps = null;
2038        PreparedStatement ps1 = null;
2039        try {
2040            String query = "insert into type (TS) values (?);";
2041            ps = conn.prepareStatement(query);
2042
2043            for (int i = 0; i < timestamps.length; i++) {
2044                ps.setTimestamp(1, timestamps[i]);
2045                assertFalse(ps.execute());
2046                assertTrue(ps.getUpdateCount() > 0);
2047            }
2048
2049            try {
2050                ps.setTimestamp(2, timestamps[0]);
2051                fail("SQLException is not thrown");
2052            } catch (Exception sqle) {
2053                // expected
2054            }
2055
2056            try {
2057                ps.setTimestamp(-2, timestamps[0]);
2058                fail("SQLException is not thrown");
2059            } catch (Exception sqle) {
2060                // expected
2061            }
2062            String query1 = "insert into type (Tint) values (?);";
2063            ps1 = conn.prepareStatement(query1);
2064            ps1.setTimestamp(1, timestamps[0]);
2065            ps1.execute();
2066        } finally {
2067            try {
2068                ps.close();
2069                ps1.close();
2070            } catch (Exception ee) {
2071            }
2072        }
2073    }
2074
2075    public void testSetBlob() {
2076        ResultSet res = null;
2077        PreparedStatement ps = null;
2078        Blob mock = new MockBlob();
2079        try {
2080            String neverExecutedQuery = "select TBlob from type;";
2081            ps = conn.prepareStatement(neverExecutedQuery);
2082            ps.setBlob(1,mock);
2083            fail("Exception expected not supported");
2084        } catch (SQLException e) {
2085            //ok
2086        }
2087    }
2088
2089    public void testSetClob() {
2090        ResultSet res = null;
2091        PreparedStatement ps = null;
2092        Clob mock = new MockClob();
2093        try {
2094            String neverExecutedQuery = "select TBlob from type;";
2095            ps = conn.prepareStatement(neverExecutedQuery);
2096            ps.setClob(1,mock);
2097            fail("Exception expected not supported");
2098        } catch (SQLException e) {
2099            //ok
2100        }
2101    }
2102
2103    // preparedStatement.execute() does not return false on update.
2104    public void testSetTimestampIntTimestampCalendar() throws SQLException {
2105        Calendar[] cals = { Calendar.getInstance(),
2106                Calendar.getInstance(Locale.GERMANY),
2107                Calendar.getInstance(TimeZone.getDefault()) };
2108
2109        Timestamp[] timestamps = { new Timestamp(2007, 10, 17, 19, 06, 50, 23),
2110                new Timestamp(123) };
2111
2112        PreparedStatement ps = null;
2113        PreparedStatement ps1 = null;
2114        try {
2115            String query = "insert into type (timeVal) values (?);";
2116            ps = conn.prepareStatement(query);
2117            Statement st = null;
2118            for (int i = 0; i < timestamps.length; i++) {
2119                try {
2120                    ps.setTimestamp(1, timestamps[i], cals[i]);
2121                    assertFalse(ps.execute());
2122                    assertTrue(ps.getUpdateCount() > 0);
2123                } finally {
2124                    try {
2125                        st.close();
2126                    } catch (Exception ee) {
2127                    }
2128                }
2129            }
2130
2131            try {
2132                ps.setTimestamp(2, timestamps[0], cals[0]);
2133                ps.execute();
2134                fail("SQLException is not thrown");
2135            } catch (Exception sqle) {
2136                // expected
2137            }
2138            ps.close();
2139            try {
2140                ps.setTimestamp(1, timestamps[0], cals[1]);
2141                ps.execute();
2142                fail("SQLException is not thrown");
2143            } catch (SQLException sqle) {
2144                // expected
2145            }
2146            String query1 = "insert into type (Tint) values (?);";
2147            ps1 = conn.prepareStatement(query1);
2148            ps1.setTimestamp(1, timestamps[0], cals[2]);
2149            ps1.execute();
2150        } finally {
2151            try {
2152                ps.close();
2153                ps1.close();
2154            } catch (Exception ee) {
2155            }
2156        }
2157    }
2158
2159    public void testSetURL() {
2160        ResultSet res = null;
2161        PreparedStatement ps = null;
2162        try {
2163            String query = "insert into type (TText) values (?);";
2164            ps = conn.prepareStatement(query);
2165            ps.setURL(1, new URL("http://www.android.com"));
2166            fail("Exception expected not supported");
2167        } catch (SQLException e) {
2168           //ok
2169        } catch (Exception e) {
2170            fail("Error in test setup "+e.getMessage());
2171            e.printStackTrace();
2172        }
2173
2174    }
2175
2176    public void testSetArray() {
2177        ResultSet res = null;
2178        PreparedStatement ps = null;
2179        Array a = new MockArray();
2180        try {
2181            String query = "insert into type (TText) values (?);";
2182            ps = conn.prepareStatement(query);
2183            ps.setArray(1, new MockArray());
2184            fail("Exception expected not supported");
2185        } catch (SQLException e) {
2186            //ok
2187        } catch (Exception e) {
2188            fail("Error in test setup "+e.getMessage());
2189            e.printStackTrace();
2190        }
2191
2192    }
2193
2194    public void testSetRef() {
2195        ResultSet res = null;
2196        PreparedStatement ps = null;
2197        Ref mock = new MockRef();
2198        try {
2199            String neverExecutedQuery = "select TBlob from type;";
2200            ps = conn.prepareStatement(neverExecutedQuery);
2201            ps.setRef(1,mock);
2202            fail("Exception expected not supported");
2203        } catch (SQLException e) {
2204            //ok
2205        }
2206
2207    }
2208
2209    public void testSetUnicodestream() {
2210        ResultSet res = null;
2211        PreparedStatement ps = null;
2212        try {
2213            String query = "insert into type (TText) values (?);";
2214            ps = conn.prepareStatement(query);
2215            InputStream file = Class.forName(this.getClass().getName())
2216            .getResourceAsStream("/blob.c");
2217            ps.setUnicodeStream(0, file, 100);
2218            fail("Exception expected not supported");
2219        } catch (SQLException e) {
2220            //ok
2221        } catch (Exception e) {
2222            fail("Error in test setup "+e.getMessage());
2223            e.printStackTrace();
2224        }
2225    }
2226
2227    public void testSetCharacterSteam() throws SQLException {
2228        String query = "insert into type (TText) values (?);";
2229        PreparedStatement ps = conn.prepareStatement(query);
2230        InputStream file = OldPreparedStatementTest.class.getResourceAsStream("/blob.c");
2231        assertNotNull("Error in test setup: file not found",file);
2232        Reader reader = new InputStreamReader(file);
2233        ps.setCharacterStream(1, reader, 100);
2234    }
2235
2236    public void testSetAsciiStream() {
2237        ResultSet res = null;
2238        try {
2239            String query = "insert into type (TText) values (?);";
2240            PreparedStatement ps = conn.prepareStatement(query);
2241            InputStream file = OldPreparedStatementTest.class.getResourceAsStream("/blob.c");
2242            ps.setAsciiStream(0, file, 100);
2243            fail("Exception expected not supported");
2244        } catch (SQLException e) {
2245            // ok
2246        } catch (Exception e) {
2247            fail("Error in test setup "+e.getMessage());
2248            e.printStackTrace();
2249        }
2250    }
2251
2252    public void testSetBinaryStream() throws Exception {
2253        try {
2254            String query = "insert into type (TText) values (?);";
2255            PreparedStatement ps = conn.prepareStatement(query);
2256            InputStream file = OldPreparedStatementTest.class.getResourceAsStream("/blob.c");
2257            ps.setBinaryStream(0, file, 100);
2258            fail("Exception expected not supported");
2259        } catch (SQLException expected) {
2260        }
2261    }
2262
2263    private class MockRef implements Ref {
2264        public String getBaseTypeName() throws SQLException {
2265            return null;
2266        }
2267
2268        public Object getObject() throws SQLException {
2269            return null;
2270        }
2271
2272        public Object getObject(Map<String, Class<?>> map) throws SQLException {
2273            return null;
2274        }
2275
2276        public void setObject(Object value) throws SQLException {}
2277    }
2278
2279    private class MockArray implements Array {
2280
2281        public Object getArray() throws SQLException {
2282            return null;
2283        }
2284
2285        public Object getArray(long index, int count) throws SQLException {
2286            return null;
2287        }
2288
2289        public Object getArray(long index, int count, Map<String, Class<?>> map)
2290                throws SQLException {
2291            return null;
2292        }
2293
2294        public Object getArray(Map<String, Class<?>> map) throws SQLException {
2295            return null;
2296        }
2297
2298        public int getBaseType() throws SQLException {
2299            return 0;
2300        }
2301
2302        public String getBaseTypeName() throws SQLException {
2303            return null;
2304        }
2305
2306        public ResultSet getResultSet() throws SQLException {
2307            return null;
2308        }
2309
2310        public ResultSet getResultSet(long index, int count)
2311                throws SQLException {
2312            return null;
2313        }
2314
2315        public ResultSet getResultSet(long index, int count,
2316                Map<String, Class<?>> map) throws SQLException {
2317            return null;
2318        }
2319
2320        public ResultSet getResultSet(Map<String, Class<?>> map)
2321                throws SQLException {
2322            return null;
2323        }
2324
2325        public void free() throws SQLException {}
2326    }
2327
2328    private class MockBlob implements Blob {
2329
2330        public InputStream getBinaryStream() throws SQLException {
2331            return null;
2332        }
2333
2334        public byte[] getBytes(long pos, int length) throws SQLException {
2335            return null;
2336        }
2337
2338        public long length() throws SQLException {
2339            return 0;
2340        }
2341
2342        public long position(Blob pattern, long start) throws SQLException {
2343            return 0;
2344        }
2345
2346        public long position(byte[] pattern, long start) throws SQLException {
2347            return 0;
2348        }
2349
2350        public OutputStream setBinaryStream(long pos) throws SQLException {
2351            return null;
2352        }
2353
2354        public int setBytes(long pos, byte[] theBytes) throws SQLException {
2355            return 0;
2356        }
2357
2358        public int setBytes(long pos, byte[] theBytes, int offset, int len)
2359                throws SQLException {
2360            return 0;
2361        }
2362
2363        public void truncate(long len) throws SQLException {
2364
2365        }
2366
2367        public void free() throws SQLException {}
2368
2369        public InputStream getBinaryStream(long pos, long length) throws SQLException {
2370            return null;
2371        }
2372    }
2373
2374    private class MockClob implements Clob {
2375
2376        public InputStream getAsciiStream() throws SQLException {
2377            return null;
2378        }
2379
2380        public Reader getCharacterStream() throws SQLException {
2381            return null;
2382        }
2383
2384        public String getSubString(long pos, int length) throws SQLException {
2385            return null;
2386        }
2387
2388        public long length() throws SQLException {
2389            return 0;
2390        }
2391
2392        public long position(Clob searchstr, long start) throws SQLException {
2393            return 0;
2394        }
2395
2396        public long position(String searchstr, long start) throws SQLException {
2397            return 0;
2398        }
2399
2400        public OutputStream setAsciiStream(long pos) throws SQLException {
2401            return null;
2402        }
2403
2404        public Writer setCharacterStream(long pos) throws SQLException {
2405            return null;
2406        }
2407
2408        public int setString(long pos, String str) throws SQLException {
2409            return 0;
2410        }
2411
2412        public int setString(long pos, String str, int offset, int len)
2413                throws SQLException {
2414            return 0;
2415        }
2416
2417        public void truncate(long len) throws SQLException {}
2418
2419        public void free() throws SQLException {}
2420
2421        public Reader getCharacterStream(long pos, long length) throws SQLException {
2422            return null;
2423        }
2424    }
2425}
2426