001 /** 002 * Copyright 2004-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.hr.time.util; 017 018 import java.math.BigDecimal; 019 import java.sql.Date; 020 import java.sql.Timestamp; 021 import java.text.SimpleDateFormat; 022 import java.util.List; 023 024 import org.joda.time.DateTime; 025 import org.joda.time.Interval; 026 import org.junit.Assert; 027 import org.junit.Test; 028 import org.kuali.hr.time.calendar.CalendarEntries; 029 030 public class TKUtilsTest extends Assert { 031 032 @Test 033 public void testGetWeekIntervals() throws Exception { 034 Date beginDate = new Date((new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 035 Date endDate = new Date((new DateTime(2010, 3, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 036 List<Interval> intervals = TKUtils.getWeekIntervals(beginDate, endDate); 037 assertEquals("Missing partial week", 9, intervals.size()); 038 for (Interval interval : intervals) { 039 System.out.println(interval); 040 } 041 042 beginDate = new Date((new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 043 endDate = new Date((new DateTime(2010, 1, 3, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 044 intervals = TKUtils.getWeekIntervals(beginDate, endDate); 045 assertEquals("Missing partial week", 1, intervals.size()); 046 for (Interval interval : intervals) { 047 System.out.println(interval); 048 } 049 050 } 051 052 @Test 053 public void testgetHoursBetween() throws Exception { 054 Timestamp beginTime = new Timestamp((new DateTime(2010, 10, 16, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 055 Timestamp endTime = new Timestamp((new DateTime(2010, 10, 17, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 056 BigDecimal hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime()); 057 assertEquals("Wrong hours", 24, hours.intValue()); 058 059 endTime = new Timestamp((new DateTime(2010, 10, 16, 18, 3, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 060 hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime()); 061 assertEquals("Wrong hours", 6, hours.intValue()); 062 063 endTime = new Timestamp((new DateTime(2010, 10, 16, 18, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 064 hours = TKUtils.getHoursBetween(beginTime.getTime(), endTime.getTime()); 065 assertEquals("Wrong hours", 5, hours.intValue()); 066 067 } 068 069 @Test 070 public void testGetFullWeekDaySpanForPayCalendarEntry() { 071 CalendarEntries payCalendarEntry = new CalendarEntries(); 072 // begin date is a Monday 073 payCalendarEntry.setBeginPeriodDateTime(new Date((new DateTime(2011, 8, 8, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis())); 074 // end date is a Thursday 075 payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis())); 076 List<Interval> intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() ); 077 SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy"); 078 assertEquals("First Interval should be 08/07/2011", "08/07/2011", format.format(intervals.get(0).getStart().toDate())); 079 assertEquals("Last Interval should be 08/26/2011", "08/26/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate())); 080 081 // begin date is a Sunday 082 payCalendarEntry.setBeginPeriodDateTime(new Date((new DateTime(2011, 8, 14, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis())); 083 intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() ); 084 assertEquals("First Interval should be 08/14/2011", "08/14/2011", format.format(intervals.get(0).getStart().toDate())); 085 assertEquals("Last Interval should be 08/26/2011", "08/26/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate())); 086 087 // end date is a Sunday 088 payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 28, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis())); 089 intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() ); 090 assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate())); 091 assertEquals("Last Interval should be 09/02/2011", "09/02/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate())); 092 093 // end date is a Sunday and end time is 0:00 094 payCalendarEntry.setEndPeriodDateTime(new Date((new DateTime(2011, 8, 28, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis())); 095 intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry, TKUtils.getSystemDateTimeZone() ); 096 assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate())); 097 assertEquals("Last Interval should be 08/27/2011", "08/27/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate())); 098 099 // 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 }