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

/*
 * @(#)resultSetClient47.java	1.14 02/01/12
 */

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

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

public class resultSetClient47 extends ServiceEETest implements Serializable
{
	private static final String testName = "jdbc.ee.resultSet.resultSet47";

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

	//Harness requirements 
	private transient Connection conn = null;
	private Statement stmt = null;
	private PreparedStatement pstmt = null;
	private DataSource ds1 = null;
	private String drManager=null;
	private rsSchema rsSch=null;
	private Properties props =null;

	/* Run test in standalone mode */
	public static void main(String[] args) 
	{
		resultSetClient47 theTests = new resultSetClient47();
		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
			{
				props=p;
				drManager = p.getProperty("DriverManager","");
				if (drManager.length() == 0)
				{
					throw new Fault("Invalid DriverManager Name");
				}
				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);
				}
				rsSch= new rsSchema();
				stmt = conn.createStatement();
			}
			catch(SQLException ex)
			{
				logErr("SQL Exception : " + ex.getMessage());
			}
		} 
		catch ( Exception e ) 
		{
			logErr("Setup Failed!");
			TestUtil.printStackTrace(e);
		}
	}



	/*
	 * @testName:         testGetString84
	 *                    
	 * @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 getString(String columnName) returns the value of column 
	 *                    columnName as a String Object in the java programming language and
	 *                    null if the value is SQL null. 
	 *                    
	 * @test_Strategy:    Get a ResultSet object from the Connection to the database. Call the 
	 *                    getString(String columnName) method with the SQL null column of JDBC datatype 
	 *                    TIMESTAMP.It should return null String object.
	 */
	public void testGetString84() throws Fault
	{
		ResultSet oRes=null;
		try
		{
			//create the table 
			rsSch.createTab("Timestamp_Tab",props,conn);
			//Execute the query and get the resultSet Object
			String sQuery=props.getProperty("Timestamp_Query_Null","");
			oRes = stmt.executeQuery(sQuery);
			oRes.next();
			logMsg("Calling ResultSet.getString(NullValue)");
			//get the Null value from the table using getString Method
			ResultSetMetaData rsMetaData=oRes.getMetaData();
			String sColName = rsMetaData.getColumnName(1);
			String oRetVal = oRes.getString(sColName);

			if(oRetVal == null)
				logMsg("getString returns the Null Value " + oRetVal);
			else
			{
				logErr("getString did not return the Null Value");
				throw new Fault("Call to getString Failed!");
			}
			
		}
		catch(SQLException sqle)
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to getString is Failed!");
		}
		catch (Exception e)
		{
			logErr("Exception has occured"+e.getMessage());
			throw new Fault("Call to getString is Failed!");
		}
		finally
		{
			try
			{
				oRes.close();
				stmt.close();
				//drop the table
				rsSch.dropTab("Timestamp_Tab",conn);
			}
			catch (Exception eclean) 
			{
				logErr("Exception has occured"+eclean.getMessage());
				throw new Fault("Call to getString is Failed!");
			}
		}
	}




	/*
	 * @testName:         testGetBytes01
	 *                    
	 * @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 getBytes(int columnIndex) returns the value of column 
	 *                    columnIndex as a byte array Object in the java programming 
	 *                    language and null if the value is SQL null. 
	 *                    
	 * @test_Strategy:    Get a ResultSet object from the Connection to the database.Update
	 *                    the column value of Binary_Tab table with a byte array using the
	 *                    PreparedStatement.setBytes(int columnIndex) method.Call the 
	 *                    getBytes(int columnIndex) method with the SQL column of JDBC datatype 
	 *                    BINARY.It should return the byte array object that has been set.
	 */
	public void testGetBytes01() throws Fault
	{
		String binarySize=null;
		String createString=null,createString1=null,createString2=null;
		String executeString=null;
		ResultSet oRes=null;
		try
		{
			rsSch.createTab("Binary_Tab",props,conn);
			// to extract the Binary Table size from property file
			binarySize = props.getProperty("binarySize");
			logMsg("Binary Table Size : " + binarySize);

			String sPrepStmt = props.getProperty("Binary_Tab_Val_Update","");
			logMsg("Prepared Statement String :" + sPrepStmt);			
			
			logMsg("From File" + props.getProperty("bytesize"));
			
			int bytearrsize = Integer.parseInt(binarySize);
			
			logMsg("ByteArraySize is :" + bytearrsize);
			
			byte[] bytearr=new byte[bytearrsize];
			String sbyteval=null;
			// to get the bytearray value			
			for (int count=0;count<bytearrsize;count++)
			{
				sbyteval=Integer.toString(count%255);
				bytearr[count]=Byte.parseByte(sbyteval);
			}			

			// get the PreparedStatement object
			pstmt= conn.prepareStatement(sPrepStmt);
			pstmt.setBytes(1,bytearr);
			pstmt.executeUpdate();

			//Execute the query and get the resultSet Object
			String sQuery=props.getProperty("Binary_Query_Val","");
			oRes = stmt.executeQuery(sQuery);
			oRes.next();
			logMsg("Calling ResultSet.GetBytes(Value)");
			//get the value from the table using GetBytes Method
			byte[] oRetVal = oRes.getBytes(1);
			for(int i=0;i<bytearrsize;i++)
			{
				if(oRetVal[i] != bytearr[i])
				{
					logErr("GetBytes did not return the proper byte array values");
					throw new Fault("Call to GetBytes Failed!");
				}
			}
			logMsg("GetBytes returns the proper byte array values");
		}
		catch(SQLException sqle)
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to GetBytes is Failed!");
		}
		catch (Exception e)
		{
			logErr("Exception has occured"+e.getMessage());
			throw new Fault("Call to GetBytes is Failed!");
		}
		finally
		{
			try
			{
			pstmt.close();
			oRes.close();
			stmt.close();
			//drop the table
			rsSch.dropTab("Binary_Tab",conn);
			}
			catch (Exception eclean) 
			{
				logErr("Exception has occured"+eclean.getMessage());
				throw new Fault("Call to getString is Failed!");
			}
		}
	}


	/*
	 * @testName:         testGetBytes02
	 *                    
	 * @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 getBytes(int columnIndex) returns the value of column 
	 *                    columnIndex as a byte array Object in the java programming 
	 *                    language and null if the value is SQL null. 
	 *                    
	 * @test_Strategy:    Get a ResultSet object from the Connection to the database.
	 *                    Call the getBytes(int columnIndex) method with the SQL null column of 
	 *                    JDBC datatype BINARY.It should return null byte array object.
	 */
	public void testGetBytes02() throws Fault
	{
		String binarySize=null;
		String createString=null,createString1=null,createString2=null;
		String executeString=null;
		ResultSet oRes=null;
		try
		{
			rsSch.createTab("Binary_Tab",props,conn);
			// to extract the Binary Table size from property file
			binarySize = props.getProperty("binarySize");
			logMsg("Binary Table Size : " + binarySize);

			//Execute the query and get the resultSet Object
			String sQuery=props.getProperty("Binary_Query_Val","");
			oRes = stmt.executeQuery(sQuery);
			oRes.next();
			logMsg("Calling ResultSet.GetBytes(NullValue)");
			//get the Null value from the table using GetBytes Method
			byte[] oRetVal = oRes.getBytes(1);
			//check whether the value is null or not
			if(oRes.wasNull())
				logMsg("GetBytes returns the Null Value " + oRetVal);
			else
			{
				logErr("GetBytes did not return the Null Value");
				throw new Fault("Call to GetBytes Failed!");
			}
			
		}
		catch(SQLException sqle)
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to GetBytes is Failed!");
		}
		catch (Exception e)
		{
			logErr("Exception has occured"+e.getMessage());
			throw new Fault("Call to GetBytes is Failed!");
		}
		finally
		{
			try
			{
				oRes.close();
				stmt.close();
				//drop the table
				rsSch.dropTab("Binary_Tab",conn);
			}
			catch (Exception eclean) 
			{
				logErr("Exception has occured"+eclean.getMessage());
				throw new Fault("Call to getString is Failed!");
			}
		}
	}


	/*
	 * @testName:         testGetBytes03
	 *                    
	 * @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 getBytes(int columnIndex) returns the value of column 
	 *                    columnIndex as a byte array Object in the java programming 
	 *                    language and null if the value is SQL null. 
	 *                    
	 * @test_Strategy:    Get a ResultSet object from the Connection to the database.Update
	 *                    the column value of Varbinary_Tab table with a byte array using the
	 *                    PreparedStatement.setBytes(int columnIndex).Call the getBytes(int columnIndex)
	 *                    method with the SQL column of JDBC datatype VARBINARY.It should return the 
	 *                    byte array object that has been set.
	 */
	public void testGetBytes03() throws Fault
	{
		String varbinarySize=null;
		String createString=null,createString1=null,createString2=null;
		String executeString=null;
		ResultSet oRes=null;
		try
		{
			rsSch.createTab("Varbinary_Tab",props,conn);
			// to extract the Binary Table size from property file
			varbinarySize = props.getProperty("varbinarySize");
			logMsg("Varbinary Table Size : " + varbinarySize);
			
			String sPrepStmt = props.getProperty("Varbinary_Tab_Val_Update","");
			logMsg("Prepared Statement String :" + sPrepStmt);

			int bytearrsize = Integer.parseInt(varbinarySize);
			
			logMsg("ByteArraySize is :" + bytearrsize);

			byte[] bytearr=new byte[bytearrsize];
			String sbyteval=null;
			// to get the bytearray value
			for (int count=0;count<bytearrsize;count++)
			{
				sbyteval=Integer.toString(count%255);
				bytearr[count]=Byte.parseByte(sbyteval);
			}

			// get the PreparedStatement object
			pstmt= conn.prepareStatement(sPrepStmt);
			pstmt.setBytes(1,bytearr);
			pstmt.executeUpdate();

			//Execute the query and get the resultSet Object
			String sQuery=props.getProperty("Varbinary_Query_Val","");
			oRes = stmt.executeQuery(sQuery);
			oRes.next();
			logMsg("Calling ResultSet.GetBytes(Value)");
			//get the value from the table using GetBytes Method
			byte[] oRetVal = oRes.getBytes(1);
			for(int i=0;i<bytearrsize;i++)
			{
				if(oRetVal[i] != bytearr[i])
				{
					logErr("GetBytes did not return the proper byte array values");
					throw new Fault("Call to GetBytes Failed!");
				}
			}
			logMsg("GetBytes returns the proper byte array values");
			
		}
		catch(SQLException sqle)
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to GetBytes is Failed!");
		}
		catch (Exception e)
		{
			logErr("Exception has occured"+e.getMessage());
			throw new Fault("Call to GetBytes is Failed!");
		}
		finally
		{
			try
			{
				pstmt.close();
				oRes.close();
				stmt.close();
				//drop the table
				rsSch.dropTab("Varbinary_Tab",conn);
			}catch (Exception eclean) 
			{
				logErr("Exception has occured"+eclean.getMessage());
				throw new Fault("Call to getString is Failed!");
			}
		}
	}


	/*
	 * @testName:         testGetBytes04
	 *                    
	 * @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 getBytes(int columnIndex) returns the value of column 
	 *                    columnIndex as a byte array Object in the java programming 
	 *                    language and null if the value is SQL null. 
	 *                    
	 * @test_Strategy:    Get a ResultSet object from the Connection to the database.
	 *                    Call the getBytes(int columnIndex) method with the SQL null column
	 *                    of JDBC datatype VARBINARY.It should return null byte array object.
	 */
	public void testGetBytes04() throws Fault
	{
		String varbinarySize=null;
		String createString=null,createString1=null,createString2=null;
		String executeString=null;
		ResultSet oRes=null;
		try
		{
			rsSch.createTab("Varbinary_Tab",props,conn);
			// to extract the Varbinary Table size from property file
			varbinarySize = props.getProperty("varbinarySize");
			logMsg("Varbinary Table Size : " + varbinarySize);

			//Execute the query and get the resultSet Object
			String sQuery=props.getProperty("Varbinary_Query_Val","");
			oRes = stmt.executeQuery(sQuery);
			oRes.next();
			logMsg("Calling ResultSet.GetBytes(NullValue)");
			//get the Null value from the table using GetBytes Method
			byte[] oRetVal = oRes.getBytes(1);
			//check whether the value is null or not
			if(oRes.wasNull())
				logMsg("GetBytes returns the Null Value " + oRetVal);
			else
			{
				logErr("GetBytes did not return the Null Value");
				throw new Fault("Call to GetBytes Failed!");
			}
			
		}
		catch(SQLException sqle)
		{
			logErr("SQL Exception " + sqle.getMessage());
			throw new Fault("Call to GetBytes is Failed!");
		}
		catch (Exception e)
		{
			logErr("Exception has occured"+e.getMessage());
			throw new Fault("Call to GetBytes is Failed!");
		}
		finally
		{
			try
			{
				oRes.close();
				stmt.close();
				//drop the table
				rsSch.dropTab("Varbinary_Tab",conn);
			}
			catch (Exception eclean) 
			{
				logErr("Exception has occured"+eclean.getMessage());
				throw new Fault("Call to getString is Failed!");
			}
		}
	}


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