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  		for (Interval interval : intervals) {
39  			System.out.println(interval);
40  		}
41  
42  		beginDate = new Date((new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
43  		endDate   = new Date((new DateTime(2010, 1, 3, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
44  		intervals = TKUtils.getWeekIntervals(beginDate, endDate);
45  		assertEquals("Missing partial week", 1, intervals.size());
46  		for (Interval interval : intervals) {
47  			System.out.println(interval);
48  		}
49  
50  	}
51  
52  	@Test
53  	public void testgetHoursBetween() throws Exception {
54  		Timestamp beginTime = new Timestamp((new DateTime(2010, 10, 16, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
55  		Timestamp endTime = new Timestamp((new DateTime(2010, 10, 17, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
56  		BigDecimal hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
57  		assertEquals("Wrong hours", 24, hours.intValue());
58  
59  		endTime = new Timestamp((new DateTime(2010, 10, 16, 18, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
60  		hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
61  		assertEquals("Wrong hours", 6, hours.intValue());
62  
63  		endTime = new Timestamp((new DateTime(2010, 10, 16, 18, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
64  		hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime());
65  		assertEquals("Wrong hours", 5, hours.intValue());
66  
67  	}
68  
69  	@Test
70  	public void testGetFullWeekDaySpanForPayCalendarEntry() {
71  		CalendarEntries payCalendarEntry = new CalendarEntries();
72  		// begin date is a Monday
73  		payCalendarEntry.setBeginPeriodDateTime(new Date((new DateTime(2011, 8, 8, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
74  		// end date is a Thursday
75  		payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
76  		List<Interval> intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
77  		SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
78  		assertEquals("First Interval should be 08/07/2011", "08/07/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  		// begin date is a Sunday
82  		payCalendarEntry.setBeginPeriodDateTime(new Date((new DateTime(2011, 8, 14, 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 08/26/2011", "08/26/2011",  format.format(intervals.get(intervals.size()-1).getStart().toDate()));
86  
87  		// end date is a Sunday
88  		payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 28, 12, 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 09/02/2011", "09/02/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
92  
93  		// end date is a Sunday and end time is 0:00
94  		payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 28, 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 08/27/2011", "08/27/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
98  
99  		// end date is a Monday and end time is 0:00
100 		payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 29, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
101 		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
102 		assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
103 		assertEquals("Last Interval should be 09/03/2011", "09/03/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
104 
105 	}
106 	
107 	@Test
108 	public void testConvertMillisToMinutes() {
109 		BigDecimal mins = TKUtils.convertMillisToMinutes(new Long(380000));
110 		assertTrue("Minutes should be between 6 and 7",  mins.compareTo(new BigDecimal(6)) > 0  && mins.compareTo(new BigDecimal(7)) < 0);
111 		mins = TKUtils.convertMillisToMinutes(new Long(240000));
112 		assertTrue("Minutes should be 4",  mins.compareTo(new BigDecimal(4)) == 0);
113 	}
114 	
115 	@Test
116 	public void testFromDateString() {
117 		String dateString = "01/01/2012..12/31/2012";
118 		String fromDateString = TKUtils.getFromDateString(dateString);
119 		assertTrue("fromDateString should be 01/01/2012, not " + fromDateString, fromDateString.equals("01/01/2012"));
120 		assertNotNull(TKUtils.formatDateString(fromDateString));
121 		
122 		dateString = ">=2/01/2012";
123 		fromDateString = TKUtils.getFromDateString(dateString);
124 		assertTrue("fromDateString should be 2/01/2012, not " + fromDateString, fromDateString.equals("2/01/2012"));
125 		assertNotNull(TKUtils.formatDateString(fromDateString));
126 	}
127 	
128 	@Test
129 	public void testToDateString() {
130 		String dateString = "01/01/2012..12/31/2012";
131 		String toDateString = TKUtils.getToDateString(dateString);
132 		assertTrue("toDateString should be 12/31/2012, not " + toDateString, toDateString.equals("12/31/2012"));
133 		assertNotNull(TKUtils.formatDateString(toDateString));
134 		
135 		dateString = "<=2/01/2012";
136 		toDateString = TKUtils.getToDateString(dateString);
137 		assertTrue("toDateString should be 2/01/2012, not " + toDateString, toDateString.equals("2/01/2012"));
138 		assertNotNull(TKUtils.formatDateString(toDateString));
139 	}
140 
141 }