File: ConnectionTest.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 (307 lines) | stat: -rw-r--r-- 9,054 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/ConnectionTest.java,v 1.23 2008/01/08 06:56:30 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc2;

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

/*
 * TestCase to test the internal functionality of org.postgresql.jdbc2.Connection
 * and it's superclass.
 *
 */

public class ConnectionTest extends TestCase
{

    private Connection con;

    /*
     * Constructor
     */
    public ConnectionTest(String name)
    {
        super(name);
    }

    // Set up the fixture for this testcase: the tables for this test.
    protected void setUp() throws Exception
    {
        con = TestUtil.openDB();

        TestUtil.createTable(con, "test_a", "imagename name,image oid,id int4");
        TestUtil.createTable(con, "test_c", "source text,cost money,imageid int4");

        TestUtil.closeDB(con);
    }

    // Tear down the fixture for this test case.
    protected void tearDown() throws Exception
    {
        TestUtil.closeDB(con);
        
        con = TestUtil.openDB();

        TestUtil.dropTable(con, "test_a");
        TestUtil.dropTable(con, "test_c");

        TestUtil.closeDB(con);
    }

    /*
     * Tests the two forms of createStatement()
     */
    public void testCreateStatement() throws Exception
    {
        con = TestUtil.openDB();

        // A standard Statement
        Statement stat = con.createStatement();
        assertNotNull(stat);
        stat.close();

        // Ask for Updateable ResultSets
        stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        assertNotNull(stat);
        stat.close();
    }

    /*
     * Tests the two forms of prepareStatement()
     */
    public void testPrepareStatement() throws Exception
    {
        con = TestUtil.openDB();

        String sql = "select source,cost,imageid from test_c";

        // A standard Statement
        PreparedStatement stat = con.prepareStatement(sql);
        assertNotNull(stat);
        stat.close();

        // Ask for Updateable ResultSets
        stat = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        assertNotNull(stat);
        stat.close();
    }

    /*
     * Put the test for createPrepareCall here
     */
    public void testPrepareCall()
    {
    }

    /*
     * Test nativeSQL
     */
    public void testNativeSQL() throws Exception
    {
        // test a simple escape
        con = TestUtil.openDB();
        assertEquals("DATE  '2005-01-24'",con.nativeSQL("{d '2005-01-24'}"));
    }

    /*
     * Test autoCommit (both get & set)
     */
    public void testTransactions() throws Exception
    {
        con = TestUtil.openDB();
        Statement st;
        ResultSet rs;

        // Turn it off
        con.setAutoCommit(false);
        assertTrue(!con.getAutoCommit());

        // Turn it back on
        con.setAutoCommit(true);
        assertTrue(con.getAutoCommit());

        // Now test commit
        st = con.createStatement();
        st.executeUpdate("insert into test_a (imagename,image,id) values ('comttest',1234,5678)");

        con.setAutoCommit(false);

        // Now update image to 9876 and commit
        st.executeUpdate("update test_a set image=9876 where id=5678");
        con.commit();
        rs = st.executeQuery("select image from test_a where id=5678");
        assertTrue(rs.next());
        assertEquals(9876, rs.getInt(1));
        rs.close();

        // Now try to change it but rollback
        st.executeUpdate("update test_a set image=1111 where id=5678");
        con.rollback();
        rs = st.executeQuery("select image from test_a where id=5678");
        assertTrue(rs.next());
        assertEquals(9876, rs.getInt(1)); // Should not change!
        rs.close();

        TestUtil.closeDB(con);
    }

    /*
     * Simple test to see if isClosed works.
     */
    public void testIsClosed() throws Exception
    {
        con = TestUtil.openDB();

        // Should not say closed
        assertTrue(!con.isClosed());

        TestUtil.closeDB(con);

        // Should now say closed
        assertTrue(con.isClosed());
    }

    /*
     * Test the warnings system
     */
    public void testWarnings() throws Exception
    {
        con = TestUtil.openDB();

        String testStr = "This Is OuR TeSt message";

        // The connection must be ours!
        assertTrue(con instanceof org.postgresql.PGConnection);

        // Clear any existing warnings
        con.clearWarnings();

        // Set the test warning
        ((org.postgresql.jdbc2.AbstractJdbc2Connection)con).addWarning(new SQLWarning(testStr));

        // Retrieve it
        SQLWarning warning = con.getWarnings();
        assertNotNull(warning);
        assertEquals(testStr, warning.getMessage());

        // Finally test clearWarnings() this time there must be something to delete
        con.clearWarnings();
        assertTrue(con.getWarnings() == null);

        TestUtil.closeDB(con);
    }

    /*
     * Transaction Isolation Levels
     */
    public void testTransactionIsolation() throws Exception
    {
        con = TestUtil.openDB();

        // PostgreSQL defaults to READ COMMITTED
        assertEquals(Connection.TRANSACTION_READ_COMMITTED,
                     con.getTransactionIsolation());

        // Begin a transaction
        con.setAutoCommit(false);

        // The isolation level should not have changed
        assertEquals(Connection.TRANSACTION_READ_COMMITTED,
                     con.getTransactionIsolation());

        // Now run some tests with autocommit enabled.
        con.setAutoCommit(true);

        assertEquals(Connection.TRANSACTION_READ_COMMITTED,
                     con.getTransactionIsolation());

        con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        assertEquals(Connection.TRANSACTION_SERIALIZABLE,
                     con.getTransactionIsolation());

        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        assertEquals(Connection.TRANSACTION_READ_COMMITTED, con.getTransactionIsolation());

        // Test if a change of isolation level before beginning the
        // transaction affects the isolation level inside the transaction.
        con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        assertEquals(Connection.TRANSACTION_SERIALIZABLE,
                     con.getTransactionIsolation());
        con.setAutoCommit(false);
        assertEquals(Connection.TRANSACTION_SERIALIZABLE,
                     con.getTransactionIsolation());
        con.setAutoCommit(true);
        assertEquals(Connection.TRANSACTION_SERIALIZABLE,
                     con.getTransactionIsolation());
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        assertEquals(Connection.TRANSACTION_READ_COMMITTED,
                     con.getTransactionIsolation());
        con.setAutoCommit(false);
        assertEquals(Connection.TRANSACTION_READ_COMMITTED,
                     con.getTransactionIsolation());
        con.commit();

        // Test that getTransactionIsolation() does not actually start a new txn.
        con.getTransactionIsolation(); // Shouldn't start a new transaction.
        con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); // Should be ok -- we're not in a transaction.
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); // Should still be ok.

        // Test that we can't change isolation mid-transaction
        Statement stmt = con.createStatement();
        stmt.executeQuery("SELECT 1");          // Start transaction.
        stmt.close();

        try
        {
            con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
            fail("Expected an exception when changing transaction isolation mid-transaction");
        }
        catch (SQLException e)
        {
            // Ok.
        }

        con.rollback();
        TestUtil.closeDB(con);
    }

    /*
     * JDBC2 Type mappings
     */
    public void testTypeMaps() throws Exception
    {
        con = TestUtil.openDB();

        // preserve the current map
        java.util.Map oldmap = con.getTypeMap();

        // now change it for an empty one
        java.util.Map newmap = new java.util.HashMap();
        con.setTypeMap(newmap);
        assertEquals(newmap, con.getTypeMap());

        // restore the old one
        con.setTypeMap(oldmap);
        assertEquals(oldmap, con.getTypeMap());

        TestUtil.closeDB(con);
    }

    /**
     * Closing a Connection more than once is not an error.
     */
    public void testDoubleClose() throws Exception
    {
        con = TestUtil.openDB();
        con.close();
        con.close();
    }
}