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