001    package org.hackystat.telemetry.analyzer.reducer.util;
002    
003    import static org.junit.Assert.assertEquals;
004    
005    import java.util.List;
006    
007    import org.hackystat.utilities.time.interval.DayInterval;
008    import org.hackystat.utilities.time.interval.MonthInterval;
009    import org.hackystat.utilities.time.interval.WeekInterval;
010    import org.hackystat.utilities.time.period.Day;
011    import org.hackystat.utilities.time.period.Month;
012    import org.hackystat.utilities.time.period.Week;
013    import org.junit.Test;
014    
015    /**
016     * Test suite for <code>IntervalUtility</code>.
017     * 
018     * @author (Cedric) Qin Zhang, Philip Johnson
019     */
020    public class TestIntervalUtility {
021      
022      private String year2002 = "2002";
023      
024    
025      /**
026       * Tests with day interval.
027       * 
028       * @throws Exception If test fails.
029       */
030      @Test
031      public void testWithDayInterval() throws Exception {
032        DayInterval interval = new DayInterval(year2002, "0", "1", year2002, "0", "3");
033        List<IntervalUtility.Period> periods = IntervalUtility.getPeriods(interval);
034        assertEquals(3, periods.size());
035        
036        Day firstDay = Day.getInstance(2002, 0, 1);
037        for (int i = 0; i < periods.size(); i++) {
038          Day expected = firstDay.inc(i);
039          IntervalUtility.Period period = periods.get(i);
040          assertEquals(expected, period.getTimePeriod());
041          assertEquals(expected, period.getStartDay());
042          assertEquals(expected, period.getEndDay());
043          assertEquals(1, period.getNumOfDays());
044        }
045      }
046      
047      /**
048       * Tests with week interval.
049       * 
050       * @throws Exception If test fails.
051       */
052      public void testWithWeekInterval() throws Exception {
053        WeekInterval interval = new WeekInterval("06-Jan-2002 to 12-Jan-2002",
054                                                 "20-Jan-2002 to 26-Jan-2002");
055        List<IntervalUtility.Period> periods = IntervalUtility.getPeriods(interval);
056        assertEquals(3, periods.size());
057        
058        Week currentWeek = new Week(Day.getInstance(2002, 0, 6));
059        for (int i = 0; i < periods.size(); i++) {
060          IntervalUtility.Period period = periods.get(i);
061          assertEquals(currentWeek, period.getTimePeriod());
062          assertEquals(currentWeek.getFirstDay(), period.getStartDay());
063          assertEquals(currentWeek.getLastDay(), period.getEndDay());
064          assertEquals(7, period.getNumOfDays());
065          currentWeek = currentWeek.inc();
066        }       
067      }
068      
069      /**
070       * Tests with month interval.
071       * 
072       * @throws Exception If test fails.
073       */
074      public void testWithMonthInterval() throws Exception {
075        MonthInterval interval = new MonthInterval(year2002, "0", year2002, "3");//jan to april
076        List<IntervalUtility.Period> periods = IntervalUtility.getPeriods(interval);
077        assertEquals(4, periods.size());
078        
079        Month currentMonth = new Month(2002, 0);
080        for (int i = 0; i < periods.size(); i++) {
081          IntervalUtility.Period period = periods.get(i);
082          assertEquals(currentMonth, period.getTimePeriod());
083          assertEquals(currentMonth.getFirstDay(), period.getStartDay());
084          assertEquals(currentMonth.getLastDay(), period.getEndDay());
085          
086          //TODO: fail in PST when day light saving is on!!!  //APR error
087          //               return 30                   return 29
088          assertEquals(currentMonth.getNumOfDays(), period.getNumOfDays()); 
089          currentMonth = currentMonth.inc();
090        }    
091      }
092    }