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.kpme.tklm.time.util;
17  
18  import java.math.BigDecimal;
19  import java.text.SimpleDateFormat;
20  import java.util.List;
21  
22  import org.joda.time.DateTime;
23  import org.joda.time.DateTimeZone;
24  import org.joda.time.Interval;
25  import org.junit.Assert;
26  import org.junit.Test;
27  import org.kuali.kpme.core.IntegrationTest;
28  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
29  import org.kuali.kpme.core.service.HrServiceLocator;
30  import org.kuali.kpme.core.service.timezone.TimezoneServiceImpl;
31  import org.kuali.kpme.core.util.TKUtils;
32  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
33  
34  @IntegrationTest
35  public class TKUtilsTest extends Assert {
36  
37  	@Test
38  	public void testgetHoursBetween() throws Exception {
39  		DateTime beginDateTime = new DateTime(2010, 10, 16, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone());
40  		DateTime endDateTime = new DateTime(2010, 10, 17, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone());
41  		BigDecimal hours = TKUtils.getHoursBetween(beginDateTime.getMillis(), endDateTime.getMillis());
42  		assertEquals("Wrong hours", 24, hours.intValue());
43  
44  		endDateTime = new DateTime(2010, 10, 16, 18, 3, 0, 0, TKUtils.getSystemDateTimeZone());
45  		hours = TKUtils.getHoursBetween(beginDateTime.getMillis(), endDateTime.getMillis());
46  		assertEquals("Wrong hours", 6, hours.intValue());
47  
48  		endDateTime = new DateTime(2010, 10, 16, 18, 0, 0, 0, TKUtils.getSystemDateTimeZone());
49  		hours = TKUtils.getHoursBetween(beginDateTime.getMillis(), endDateTime.getMillis());
50  		assertEquals("Wrong hours", 5, hours.intValue());
51  
52  	}
53  
54  	@Test
55  	public void testGetFullWeekDaySpanForPayCalendarEntry() {
56  		CalendarEntry payCalendarEntry = new CalendarEntry();
57  		// begin date is a Monday
58  		payCalendarEntry.setBeginPeriodFullDateTime(new DateTime(2011, 8, 8, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
59  		// end date is a Thursday
60  		payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
61  		List<Interval> intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
62  		SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
63  		assertEquals("First Interval should be 08/07/2011", "08/07/2011", format.format(intervals.get(0).getStart().toDate()));
64  		assertEquals("Last Interval should be 08/26/2011", "08/26/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
65  
66  		// begin date is a Sunday
67  		payCalendarEntry.setBeginPeriodFullDateTime(new DateTime(2011, 8, 14, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
68  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
69  		assertEquals("First Interval should be 08/14/2011", "08/14/2011", format.format(intervals.get(0).getStart().toDate()));
70  		assertEquals("Last Interval should be 08/26/2011", "08/26/2011",  format.format(intervals.get(intervals.size()-1).getStart().toDate()));
71  
72  		// end date is a Sunday
73  		payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 28, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
74  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
75  		assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
76  		assertEquals("Last Interval should be 09/02/2011", "09/02/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
77  
78  		// end date is a Sunday and end time is 0:00
79  		payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 28, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
80  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
81  		assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
82  		assertEquals("Last Interval should be 08/27/2011", "08/27/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
83  
84  		// end date is a Monday and end time is 0:00
85  		payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 29, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
86  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
87  		assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
88  		assertEquals("Last Interval should be 09/03/2011", "09/03/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
89  
90  	}
91  	
92  	@Test
93  	public void testConvertMillisToMinutes() {
94  		BigDecimal mins = TKUtils.convertMillisToMinutes(new Long(380000));
95  		assertTrue("Minutes should be between 6 and 7",  mins.compareTo(new BigDecimal(6)) > 0  && mins.compareTo(new BigDecimal(7)) < 0);
96  		mins = TKUtils.convertMillisToMinutes(new Long(240000));
97  		assertTrue("Minutes should be 4",  mins.compareTo(new BigDecimal(4)) == 0);
98  	}
99  	
100 	@Test
101 	public void testFromDateString() {
102 		String dateString = "01/01/2012..12/31/2012";
103 		String fromDateString = TKUtils.getFromDateString(dateString);
104 		assertTrue("fromDateString should be 01/01/2012, not " + fromDateString, fromDateString.equals("01/01/2012"));
105 		assertNotNull(TKUtils.formatDateString(fromDateString));
106 		
107 		dateString = ">=2/01/2012";
108 		fromDateString = TKUtils.getFromDateString(dateString);
109 		assertTrue("fromDateString should be 2/01/2012, not " + fromDateString, fromDateString.equals("2/01/2012"));
110 		assertNotNull(TKUtils.formatDateString(fromDateString));
111 	}
112 	
113 	@Test
114 	public void testToDateString() {
115 		String dateString = "01/01/2012..12/31/2012";
116 		String toDateString = TKUtils.getToDateString(dateString);
117 		assertTrue("toDateString should be 12/31/2012, not " + toDateString, toDateString.equals("12/31/2012"));
118 		assertNotNull(TKUtils.formatDateString(toDateString));
119 		
120 		dateString = "<=2/01/2012";
121 		toDateString = TKUtils.getToDateString(dateString);
122 		assertTrue("toDateString should be 2/01/2012, not " + toDateString, toDateString.equals("2/01/2012"));
123 		assertNotNull(TKUtils.formatDateString(toDateString));
124 	}
125 	
126 	@Test
127 	public void testConvertTimeForDifferentTimeZone() {
128 		// it's hard to test this functionality since it depends on the sytem time the user is running the test from
129 		// picked Alaska time since we have no user working from Alaska time yet
130 		DateTimeZone fromTimeZone = TKUtils.getSystemDateTimeZone();
131 		DateTimeZone toTimeZone = DateTimeZone.forID("America/Anchorage");
132 		DateTime originalDateTime = new DateTime(2011, 8, 28, 0, 0, 0, 0, fromTimeZone);
133 		
134 		DateTime newDateTime = TKUtils.convertTimeForDifferentTimeZone(originalDateTime, fromTimeZone, toTimeZone);	
135 		assertNotNull(newDateTime);
136 		assertTrue("newDateTime should be different than originalDateTime", !newDateTime.equals(originalDateTime));
137 		
138 		newDateTime = TKUtils.convertTimeForDifferentTimeZone(originalDateTime, null, toTimeZone);
139 		assertTrue("newDateTime should be the same as originalDateTime", newDateTime.equals(originalDateTime));
140 		
141 		newDateTime = TKUtils.convertTimeForDifferentTimeZone(originalDateTime, null, null);
142 		assertTrue("newDateTime should be the same as originalDateTime", newDateTime.equals(originalDateTime));
143 		
144 		newDateTime = TKUtils.convertTimeForDifferentTimeZone(originalDateTime, fromTimeZone, null);
145 		assertTrue("newDateTime should be the same as originalDateTime", newDateTime.equals(originalDateTime));
146 	}
147 
148 }