001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.util;
017    
018    import java.math.BigDecimal;
019    import java.sql.Date;
020    import java.sql.Timestamp;
021    import java.text.SimpleDateFormat;
022    import java.util.List;
023    
024    import org.joda.time.DateTime;
025    import org.joda.time.Interval;
026    import org.junit.Assert;
027    import org.junit.Test;
028    import org.kuali.hr.time.calendar.CalendarEntries;
029    
030    public class TKUtilsTest extends Assert {
031    
032            @Test
033            public void testGetWeekIntervals() throws Exception {
034                    Date beginDate = new Date((new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
035                    Date endDate   = new Date((new DateTime(2010, 3, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
036                    List<Interval> intervals = TKUtils.getWeekIntervals(beginDate, endDate);
037                    assertEquals("Missing partial week", 9, intervals.size());
038    
039                    beginDate = new Date((new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
040                    endDate   = new Date((new DateTime(2010, 1, 3, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
041                    intervals = TKUtils.getWeekIntervals(beginDate, endDate);
042                    assertEquals("Missing partial week", 1, intervals.size());
043    
044            }
045    
046            @Test
047            public void testgetHoursBetween() throws Exception {
048                    Timestamp beginTime = new Timestamp((new DateTime(2010, 10, 16, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
049                    Timestamp endTime = new Timestamp((new DateTime(2010, 10, 17, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
050                    BigDecimal hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
051                    assertEquals("Wrong hours", 24, hours.intValue());
052    
053                    endTime = new Timestamp((new DateTime(2010, 10, 16, 18, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
054                    hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
055                    assertEquals("Wrong hours", 6, hours.intValue());
056    
057                    endTime = new Timestamp((new DateTime(2010, 10, 16, 18, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
058                    hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
059                    assertEquals("Wrong hours", 5, hours.intValue());
060    
061            }
062    
063            @Test
064            public void testGetFullWeekDaySpanForPayCalendarEntry() {
065                    CalendarEntries payCalendarEntry = new CalendarEntries();
066                    // begin date is a Monday
067                    payCalendarEntry.setBeginPeriodDateTime(new Date((new DateTime(2011, 8, 8, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
068                    // end date is a Thursday
069                    payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
070                    List<Interval> intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
071                    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
072                    assertEquals("First Interval should be 08/07/2011", "08/07/2011", format.format(intervals.get(0).getStart().toDate()));
073                    assertEquals("Last Interval should be 08/26/2011", "08/26/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
074    
075                    // begin date is a Sunday
076                    payCalendarEntry.setBeginPeriodDateTime(new Date((new DateTime(2011, 8, 14, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
077                    intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
078                    assertEquals("First Interval should be 08/14/2011", "08/14/2011", format.format(intervals.get(0).getStart().toDate()));
079                    assertEquals("Last Interval should be 08/26/2011", "08/26/2011",  format.format(intervals.get(intervals.size()-1).getStart().toDate()));
080    
081                    // end date is a Sunday
082                    payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 28, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
083                    intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
084                    assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
085                    assertEquals("Last Interval should be 09/02/2011", "09/02/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
086    
087                    // end date is a Sunday and end time is 0:00
088                    payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 28, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
089                    intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
090                    assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
091                    assertEquals("Last Interval should be 08/27/2011", "08/27/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
092    
093                    // end date is a Monday and end time is 0:00
094                    payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 29, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
095                    intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
096                    assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
097                    assertEquals("Last Interval should be 09/03/2011", "09/03/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
098    
099            }
100            
101            @Test
102            public void testConvertMillisToMinutes() {
103                    BigDecimal mins = TKUtils.convertMillisToMinutes(new Long(380000));
104                    assertTrue("Minutes should be between 6 and 7",  mins.compareTo(new BigDecimal(6)) > 0  && mins.compareTo(new BigDecimal(7)) < 0);
105                    mins = TKUtils.convertMillisToMinutes(new Long(240000));
106                    assertTrue("Minutes should be 4",  mins.compareTo(new BigDecimal(4)) == 0);
107            }
108            
109            @Test
110            public void testFromDateString() {
111                    String dateString = "01/01/2012..12/31/2012";
112                    String fromDateString = TKUtils.getFromDateString(dateString);
113                    assertTrue("fromDateString should be 01/01/2012, not " + fromDateString, fromDateString.equals("01/01/2012"));
114                    assertNotNull(TKUtils.formatDateString(fromDateString));
115                    
116                    dateString = ">=2/01/2012";
117                    fromDateString = TKUtils.getFromDateString(dateString);
118                    assertTrue("fromDateString should be 2/01/2012, not " + fromDateString, fromDateString.equals("2/01/2012"));
119                    assertNotNull(TKUtils.formatDateString(fromDateString));
120            }
121            
122            @Test
123            public void testToDateString() {
124                    String dateString = "01/01/2012..12/31/2012";
125                    String toDateString = TKUtils.getToDateString(dateString);
126                    assertTrue("toDateString should be 12/31/2012, not " + toDateString, toDateString.equals("12/31/2012"));
127                    assertNotNull(TKUtils.formatDateString(toDateString));
128                    
129                    dateString = "<=2/01/2012";
130                    toDateString = TKUtils.getToDateString(dateString);
131                    assertTrue("toDateString should be 2/01/2012, not " + toDateString, toDateString.equals("2/01/2012"));
132                    assertNotNull(TKUtils.formatDateString(toDateString));
133            }
134    
135    }