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.Interval;
24  import org.junit.Assert;
25  import org.junit.Test;
26  import org.kuali.kpme.core.IntegrationTest;
27  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
28  import org.kuali.kpme.core.util.TKUtils;
29  
30  @IntegrationTest
31  public class TKUtilsTest extends Assert {
32  
33  	@Test
34  	public void testgetHoursBetween() throws Exception {
35  		DateTime beginDateTime = new DateTime(2010, 10, 16, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone());
36  		DateTime endDateTime = new DateTime(2010, 10, 17, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone());
37  		BigDecimal hours = TKUtils.getHoursBetween(beginDateTime.getMillis(), endDateTime.getMillis());
38  		assertEquals("Wrong hours", 24, hours.intValue());
39  
40  		endDateTime = new DateTime(2010, 10, 16, 18, 3, 0, 0, TKUtils.getSystemDateTimeZone());
41  		hours = TKUtils.getHoursBetween(beginDateTime.getMillis(), endDateTime.getMillis());
42  		assertEquals("Wrong hours", 6, hours.intValue());
43  
44  		endDateTime = new DateTime(2010, 10, 16, 18, 0, 0, 0, TKUtils.getSystemDateTimeZone());
45  		hours = TKUtils.getHoursBetween(beginDateTime.getMillis(), endDateTime.getMillis());
46  		assertEquals("Wrong hours", 5, hours.intValue());
47  
48  	}
49  
50  	@Test
51  	public void testGetFullWeekDaySpanForPayCalendarEntry() {
52  		CalendarEntry payCalendarEntry = new CalendarEntry();
53  		// begin date is a Monday
54  		payCalendarEntry.setBeginPeriodFullDateTime(new DateTime(2011, 8, 8, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
55  		// end date is a Thursday
56  		payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
57  		List<Interval> intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
58  		SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
59  		assertEquals("First Interval should be 08/07/2011", "08/07/2011", format.format(intervals.get(0).getStart().toDate()));
60  		assertEquals("Last Interval should be 08/26/2011", "08/26/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
61  
62  		// begin date is a Sunday
63  		payCalendarEntry.setBeginPeriodFullDateTime(new DateTime(2011, 8, 14, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
64  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
65  		assertEquals("First Interval should be 08/14/2011", "08/14/2011", format.format(intervals.get(0).getStart().toDate()));
66  		assertEquals("Last Interval should be 08/26/2011", "08/26/2011",  format.format(intervals.get(intervals.size()-1).getStart().toDate()));
67  
68  		// end date is a Sunday
69  		payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 28, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
70  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
71  		assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
72  		assertEquals("Last Interval should be 09/02/2011", "09/02/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
73  
74  		// end date is a Sunday and end time is 0:00
75  		payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 28, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
76  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
77  		assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
78  		assertEquals("Last Interval should be 08/27/2011", "08/27/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
79  
80  		// end date is a Monday and end time is 0:00
81  		payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 29, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
82  		intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() );
83  		assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
84  		assertEquals("Last Interval should be 09/03/2011", "09/03/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
85  
86  	}
87  	
88  	@Test
89  	public void testConvertMillisToMinutes() {
90  		BigDecimal mins = TKUtils.convertMillisToMinutes(new Long(380000));
91  		assertTrue("Minutes should be between 6 and 7",  mins.compareTo(new BigDecimal(6)) > 0  && mins.compareTo(new BigDecimal(7)) < 0);
92  		mins = TKUtils.convertMillisToMinutes(new Long(240000));
93  		assertTrue("Minutes should be 4",  mins.compareTo(new BigDecimal(4)) == 0);
94  	}
95  	
96  	@Test
97  	public void testFromDateString() {
98  		String dateString = "01/01/2012..12/31/2012";
99  		String fromDateString = TKUtils.getFromDateString(dateString);
100 		assertTrue("fromDateString should be 01/01/2012, not " + fromDateString, fromDateString.equals("01/01/2012"));
101 		assertNotNull(TKUtils.formatDateString(fromDateString));
102 		
103 		dateString = ">=2/01/2012";
104 		fromDateString = TKUtils.getFromDateString(dateString);
105 		assertTrue("fromDateString should be 2/01/2012, not " + fromDateString, fromDateString.equals("2/01/2012"));
106 		assertNotNull(TKUtils.formatDateString(fromDateString));
107 	}
108 	
109 	@Test
110 	public void testToDateString() {
111 		String dateString = "01/01/2012..12/31/2012";
112 		String toDateString = TKUtils.getToDateString(dateString);
113 		assertTrue("toDateString should be 12/31/2012, not " + toDateString, toDateString.equals("12/31/2012"));
114 		assertNotNull(TKUtils.formatDateString(toDateString));
115 		
116 		dateString = "<=2/01/2012";
117 		toDateString = TKUtils.getToDateString(dateString);
118 		assertTrue("toDateString should be 2/01/2012, not " + toDateString, toDateString.equals("2/01/2012"));
119 		assertNotNull(TKUtils.formatDateString(toDateString));
120 	}
121 
122 }