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

/*
 * @(#)dateTimeClient2.java	1.7 02/01/12
 */

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

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

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

	//Harness requirements 
	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;

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

			/*sqlp=new Properties();
			sqlStmt= p.getProperty("rsQuery","");
			InputStream istr= new FileInputStream(sqlStmt);
			sqlp.load(istr);*/
			sqlp=p;
		} 
		catch ( Exception e ) 
		{
			logErr("Setup Failed!");
		}
	
	}


	/*
	 * @testName:         testTime01
	 * @assertion:        The class java.sql.Time represents a JDBC Time 
	 *                    value in Java Programming language.A JDBC TIME value
	 *                    has only the time information 
	 *                    
	 *                    The constructor Time(long milliseconds) constructs 
	 *                    a java.sql.Time object from milliseconds,a 
	 *                    milliseconds time value.to represent a value that can
	 *                    be used as JDBC Time Value.
	 *                    
	 *                    (Refer JDBC 2.0 API Reference and Tuorial Section 42.1)
	 *                    
	 @ test_Strategy:     Create a Time Object with a long value as an argument.
	 *                    Then get the String representation of that Time object.
	 *                    Check whether it is same as equivalent String Value in 
	 *                    property file. 
	 */
	public void testTime01() throws Fault
	{
		String sTimeLongVal = null;
		String sTimeVal = null;
		String sTimeObjVal = null;
		java.sql.Time timeObj = 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);

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

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

			timeObj = new Time(generalisedVal);
			sTimeObjVal = timeObj.toString().trim();
			logMsg("The time object's value is : "+sTimeObjVal);

			booRetVal=sTimeVal.trim().equals(sTimeObjVal);
			if(booRetVal)
			{
				logMsg("Time Constructor constructs a Time object");
			}
			else 
			{
				logErr("Time Constructor does not construct a Time object");
				throw new Fault("Call to Time Constructor is Failed!");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to Time Constructor is Failed!"+ e);
		}
	}


	/*
	 * @testName:         testTime02
	 * @assertion:        The class java.sql.Time represents a JDBC Time 
	 *                    value in Java Programming language.A JDBC TIME value
	 *                    has only the time information 
	 *                    
	 *                    The constructor Time(long milliseconds) constructs 
	 *                    a java.sql.Time object from milliseconds,a 
	 *                    milliseconds time value.to represent a value that can
	 *                    be used as JDBC Time Value.
	 *                    
	 *                    (Refer JDBC 2.0 API Reference and Tuorial Section 42.1)
	 *                    
	 @ test_Strategy:     Create a Time Object with a long value as an argument.
	 *                    Then get the String representation of that Time object.
	 *                    Check whether it is same as equivalent String Value in 
	 *                    property file. 
	 */
	public void testTime02() throws Fault
	{
		String sTimeLongVal = null;
		String sTimeVal = null;
		String sTimeObjVal = null;
		java.sql.Time timeObj = null;
		boolean booRetVal;

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

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

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

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

			sTimeVal = sTimeVal.substring(sTimeVal.indexOf(' '),sTimeVal.indexOf('.'));
			logMsg("String value of Time "+sTimeVal);

			timeObj = new Time(generalisedVal);
			sTimeObjVal = timeObj.toString().trim();
			logMsg("The time object's value is : "+sTimeObjVal);

			booRetVal=sTimeVal.trim().equals(sTimeObjVal);
			if(booRetVal)
			{
				logMsg("Time Constructor constructs a Time object");
			}
			else 
			{
				logErr("Time Constructor does not construct a expected Time object");
				throw new Fault("Call to Time Constructor is Failed!");
			}
		}
		catch (Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to Time Constructor is Failed!"+ e);
		}
	}


	/*
	 * @testName:         testToString01
	 * @assertion:        The class java.sql.Time represents a JDBC Time 
	 *                    value in Java Programming language.A JDBC TIME value
	 *                    has only the time information 
	 *                    
	 *                    The Method toString() formats the Time Object as a
	 *                    a String with the format hh:mm:ss.
	 *                    The driver uses a default Timezone in its conversion from
	 *                    milliseconds to String Object
	 *                    
	 *                    (Refer JDBC 2.0 API Reference and Tuorial Section 42.1)
	 *                    
	 @ test_Strategy:     Create a Time Object with a long value as an argument.
	 *                    Then get the String representation of that Time object.
	 *                    using the toString() method.Check whether it is same as 
	 *                    equivalent String Value in property file. 
	 */
	public void testToString01() throws Fault
	{
		String sTimeLongVal = null;
		String sTimeVal = null;
		String sTimeObjVal = null;
		java.sql.Time timeObj = 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);

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

			sTimeVal = sTimeVal.substring(sTimeVal.indexOf(' '),sTimeVal.indexOf('.'));
			logMsg("String value of Time  "+sTimeVal);

			timeObj = new Time(generalisedVal);
			sTimeObjVal = timeObj.toString().trim();
			logMsg("The time object's value is : "+sTimeObjVal);

			booRetVal=sTimeVal.trim().equals(sTimeObjVal);
			if(booRetVal)
			{
				logMsg("toString method returns equivalent String object");
			}
			else 
			{
				logErr("toString method does not return equivalent String object");
				throw new Fault("Call to toString is Failed!");
			}
		}
		catch (Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to toString is Failed!"+ e);
		}
	}


	/*
	 * @testName:         testToString02
	 * @assertion:        The class java.sql.Time represents a JDBC Time 
	 *                    value in Java Programming language.A JDBC TIME value
	 *                    has only the time information 
	 *                    
	 *                    The Method toString() formats the Time Object as a
	 *                    a String with the format hh:mm:ss.
	 *                    The driver uses a default Timezone in its conversion from
	 *                    milliseconds to String Object
	 *                    
	 *                    (Refer JDBC 2.0 API Reference and Tuorial Section 42.1)
	 *                    
	 @ test_Strategy:     Create a Time Object with a long value as an argument.
	 *                    Then get the String representation of that Time object.
	 *                    using the toString() method.Check whether it is same as 
	 *                    equivalent String Value in property file. 
	 */
	public void testToString02() throws Fault
	{
		String sTimeLongVal = null;
		String sTimeVal = null;
		String sTimeObjVal = null;
		java.sql.Time timeObj = null;
		boolean booRetVal;

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

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

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

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

			sTimeVal = sTimeVal.substring(sTimeVal.indexOf(' '),sTimeVal.indexOf('.'));
			logMsg("String value of Time  "+sTimeVal);

			timeObj = new Time(generalisedVal);
			sTimeObjVal = timeObj.toString().trim();
			logMsg("The time object's value is : "+sTimeObjVal);

			booRetVal=sTimeVal.trim().equals(sTimeObjVal);
			if(booRetVal)
			{
				logMsg("toString method returns equivalent String object");
			}
			else 
			{
				logErr("toString method does not return equivalent String object");
				throw new Fault("Call to toString is Failed!");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to toString is Failed!"+ e);
		}
	}


	/*
	 * @testName:         testValueOf01
	 * @assertion:        The class java.sql.Time represents a JDBC Time 
	 *                    value in Java Programming language.A JDBC TIME value
	 *                    has only the time information 
	 *                    
	 *                    The valueOf a static method in java.sql.Time class
	 *                    converts a formatted String (hh:mm:ss) to a
	 *                    valid Time Object .The driver uses default time zone 
	 *                    in its conversion of a String Object to Time Object.
	 *                    
	 *                    (Refer JDBC 2.0 API Tutorial and Reference Section 42.1)
	 *                    
	 * @test_Strategy:    Call valueof(String ts) static method in java.sql.Time
	 *                    class with a String argument to get a Time object 
	 *                    Check whether it is same as Time object obtained from
	 *                    equivalent long value .

	 */
	public void testValueOf01() throws Fault
	{
		String sTimeLongVal = null;
		String sTimeVal = null;
		java.sql.Time timeObj = null;
		java.sql.Time retTimeObj = null;
		String sTimeObjVal = null;
		String sRetTimeObjVal = null;
		boolean booRetVal;

		try
		{
			sTimeLongVal = sqlp.getProperty("DateTime_Long_Val1","");
			long timeLongVal = Long.parseLong(sTimeLongVal);
			long generalisedVal = timeLongVal-valToGeneralize;

			sTimeVal = sqlp.getProperty("DateTime_Str_Val1","");
			sTimeVal = sTimeVal.substring(sTimeVal.indexOf(' '),sTimeVal.indexOf('.'));
			logMsg("String value of Time"+sTimeVal);

			timeObj = new Time(generalisedVal);
			sTimeObjVal = timeObj.toString().trim();
			logMsg("The timeObj object's value is :"+sTimeObjVal);

			retTimeObj = java.sql.Time.valueOf(sTimeObjVal);
			sRetTimeObjVal = retTimeObj.toString();
			logMsg("The retTimeObj object's value is :"+ sRetTimeObjVal);

			booRetVal=sTimeObjVal.equals(sRetTimeObjVal);
			if(booRetVal)
			{
				logMsg("valueOf method returns the equivalent Time object");
			}
			else 
			{
				logErr("valueOf method does not return the equivalent Time object");
				throw new Fault("Call to valueOf is Failed!");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to valueOf is Failed!"+ e);
		}
	}


	/*
	 * @testName:         testValueOf02
	 * @assertion:        The class java.sql.Time represents a JDBC Time 
	 *                    value in Java Programming language.A JDBC TIME value
	 *                    has only the time information 
	 *                    
	 *                    The valueOf a static method in java.sql.Time class
	 *                    converts a formatted String (hh:mm:ss) to a
	 *                    valid Time Object .The driver uses default time zone 
	 *                    in its conversion of a String Object to Time Object.
	 *                    
	 *                    (Refer JDBC 2.0 API Tutorial and Reference Section 42.1)
	 *                    
	 * @test_Strategy:    Call valueof(String ts) static method in java.sql.Time
	 *                    class with a String argument to get a Time object 
	 *                    Check whether it is same as Time object obtained from
	 *                    equivalent long value .
	 */
	public void testValueOf02() throws Fault
	{
		String sTimeLongVal = null;
		String sTimeVal = null;
		java.sql.Time timeObj = null;
		java.sql.Time retTimeObj = null;
		String sTimeObjVal = null;
		String sRetTimeObjVal = null;
		boolean booRetVal;

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

			sTimeVal = sqlp.getProperty("DateTime_Str_Val2","");
			sTimeVal = sTimeVal.substring(sTimeVal.indexOf(' '),sTimeVal.indexOf('.'));
			logMsg("String value of Time  "+sTimeVal);

			timeObj = new Time(generalisedVal);
			sTimeObjVal = timeObj.toString().trim();
			logMsg("The timeObj object's value is :"+sTimeObjVal);

			retTimeObj = java.sql.Time.valueOf(sTimeObjVal);
			sRetTimeObjVal = retTimeObj.toString();
			logMsg("The retTimeObj object's value is :"+ sRetTimeObjVal);

			booRetVal=sTimeObjVal.equals(sRetTimeObjVal);
			if(booRetVal)
			{
				logMsg("valueOf method returns the equivalent Time object");
			}
			else 
			{
				logErr("valueOf method does not return the equivalent Time object");
				throw new Fault("Call to valueOf is Failed!");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to valueOf is Failed!"+ e);
		}
	}


	/*
	 * @testName:         testSetTime01
	 * @assertion:        The class java.sql.Time represents a JDBC Time 
	 *                    value in Java Programming language.A JDBC TIME value
	 *                    has only the time information 
	 *                    
	 *                    This method setTime(long l) sets this Time Object to time 
	 *                    which represents the milliseconds since January 1, 1970 .
	 *                    This method overides the setTime method in the class java.util.Time
	 *                    The long value indicates the number of 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 42.1)
	 *                    
	 * @test_Strategy:    Create two Time 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 Time objects are equal using equals method
	 */
	public void testSetTime01() throws Fault
	{
		String sTimeLongVal1 = null;
		String sTimeLongVal2 = null;
		String sTimeVal1 = null;
		String sTimeVal2 = null;
		java.sql.Time timeObj1 = null;
		java.sql.Time timeObj2 = null;
		String sTimeObjVal1 = null;
		String sTimeObjVal2 = null;
		boolean booRetVal;

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

			sTimeVal1 = sqlp.getProperty("DateTime_Str_Val1","");
			sTimeVal1 = sTimeVal1.substring(sTimeVal1.indexOf(' '),sTimeVal1.indexOf('.'));
			logMsg("String Value of Time1  "+sTimeVal1);

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

			sTimeVal2 = sqlp.getProperty("DateTime_Str_Val2","");
			sTimeVal2 = sTimeVal2.substring(sTimeVal2.indexOf(' '),sTimeVal2.indexOf('.'));
			logMsg("String Value of Time2"+sTimeVal2);

			timeObj1 = new Time(generalisedVal1);
			sTimeObjVal1 = timeObj1.toString().trim();
			logMsg("The timeObj1 object's value is :"+sTimeObjVal1);

			timeObj2 = new Time(generalisedVal2);
			sTimeObjVal2 = timeObj2.toString().trim();
			logMsg("The timeObj2 object's value before setting is :"+sTimeObjVal2);

			timeObj2.setTime(generalisedVal1);
			sTimeObjVal2 = timeObj2.toString().trim();
			logMsg("The retTimeObj object's value is after  setting time:"+ sTimeObjVal2);

			booRetVal=sTimeObjVal1.equals(sTimeObjVal2);
			if(booRetVal)
			{
				logMsg("setTime() method sets the Time value");
			}
			else 
			{
				logErr("setTime method does not set the Time value");
				throw new Fault("call to setTime is Failed");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to setTime is Failed!"+ e);
		}
	}


	/*
	 * @testName:         testSetTime02
	 * @assertion:        The class java.sql.Time represents a JDBC Time 
	 *                    value in Java Programming language.A JDBC TIME value
	 *                    has only the time information 
	 *                    
	 *                    This method setTime(long l) sets this Time Object to time 
	 *                    which represents the milliseconds since January 1, 1970 .
	 *                    This method overides the setTime method in the class java.util.Time
	 *                    The long value indicates the number of 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 42.1)
	 *                    
	 * @test_Strategy:    Create two Time 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 Time objects are equal using equals method
	 */
	public void testSetTime02() throws Fault
	{
		String sTimeLongVal1 = null;
		String sTimeLongVal2 = null;
		String sTimeVal1 = null;
		String sTimeVal2 = null;
		java.sql.Time timeObj1 = null;
		java.sql.Time timeObj2 = null;
		String sTimeObjVal1 = null;
		String sTimeObjVal2 = null;
		String sTestTimeObjectVal2 = null;
		boolean booRetVal;
		try
		{
			sTimeLongVal1 = sqlp.getProperty("DateTime_Long_Val1","");
			long timeLongVal1 = Long.parseLong(sTimeLongVal1);
			long generalisedVal1 = timeLongVal1-valToGeneralize;

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

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

			sTimeVal2 = sqlp.getProperty("DateTime_Str_Val2","");
			sTimeVal2 = sTimeVal2.substring(sTimeVal2.indexOf(' '),sTimeVal2.indexOf('.'));
			logMsg("String value of Time1 "+sTimeVal2);

			timeObj1 = new Time(generalisedVal2);
			sTimeObjVal1 = timeObj1.toString().trim();
			logMsg("The timeObj1 object's value is :"+sTimeObjVal1);

			timeObj2 = new Time(generalisedVal1);
			sTimeObjVal2 = timeObj2.toString().trim();
			logMsg("The timeObj2 object's value before setting is :"+sTimeObjVal2);

			timeObj2.setTime(generalisedVal2);
			sTestTimeObjectVal2 = timeObj2.toString().trim();
			logMsg("The retTimeObj object's value is after  setting time:"+ sTestTimeObjectVal2);

			booRetVal=sTimeObjVal1.equals(sTestTimeObjectVal2);
			if(booRetVal)
				logMsg("setTime method sets the Time value ");
			else 
			{
				logErr("setTime method does not set the Time value ");
				throw new Fault("Call to setTime is Failed!");
			}
		}
		catch(Exception e)
		{
			logErr("Unexpected exception " + e.getMessage() );
			throw new Fault("Call to setTime is Failed!"+ e);
		}
	}


	/* cleanup */
	public void  cleanup() throws Fault	
	{
		try
		{
			logMsg("Cleanup ok;");
		}
		catch (Exception e)
		{
			logErr("An error occurred in Cleanup method");
		}
	}
}
