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

/*
 * @(#)resultSetClient1.java	1.22 02/01/12
 */

package com.sun.cts.tests.jdbc.ee.resultSet.resultSet1;

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

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 resultSetClient1 class tests methods of resultSet interface using 
 * Sun's J2EE Reference Implementation.
 * @author  
 * @version 1.7, 06/16/99
 */

public class resultSetClient1 extends ServiceEETest implements Serializable
{		  
	private static final String testName = "jdbc.ee.resultSet.resultSet1";
	
	//Naming specific member variables
	private CTSNamingContextInterface jc = null;

	//Harness requirements 

	private transient Connection coffeeCon = null;
	private Statement stmt = null;
	private DataSource ds1 = null;
	private ResultSet rs = null;
	private	String drManager=null;
	private String sqlStmt=null;
	private dbSchema dbSch=null;
	private int rscount=0;
	private ResultSet rs1=null;
	private String query1 =null;
	private int rowMaxVal=0;
	private String query = null;

	/* Run test in standalone mode */
	public static void main(String[] args) 
	{
		resultSetClient1 theTests = new resultSetClient1();
		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");

				String fTableName = p.getProperty("ftable","");
				
				/*String query = "SELECT COF_NAME, PRICE FROM " + fTableName;*/
				query = p.getProperty("SelCoffeeAll","");
				if (query.length() == 0)
					throw new Fault("Invalid SQL Statement ");
				logMsg("Query to select All the rows :" + query);

				query1=p.getProperty("SelCoffeeNull","");

				/*query1="SELECT * FROM " + fTableName + " WHERE TYPE_ID=0";*/
				if (query1.length() == 0)
					throw new Fault("Invalid SQL Statement ");
				logMsg("Query to select Null rows :" + query1);

				if(drManager.equals("yes"))
				{
					logMsg("Using DriverManager");
					DriverManagerConnection dmCon = new DriverManagerConnection();
					coffeeCon = dmCon.getConnection(p);
				}
				else 
				{
					logMsg("Using DataSource");
					DataSourceConnection dsCon = new DataSourceConnection();
					coffeeCon = dsCon.getConnection(p);
				}
				dbSch = new dbSchema();
				dbSch.createData(p,coffeeCon);
				stmt = coffeeCon.createStatement(/*ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY*/);
				rs = stmt.executeQuery(query.trim());
			}
			catch(SQLException ex)
			{
				logErr("SQL Exception : " + ex.getMessage());
			}
		} 
		catch( Exception e ) 
		{
			logErr("Setup Failed!");
		}
	}


	/*
	 *      This method is to calculate the number of rows in a ResultSet.
	 *      Call the rsCount(ResultSet rs) method.
	 *      It Should return an Integer value 
	 *
	 */
	public int rsCount(ResultSet rs) throws Fault
	{
		int count=0;
		try
		{
			while (rs.next())
			{
				count++;
			}
		}
		catch(SQLException sqle)
		{
			logErr("SQL Exception :" + sqle.getMessage());
			throw new Fault("Call to rsCount is Failed!",sqle);
		}
		return count;
	}

	
	/* @testName:       testGetConcurrency
	 *                    
	 * @assertion:      The ResultSet object contains the results of executing an 
	 *                  SQL Query.  ResultSets may hava different levels of functionality 
	 *                  (ResultSet Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, 
	 *                  TYPE_SCROLL_INSENSITIVE) and update capabilities (ResultSet 
	 *                  Concurrency). (See section 26.1 of JDBC 2.0 API Reference 
	 *                  & Tutorial second edition).  The JDBC driver must provide full 
	 *                  support for TYPE_SCROLL_INSENSITIVE result sets. (See section 
	 *                  6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
	 *                  The getCocurrency() returns the concurrency mode of this ResultSet object. 
	 *                  The concurrency used is determined by the Statement object that created 
	 *                  the result set.
	 *
	 * @test_Strategy:  Get a ResultSet object by executing the query  and call the 
	 *                  getConcurrency() method on that resultset object. It should return an
	 *                  Integer value and the value should be equal to any of the values 
	 *                  CONCUR_READ_ONLY or CONCUR_UPDATABLE which are defined in the Resutset
	 *                  interface.
	 */
	 
	public void testGetConcurrency() throws Fault
	{
		try
		{
			// invoke on the getConcurrency
			logMsg("Calling getConcurrency on ResultSet");
			int concurrencyValue = rs.getConcurrency();
			if(concurrencyValue == ResultSet.CONCUR_READ_ONLY )
			{
				logMsg("getConcurrency method returns ResultSet.CONCUR_READ_ONLY ");
			}
			else if(concurrencyValue ==  ResultSet.CONCUR_UPDATABLE)
			{
				logMsg("getConcurrency method returns ResultSet.CONCUR_UPDATABLE");
			}
			else 
			{
				logErr(" getConcurrency method returns a invalid value" );
				throw new Fault("Call to getConcurrency is Failed");
			}
		}
		catch ( SQLException sqle )
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to getConcurrency is Failed!", sqle);
		}
		catch ( Exception e )
		{ 
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to getConcurrency is Failed!", e);
		}
	}
	
	
	/* @testName:       testGetFetchDirection
	 *                    
	 * @assertion:      The ResultSet object contains the results of executing an 
	 *                  SQL Query.  ResultSets may hava different levels of functionality 
	 *                  (ResultSet Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, 
	 *                  TYPE_SCROLL_INSENSITIVE) and update capabilities (ResultSet 
	 *                  Concurrency). (See section 26.1 of JDBC 2.0 API Reference 
	 *                  & Tutorial second edition).  The JDBC driver must provide full 
	 *                  support for TYPE_SCROLL_INSENSITIVE result sets. (See section 
	 *                  6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
	 *                  The getFetchDirection() method returns the fetch direction for this ResultSet
	 *                  object.
	 *
	 * @test_Strategy:  Get a ResultSet object by executing a query and call the getFetchDirection() 
	 *                  method.It should return an Integer value and the value should be equal
	 *                  to any of the values FETCH_FORWARD or FETCH_REVERSE or FETCH_UNKNOWN 
	 *                  which are defined in the Resultset interface.
	 *
	 */
	 
	public void testGetFetchDirection() throws Fault
	{
		try
		{
			// invoke on the getFetchDirection
			logMsg("Calling getFetchDirection on ResultSet");
			int fetchDirectionValue = rs.getFetchDirection();
			if(fetchDirectionValue == ResultSet.FETCH_FORWARD)
			{
				logMsg("getFetchDirection method returns ResultSet.FETCH_FORWARD " );
			}
			else if(fetchDirectionValue == ResultSet.FETCH_REVERSE)
			{
				logMsg("getFetchDirection method returns ResultSet.FETCH_REVERSE");
			}
			else if(fetchDirectionValue == ResultSet.FETCH_UNKNOWN)
			{
				logMsg("getFetchDirection method returns ResultSet.FETCH_UNKNOWN");
			}
			else
			{
				logErr(" getFetchDirection method returns a invalid value" );
				throw new Fault("Call to getFetchDirection is Failed");
			}
		} 
		catch ( SQLException sqle )
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to getFetchDirection is Failed!", sqle);
		}	
		catch ( Exception e )
		{ 
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to getFetchDirection is Failed!", e);
		}
	}

	
	
	/* @testName:        testGetFetchSize
	 *                    
	 * @assertion:       The ResultSet object contains the results of executing an 
	 *                   SQL Query.  ResultSets may hava different levels of functionality 
	 *                   (ResultSet Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, 
	 *                   TYPE_SCROLL_INSENSITIVE) and update capabilities (ResultSet 
	 *                   Concurrency). (See section 26.1 of JDBC 2.0 API Reference 
	 *                   & Tutorial second edition).  The JDBC driver must provide full 
	 *                   support for TYPE_SCROLL_INSENSITIVE result sets. (See section 
	 *                   6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
	 *                   The getFetchSize() method returns the fetch size for this ResultSet object.
	 *                   i.e.,the number of rowsthat should be fetched from the database when more rows are needed for 
	 *                   this ResultSet object.
	 *
	 * @test_Strategy:  Get a ResultSet object and call the getFetchSize() method
	 *                  It should return a Integer value which should be greater than or
	 *                  equal to zero.
	 *
	 */
	public void testGetFetchSize() throws Fault
	{
		try
		{
			// invoke on the getFetchSize
			logMsg("Calling getFetchSize on ResultSet");
			int fetchSizeValue = rs.getFetchSize();
			if(fetchSizeValue >= 0)
			{
				logMsg("getFetchSize method returns :" + fetchSizeValue );
			}
			else
			{
				logMsg(" getFetchSize method returns a invalid value" );
			}
		} 
		catch ( SQLException sqle )
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to getFetchSize is Failed!", sqle);
		}
		catch ( Exception e )
		{ 
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to getFetchSize is Failed!", e);
		}
	}



	/* @testName:       testGetType
	 *                    
	 * @assertion:      The ResultSet object contains the results of executing an 
	 *                  SQL Query.  ResultSets may hava different levels of functionality 
	 *                  (ResultSet Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, 
	 *                  TYPE_SCROLL_INSENSITIVE) and update capabilities (ResultSet 
	 *                  Concurrency). (See section 26.1 of JDBC 2.0 API Reference 
	 *                  & Tutorial second edition).  The JDBC driver must provide full 
	 *                  support for TYPE_SCROLL_INSENSITIVE result sets. (See section 
	 *                  6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
	 *                  The getType() method returns the type of this ResultSet object. 
	 *                  The type is determined by the Statement object that created the result set.
	 *
	 * @test_Strategy:  Get a ResultSet object and call the getType() method on that object.
	 *                  It should return an Integer value and the value should be equal to
	 *                  any of the values TYPE_FORWARD_ONLY or TYPE_SCROLL_INSENSITIVE
	 *                  or TYPE_SCROLL_SENSITIVE which are defined in the Resultset interface.
	 *
	 */
	public void testGetType() throws Fault
	{
		try
		{
			// invoke on the getType
			logMsg("Calling getType on ResultSet");
			int typeValue = rs.getType();
			if(typeValue == ResultSet.TYPE_FORWARD_ONLY )
			{
				logMsg("getType method returns ResultSet.TYPE_FORWARD_ONLY ");
			}
			else if(typeValue == ResultSet.TYPE_SCROLL_INSENSITIVE)
			{
				logMsg("getType method returns ResultSet.TYPE_SCROLL_INSENSITIVE");
			}
			else if(typeValue == ResultSet.TYPE_SCROLL_SENSITIVE)
			{
				logMsg("getType method returns ResultSet.TYPE_SCROLL_SENSITIVE");
			}
			else
			{
				logErr(" getType method returns a invalid value" );
				throw new Fault("Call to getType is Failed");
			}
		} 
		catch ( SQLException sqle )
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to getType is Failed!", sqle);
		}
		catch ( Exception e )
		{ 
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to getType is Failed!", e);
		}
	}


	
	
	/* @testName:       testSetFetchSize01
	 *                    
	 * @assertion:      The ResultSet object contains the results of executing an 
	 *                  SQL Query.  ResultSets may hava different levels of functionality 
	 *                  (ResultSet Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, 
	 *                  TYPE_SCROLL_INSENSITIVE) and update capabilities (ResultSet 
	 *                  Concurrency). (See section 26.1 of JDBC 2.0 API Reference 
	 *                  & Tutorial second edition).  The JDBC driver must provide full 
	 *                  support for TYPE_SCROLL_INSENSITIVE result sets. (See section 
	 *                  6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
	 *                  setFetchSize(int rows) gives the JDBC driver a hint as to the number of rows that should be 
	 *                  fetched from the database when more rows are needed for this ResultSet 
	 *                  object. If the fetch size specified is zero, the JDBC driver ignores the 
	 *                  value and is free to make its own best guess as to what the fetch size 
	 *                  should be. The default value is set by the Statement object that created 
	 *                  the result set. The fetch size may be changed at any time.
	 *
	 * @test_Strategy:  Get a ResultSet object by executing a query and call the setFetchSize(int rows).
	 *                  Set the value of rows to zero. The JDBC driver is free to make its own best guess as to 
	 *                  what the fetch size should be. Then call getFetchSize() method.
	 *                  
	 *
	 */
	public void testSetFetchSize01() throws Fault
	{
		try
		{
			// invoke on the setFetchSize
			logMsg("Calling setFetchSize on ResultSet");
			rs.setFetchSize(0);
			int fetchSizeValue = rs.getFetchSize();
		} 
		catch ( SQLException sqle )
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to setFetchSize is Failed!", sqle);
		}	
		catch ( Exception e )
		{ 
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("setFetchSize Failed!", e);
		}
	}
	


	/* @testName:       testSetFetchSize02
	 *                    
	 * @assertion:      The ResultSet object contains the results of executing an 
	 *                  SQL Query.  ResultSets may hava different levels of functionality 
	 *                  (ResultSet Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, 
	 *                  TYPE_SCROLL_INSENSITIVE) and update capabilities (ResultSet 
	 *                  Concurrency). (See section 26.1 of JDBC 2.0 API Reference 
	 *                  & Tutorial second edition).  The JDBC driver must provide full 
	 *                  support for TYPE_SCROLL_INSENSITIVE result sets. (See section 
	 *                  6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
	 *                  setFetchSize(int rows) gives the JDBC driver a hint as to the number of rows that should be 
	 *                  fetched from the database when more rows are needed for this ResultSet 
	 *                  object. If the fetch size specified is zero, the JDBC driver ignores the 
	 *                  value and is free to make its own best guess as to what the fetch size 
	 *                  should be. The default value is set by the Statement object that created 
	 *                  the result set. The fetch size may be changed at any time.
	 *
	 * @test_Strategy:  Get a ResultSet object and call the setFetchSize(int rows) on that obect.
	 *                  Call Statement's getMaxRows() before calling setFetchSize() method
	 *                  and pass the returned value from getMaxRows() method as the argument 
	 *                  to the setFetchSize() method. Then call getFetchSize() method to check 
	 *                  whether the returned value is the same that has been set.
	 *
	 */
	public void testSetFetchSize02() throws Fault
	{
		try
		{
			//Setting the maxRows to 5 to make sure that we have a non-zero value
			//for maxrows
			
			Statement stmt = coffeeCon.createStatement();
			stmt.setMaxRows(5);			
						
			ResultSet rs = stmt.executeQuery(query.trim());

			// invoke on the setFetchSize
			//rowMaxVal is the maximum Rows that ResultSet can contain
			rowMaxVal=stmt.getMaxRows();			

			logMsg("Calling setFetchSize() on ResultSet");			
			rs.setFetchSize(rowMaxVal);			
			int fetchSizeValue = rs.getFetchSize();

			if(fetchSizeValue == rowMaxVal)
			{
				logMsg("setFetchSize method sets number of rows that has been specified :"+fetchSizeValue); 
			}
			else
			{
				logErr("setFetchSize method does not set number of rows that has been specified" );
				throw new Fault("Call to setFetchSize is Failed");
			}
		}
		catch( SQLException sqle )
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to setFetchSize is Failed!", sqle);
		}
		catch( Exception e )
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("setFetchSize Failed!", e);
		}
	}



	
	/* @testName:       testSetFetchSize03
	 *                   
	 * @assertion:      The ResultSet object contains the results of executing an 
	 *                  SQL Query.  ResultSets may hava different levels of functionality 
	 *                  (ResultSet Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, 
	 *                  TYPE_SCROLL_INSENSITIVE) and update capabilities (ResultSet 
	 *                  Concurrency). (See section 26.1 of JDBC 2.0 API Reference 
	 *                  & Tutorial second edition).  The JDBC driver must provide full 
	 *                  support for TYPE_SCROLL_INSENSITIVE result sets. (See section 
	 *                  6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
	 *                  setFetchSize(int rows) gives the JDBC driver a hint as to the number of rows that should be 
	 *                  fetched from the database when more rows are needed for this ResultSet 
	 *                  object. If the fetch size specified is zero, the JDBC driver ignores the 
	 *                  value and is free to make its own best guess as to what the fetch size 
	 *                  should be. The default value is set by the Statement object that created 
	 *                  the result set. The fetch size may be changed at any time.
	 *
	 * @test_Strategy:  Get a ResultSet object and call the setFetchSize(int rows) on 
	 *                  that object with the value greater than the Statement's maximum possible
	 *                  rows as returned by getMaxRows() method and it should throw SQLException.
	 *                  In case if the getMaxRows() method returns 0 which means unlimited rows then
	 *                  appropriate message is displayed and test passes
	 */
	public void testSetFetchSize03() throws Fault
	{	
		boolean flag=false;
		try
		{
			// invoke on the setFetchSize
			// rowMaxVal is the maximum Rows that ResultSet can contain
			rowMaxVal=stmt.getMaxRows();
			logMsg("Calling setFetchSize on ResultSet");
			try
			{
				if(rowMaxVal > 0)
				{ 
					rs.setFetchSize(rowMaxVal+2);
				}
				else if(rowMaxVal == 0)
				{
					logMsg("Resultset supports unlimited number of rows");
					flag = true ;
				}
			}
			catch(SQLException sqe)
			{
				flag=true;
			}
			if (flag)
			{
				logMsg("setFetchSize method does not set the size greater than the Maximum Rows that ResultSet can contain");
			}
			else
			{
				logErr("setFetchSize method sets the size greater than the Maximum Rows that ResultSet can contain ");
				throw new Fault("Call to setFetchSize is Failed!");
			}
			
		}
		catch( Exception e )
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("setFetchSize Failed!", e);
		}
	}
	
	
	
	 
	/* @testName:       testSetFetchSize04
	 *                    
	 * @assertion:      The ResultSet object contains the results of executing an 
	 *                  SQL Query.  ResultSets may hava different levels of functionality 
	 *                  (ResultSet Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, 
	 *                  TYPE_SCROLL_INSENSITIVE) and update capabilities (ResultSet 
	 *                  Concurrency). (See section 26.1 of JDBC 2.0 API Reference 
	 *                  & Tutorial second edition).  The JDBC driver must provide full 
	 *                  support for TYPE_SCROLL_INSENSITIVE result sets. (See section 
	 *                  6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
	 *                  setFetchSize(int rows) gives the JDBC driver a hint as to the number of rows that should be 
	 *                  fetched from the database when more rows are needed for this ResultSet 
	 *                  object. If the fetch size specified is zero, the JDBC driver ignores the 
	 *                  value and is free to make its own best guess as to what the fetch size 
	 *                  should be. The default value is set by the Statement object that created 
	 *                  the result set. The fetch size may be changed at any time.
	 *
	 * @test_Strategy:  Get a ResultSet object and call the setFetchSize(int rows) on that object.
	 *                  And try to set a negative value .It should throw SQLException.
	 */
	 
	public void testSetFetchSize04() throws Fault
	{
		boolean flag=false;
		try
		{
			// invoke on the setFetchSize
			logMsg("Calling setFetchSize(-1) on ResultSet");
			try
			{
				
				rs.setFetchSize(-1);
			}
			catch(SQLException sqe)
			{
				flag=true;
			}
			if (flag)
			{
				logMsg("setFetchSize method does not set the size as negative ");
			}
			else
			{
				logErr("setFetchSize method sets the size as negative ");
				throw new Fault("Call to setFetchSize is Failed!");
			}
		}
		catch(Exception e)
		{ 
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("setFetchSize Failed!", e);
		}
	}

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

