View Javadoc

1   /**
2    * Copyright 2004-2013 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.Interval;
26  import org.junit.Assert;
27  import org.junit.Test;
28  import org.kuali.hr.time.calendar.CalendarEntries;
29  
30  public class TKUtilsTest extends Assert {
31  
32  	@Test
33  	public void testGetWeekIntervals() throws Exception {
34  		Date beginDate = new Date((new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
35  		Date endDate   = new Date((new DateTime(2010, 3, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
36  		List<Interval> intervals = TKUtils.getWeekIntervals(beginDate, endDate);
37  		assertEquals("Missing partial week", 9, intervals.size());
38  
39  		beginDate = new Date((new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
40  		endDate   = new Date((new DateTime(2010, 1, 3, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
41  		intervals = TKUtils.getWeekIntervals(beginDate, endDate);
42  		assertEquals("Missing partial week", 1, intervals.size());
43  
44  	}
45  
46  	@Test
47  	public void testgetHoursBetween() throws Exception {
48  		Timestamp beginTime = new Timestamp((new DateTime(2010, 10, 16, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
49  		Timestamp endTime = new Timestamp((new DateTime(2010, 10, 17, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
50  		BigDecimal hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
51  		assertEquals("Wrong hours", 24, hours.intValue());
52  
53  		endTime = new Timestamp((new DateTime(2010, 10, 16, 18, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
54  		hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
55  		assertEquals("Wrong hours", 6, hours.intValue());
56  
57  		endTime = new Timestamp((new DateTime(2010, 10, 16, 18, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
58  		hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
59  		assertEquals("Wrong hours", 5, hours.intValue());
60  
61  	}
62  
63  	@Test
64  	public void testGetFullWeekDaySpanForPayCalendarEntry() {
65  		CalendarEntries payCalendarEntry = new CalendarEntries();
66  		// begin date is a Monday
67  		payCalendarEntry.setBeginPeriodDateTime(new Date((new DateTime(2011, 8, 8, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
68  		// end date is a Thursday
69  		payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
70  		List<Interval> intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
71  		SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
72  		assertEquals("First Interval should be 08/07/2011", "08/07/2011", format.format(intervals.get(0).getStart().toDate()));
73  		assertEquals("Last Interval should be 08/26/2011", "08/26/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
74  
75  		// begin date is a Sunday
76  		payCalendarEntry.setBeginPeriodDateTime(new Date((new DateTime(2011, 8, 14, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
77  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
78  		assertEquals("First Interval should be 08/14/2011", "08/14/2011", format.format(intervals.get(0).getStart().toDate()));
79  		assertEquals("Last Interval should be 08/26/2011", "08/26/2011",  format.format(intervals.get(intervals.size()-1).getStart().toDate()));
80  
81  		// end date is a Sunday
82  		payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 28, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
83  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
84  		assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
85  		assertEquals("Last Interval should be 09/02/2011", "09/02/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
86  
87  		// end date is a Sunday and end time is 0:00
88  		payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 28, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
89  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
90  		assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
91  		assertEquals("Last Interval should be 08/27/2011", "08/27/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
92  
93  		// end date is a Monday and end time is 0:00
94  		payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 29, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
95  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
96  		assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
97  		assertEquals("Last Interval should be 09/03/2011", "09/03/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
98  
99  	}
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 }