package org.postgresql.test.jdbc2;

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

/*
 * $PostgreSQL: pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java,v 1.11 2003/11/29 22:41:23 pgsql Exp $
 *
 * Some simple tests based on problems reported by users. Hopefully these will
 * help prevent previous problems from re-occuring ;-)
 *
 */
public class MiscTest extends TestCase
{

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

	/*
	 * Some versions of the driver would return rs as a null?
	 *
	 * Sasha <ber0806@iperbole.bologna.it> was having this problem.
	 *
	 * Added Feb 13 2001
	 */
	public void testDatabaseSelectNullBug()
	{
		try
		{
			Connection con = TestUtil.openDB();

			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select datname from pg_database");
			assertNotNull(rs);

			while (rs.next())
			{
				rs.getString(1);
			}

			rs.close();
			st.close();

			TestUtil.closeDB(con);
		}
		catch (Exception ex)
		{
			fail(ex.getMessage());
		}
	}

	public void testError()
	{
		Connection con = TestUtil.openDB();
		try
		{

			// transaction mode
			con.setAutoCommit(false);
			Statement stmt = con.createStatement();
			stmt.execute("select 1/0");
			fail( "Should not execute this, as a SQLException s/b thrown" );
			con.commit();
		}
		catch ( Exception ex )
		{}
		try
		{
			con.commit();
			con.close();
		}
		catch ( Exception ex)
		{}
	}

	public void xtestLocking()
	{

		try
		{
			Connection con = TestUtil.openDB();
			Connection con2 = TestUtil.openDB();

			TestUtil.createTable(con, "test_lock", "name text");
			Statement st = con.createStatement();
			Statement st2 = con2.createStatement();
			con.setAutoCommit(false);
			st.execute("lock table test_lock");
			st2.executeUpdate( "insert into test_lock ( name ) values ('hello')" );
			con.commit();
			TestUtil.dropTable(con, "test_lock");
			con.close();
		}
		catch ( Exception ex )
		{
			fail( ex.getMessage() );
		}
	}
}

@@ -117,16 +117,34 @@
 a.sql.*;
+%0Aimport java.io.*;
 %0A%0A/*%0A * 
@@ -231,35 +231,35 @@
  1.1
-1
+2
  2003/1
-1/29 22:41:23 pgsql
+2/11 15:11:43 davec
  Exp
@@ -1359,32 +1359,35 @@
 ;%0A%09%09%7D%0A%09%09catch ( 
+SQL
 Exception ex )%0A%09
@@ -1388,16 +1388,307 @@
 ex )%0A%09%09%7B
+%0A%09%09%09// Verify that the SQLException is serializable.%0A%09%09%09try %7B%0A%09%09%09%09ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A%09%09%09%09ObjectOutputStream oos = new ObjectOutputStream(baos);%0A%09%09%09%09oos.writeObject(ex);%0A%09%09%09%09oos.close();%0A%09%09%09%7D catch (IOException ioe) %7B%0A%09%09%09%09fail(ioe.getMessage());%0A%09%09%09%7D%0A%09%09
 %7D%0A%09%09try%0A

patch to make sure PSQLState is Serializable and a test case to prove it