1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package libcore.java.sql;
18 
19 import java.io.File;
20 import java.sql.Connection;
21 import java.sql.DriverManager;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.sql.Statement;
25 import junit.framework.TestCase;
26 
27 public abstract class OldSQLTest extends TestCase {
28     static Connection conn;
29 
setUp()30     @Override public void setUp() throws Exception {
31         getSQLiteConnection();
32         createZoo();
33     }
34 
35     protected File dbFile;
36 
getSQLiteConnection()37     protected void getSQLiteConnection() throws Exception {
38         String tmp = System.getProperty("java.io.tmpdir");
39         File tmpDir = new File(tmp);
40         if (tmpDir.isDirectory()) {
41             dbFile = File.createTempFile("sqliteTest", ".db", tmpDir);
42             dbFile.deleteOnExit();
43         } else {
44             System.err.println("java.io.tmpdir does not exist");
45         }
46 
47         Class.forName("SQLite.JDBCDriver").newInstance();
48         conn = DriverManager.getConnection("jdbc:sqlite:/" + dbFile.getPath());
49         assertNotNull("Connection created ", conn);
50     }
51 
tearDown()52     @Override public void tearDown() throws SQLException {
53         Statement st = null;
54         try {
55             if (! conn.isClosed()) {
56                 st = conn.createStatement();
57                 st.execute("drop table if exists zoo");
58             }
59         } finally {
60             try {
61                 if (st != null) {
62                     st.close();
63                     conn.close();
64                 }
65             } catch(SQLException ee) {
66                 //ignore
67             }
68         }
69     }
70 
createZoo()71     public void createZoo() throws SQLException {
72         String[] queries = {
73                 "create table zoo(id smallint,  name varchar(10), family varchar(10))",
74                 "insert into zoo values (1, 'Kesha', 'parrot')",
75                 "insert into zoo values (2, 'Yasha', 'sparrow')" };
76 
77         Statement st = null;
78         try {
79             st = conn.createStatement();
80             for (int i = 0; i < queries.length; i++) {
81                 st.execute(queries[i]);
82             }
83         } finally {
84             try {
85                 if (st != null) {
86                     st.close();
87                 }
88              } catch (SQLException ee) {}
89         }
90     }
91 
getCount(ResultSet rs)92     public int getCount(ResultSet rs) {
93         int count = 0;
94         try {
95             while (rs.next()) {
96                 count++;
97             }
98         } catch (SQLException e) {
99             fail("SQLException is thrown");
100         }
101         return count;
102     }
103 }
104