package org.postgresql.test.jdbc2;

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

/*
 * A few simple tests that check to make sure that a basic Statement
 * has some functions that were added.
 *
 */
 
 public class StatementTest extends TestCase
 {
 	private Connection con;	
	private Statement stmt;
	private PreparedStatement pstmt;
	private CallableStatement cstmt;
 
 	public StatementTest(String name)
	{
		super(name);
	}
	
	public void setUp()
	{
		try
		{
			con = TestUtil.openDB();
			con.createStatement().execute("create table test_table (A integer)");
			con.createStatement().execute("create or replace function test_function() returns boolean as 'select true' language sql");
			stmt = con.createStatement();
			pstmt = con.prepareStatement("select * from test_table where A = ?");
			cstmt = con.prepareCall("{? = call test_function()}");
		}
		catch (SQLException sqle)
		{
			fail(sqle.getMessage());
		}
	}
	
	public void tearDown()
	{
		try
		{
			con.createStatement().execute("drop table test_table");
			con.createStatement().execute("drop function test_function()");		
			con.close();
		}
		catch (SQLException sqle)
		{
			fail(sqle.getMessage());
		}
	}
	
	public void testClose()
	{
		try
		{
			stmt.close();
			pstmt.close();
			cstmt.close();
			try
			{
				stmt.execute("select * from test_table");
				fail("Statement not closed properly");
			}
			catch (SQLException sqle)
			{
				//expected
			}
			try
			{
				pstmt.setObject(1, new Integer("1"),java.sql.Types.INTEGER);
				fail("Statement not closed properly");
			}
			catch (SQLException sqle)
			{
				//expected
			}
			try
			{
				cstmt.getObject(1);
				fail("Statement not closed properly");
			}
			catch (SQLException sqle)
			{
				//expected
			}
		}
		catch (SQLException sqle)
		{
			fail(sqle.getMessage());
		}
	}
}
