ConnectionTest.java 8.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package org.postgresql.test.jdbc2;

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

/**
 * TestCase to test the internal functionality of org.postgresql.jdbc2.Connection
 * and it's superclass.
 *
 * PS: Do you know how difficult it is to type on a train? ;-)
 *
13
 * $Id: ConnectionTest.java,v 1.5 2001/09/23 04:11:14 momjian Exp $
14 15 16 17 18 19 20 21 22 23 24
 */

public class ConnectionTest extends TestCase {

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

25 26 27 28
  // Set up the fixture for this testcase: the tables for this test.
  protected void setUp() throws Exception {
  	Connection con = JDBC2Tests.openDB();

29 30
	JDBC2Tests.createTable(con, "test_a", "imagename name,image oid,id int4");
	JDBC2Tests.createTable(con, "test_c", "source text,cost money,imageid int4");
31 32 33 34 35 36 37 38

	JDBC2Tests.closeDB(con);
  }

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

39 40 41
	JDBC2Tests.dropTable(con, "test_a");
	JDBC2Tests.dropTable(con, "test_c");
	
42 43 44
    JDBC2Tests.closeDB(con);
  }

45 46 47 48 49 50 51 52 53
  /**
   * Tests the two forms of createStatement()
   */
  public void testCreateStatement() {
    try {
      java.sql.Connection conn = JDBC2Tests.openDB();

      // A standard Statement
      java.sql.Statement stat = conn.createStatement();
54
      assertNotNull(stat);
55 56 57 58
      stat.close();

      // Ask for Updateable ResultSets
      stat = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE);
59
      assertNotNull(stat);
60 61 62
      stat.close();

    } catch(SQLException ex) {
63
      assertTrue(ex.getMessage(),false);
64 65 66 67 68 69 70 71 72 73 74 75 76 77
    }
  }

  /**
   * Tests the two forms of prepareStatement()
   */
  public void testPrepareStatement() {
    try {
      java.sql.Connection conn = JDBC2Tests.openDB();

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

      // A standard Statement
      java.sql.PreparedStatement stat = conn.prepareStatement(sql);
78
      assertNotNull(stat);
79 80 81 82
      stat.close();

      // Ask for Updateable ResultSets
      stat = conn.prepareStatement(sql,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE);
83
      assertNotNull(stat);
84 85 86
      stat.close();

    } catch(SQLException ex) {
87
      assertTrue(ex.getMessage(),false);
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
    }
  }

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

  /**
   * Test nativeSQL
   */
  public void testNativeSQL() {
    // For now do nothing as it returns itself
  }

  /**
   * Test autoCommit (both get & set)
   */
  public void testTransactions() {
    try {
      java.sql.Connection con = JDBC2Tests.openDB();
      java.sql.Statement st;
      java.sql.ResultSet rs;

      // Turn it off
      con.setAutoCommit(false);
115
      assertTrue(!con.getAutoCommit());
116 117 118

      // Turn it back on
      con.setAutoCommit(true);
119
      assertTrue(con.getAutoCommit());
120 121 122 123 124 125 126 127 128 129 130

      // 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");
131 132
      assertTrue(rs.next());
      assertEquals(9876, rs.getInt(1));
133 134 135 136 137 138
      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");
139 140
      assertTrue(rs.next());
      assertEquals(9876, rs.getInt(1)); // Should not change!
141 142 143 144
      rs.close();

      JDBC2Tests.closeDB(con);
    } catch(SQLException ex) {
145
      assertTrue(ex.getMessage(),false);
146 147 148 149
    }
  }

  /**
150
   * Simple test to see if isClosed works.
151 152 153 154 155 156
   */
  public void testIsClosed() {
    try {
      Connection con = JDBC2Tests.openDB();

      // Should not say closed
157
      assertTrue(!con.isClosed());
158 159 160 161

      JDBC2Tests.closeDB(con);

      // Should now say closed
162
      assertTrue(con.isClosed());
163 164

    } catch(SQLException ex) {
165
      assertTrue(ex.getMessage(),false);
166 167 168
    }
  }

169 170 171 172 173 174 175 176 177 178
  /**
   * Test the warnings system
   */
  public void testWarnings() {
    try {
      Connection con = JDBC2Tests.openDB();

      String testStr = "This Is OuR TeSt message";

      // The connection must be ours!
179
      assertTrue(con instanceof org.postgresql.Connection);
180 181 182 183 184 185 186 187 188

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

      // Set the test warning
      ((org.postgresql.Connection)con).addWarning(testStr);

      // Retrieve it
      SQLWarning warning = con.getWarnings();
189 190
      assertNotNull(warning);
      assertEquals(testStr, warning.getMessage());
191 192 193

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

      JDBC2Tests.closeDB(con);
    } catch(SQLException ex) {
198
      assertTrue(ex.getMessage(),false);
199 200 201
    }
  }

202 203 204 205 206 207 208 209 210 211
	/**
	 * Transaction Isolation Levels
	 */
	public void testTransactionIsolation()
	{
		try
		{
			Connection con = JDBC2Tests.openDB();

			// PostgreSQL defaults to READ COMMITTED
212 213
			assertEquals(Connection.TRANSACTION_READ_COMMITTED,
				     con.getTransactionIsolation());
214 215 216 217 218

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

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

			// Now change the default for future transactions
223
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
224 225 226 227 228

			// Since the call to setTransactionIsolation() above was made
			// inside the transaction, the isolation level of the current
			// transaction did not change. It affects only future transactions.
			// This behaviour is recommended by the JDBC spec.
229 230
			assertEquals(Connection.TRANSACTION_READ_COMMITTED,
				     con.getTransactionIsolation());
231 232 233 234 235

			// Begin a new transaction
			con.commit();

			// Now we should see the new isolation level
236 237
			assertEquals(Connection.TRANSACTION_SERIALIZABLE,
				     con.getTransactionIsolation());
238 239 240

			// Repeat the steps above with the transition back to
			// READ COMMITTED.
241 242 243
			con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
			assertEquals(Connection.TRANSACTION_SERIALIZABLE,
						 con.getTransactionIsolation());
244
			con.commit();
245 246
			assertEquals(Connection.TRANSACTION_READ_COMMITTED,
						 con.getTransactionIsolation());
247 248 249 250

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

251 252
			assertEquals(Connection.TRANSACTION_READ_COMMITTED,
						 con.getTransactionIsolation());
253

254 255 256
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			assertEquals(Connection.TRANSACTION_SERIALIZABLE,
						 con.getTransactionIsolation());
257

258 259
			con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
			assertEquals(Connection.TRANSACTION_READ_COMMITTED, con.getTransactionIsolation());
260 261 262

			// Test if a change of isolation level before beginning the
			// transaction affects the isolation level inside the transaction.
263 264 265
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			assertEquals(Connection.TRANSACTION_SERIALIZABLE,
						 con.getTransactionIsolation());
266
			con.setAutoCommit(false);
267 268
			assertEquals(Connection.TRANSACTION_SERIALIZABLE,
						 con.getTransactionIsolation());
269
			con.setAutoCommit(true);
270 271 272 273 274
			assertEquals(Connection.TRANSACTION_SERIALIZABLE,
						 con.getTransactionIsolation());
			con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
			assertEquals(Connection.TRANSACTION_READ_COMMITTED,
						 con.getTransactionIsolation());
275
			con.setAutoCommit(false);
276 277
			assertEquals(Connection.TRANSACTION_READ_COMMITTED,
						 con.getTransactionIsolation());
278 279 280 281 282 283 284 285

			JDBC2Tests.closeDB(con);
		} 
		catch ( SQLException ex ) 
		{
			fail( ex.getMessage() );
		}
	}
286 287 288 289 290 291 292 293 294 295 296 297 298 299

  /**
   * JDBC2 Type mappings
   */
  public void testTypeMaps() {
    try {
      Connection con = JDBC2Tests.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);
300
      assertEquals(newmap, con.getTypeMap());
301 302 303

      // restore the old one
      con.setTypeMap(oldmap);
304
      assertEquals(oldmap, con.getTypeMap());
305 306 307

      JDBC2Tests.closeDB(con);
    } catch(SQLException ex) {
308
      assertTrue(ex.getMessage(),false);
309 310
    }
  }
311
}