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

/*
 * @(#)dateTimeClient3.java	1.6 02/01/12
 */

package com.sun.cts.tests.jdbc.ee.dateTime.dateTime3;

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

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

	//Harness requirements 
	private String drManager=null;
	private String sqlStmt=null;
	private Properties sqlp=null;
	private TimeZone tz = null;
	private int valToGeneralize = 0;
	private Calendar cal = null;
	private String sInTimeVal = null;
	private Timestamp inTimeVal = null;
	private String Reference_Value = null;
	private Timestamp inTimeVal1 = null;
	private Timestamp inTimeVal2 = null;

	/* Run test in standalone mode */
	public static void main(String[] args) 
	{
		dateTimeClient3 theTests = new dateTimeClient3();
		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 
		{
			tz = TimeZone.getDefault();
			boolean b1 = tz.useDaylightTime();

			if(!b1)
			{
				cal = Calendar.getInstance(tz);
				valToGeneralize = tz.getOffset(cal.get(Calendar.ERA),cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH),cal.get(Calendar.DAY_OF_WEEK),cal.get(Calendar.MILLISECOND));
			}
			else
			{
				valToGeneralize = tz.getRawOffset();
			}


			drManager = p.getProperty("DriverManager","");
			if (drManager.length() == 0)
			{
				throw new Fault("Invalid DriverManager Name");
			}
			/*sqlp=new Properties();
			sqlStmt= p.getProperty("rsQuery","");
			InputStream istr= new FileInputStream(sqlStmt);
			sqlp.load(istr);*/
			sqlp=p;
		}
		catch( Exception e ) 
		{
			logErr("Setup Failed!");
		}
	}


	/*
	 * @testName:         testDate01
	 * @assertion:        The class java.sql.Date represents a JDBC Date 
	 *                    value in Java Programming language.A JDBC Date value
	 *                    has only the Date information 
	 *                    
	 *                    The constructor Date(long milliseconds) constructs 
	 *                    a java.sql.Date object from milliseconds,milliseconds since 
	 *                    January 1, 1970, 00:00:00 GMT. A negative number indicates 
	 *                    the number of milliseconds before January 1, 1970, 00:00:00 GMT. 
	 *                    
	 *                    (Refer JDBC 2.0 API Reference and Tutorial Section 18.3)
	 *                    
	 * @ test_Strategy:   Create a Date Object with a long value as an argument.
	 *                    Then get the String representation of that Date object.
	 *                    Check whether it is same as equivalent String Value in 
	 *                    property file.
	 */
	
	public void testDate01() throws Fault
	{
		String sTimeLongVal = null;
		String sDateVal = null;
		String sDateObjVal = null;
		java.sql.Date dateObj = null;
		boolean booRetVal;
		try
		{
			sTimeLongVal = sqlp.getProperty("DateTime_Long_Val1","");
			logMsg("Time Long Value " + sTimeLongVal);

			long timeLongVal = Long.parseLong(sTimeLongVal);
			logMsg("Long Value " + timeLongVal);

			long generalisedVal = timeLongVal-valToGeneralize;
			logMsg("Generalised Value " + generalisedVal);

			sDateVal = sqlp.getProperty("DateTime_Str_Val1","");
			logMsg("sDate ="+sDateVal);

			sDateVal = sDateVal.substring(0,sDateVal.indexOf(' '));
			logMsg("The date string got from the properties file is"+sDateVal);

			dateObj = new java.sql.Date(generalisedVal);
			sDateObjVal = dateObj.toString().trim();
			logMsg("The date object's value is : "+sDateObjVal);

			booRetVal = sDateVal.trim().equals(sDateObjVal);
			if(booRetVal)
			{
				logMsg("Date object created as expected");
			}
			else 
			{
				logErr("Date object not created as expected");
				throw new Fault("Date object construction failed");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Date object creation Failed!"+ e);
		}
	}


	/*
	 * @testName:         testDate02
	 * @assertion:        The class java.sql.Date represents a JDBC Date 
	 *                    value in Java Programming language.A JDBC Date value
	 *                    has only the Date information 
	 *                    
	 *                    The constructor Date(long milliseconds) constructs 
	 *                    a java.sql.Date object from milliseconds,milliseconds since 
	 *                    January 1, 1970, 00:00:00 GMT. A negative number indicates 
	 *                    the number of milliseconds before January 1, 1970, 00:00:00 GMT. 
	 *                    
	 *                    (Refer JDBC 2.0 API Reference and Tutorial Section 18.3)
	 *                    
	 * @ test_Strategy:   Create a Date Object with a long value as an argument.
	 *                    Then get the String representation of that Date object.
	 *                    Check whether it is same as equivalent String Value in 
	 *                    property file.
	 */
	public void testDate02() throws Fault
	{
		String sDateLongVal = null;
		String sDateVal = null;
		String sDateObjVal = null;
		java.sql.Date dateObj = null;
		boolean booRetVal;

		try
		{
			sDateLongVal = sqlp.getProperty("DateTime_Long_Val2","");
			logMsg("Date Long Value " + sDateLongVal);

			long dateLongVal = Long.parseLong(sDateLongVal);
			logMsg("Long Value " + dateLongVal);

			long generalisedVal = dateLongVal-valToGeneralize;
			logMsg("Generalised Value " + generalisedVal);

			sDateVal = sqlp.getProperty("DateTime_Str_Val2","");
			logMsg("sTime ="+sDateVal);

			sDateVal = sDateVal.substring(0,sDateVal.indexOf(' '));
			logMsg("The date string got from the properties file is"+sDateVal);

			dateObj = new java.sql.Date(generalisedVal);
			sDateObjVal = dateObj.toString().trim();
			logMsg("The date object's value is : "+sDateObjVal);
			booRetVal = sDateVal.trim().equals(sDateObjVal) ;

			if(booRetVal)
			{
				logMsg("Date object created as expected");
			}
			else 
			{
				logErr("Date object not created as expected");
				throw new Fault("Date object construction failed");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Unexpected exception and Date object creation Failed!"+ e);
		}
	}


	/*
	 * @testName:         testToString01
	 * @assertion:        The class java.sql.Date represents a JDBC Date 
	 *                    value in Java Programming language.A JDBC Date value
	 *                    has only the Date information 
	 *                    
	 *                    The Method toString() formats the Date Object as a
	 *                    a String with the format yyyy-mm-dd.
	 *                    The driver uses a default Timezone in its conversion from
	 *                    milliseconds to String Object
	 *                    
	 *                    (Refer JDBC 2.0 API Reference and Tuorial Section 18.3)
	 *                    
	 * @ test_Strategy:   Create a Date Object with a long value as an argument.
	 *                    Then get the String representation of that Date object.
	 *                    using the toString() method.Check whether it is same as 
	 *                    equivalent String Value in property file. 
	 */
	public void testToString01() throws Fault
	{
		String sDateLongVal = null;
		String sDate = null;
		String sDateVal = null;
		String sDateObjVal = null;
		java.sql.Date dateObj = null;
		boolean booRetVal;
		try
		{
			sDateLongVal = sqlp.getProperty("DateTime_Long_Val1","");
			logMsg("Date Long Value " + sDateLongVal);

			long dateLongVal = Long.parseLong(sDateLongVal);
			logMsg("Long Value " + dateLongVal);

			long generalisedVal = dateLongVal-valToGeneralize;
			logMsg("Generalised Value " + generalisedVal);

			sDateVal = sqlp.getProperty("DateTime_Str_Val1","");
			logMsg("sTime ="+sDateVal);

			sDateVal = sDateVal.substring(0,sDateVal.indexOf(' '));
			logMsg("The date string got from the properties file is"+sDateVal);

			dateObj = new java.sql.Date(generalisedVal);
			sDateObjVal = dateObj.toString().trim();
			logMsg("The date object's value is : "+sDateObjVal);
			booRetVal = sDateVal.trim().equals(sDateObjVal);

			if(booRetVal)
			{
				logMsg("Date object created as expected and call to toString method passes");
			}
			else 
			{
				logErr("call to toString method doesnt returns the expected value");
				throw new Fault("call to toString() nmethod failed");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Unexpected exception!"+ e);
		}
	}


	/*
	 * @testName:         testToString02
	 * @assertion:        The class java.sql.Date represents a JDBC Date 
	 *                    value in Java Programming language.A JDBC Date value
	 *                    has only the Date information 
	 *                    
	 *                    The Method toString() formats the Date Object as a
	 *                    a String with the format yyyy-mm-dd.
	 *                    The driver uses a default Timezone in its conversion from
	 *                    milliseconds to String Object
	 *                    
	 *                    (Refer JDBC 2.0 API Reference and Tuorial Section 18.3)
	 *                    
	 * @ test_Strategy:   Create a Date Object with a long value as an argument.
	 *                    Then get the String representation of that Date object.
	 *                    using the toString() method.Check whether it is same as 
	 *                    equivalent String Value in property file. 
	 */
	public void testToString02() throws Fault
	{
		String sDateLongVal = null;
		String sDateVal = null;
		String sDateObjVal = null;
		java.sql.Date dateObj = null;
		boolean booRetVal;
		try
		{
			sDateLongVal = sqlp.getProperty("DateTime_Long_Val2","");
			logMsg("Date Long Value " + sDateLongVal);

			long dateLongVal = Long.parseLong(sDateLongVal);
			logMsg("Long Value " + dateLongVal);

			long generalisedVal = dateLongVal-valToGeneralize;
			logMsg("Generalised Value " + generalisedVal);

			sDateVal = sqlp.getProperty("DateTime_Str_Val2","");
			logMsg("sDate ="+sDateVal);

			sDateVal = sDateVal.substring(0,sDateVal.indexOf(' '));
			logMsg("The date string got from the properties file is"+sDateVal);

			dateObj = new java.sql.Date(generalisedVal);
			sDateObjVal = dateObj.toString().trim();
			logMsg("The date object's value is : "+sDateObjVal);

			booRetVal = sDateVal.trim().equals(sDateObjVal);
			if(booRetVal)
			{
				logMsg("Date object created as expected and call to toString method passes");
			}
			else 
			{
				logErr("call to toString method doesnt returns the expected value");
				throw new Fault("call to toString() nmethod failed");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Unexpected exception and test failed!"+ e);
		}
	}	


	/*
	 * @testName:         testValueOf01
	 * @assertion:        The class java.sql.Date represents a JDBC Date 
	 *                    value in Java Programming language.A JDBC Date value
	 *                    has only the Date information 
	 *                    
	 *                    The valueOf a static method in java.sql.Date class
	 *                    converts a formatted String (yyyy:mm:dd) to a
	 *                    valid Date Object .The driver uses default time zone 
	 *                    in its conversion of a String Object to Date Object.
	 *                    
	 *                    (Refer JDBC 2.0 API Tutorial and Reference Section 18.3)
	 *                    
	 * @test_Strategy:    Call valueof(String ts) static method in java.sql.Date
	 *                    class with a String argument to get a Date object 
	 *                    Check whether it is same as Date object obtained from
	 *                    equivalent long value .
	 */
	public void testValueOf01() throws Fault
	{
		String sDateLongVal = null;
		String sDateVal = null;
		java.sql.Date dateObj = null;
		java.sql.Date retDateObj = null;
		String sDateObjectVal = null;
		String sRetDateObjectVal = null;
		boolean booRetVal;
		try
		{
			sDateLongVal = sqlp.getProperty("DateTime_Long_Val1","");
			long dateLongVal = Long.parseLong(sDateLongVal);
			long generalisedVal = dateLongVal-valToGeneralize;
			
			sDateVal = sqlp.getProperty("DateTime_Str_Val1","");
			sDateVal = sDateVal.substring(0,sDateVal.indexOf('.'));
			logMsg("The date string got from the properties file is"+sDateVal);
			
			dateObj = new java.sql.Date(generalisedVal);
			sDateObjectVal = dateObj.toString().trim();
			logMsg("The dateObj object's value is :"+sDateObjectVal);
			
			retDateObj = java.sql.Date.valueOf(sDateObjectVal);
			sRetDateObjectVal = retDateObj.toString();
			logMsg("The retDateObj object's value is :"+ sRetDateObjectVal);
			booRetVal = sDateObjectVal.equals(sRetDateObjectVal) ;
			if(booRetVal)
			{
				logMsg("Date object created as expected cross checking with valuOf() method ");
			}
			else 
			{
				logErr("date object not created as expected cross checking with valuOf() method ");
				throw new Fault("call to static method valueOf failed");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Unexpected exception Date object creation Failed!"+ e);
		}
	}


	/*
	 * @testName:         testValueOf02
	 * @assertion:        The class java.sql.Date represents a JDBC Date 
	 *                    value in Java Programming language.A JDBC Date value
	 *                    has only the Date information 
	 *                    
	 *                    The valueOf a static method in java.sql.Date class
	 *                    converts a formatted String (yyyy:mm:dd) to a
	 *                    valid Date Object .The driver uses default time zone 
	 *                    in its conversion of a String Object to Date Object.
	 *                    
	 *                    (Refer JDBC 2.0 API Tutorial and Reference Section 18.3)
	 *                    
	 * @test_Strategy:    Call valueof(String ts) static method in java.sql.Date
	 *                    class with a String argument to get a Date object 
	 *                    Check whether it is same as Date object obtained from
	 *                    equivalent long value .
	 */
	public void testValueOf02() throws Fault
	{
		String sDateLongVal = null;
		String sDateVal = null;
		java.sql.Date dateObj = null;
		java.sql.Date retDateObj = null;
		String sDateObjectVal = null;
		String sRetDateObjectVal = null;
		boolean booRetVal ;

		try
		{
			sDateLongVal = sqlp.getProperty("DateTime_Long_Val2","");
			long dateLongVal = Long.parseLong(sDateLongVal);
			long generalisedVal = dateLongVal-valToGeneralize;

			sDateVal = sqlp.getProperty("DateTime_Str_Val2","");
			sDateVal = sDateVal.substring(sDateVal.indexOf(' '),sDateVal.indexOf('.'));
			logMsg("The date string got from the properties file is"+sDateVal);

			dateObj = new java.sql.Date(generalisedVal);
			sDateObjectVal = dateObj.toString().trim();
			logMsg("The dateObj object's value is :"+sDateObjectVal);

			retDateObj = java.sql.Date.valueOf(sDateObjectVal);
			sRetDateObjectVal = retDateObj.toString();
			logMsg("The retDateObj object's value is :"+ sRetDateObjectVal);

			booRetVal = sDateObjectVal.equals(sRetDateObjectVal);
			if(booRetVal)
			{
				logMsg("Date object created as expected cross checking with valuOf() method ");
			}
			else 
			{
				logErr("Date object not created as expected cross checking with valuOf() method ");
				throw new Fault("call to static method valueOf failed");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Unexpected exception and date object creation Failed!"+ e);
		}
	}
	


	/*
	 * @testName:         testSetTime01
	 * @assertion:        The class java.sql.Date represents a JDBC Date 
	 *                    value in Java Programming language.A JDBC Date value
	 *                    has only the Date information 
	 *                    
	 *                    This method setTime(long l) sets this Date Object to milliseconds 
	 *                    since January 1, 1970; negative value indicates the number of 
	 *                    milliseconds before January 1 1970 .
	 *                    
	 *                    (Refer JDBC 2.0 API Tutorial and Reference Section 18.3)
	 *                    
	 * @test_Strategy:    Create two Date objects with two different long values.
	 *                    Set the same long value in the second object as used in the first
	 *                    object using setTime(long) method 
	 *                    Check whether both the Date objects are equal using equals method
	 */
	public void testSetTime01() throws Fault
	{
		String sDateLongVal1 = null;
		String sDateLongVal2 = null;
		String sDateVal1 = null;
		String sDateVal2 = null;
		java.sql.Date dateObj1 = null;
		java.sql.Date dateObj2 = null;
		String sDateObjVal1 = null;
		String sDateObjVal2 = null;
		String sTestDateObjectVal2 = null;
		boolean booRetVal;
		try
		{
			sDateLongVal1 = sqlp.getProperty("DateTime_Long_Val1","");
			long dateLongVal1 = Long.parseLong(sDateLongVal1);
			long generalisedVal1 = dateLongVal1-valToGeneralize;

			sDateVal1 = sqlp.getProperty("DateTime_Str_Val1","");
			sDateVal1 = sDateVal1.substring(0,sDateVal1.indexOf(' '));
			logMsg("The date string got from the properties file is"+sDateVal1);

			sDateLongVal2= sqlp.getProperty("DateTime_Long_Val2","");
			long dateLongVal2 = Long.parseLong(sDateLongVal2);
			long generalisedVal2 = dateLongVal2-valToGeneralize;

			sDateVal2 = sqlp.getProperty("DateTime_Str_Val2","");
			sDateVal2 = sDateVal1.substring(0,sDateVal2.indexOf(' '));
			logMsg("The date string got from the properties file is"+sDateVal2);

			dateObj1 = new java.sql.Date(generalisedVal1);
			sDateObjVal1 = dateObj1.toString().trim();
			logMsg("The dateObj1 object's value is :"+sDateObjVal1);

			dateObj2 = new java.sql.Date(generalisedVal2);
			sDateObjVal2 = dateObj2.toString().trim();
			logMsg("The dateObj2 object's value before setting is :"+sDateObjVal2);

			dateObj2.setTime(generalisedVal1);
			sTestDateObjectVal2 = dateObj2.toString().trim();
			logMsg("The test date object's value after setting time:"+ sTestDateObjectVal2);
			booRetVal = sDateObjVal1.equals(sTestDateObjectVal2) ;

			if(booRetVal)
			{
				logMsg("Date object created as expected cross checking with setTime() method ");
			}
			else 
			{
				logErr("Date object not created as expected cross checking with setTime() method ");
				throw new Fault("call to  method setTime() failed");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Unexpected exception and test for setTime() method failed!"+ e);
		}
	}	
	


	/*
	 * @testName:         testSetTime02
	 * @assertion:        The class java.sql.Date represents a JDBC Date 
	 *                    value in Java Programming language.A JDBC Date value
	 *                    has only the Date information 
	 *                    
	 *                    This method setTime(long l) sets this Date Object to milliseconds 
	 *                    since January 1, 1970; negative value indicates the number of 
	 *                    milliseconds before January 1 1970 .
	 *                    
	 *                    (Refer JDBC 2.0 API Tutorial and Reference Section 18.3)
	 *                    
	 * @test_Strategy:    Create two Date objects with two different long values.
	 *                    Set the same long value in the second object as used in the first
	 *                    object using setTime(long) method 
	 *                    Check whether both the Date objects are equal using equals method
	 */


	public void testSetTime02() throws Fault
	{
		String sDateLongVal1 = null;
		String sDateLongVal2 = null;
		String sDateVal1 = null;
		String sDateVal2 = null;
		java.sql.Date dateObj1 = null;
		java.sql.Date dateObj2 = null;
		String sDateObjVal1 = null;
		String sDateObjVal2 = null;
		String sTestDateObjectVal2 = null;
		boolean booRetVal;

		try
		{
			sDateLongVal1 = sqlp.getProperty("DateTime_Long_Val1","");
			long dateLongVal1 = Long.parseLong(sDateLongVal1);
			long generalisedVal1 = dateLongVal1-valToGeneralize;

			sDateVal1 = sqlp.getProperty("DateTime_Str_Val1","");
			sDateVal1 = sDateVal1.substring(0,sDateVal1.indexOf(' '));
			logMsg("The date string got from the properties file is"+sDateVal1);

			sDateLongVal2= sqlp.getProperty("DateTime_Long_Val2","");
			long dateLongVal2 = Long.parseLong(sDateLongVal2);
			long generalisedVal2 = dateLongVal2-valToGeneralize;

			sDateVal2 = sqlp.getProperty("DateTime_Str_Val2","");
			sDateVal2 = sDateVal1.substring(0,sDateVal2.indexOf(' '));
			logMsg("The date string got from the properties file is"+sDateVal2);

			dateObj1 = new java.sql.Date(generalisedVal2);
			sDateObjVal1 = dateObj1.toString().trim();
			logMsg("The dateObj1 object's value is :"+sDateObjVal1);

			dateObj2 = new java.sql.Date(generalisedVal1);
			sDateObjVal2 = dateObj2.toString().trim();
			logMsg("The dateObj2 object's value before setting is :"+sDateObjVal2);

			dateObj2.setTime(generalisedVal2);
			sTestDateObjectVal2 = dateObj2.toString().trim();
			logMsg("The test date object's value after  setting time is:"+ sTestDateObjectVal2);
			booRetVal = sDateObjVal1.equals(sTestDateObjectVal2); 
			if(booRetVal)
			{
				logMsg("Date object created as expected cross checking with setTime() method ");
			}
			else 
			{
				logErr("Date object not created as expected cross checking with setTime() method ");
				throw new Fault("call to  method setTime() failed");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Unexpected exception and test for setTime() method failed!"+ e);
		}
	}


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