File: DriverTest.java

package info (click to toggle)
libpgjava 8.4-701-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,532 kB
  • ctags: 4,162
  • sloc: java: 33,948; xml: 3,158; makefile: 14; sh: 10
file content (75 lines) | stat: -rw-r--r-- 2,417 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/DriverTest.java,v 1.14 2008/01/08 06:56:31 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc2;

import org.postgresql.test.TestUtil;
import junit.framework.TestCase;
import java.sql.*;

/*
 * Tests the dynamically created class org.postgresql.Driver
 *
 */
public class DriverTest extends TestCase
{

    public DriverTest(String name)
    {
        super(name);
    }

    /*
     * This tests the acceptsURL() method with a couple of well and poorly
     * formed jdbc urls.
     */
    public void testAcceptsURL() throws Exception
    {
        TestUtil.initDriver(); // Set up log levels, etc.

        // Load the driver (note clients should never do it this way!)
        org.postgresql.Driver drv = new org.postgresql.Driver();
        assertNotNull(drv);

        // These are always correct
        assertTrue(drv.acceptsURL("jdbc:postgresql:test"));
        assertTrue(drv.acceptsURL("jdbc:postgresql://localhost/test"));
        assertTrue(drv.acceptsURL("jdbc:postgresql://localhost:5432/test"));
        assertTrue(drv.acceptsURL("jdbc:postgresql://127.0.0.1/anydbname"));
        assertTrue(drv.acceptsURL("jdbc:postgresql://127.0.0.1:5433/hidden"));
        assertTrue(drv.acceptsURL("jdbc:postgresql://[::1]:5740/db"));

        // Badly formatted url's
        assertTrue(!drv.acceptsURL("jdbc:postgres:test"));
        assertTrue(!drv.acceptsURL("postgresql:test"));
        assertTrue(!drv.acceptsURL("db"));

    }

    /*
     * Tests parseURL (internal)
     */
    /*
     * Tests the connect method by connecting to the test database
     */
    public void testConnect() throws Exception
    {
        TestUtil.initDriver(); // Set up log levels, etc.

        // Test with the url, username & password
        Connection con = DriverManager.getConnection(TestUtil.getURL(), TestUtil.getUser(), TestUtil.getPassword());
        assertNotNull(con);
        con.close();

        // Test with the username in the url
        con = DriverManager.getConnection(TestUtil.getURL() + "&user=" + TestUtil.getUser() + "&password=" + TestUtil.getPassword());
        assertNotNull(con);
        con.close();
    }
}