001    package org.hackystat.utilities.time.interval;
002    
003    import java.util.TreeMap;
004    
005    import junit.framework.TestCase;
006    
007    import org.hackystat.utilities.time.period.Day;
008    import org.hackystat.utilities.time.period.Week;
009    
010    /**
011     * Tests the IntervalUtility class. Note that certain tests are commented out.  
012     * It's not clear whether this class is used in Hackystat 8.
013     * 
014     * @author Hongbing Kou
015     */
016    public class TestIntervalUtility extends TestCase {
017      /**
018       * Test the singleton instance implementation.
019       */
020      public void testSingleton() {
021        IntervalUtility utility1 = IntervalUtility.getInstance();
022        IntervalUtility utility2 = IntervalUtility.getInstance();
023    
024        assertSame(
025            "Checking whether IntervalUtility is a singleton implementation",
026            utility1, utility2);
027      }
028    
029      /**
030       * Tests day options.
031       */
032      public void testDayOptions() {
033        IntervalUtility instance = IntervalUtility.getInstance();
034        TreeMap<String, String> dayOptions = instance.getDayOptions();
035    
036        assertEquals("Checking the size of day options", 31, dayOptions.size());
037        assertEquals("Checking the first day key", "01", dayOptions.firstKey());
038        assertEquals("Checking the last day key", "31", dayOptions.lastKey());
039      }
040    
041      /**
042       * Tests year options.
043       */
044      public void testYearOptions() {
045        IntervalUtility instance = IntervalUtility.getInstance();
046        TreeMap<String, String> yearOptions = instance.getYearOptions();
047    
048        assertEquals("Checking the number of years", 20, yearOptions.size());
049        assertEquals("Checking the first year", "2000", yearOptions.firstKey());
050        assertEquals("Checking the last year", "2019", yearOptions.lastKey());
051      }
052    
053      /**
054       * Tests month options.
055       */
056      public void testMonthOptions() {
057        IntervalUtility instance = IntervalUtility.getInstance();
058        TreeMap<String, String> monthOptions = instance.getMonthOptions();
059    
060        assertEquals("Checking the number of month in a year", 12, monthOptions.size());
061        //String firstMonthKey = (String) monthOptions.firstKey();
062        //String firstMonthValue = (String) monthOptions.get(firstMonthKey);
063        // if (Locale.getDefault().getLanguage().equals("en")) {
064          //assertEquals("Checking the first month key in a year", "January", firstMonthKey);
065        //}
066        //assertEquals("Checking the first month value in a year", "00", firstMonthValue);
067    
068        //String lastMonthKey = (String) monthOptions.lastKey();
069        //String lastMonthValue = (String) monthOptions.get(lastMonthKey);
070        // if (Locale.getDefault().getLanguage().equals("en")) {
071          //assertEquals("Checking the last month key in a year", "December", lastMonthKey);
072        // }
073        //assertEquals("Checking the last month value in a year", "11",  lastMonthValue);
074      }
075    
076      /**
077       * Tests week options.
078       */
079      public void testWeekOptions() {
080        IntervalUtility instance = IntervalUtility.getInstance();
081        TreeMap<String, String> weekOptions = instance.getWeekOptions();
082    
083        // assertEquals("Checking number of weeks look behind", 52,
084        // weekOptions.size());
085        assertFalse("Checking number of weeks look behind", weekOptions.isEmpty());
086      }
087    
088      /**
089       * Tests the week parser.
090       */
091      public void testWeekParser() {
092        IntervalUtility instance = IntervalUtility.getInstance();
093    
094        // Test week string
095        String weekString = "28-Dec-2003 to 03-Jan-2004";
096    
097        Week week = instance.getWeek(weekString);
098        Day firstDay = Day.getInstance(2003, 11, 28);
099        assertEquals("Testing the method to get first day of the week", firstDay, week.getFirstDay());
100    
101        Day lastDay = Day.getInstance(2004, 0, 3);
102        assertEquals("Testing the method to get last day of the week", lastDay,  week.getLastDay());
103    
104        // Test another week string
105        weekString = "11-Jan-2004 to 17-Jan-2004";
106    
107        week = instance.getWeek(weekString);
108        firstDay = Day.getInstance(2004, 0, 11);
109        assertEquals("Testing the method to get first day of the week", firstDay, week.getFirstDay());
110    
111        lastDay = Day.getInstance(2004, 0, 17);
112        assertEquals("Testing the method to get last day of the week", lastDay, week.getLastDay());
113      }
114    
115      /**
116       * Tests the day parser.
117       */
118      public void testDayParser() {
119        IntervalUtility instance = IntervalUtility.getInstance();
120    
121        String dayString = "01";
122        String monthString = "00";
123        String yearString = "2004";
124        Day day = Day.getInstance(2004, 0, 1);
125        assertEquals("Checking day parser", day, instance.getDay(yearString, monthString, dayString));
126    
127        dayString = "5";
128        monthString = "11";
129        yearString = "2004";
130        day = Day.getInstance(2004, 11, 5);
131        assertEquals("Checking day parser", day, instance.getDay(yearString, monthString, dayString));
132    
133        dayString = "14";
134        monthString = "06";
135        yearString = "2004";
136        day = Day.getInstance(2004, 6, 14);
137        assertEquals("Checking day parser", day, instance.getDay(yearString, monthString, dayString));
138      }
139    
140      /**
141       * Tests current year value.
142       */
143      public void testCurrentYearValue() {
144        IntervalUtility instance = IntervalUtility.getInstance();
145        assertTrue("Tests current year", Integer.parseInt(instance.getCurrentYear()) > 2000);
146      }
147    
148      /**
149       * Tests current month.
150       */
151      public void testCurrentMonthValue() {
152        IntervalUtility instance = IntervalUtility.getInstance();
153        assertTrue("Tests current month", Integer.parseInt(instance.getCurrentMonth()) >= 0);
154    
155      }
156    
157      /**
158       * Tests current day.
159       */
160      public void testCurrentDayValue() {
161        IntervalUtility instance = IntervalUtility.getInstance();
162        assertTrue("Tests current day", instance.getCurrentDay().length() > 0);
163      }
164    
165      /**
166       * Tests current week.
167       */
168      public void testCurrentWeekValue() {
169        IntervalUtility instance = IntervalUtility.getInstance();
170        assertTrue("Tests current week", instance.getCurrentWeek().length() > 0);
171      }
172    }