View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.util;
17  
18  import java.math.BigDecimal;
19  import java.sql.Date;
20  import java.sql.Timestamp;
21  import java.text.SimpleDateFormat;
22  import java.util.List;
23  
24  import org.joda.time.DateTime;
25  import org.joda.time.DateTimeZone;
26  import org.joda.time.Interval;
27  import org.junit.Assert;
28  import org.junit.Test;
29  import org.kuali.hr.time.calendar.CalendarEntries;
30  
31  public class TKUtilsTest extends Assert {
32  
33  	@Test
34  	public void testGetWeekIntervals() throws Exception {
35  		Date beginDate = new Date((new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
36  		Date endDate   = new Date((new DateTime(2010, 3, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
37  		List<Interval> intervals = TKUtils.getWeekIntervals(beginDate, endDate);
38  		assertEquals("Missing partial week", 9, intervals.size());
39  		for (Interval interval : intervals) {
40  			System.out.println(interval);
41  		}
42  
43  		beginDate = new Date((new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
44  		endDate   = new Date((new DateTime(2010, 1, 3, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
45  		intervals = TKUtils.getWeekIntervals(beginDate, endDate);
46  		assertEquals("Missing partial week", 1, intervals.size());
47  		for (Interval interval : intervals) {
48  			System.out.println(interval);
49  		}
50  
51  	}
52  
53  	@Test
54  	public void testgetHoursBetween() throws Exception {
55  		Timestamp beginTime = new Timestamp((new DateTime(2010, 10, 16, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
56  		Timestamp endTime = new Timestamp((new DateTime(2010, 10, 17, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
57  		BigDecimal hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
58  		assertEquals("Wrong hours", 24, hours.intValue());
59  
60  		endTime = new Timestamp((new DateTime(2010, 10, 16, 18, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
61  		hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
62  		assertEquals("Wrong hours", 6, hours.intValue());
63  
64  		endTime = new Timestamp((new DateTime(2010, 10, 16, 18, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
65  		hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
66  		assertEquals("Wrong hours", 5, hours.intValue());
67  
68  	}
69  
70  	@Test
71  	public void testGetFullWeekDaySpanForPayCalendarEntry() {
72  		CalendarEntries payCalendarEntry = new CalendarEntries();
73  		// begin date is a Monday
74  		payCalendarEntry.setBeginPeriodDateTime(new Date((new DateTime(2011, 8, 8, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
75  		// end date is a Thursday
76  		payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
77  		List<Interval> intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
78  		SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
79  		assertEquals("First Interval should be 08/07/2011", "08/07/2011", format.format(intervals.get(0).getStart().toDate()));
80  		assertEquals("Last Interval should be 08/26/2011", "08/26/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
81  
82  		// begin date is a Sunday
83  		payCalendarEntry.setBeginPeriodDateTime(new Date((new DateTime(2011, 8, 14, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
84  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
85  		assertEquals("First Interval should be 08/14/2011", "08/14/2011", format.format(intervals.get(0).getStart().toDate()));
86  		assertEquals("Last Interval should be 08/26/2011", "08/26/2011",  format.format(intervals.get(intervals.size()-1).getStart().toDate()));
87  
88  		// end date is a Sunday
89  		payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 28, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
90  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
91  		assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
92  		assertEquals("Last Interval should be 09/02/2011", "09/02/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
93  
94  		// end date is a Sunday and end time is 0:00
95  		payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 28, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
96  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
97  		assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
98  		assertEquals("Last Interval should be 08/27/2011", "08/27/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
99  
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 }