/*
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * @(#)stmtClient3.java	1.8 02/01/12
 */

package com.sun.cts.tests.jdbc.ee.stmt.stmt3;

import java.io.*;
import java.util.*;
import java.math.*;

import java.sql.*;
import javax.sql.*;

import com.sun.cts.util.*;
import com.sun.cts.porting.*;
import com.sun.cts.harness.*;

import java.rmi.RemoteException;

import javasoft.sqe.javatest.Status;
import com.sun.cts.tests.jdbc.ee.common.*;

// Merant DataSource class
//import com.merant.sequelink.jdbcx.datasource.*;


/**
 * The stmtClient3 class tests methods of Statement interface using 
 * Sun's J2EE Reference Implementation.
 * @author  
 * @version 1.7, 06/16/99
 */

public class stmtClient3 extends ServiceEETest implements Serializable
{
	private static final String testName = "jdbc.ee.stmt.stmt3";

	//Naming specific member variables
	private CTSNamingContextInterface jc = null;

	//Harness requirements 

	private transient Connection conn = null;
	private Statement stmt=null;
	private DataSource ds1 = null;
	private	String drManager=null;
	private Properties sqlp = null;

	/* Run test in standalone mode */
	public static void main(String[] args) 
	{
		stmtClient3 theTests = new stmtClient3();
		Status s=theTests.run(args, System.out, System.err);
		s.exit();
	}

	/*  Test setup: */
	/*
	 *   @class.setup_props: Driver, the Driver name;
	 *			db1, the database name with url;
	 *			user1, the database user name;
	 *			password1, the database password;
	 *			db2, the database name with url;
	 *			user2, the database user name;
	 *			password2, the database password;
	 *			DriverManager, flag for DriverManager;
	 *			ptable, the primary table;
	 *			ftable, the foreign table;
	 *			cofSize, the initial size of the ptable;
	 *			cofTypeSize, the initial size of the ftable;
	 *			binarySize, size of binary data type;
	 *			varbinarySize, size of varbinary data type;
	 *			longvarbinarySize, size of longvarbinary data type;
	 *  @class.testArgs:     -ap ctssql.stmt
	 */

	public void setup(String[] args, Properties p) throws Fault 
	{
		try 
		{
			try
			{
				drManager = p.getProperty("DriverManager","");
				if (drManager.length() == 0)
					throw new Fault("Invalid DriverManager Name");
				sqlp=p;

				if(drManager.equals("yes"))
				{
					logMsg("Using DriverManager");
					DriverManagerConnection dmCon = new DriverManagerConnection();
					conn = dmCon.getConnection(p);
				}
				else
				{
					logMsg("Using DataSource");
					DataSourceConnection dsCon = new DataSourceConnection();
					conn = dsCon.getConnection(p);
				}
				stmt=conn.createStatement();
			}
			catch (SQLException ex)
			{
				logErr("SQL Exception : " + ex.getMessage());
			}
		} 
		catch (Exception e)
		{
			logErr("Setup Failed!");
			TestUtil.printStackTrace(e);
		}
	}


	/*
	 * @testName:         testSetFetchSize05
	 * @assertion:        The Statement object provides methods for executing SQL 
	 *                    statements and retrieving results.(See section 40.1 of 
	 *                    JDBC 2.0 API Reference & Tutorial second edition). 
	 *                    
	 *                    The driver must provide full support for Statement methods. 
	 *                    The driver must also support all the methods for executing 
	 *                    SQL Statements in a single batch (Batch Updates). (See 
	 *                    section 6.2.2.3 of Java2 Platform Enterprise Edition(J2EE)
	 *                    Specification v1.2)
	 *                    
	 *                    The setFetchSize(int size) method sets the number of rows
	 *                    to fetch from the database specified by the value size. 
	 *                    The method does not return any value and throws SQLException
	 *                    if a database access error occurs or the condition
	 *                    0 <= size <= this.getMaxRows is not satisfied.
	 *                    (See JDK 1.2.2 API Documentation)
	 *
	 * @test_Strategy:    Get a Statement object and call the setFetchSize(int rows)
	 *                    method with the negative value and it should throw
	 *                    SQLException
	 *
	 */
	public void testSetFetchSize05() throws Fault
	{
		int maxFetchSizeVal=0;
		String sMaxFetchSizeVal=null;
		boolean sqlExceptFlag=false;
		try
		{
			
			sMaxFetchSizeVal = sqlp.getProperty("Max_Set_Val","");
			maxFetchSizeVal=Integer.parseInt(sMaxFetchSizeVal);
			maxFetchSizeVal=maxFetchSizeVal * (-1);
			logMsg("Maximum Value to be set as Fetch Size " + maxFetchSizeVal);
			
			
			logMsg("Calling setFetchSize method");
			try
			{
				stmt.setFetchSize(maxFetchSizeVal);
			}
			catch ( SQLException sqle )
			{
				sqlExceptFlag=true;
			}
			if(sqlExceptFlag)
			{
				logMsg("setFetchSize method does not set the invalid value " );
			}
			else
			{
				logErr("setFetchSize method sets the Invalid value " );
				throw new Fault("Call to setFetchSize is Failed");
			}
		}
		catch ( Exception e )
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to setFetchSize is Failed!", e);
		}
	}


	/*
	 * @testName:         testSetMaxFieldSize01
	 * @assertion:        The Statement object provides methods for executing SQL 
	 *                    statements and retrieving results.(See section 40.1 of 
	 *                    JDBC 2.0 API Reference & Tutorial second edition). 
	 *                    
	 *                    The driver must provide full support for Statement methods. 
	 *                    The driver must also support all the methods for executing 
	 *                    SQL Statements in a single batch (Batch Updates). (See 
	 *                    section 6.2.2.3 of Java2 Platform Enterprise Edition(J2EE)
	 *                    Specification v1.2)
	 *                    
	 *                    The setMaxFieldSize(int maxsize) method sets the maximum size
	 *                    for a column in a result set specified by the value maxsize (in
	 *                    bytes). For maximum portability, the maximum field size should
	 *                    be set to a value greater than 256. If the value maxsize is 0  
	 *                    then it means that there is no limit to the size of a column.
	 *                    The setMaxFieldSize(int maxsize) does not return any value.
	 *                    (See JDK 1.2.2 API Documentation)
	 *
	 * @test_Strategy:    Get a Statement object and call the setMaxFieldSize(int max)
	 *                    method and call getMaxFieldSize() method and it should return 
	 *                    an integer value that is been set
	 *
	 */
	public void testSetMaxFieldSize01() throws Fault
	{
		int maxFieldSizeVal=0;
		String sMaxFieldSizeVal=null;
		int retVal=0;
		try
		{
			sMaxFieldSizeVal=sqlp.getProperty("Max_Set_Val","");
			maxFieldSizeVal=Integer.parseInt(sMaxFieldSizeVal);
			
			// for Maximum Portability the value is multiplied with 256 
			maxFieldSizeVal=maxFieldSizeVal * 256;
			logMsg("Maximum Field Size Value to be set " + maxFieldSizeVal);
			
			logMsg("Calling maxFieldSize method ");
			stmt.setMaxFieldSize(maxFieldSizeVal);
			
			// invoke on the getMaxFieldSize
			retVal = stmt.getMaxFieldSize();
			
			if(maxFieldSizeVal == retVal)
			{
				logMsg("setMaxFieldSize method sets the value for Maximum Field Size" );
			}
			else
			{
				logErr("setMaxFieldSize method does not set the value for Maximum Field Size" );
				throw new Fault("Call to setMaxFieldSize is Failed");
			}
		}
		catch ( SQLException sqle )
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to setMaxFieldSize is Failed!", sqle);
		}
		catch ( Exception e )
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to setMaxFieldSize is Failed!", e);
		}
	}


	/*
	 * @testName:         testSetMaxFieldSize02
	 * @assertion:        The Statement object provides methods for executing SQL 
	 *                    statements and retrieving results.(See section 40.1 of 
	 *                    JDBC 2.0 API Reference & Tutorial second edition). 
	 *                    
	 *                    The driver must provide full support for Statement methods. 
	 *                    The driver must also support all the methods for executing 
	 *                    SQL Statements in a single batch (Batch Updates). (See 
	 *                    section 6.2.2.3 of Java2 Platform Enterprise Edition(J2EE)
	 *                    Specification v1.2)
	 *                    
	 *                    The setMaxFieldSize(int maxsize) method sets the maximum size
	 *                    for a column in a result set specified by the value maxsize (in
	 *                    bytes). For maximum portability, the maximum field size should
	 *                    be set to a value greater than 256. If the value maxsize is 0  
	 *                    then it means that there is no limit to the size of a column.
	 *                    The setMaxFieldSize(int maxsize) does not return any value.
	 *                    (See JDK 1.2.2 API Documentation)
	 *
	 * @test_Strategy:    Get a Statement object and call the setMaxFieldSize(int max)
	 *                    method with an invalid value (negative value) and It should 
	 *                    throw a SQLException
	 *
	 */
	 
	public void testSetMaxFieldSize02() throws Fault
	{
		int maxFieldSizeVal=0;
		String sMaxFieldSizeVal=null;
		boolean sqlExceptFlag=false;
		
		try
		{
			sMaxFieldSizeVal=sqlp.getProperty("Max_Set_Val","");
			maxFieldSizeVal=Integer.parseInt(sMaxFieldSizeVal);
			maxFieldSizeVal=maxFieldSizeVal * (-1);
			logMsg("Rows Value to be set " + maxFieldSizeVal);

			logMsg("Calling the setMaxFieldSize method");
			try
			{
				stmt.setMaxFieldSize(maxFieldSizeVal);
			}
			catch (SQLException sqe)
			{
				sqlExceptFlag=true;
			}
			
			if(sqlExceptFlag)
			{
				logMsg("setMaxFieldSize method does not set the Invalid value " );
			}
			else
			{
				logErr("setMaxFieldSize method sets the Invalid value" );
				throw new Fault("Call to setMaxFieldSize is Failed");
			}
		}
		catch ( Exception e )
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to setMaxFieldSize is Failed!", e);
		}
	}


	/*
	 * @testName:         testSetMaxRows01
	 * @assertion:        The Statement object provides methods for executing SQL 
	 *                    statements and retrieving results.(See section 40.1 of 
	 *                    JDBC 2.0 API Reference & Tutorial second edition). 
	 *                    
	 *                    The driver must provide full support for Statement methods. 
	 *                    The driver must also support all the methods for executing 
	 *                    SQL Statements in a single batch (Batch Updates). (See 
	 *                    section 6.2.2.3 of Java2 Platform Enterprise Edition(J2EE)
	 *                    Specification v1.2)
	 *                    
	 *                    The setMaxRows(int maxsize) method sets the maximum number
	 *                    of rows that any ResultSet object can contain is specified 
	 *                    by the value maxsize. If the value maxsize is 0 then it means
	 *                    that there is no limit. The setMaxRows(int maxsize) does not
	 *                    return any value.(See JDK 1.2.2 API Documentation)
	 *
	 * @test_Strategy:    Get a Statement object and call the setMaxRows(int rows)
	 *                    method and call getMaxRows() method and it should return a 
	 *                    integer value that is been set
	 *
	 */
	public void testSetMaxRows01() throws Fault
	{
		int maxRowsVal=0;
		String sMaxRowsVal=null;
		int retVal=0;
		try
		{
			sMaxRowsVal=sqlp.getProperty("Max_Set_Val","");
			maxRowsVal=Integer.parseInt(sMaxRowsVal);
			logMsg("Maximum Rows Value to be set " + maxRowsVal);
			
			logMsg("Calling maxRowsVal method");
			stmt.setMaxRows(maxRowsVal);
			
			// invoke getMaxRows
			retVal = stmt.getMaxRows();
			
			if(maxRowsVal == retVal)
			{
				logMsg("setMaxRows method sets the value for Maximum Rows" );
			}
			else
			{
				logErr("setMaxRows method does not set the value for Maximum Rows" );
				throw new Fault("Call to setMaxRows is Failed");
			}
		}
		catch ( SQLException sqle )
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to setMaxRows is Failed!", sqle);
		}
		catch ( Exception e )
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to setMaxRows is Failed!", e);
		}
	}


/*
	 * @testName:         testSetMaxRows02
	 * @assertion:        The Statement object provides methods for executing SQL 
	 *                    statements and retrieving results.(See section 40.1 of 
	 *                    JDBC 2.0 API Reference & Tutorial second edition). 
	 *                    
	 *                    The driver must provide full support for Statement methods. 
	 *                    The driver must also support all the methods for executing 
	 *                    SQL Statements in a single batch (Batch Updates). (See 
	 *                    section 6.2.2.3 of Java2 Platform Enterprise Edition(J2EE)
	 *                    Specification v1.2)
	 *                    
	 *                    The setMaxRows(int maxsize) method sets the maximum number
	 *                    of rows that any ResultSet object can contain is specified 
	 *                    by the value maxsize. If the value maxsize is 0 then it means
	 *                    that there is no limit. The setMaxRows(int maxsize) does not
	 *                    return any value.(See JDK 1.2.2 API Documentation)
	 *
	 * @test_Strategy:    Get a Statement object and call the setMaxRows(int rows)
	 *                    method with an invalid value (negative value) and It should 
	 *                    throw an SQLException
	 *
	 */
	public void testSetMaxRows02() throws Fault
	{
		int maxRowsVal=0;
		String sMaxRowsVal=null;
		boolean sqlExceptFlag=false;
		
		try
		{
			sMaxRowsVal=sqlp.getProperty("Max_Set_Val","");
			maxRowsVal=Integer.parseInt(sMaxRowsVal);
			maxRowsVal=maxRowsVal * (-1);
			logMsg("Rows Value to be set " + maxRowsVal);

			logMsg("Calling setMaxRows method");
			try
			{
				stmt.setMaxRows(maxRowsVal);
			}
			catch (SQLException sqe)
			{
				sqlExceptFlag=true;
			}

			if(sqlExceptFlag)
			{
				logMsg("setMaxRows method does not set the Invalid value " );
			}
			else
			{
				logErr("setMaxRows method sets the Invalid value" );
				throw new Fault("Call to setMaxRows is Failed");
			}
		}
		catch ( Exception e )
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to setMaxRows is Failed!", e);
		}
	}


	/*
	 * @testName:         testSetQueryTimeout02
	 * @assertion:        The Statement object provides methods for executing SQL 
	 *                    statements and retrieving results.(See section 40.1 of 
	 *                    JDBC 2.0 API Reference & Tutorial second edition). 
	 *                    
	 *                    The driver must provide full support for Statement methods. 
	 *                    The driver must also support all the methods for executing 
	 *                    SQL Statements in a single batch (Batch Updates). (See 
	 *                    section 6.2.2.3 of Java2 Platform Enterprise Edition(J2EE)
	 *                    Specification v1.2)
	 *                    
	 *                    The setQueryTimeout(int secval) method sets the time limit
	 *                    for the number of secval seconds a driver will wait for a
	 *                    statement object to be executed. If the value secval is 0 
	 *                    then it means that there is no limit. The setQueryTimeout 
	 *                    method does not return any value. (See JDK 1.2.2 API 
	 *                    Documentation)
	 *
	 * @test_Strategy:    Get a Statement object and call the setQueryTimeout(int secval)
	 *                    method with an invalid value (negative value)and It should 
	 *                    throw an SQLException
	 *
	 */
	 
	public void testSetQueryTimeout02() throws Fault
	{
		int maxQueryTimeVal=0;
		String sMaxQueryTimeVal=null;
		boolean sqlExceptFlag=false;
		
		try
		{
			sMaxQueryTimeVal=sqlp.getProperty("Max_Set_Val","");
			maxQueryTimeVal=Integer.parseInt(sMaxQueryTimeVal);
			maxQueryTimeVal=maxQueryTimeVal * (-1);
			logMsg("Seconds Value to be set as QueryTimeout " + maxQueryTimeVal);

			logMsg("Calling maxQueryTimeout method");
			try
			{
				stmt.setQueryTimeout(maxQueryTimeVal);
			}
			catch (SQLException sqe)
			{
				sqlExceptFlag=true;
			}

			if(sqlExceptFlag)
			{
				logMsg("setQueryTimeout method does not set the Invalid value " );
			}
			else
			{
				logErr("setQueryTimeout method sets the Invalid value" );
				throw new Fault("Call to setQueryTimeout is Failed");
			}
		}
		catch ( Exception e )
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to setQueryTimeout is Failed!", e);
		}
	}


	/* cleanup */
	public void  cleanup() throws Fault
	{
		try
		{
			// Close the Statement object
			stmt.close();
			conn.close();
			logMsg("Cleanup ok;");
		}
		catch (Exception e)
		{
			logErr("An error occurred while closing the database connection");
		}
	}

}
