001    /**
002     * Copyright 2004-2012 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.DateTimeZone;
026    import org.joda.time.Interval;
027    import org.junit.Assert;
028    import org.junit.Test;
029    import org.kuali.hr.time.calendar.CalendarEntries;
030    
031    public class TKUtilsTest extends Assert {
032    
033            @Test
034            public void testGetWeekIntervals() throws Exception {
035                    Date beginDate = new Date((new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
036                    Date endDate   = new Date((new DateTime(2010, 3, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
037                    List<Interval> intervals = TKUtils.getWeekIntervals(beginDate, endDate);
038                    assertEquals("Missing partial week", 9, intervals.size());
039                    for (Interval interval : intervals) {
040                            System.out.println(interval);
041                    }
042    
043                    beginDate = new Date((new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
044                    endDate   = new Date((new DateTime(2010, 1, 3, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
045                    intervals = TKUtils.getWeekIntervals(beginDate, endDate);
046                    assertEquals("Missing partial week", 1, intervals.size());
047                    for (Interval interval : intervals) {
048                            System.out.println(interval);
049                    }
050    
051            }
052    
053            @Test
054            public void testgetHoursBetween() throws Exception {
055                    Timestamp beginTime = new Timestamp((new DateTime(2010, 10, 16, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
056                    Timestamp endTime = new Timestamp((new DateTime(2010, 10, 17, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
057                    BigDecimal hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
058                    assertEquals("Wrong hours", 24, hours.intValue());
059    
060                    endTime = new Timestamp((new DateTime(2010, 10, 16, 18, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
061                    hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
062                    assertEquals("Wrong hours", 6, hours.intValue());
063    
064                    endTime = new Timestamp((new DateTime(2010, 10, 16, 18, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
065                    hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
066                    assertEquals("Wrong hours", 5, hours.intValue());
067    
068            }
069    
070            @Test
071            public void testGetFullWeekDaySpanForPayCalendarEntry() {
072                    CalendarEntries payCalendarEntry = new CalendarEntries();
073                    // begin date is a Monday
074                    payCalendarEntry.setBeginPeriodDateTime(new Date((new DateTime(2011, 8, 8, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
075                    // end date is a Thursday
076                    payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
077                    List<Interval> intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
078                    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
079                    assertEquals("First Interval should be 08/07/2011", "08/07/2011", format.format(intervals.get(0).getStart().toDate()));
080                    assertEquals("Last Interval should be 08/26/2011", "08/26/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
081    
082                    // begin date is a Sunday
083                    payCalendarEntry.setBeginPeriodDateTime(new Date((new DateTime(2011, 8, 14, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
084                    intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
085                    assertEquals("First Interval should be 08/14/2011", "08/14/2011", format.format(intervals.get(0).getStart().toDate()));
086                    assertEquals("Last Interval should be 08/26/2011", "08/26/2011",  format.format(intervals.get(intervals.size()-1).getStart().toDate()));
087    
088                    // end date is a Sunday
089                    payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 28, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
090                    intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
091                    assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
092                    assertEquals("Last Interval should be 09/02/2011", "09/02/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
093    
094                    // end date is a Sunday and end time is 0:00
095                    payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 28, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
096                    intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
097                    assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
098                    assertEquals("Last Interval should be 08/27/2011", "08/27/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
099    
100                    // end date is a Monday and end time is 0:00
101                    payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 29, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
102                    intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
103                    assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
104                    assertEquals("Last Interval should be 09/03/2011", "09/03/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
105    
106            }
107    
108    
109    }