Test code for Time Utilities with global conversions
//
// Lapetus Ltd Java Class. Copyright (c) Lapetus Systems Ltd, 2009, 2010.
// -----------------------------------------------------------------------
// This is the intellectual property of Lapetus Systems Ltd, Artemis, Greece.
// --------------------------------------------------------------------------
// www.lapetus-ltd.com, www.lapetus.com.gr, www.lapetus.eu
// -------------------------------------------------------
// $LastChangedRevision: 1168 $
// $LastChangedDate:: 2010-11-06 16:41:19#$
// ----------------------------------------
import com.lapetus_ltd.api.TLptsMainJut;
import com.lapetus_ltd.api.common.TLptsTimeUtil;
import junit.framework.TestCase;
import java.util.Date;
// ######################################################################################################
// #### Test code for the JUT build process. This code will stop the build process on ERROR ####
// ######################################################################################################
// Class Description : This tests the Time utils.
//
// The following tests verify the time operations for creation of dates and timezone operations.
//
// The tests themselves are fairly straight forward, with the exception of the test that deal with timezones changes.
//
public class TestTimeUtil extends TestCase
{
public static void main(String[] args)
{
TestTimeUtil test = new TestTimeUtil();
test.testTimeUtils();
}
public void testTimeUtils()
{
TLptsMainJut.init();
System.out.println("Running Time Util Tests");
try // crash test with weird values
{
assertNull("-->Date must be null", TLptsTimeUtil.getDateString(null,false,false));
assertNotNull("-->Valid date should not be null", TLptsTimeUtil.getDateString(new Date(),false,false));
assertFalse("-->Valid date should return false", TLptsTimeUtil.getDateString(new Date(),false,false).equals(""));
assertNotNull("-->Valid date should not be null", TLptsTimeUtil.getDateString(new Date(),true,false));
assertFalse("-->Valid date should return false", TLptsTimeUtil.getDateString(new Date(),true,false).equals(""));
assertNotNull("-->Valid date should not be null", TLptsTimeUtil.getDateString(new Date(),false,true));
assertFalse("-->Valid date should return false", TLptsTimeUtil.getDateString(new Date(),false,true).equals(""));
assertNotNull("-->Valid date should not be null", TLptsTimeUtil.getDateString(new Date(),true,true));
assertFalse("-->Valid date should return false", TLptsTimeUtil.getDateString(new Date(),true,true).equals(""));
assertNotNull("-->Local Time should not be null", TLptsTimeUtil.getTimeString(new Date(),"GyMwWDdFEaHkKhmsSzZ"));
assertFalse("-->Local Time should not have zero length", TLptsTimeUtil.getTimeString(new Date(),"GyMwWDdFEaHkKhmsSzZ").equals(""));
assertNull("-->Local Time should be null, with invalid char", TLptsTimeUtil.getTimeString(new Date(),"b"));
assertNotNull("-->Time With Zone should be not null", TLptsTimeUtil.getTimeStringForZone(new Date(),"GyMwWDdFEaHkKhmsSzZ",121));
assertFalse("-->Time With Zone should not have zero length", TLptsTimeUtil.getTimeStringForZone(new Date(),"GyMwWDdFEaHkKhmsSzZ",444).equals(""));
assertNotNull("-->Create Date should not be null", TLptsTimeUtil.createDate(166,7772,388,1111230,2134120,01221312));
} catch (Throwable e)
{
System.out.println(e.getMessage());
}
System.out.println("Testing straight forward time conversions from all over the world");
Date date = TLptsTimeUtil.createDate(2010, 12, 13, 10, 20, 30, 2); // local timezone
String strDate = TLptsTimeUtil.getTimeStringForZone(date, TLptsTimeUtil.DATE_DD_MM_YY_HH_MM,2);
if (!strDate.equals("13-12-10 10:20"))
fail("Incorrect format : " + strDate + " should be 13-12-10 10:20");
date = TLptsTimeUtil.createDate(2010, 12, 13, 10, 20, 30, -12); // Date / Time line - west coast
strDate = TLptsTimeUtil.getTimeStringForZone(date, TLptsTimeUtil.DATE_DD_MM_YY_HH_MM,-12);
if (!strDate.equals("13-12-10 10:20"))
fail("Incorrect format : " + strDate + " should be 13-12-10 10:20");
date = TLptsTimeUtil.createDate(2010, 12, 13, 10, 20, 30, 12); // New Zealand Time
strDate = TLptsTimeUtil.getTimeStringForZone(date, TLptsTimeUtil.DATE_DD_MM_YY_HH_MM,12);
if (!strDate.equals("13-12-10 10:20"))
fail("Incorrect format : " + strDate + " should be 13-12-10 10:20");
System.out.println("Testing converted time values - setting various timezones and then converting to another as a string");
date = TLptsTimeUtil.createDate(2010, 12, 13, 23, 20, 30, 12); // New Zealand Time
strDate = TLptsTimeUtil.getTimeStringForZone(date, TLptsTimeUtil.DATE_DD_MM_YY_HH_MM,10);
if (!strDate.equals("13-12-10 21:20"))
fail("Incorrect format : " + strDate + " should be 13-12-10 21:20");
// change day
date = TLptsTimeUtil.createDate(2010, 12, 13, 23, 20, 30, -12); // Date line
strDate = TLptsTimeUtil.getTimeStringForZone(date, TLptsTimeUtil.DATE_DD_MM_YY_HH_MM,0);
if (!strDate.equals("14-12-10 11:20"))
fail("Incorrect format : " + strDate + " should be 14-12-10 11:20");
date = TLptsTimeUtil.createDate(2010, 10, 13, 13, 59, 59, 10); // +10 timezone
strDate = TLptsTimeUtil.getTimeString(date, TLptsTimeUtil.DATE_DDMMYY_HH_MM_SS_1); // local time +3
if (!strDate.equals("131010 06:59:59"))
fail("Incorrect format : " + strDate + " should be 131210 06:59:59");
date = TLptsTimeUtil.createDate(2010, 10, 13, 13, 59, 59); // Local time
strDate = TLptsTimeUtil.getTimeString(date, TLptsTimeUtil.DATE_DDMMYY_HH_MM_SS_1);
if (!strDate.equals("131010 13:59:59"))
fail("Incorrect format : " + strDate + " should be 131210 13:59:59");
date = TLptsTimeUtil.createDate(2010, 10, 13, 13, 59, 59); // Local time
strDate = TLptsTimeUtil.getTimeString(date);
if (!strDate.startsWith("13:59:59"))
fail("Incorrect format : " + strDate + " should start with 13:59:59");
// a bit of trickery; 31 Feb = 3 March
date = TLptsTimeUtil.createDate(2010, 2, 31, 13, 59, 59); // Local time
strDate = TLptsTimeUtil.getDateString(date,true,true);
if (!strDate.equals("03-03-2010 (mm-dd-yyyy)"))
fail("Incorrect format : " + strDate + " should be 12-13-2010 (mm-dd-yyyy)");
// a bit of trickery; 13 = 1
date = TLptsTimeUtil.createDate(2010, 13, 31, 13, 59, 59); // Local time
strDate = TLptsTimeUtil.getDateString(date,false,false);
if (!strDate.equals("31-01-2011"))
fail("Incorrect format : " + strDate + " should be 31-01-2011");
System.out.println("Time tests passed successfully!");
}
}