• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.core;
18 
19 import java.io.File;
20 import java.sql.Connection;
21 import java.sql.DriverManager;
22 import java.sql.PreparedStatement;
23 import java.sql.SQLException;
24 import java.sql.Statement;
25 import android.test.suitebuilder.annotation.MediumTest;
26 
27 /**
28  * Minimal test for JDBC driver
29  */
30 public class SQLiteJDBCDriverTest extends AbstractJDBCDriverTest {
31 
32     private File dbFile;
33 
34     @Override
setUp()35     protected void setUp() throws Exception {
36         super.setUp();
37         dbFile = File.createTempFile("sqliteTestDB", null);
38     }
39 
40     @Override
tearDown()41     protected void tearDown() throws Exception {
42         if(dbFile != null) {
43             dbFile.delete();
44         }
45         super.tearDown();
46     }
47 
48     @Override
getConnectionURL()49     protected String getConnectionURL() {
50         return "jdbc:sqlite:/" + dbFile;
51     }
52 
53     @Override
getDbFile()54     protected File getDbFile() {
55         return dbFile;
56     }
57 
58     @Override
getJDBCDriverClassName()59     protected String getJDBCDriverClassName() {
60         return "SQLite.JDBCDriver";
61     }
62 
63     // Regression test for (Noser) #255: PreparedStatement.executeUpdate results
64     // in VM crashing with SIGABRT.
65     @MediumTest
test_connection3()66     public void test_connection3() throws Exception {
67         PreparedStatement prst = null;
68         Statement st = null;
69         Connection conn = null;
70         try {
71             Class.forName("SQLite.JDBCDriver").newInstance();
72             if (dbFile.exists()) {
73                 dbFile.delete();
74             }
75             conn = DriverManager.getConnection("jdbc:sqlite:/"
76                         + dbFile.getPath());
77             assertNotNull(conn);
78 
79             // create table
80             st = conn.createStatement();
81             String sql = "CREATE TABLE zoo (ZID INTEGER NOT NULL, family VARCHAR (20) NOT NULL, name VARCHAR (20) NOT NULL, PRIMARY KEY(ZID) )";
82             st.executeUpdate(sql);
83 
84             String update = "update zoo set family = ? where name = ?;";
85             prst = conn.prepareStatement(update);
86             prst.setString(1, "cat");
87             prst.setString(2, "Yasha");
88             // st = conn.createStatement();
89             // st.execute("select * from zoo where family = 'cat'");
90             // ResultSet rs = st.getResultSet();
91             // assertEquals(0, getCount(rs));
92             prst.executeUpdate();
93             // st.execute("select * from zoo where family = 'cat'");
94             // ResultSet rs1 = st.getResultSet();
95             // assertEquals(1, getCount(rs1));
96             try {
97                 prst = conn.prepareStatement("");
98                 prst.execute();
99                 fail("SQLException is not thrown");
100             } catch (SQLException e) {
101                 // expected
102             }
103 
104             try {
105                 conn.prepareStatement(null);
106                 fail("NPE is not thrown");
107             } catch (Exception e) {
108                 // expected
109             }
110             try {
111                 st = conn.createStatement();
112                 st.execute("drop table if exists zoo");
113 
114             } catch (SQLException e) {
115                 fail("Couldn't drop table: " + e.getMessage());
116             } finally {
117                 try {
118                     st.close();
119                     conn.close();
120                 } catch (SQLException ee) {
121                 }
122             }
123         } finally {
124             try {
125                 if (prst != null) {
126                     prst.close();
127                 }
128                 if (st != null) {
129                     st.close();
130                 }
131             } catch (SQLException ee) {
132             }
133         }
134 
135     }
136 
137 }
138