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