1package libcore.java.sql;
2
3import junit.framework.TestCase;
4
5import java.io.File;
6import java.io.IOException;
7import java.sql.Connection;
8import java.sql.Driver;
9import java.sql.DriverManager;
10
11public class ConnectionTest extends TestCase {
12
13    private File dbFile = null;
14    private String connectionURL = null;
15
16    @Override
17    public void setUp() throws Exception {
18        super.setUp();
19
20        // Trigger the static initializer that will cause the driver to register itself with
21        // DriverManager.
22        Class.forName("SQLite.JDBCDriver");
23    }
24
25    @Override
26    protected void tearDown() throws Exception {
27        super.tearDown();
28        if (dbFile != null) {
29            dbFile.delete();
30        }
31    }
32
33    public void testDriverManager_getConnection() throws Exception {
34        Connection c = DriverManager.getConnection(getConnectionURL());
35        assertFalse(c.isClosed());
36        c.close();
37        assertTrue(c.isClosed());
38    }
39
40    public void testConnect() throws Exception {
41        Driver driver = DriverManager.getDriver(getConnectionURL());
42        assertNotNull(driver);
43        Connection c = driver.connect(getConnectionURL(), null);
44        assertFalse(c.isClosed());
45        c.close();
46        assertTrue(c.isClosed());
47    }
48
49    private String getConnectionURL() {
50        if (connectionURL == null) {
51            String tmp = System.getProperty("java.io.tmpdir");
52            File tmpDir = new File(tmp);
53            if (tmpDir.isDirectory()) {
54                try {
55                    dbFile = File.createTempFile("OldJDBCDriverTest", ".db", tmpDir);
56                } catch (IOException e) {
57                    System.err.println("error creating temporary DB file.");
58                }
59                dbFile.deleteOnExit();
60            } else {
61                System.err.println("java.io.tmpdir does not exist");
62            }
63
64            connectionURL = "jdbc:sqlite:/" + dbFile.getPath();
65        }
66
67        return connectionURL;
68    }
69}
70