//------------------------------------------------------------------------------ // Compilation Unit Header //------------------------------------------------------------------------------ // // Copyright (c) 2006 Waysys, Inc. All Rights Reserved. // // Permission to use, copy, modify, and distribute this software // and its documentation for NON-COMMERCIAL purposes and without // fee is hereby granted provided that this copyright notice // appears in all copies. // // Waysys MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. Waysys SHALL NOT BE LIABLE FOR // ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR // DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. // // For further information, contact Waysys Inc. at waysys@acm.org // or 800-622-5315 (USA). // //------------------------------------------------------------------------------ // Maintenance History //------------------------------------------------------------------------------ // // Person Date Change // ------ ----------- ---------------------------------------------------- // // Shaffer 09-Jul-2000 File create // Shaffer 07-Nov-2006 Converted to JUnit 4 // //------------------------------------------------------------------------------ // Package Declaration //------------------------------------------------------------------------------ import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import junit.framework.JUnit4TestAdapter; import java.util.Date; import com.waysysweb.util.WayDate; import com.waysysweb.util.WayDateException; import com.waysysweb.util.Holiday; //------------------------------------------------------------------------------ // Public Class Declaration //------------------------------------------------------------------------------ /** * This is the test harness for testing WayDate. It uses the JUnit * test framework developed by Erich Gamma and Kent Beck. * * @author William A. Shaffer * @version 2.00 */ public class DateTest { /** * Create an instance of this class. * */ public DateTest() { super(); return; } /** * Return an instance of junit.framework.Test * * @return a junit test adapter */ public static junit.framework.Test suite() { junit.framework.Test suite = new JUnit4TestAdapter(DateTest.class); return suite; } /** * Perform initial setup * */ @org.junit.Before public void setUp() { return; } /** * Perform final teardown * */ @org.junit.After public void tearDown() { return; } /** * Utility routine to calculate absolute date from Gregorian date * * @param date the date to convert to absolute * @return the abosulte date */ public static int absoluteFromGregorian(WayDate date) { WayDate epoc = new WayDate(1, 1, 1601); int count = date.difference(epoc) + 1; return count; } /** * Test creation of dates at beginning and end of month. * */ @Test public void testCreationMonth() { int month; int days[] = {31,28,31,30,31,30,31,31,30,31,30,31}; for(month = 1; month <= 12; month++) { WayDate aDate1 = new WayDate(month, 1, 1997); WayDate aDate2 = new WayDate(month, days[month-1], 1997); } return; } /** * Test creation of dates a MINYEAR and MAXYEAR * */ @Test public void testCreationYears() { // test good years at end of range WayDate aDate1 = new WayDate(1, 1, 1601); WayDate aDate2 = new WayDate(12, 31, 3999); return; } /** * Test leap years */ @Test public void testLeapYear() { // test of 1900 as a non-leap year assertTrue("1900 is not a leap year", !WayDate.isLeapYear(1900)); // test of days in year assertTrue("Error in days of year 1900",WayDate.daysInYear(1900) == 365); // test of leap year 2000 assertTrue("2000 is a leap year", WayDate.isLeapYear(2000)); // test of number of days in leap year assertTrue("Error in days of year 2000", WayDate.daysInYear(2000) == 366); // test of leap year 1996 assertTrue("1996 is a leap year", WayDate.isLeapYear(1996)); // test of non-leap year assertTrue("1997 is not a leap year", !WayDate.isLeapYear(1997)); return; } /** * Test days of year calculation */ @Test public void testDayOfYear() { WayDate date1 = new WayDate( 1, 1, 1601); WayDate dateMax = new WayDate( 12, 31, 2001); int count = 1; while (date1.compareTo(dateMax) != 0) { // Test conversion to day of year assertTrue("Error in day of year for ", count == date1.getDayOfYear()); // Test conversion from day of year to gregorian date WayDate date2 = new WayDate(count, date1.getYear()); assertEquals(date1,date2); if (count == WayDate.daysInYear(date1.getYear())) count = 0; count++; date1.increment(); } return; } /** * Test creation of day in year * */ @Test public void testCreateDayOfYear() { WayDate date1 = new WayDate(1, 1601); WayDate date2 = new WayDate(365, 1900); WayDate date3 = new WayDate(366, 2000); return; } /** * Test absolute calculation */ @Test public void testAbsolute() { WayDate date1 = new WayDate( 1, 1, 1601); // must be this for count = 1 WayDate maxDate = new WayDate(12, 31, 2001); int year; int count = 1; while (!date1.equals(maxDate)) { // Case 14 -- test absolute from gregorian assertTrue("Error in comparison of absolute date " + date1, absoluteFromGregorian(date1) == count); date1.increment(); year = date1.getYear(); //force conversion to display form count++; } // test of first absolute date WayDate date3 = new WayDate(1, 1, WayDate.MINYEAR); date3.increment(); date3.decrement(); // forces conversion to absolute assertTrue("Day error converting from absolute" + date3, date3.getDay() == 1); assertTrue("Month error converting from absolute" + date3, date3.getMonth() == 1); assertTrue("Year error converting from absolute" + date3, date3.getYear() == WayDate.MINYEAR); // test of last absolute date date1= new WayDate(12, 31, WayDate.MAXYEAR); date1.decrement(); // forces conversion to absolute date1.increment(); WayDate date2 = new WayDate(12, 31, WayDate.MAXYEAR); assertTrue("Error converting from absolute" + date1, date1.equals(date2)); return; } /** * Test calculation interface */ @Test public void testMath() { WayDate maxDate = null; WayDate minDate = null; minDate = new WayDate( 1, 1, 1800); maxDate = new WayDate(12, 31, 2100); int count; WayDate date1; try { while (minDate.compareTo(maxDate) != 0) { count = maxDate.difference(minDate); // test of negative add date1 = new WayDate(maxDate.getMonth(), maxDate.getDay(), maxDate.getYear()); date1.add(-count); assertTrue("Error adding negative count", minDate.equals(date1)); // test of positive add date1 = new WayDate(minDate.getMonth(), minDate.getDay(), minDate.getYear()); date1.add(count); assertTrue("Error adding positive count", maxDate.equals(date1)); maxDate.decrement(); } } catch (WayDateException e) { assertTrue("Unexpected WayDateException: " + e.getMessage(), false); } return; } /** * Test day of week */ @Test public void testDayOfWeek() { WayDate date1 = new WayDate(1, 1, 1601); int day = 1; // 1-Jan-1601 is a monday WayDate maxDate = new WayDate(12, 31, 3999); int dow; while (!date1.equals(maxDate)) { dow = date1.getDayOfWeek(); assertTrue( dow == (day % 7)); day++; date1.increment(); } } /** * Test the generation of a date today. * * Note: update the date each day of testing. * */ @Test public void testToday() { WayDate date = WayDate.today(); assertEquals(date, new WayDate(1, 8, 2007)); Date javaDate1 = new Date(); WayDate date1 = WayDate.asWayDate(javaDate1); assertEquals(date, date1); Date javaDate2 = date1.asDate(); WayDate date2 = WayDate.asWayDate(javaDate2); assertEquals(date1, date2); return; } /** * Test the equals function. * */ @Test public void testEquals() { WayDate date1 = WayDate.today(); WayDate date2 = (WayDate)date1.clone(); assertEquals(date1, date2); assertEquals(date1, date1); assertTrue("Problem with equivalence of clone", !(date1==date2)); date1.increment(); assertTrue("Problem with unequal", !date1.equals(date2)); assertTrue("Problem with null" , !date1.equals(null )); assertTrue("Problem with object" , !date1.equals(new Object())); } /** * Test of compareTo function * */ @Test public void testCompare() { WayDate date1 = new WayDate(3, 3, 2000); WayDate date2 = new WayDate(2, 2, 2001); WayDate date3 = new WayDate(1, 1, 2003); WayDate date4 = new WayDate(3, 3, 2000); assertTrue("Compare less than", date1.compareTo(date2) < 0); assertTrue("Compare equal" , date1.compareTo(date4) == 0); assertTrue("Compare greater" , date3.compareTo(date2) > 0); } /** * Test hashCode function * */ @Test public void testHash() { WayDate date1 = new WayDate(3, 3, 2000); WayDate date2 = new WayDate(2, 2, 2001); WayDate date3 = new WayDate(1, 1, 2003); WayDate date4 = new WayDate(3, 3, 2000); assertEquals(date1.hashCode(), date4.hashCode()); assertEquals(date1.hashCode(), date1.hashCode()); assertTrue("not equals", date1.hashCode() != date2.hashCode()); assertTrue("second not equals", date4.hashCode() != date3.hashCode()); } /** * Test holidays */ @Test public void testHolidays() { try { // // New Years // WayDate newYears1 = Holiday.getNewYearsDay(2006); WayDate newYears2 = new WayDate(1, 1, 2006); assertTrue("new years", newYears1.equals(newYears2)); // // Martin Luther King Day // WayDate kingDay1 = Holiday.getMartinLutherKingsBirthday(2006); String kingString = kingDay1.toString(); WayDate kingDay2 = new WayDate(1, 16, 2006); assertTrue("Martin Luther King", kingDay1.equals(kingDay2)); // // Washington's Birthday // WayDate washington1 = Holiday.getWashingtonsBirthday(2006); WayDate washington2 = new WayDate(2, 20, 2006); assertTrue("Washington's Birthday", washington1.equals(washington2)); // // Memorial Day // WayDate memorial1 = Holiday.getMemorialDay(2006); WayDate memorial2 = new WayDate(5, 29, 2006); assertTrue("Memorial Day", memorial1.equals(memorial2)); // // Independence Day // WayDate independence1 = Holiday.getIndependenceDay(2006); WayDate independence2 = new WayDate(7, 4, 2006); assertTrue("Independence Day", independence1.equals(independence2)); // // Labor Day // WayDate labor1 = Holiday.getLaborDay(2006); WayDate labor2 = new WayDate(9, 4, 2006); assertTrue("Labor Day", labor1.equals(labor2)); // // Columbus Day // WayDate columbus1 = Holiday.getColumbusDay(2006); String colString = columbus1.toString(); WayDate columbus2 = new WayDate(10, 9, 2006); assertTrue("Columbus Day", columbus1.equals(columbus2)); // // Veterans' Day // WayDate veteran1 = Holiday.getVeteransDay(2006); String vetString = veteran1.toString(); WayDate veteran2 = new WayDate(11, 11, 2006); assertTrue("Veteran's Day", veteran1.equals(veteran2)); // // Thanksgiving // WayDate thanks1 = Holiday.getThanksgiving(2006); String thankString = thanks1.toString(); WayDate thanks2 = new WayDate(11, 23, 2006); assertTrue("Thanksgiving", thanks1.equals(thanks2)); // // Christmas // WayDate chris1 = Holiday.getChristmas(2006); WayDate chris2 = new WayDate(12, 25, 2006); assertTrue("Christmas", chris1.equals(chris2)); } catch (WayDateException e) { assertTrue(e.getMessage(), false); } return; } }