001 package org.hackystat.utilities.time.period; 002 003 import java.util.Calendar; 004 import java.util.Date; 005 import java.util.Locale; 006 import java.util.TimeZone; 007 008 import junit.framework.TestCase; 009 010 /** 011 * Tests the Day implementation. 012 * 013 * @author Hongbing Kou 014 */ 015 public class TestDay extends TestCase { 016 017 018 /** 019 * Test the APIs of day object. 020 * @throws Exception If problems occur. 021 */ 022 public void testDay() throws Exception { 023 Day day = Day.getInstance("01-Jan-2001"); 024 Day sameDay = Day.getInstance(2001, 0, 1); 025 assertEquals("Check the year string", "2001", day.getYearString()); 026 assertEquals("Check the month string", "01", day.getMonthString()); 027 assertEquals("Check the day string", "01", day.getDayString()); 028 assertTrue("Checking the hashcode of the day object", day.hashCode() > 0); 029 assertEquals("Checking day and sameday.", day, sameDay); 030 031 Day day2 = Day.getInstance(); 032 Day day3 = Day.getInstance(); 033 assertEquals("Checking that two days in same day are equal", day2, day3); 034 035 Day day4 = day3.inc(1); 036 assertTrue("Checking the comparison less", day3.compareTo(day4) < 0); 037 assertEquals("Checking the comparison equal", day3.compareTo(day3), 0); 038 assertTrue("Checking the comparison more", day4.compareTo(day3) > 0); 039 040 assertTrue("Checking isBefore", day3.isBefore(day4)); 041 assertFalse("Checking isBefore with the same day", day3.isBefore(day3)); 042 043 assertEquals("Checking daysBetween on same day", 0, Day.daysBetween(day, day)); 044 assertEquals("Checking daysBetween on adjacent days.", 1, Day.daysBetween(day3, day4)); 045 assertEquals("Checking daysBetween on adjacent days.", -1, Day.daysBetween(day4, day3)); 046 assertEquals("Checking daysBetween on dist days.", 100, Day.daysBetween(day3, day3.inc(100))); 047 048 TimeZone defaultTimeZone = TimeZone.getDefault(); 049 Calendar cal = Calendar.getInstance(Locale.US); 050 // Test the DayCache because of daylight saving 051 if (defaultTimeZone.inDaylightTime(cal.getTime())) { 052 // January first is not in daylight saving 053 cal.set(Calendar.MONTH, 0); 054 cal.set(Calendar.DAY_OF_MONTH, 1); 055 056 // 11:15pm 057 cal.set(Calendar.HOUR_OF_DAY, 23); 058 cal.set(Calendar.MINUTE, 15); 059 Date lateNight = cal.getTime(); 060 Day.getInstance(lateNight); // late night day. 061 062 // 9:30am 063 cal.set(Calendar.HOUR_OF_DAY, 9); 064 cal.set(Calendar.MINUTE, 30); 065 Date officeHour = cal.getTime(); 066 067 Day officeHourDay = Day.getInstance(officeHour); 068 069 // Gets next day 070 Day nextDay = officeHourDay.inc(1); 071 072 assertNotSame("Next day should be 1/2 but 1/1 ", officeHourDay.compareTo(nextDay), 0); 073 } 074 else { 075 // July 4th is in daylight saving 076 cal.add(Calendar.YEAR, 1); 077 cal.set(Calendar.MONTH, 6); 078 cal.set(Calendar.DAY_OF_MONTH, 4); 079 080 // 12:10am 081 cal.set(Calendar.HOUR_OF_DAY, 0); 082 cal.set(Calendar.MINUTE, 10); 083 Date earlyMorning = cal.getTime(); 084 Day.getInstance(earlyMorning); // early morning day 085 086 // 9:30am 087 cal.set(Calendar.HOUR_OF_DAY, 9); 088 cal.set(Calendar.MINUTE, 30); 089 Date officeHour = cal.getTime(); 090 Day officeHourDay = Day.getInstance(officeHour); 091 092 Day previousDay = officeHourDay.inc(-1); 093 assertNotSame("Previous day should be 7/3 not 7/4", officeHourDay.compareTo(previousDay), 0); 094 } 095 } 096 097 /** 098 * Tests the day light savings issue associated with getting a day. When a Day instance is 099 * created it is retrieved from DayCache. The DayCache must account for differences 100 * in timestamps due to TimeZone and Daylight Saving Issues. This unit tests ensures 101 * that we are able to correctly create Day objects for a 3 year period at 30 minute intervals. 102 * <p> 103 * A failure in this method is caused by a date (ie. 0-31) of the Day object does not 104 * equal the date of the Date object. This indicates that the DayCache is not interpreting 105 * the TimeZone and DST correctly. 106 */ 107 public void testDaylightSavingsIssues() { 108 Calendar calendar = Calendar.getInstance(Locale.US); 109 Calendar tempCal = Calendar.getInstance(Locale.US); 110 Calendar calendarNextYear = Calendar.getInstance(Locale.US); 111 tempCal.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1); 112 calendarNextYear.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 2); 113 while (!tempCal.equals(calendarNextYear)) { 114 Day day = Day.getInstance(tempCal.getTime()); 115 assertEquals("checking that the day values are the same " + 116 "[date=" + tempCal.getTime() + ", day=" + day + "]", 117 tempCal.get(Calendar.DATE), Integer.parseInt(day.getDayString())); 118 tempCal.add(Calendar.MINUTE, 30); 119 } 120 } 121 122 /** 123 * Tests <code>getFirstTickOfTheDay</code> and <code>getLastTickOfTheDay</code> method. 124 */ 125 public void testGetFirstTickOfTheDay() { 126 long millisInADay = 24 * 60 * 60 * 1000; 127 Calendar cal = Calendar.getInstance(Locale.US); 128 cal.set(2005, 0, 1, 10, 10, 10); 129 Day day = Day.getInstance(cal.getTime()); 130 131 cal.set(2005, 0, 1, 0, 0, 0); 132 cal.set(Calendar.MILLISECOND, 0); 133 long firstTickOfDay = cal.getTimeInMillis(); 134 long lastTickOfDay = firstTickOfDay + millisInADay - 1; 135 assertEquals("Checking first tick", firstTickOfDay, day.getFirstTickOfTheDay()); 136 assertEquals("Checking last tick", lastTickOfDay, day.getLastTickOfTheDay()); 137 } 138 }