1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.time.util;
17
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.assertFalse;
21
22 import java.math.BigDecimal;
23 import java.sql.Timestamp;
24 import java.text.SimpleDateFormat;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TimeZone;
28 import java.util.TreeSet;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 import org.apache.commons.lang.StringUtils;
33 import org.joda.time.DateTime;
34 import org.joda.time.DateTimeZone;
35 import org.joda.time.Interval;
36 import org.joda.time.LocalDate;
37 import org.junit.Assert;
38 import org.junit.Test;
39 import org.kuali.kpme.core.IntegrationTest;
40 import org.kuali.kpme.core.api.KPMEConstants;
41 import org.kuali.kpme.core.api.assignment.Assignment;
42 import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
43 import org.kuali.kpme.core.api.util.KpmeUtils;
44 import org.kuali.kpme.core.service.HrServiceLocator;
45 import org.kuali.kpme.core.util.TKUtils;
46 import org.kuali.kpme.tklm.TKLMIntegrationTestCase;
47 import org.kuali.rice.core.api.config.property.ConfigContext;
48
49 @IntegrationTest
50 public class TKUtilsTest extends TKLMIntegrationTestCase {
51
52 @Test
53 public void testGetDayOfMonthFromDateString() throws Exception {
54 Assert.assertEquals("Wrong Day","01",TKUtils.getDayOfMonthFromDateString("01/01/2013"));
55 Assert.assertEquals("Wrong Day","10",TKUtils.getDayOfMonthFromDateString("01/10/2013"));
56 Assert.assertEquals("Wrong Day","20",TKUtils.getDayOfMonthFromDateString("01/20/2013"));
57 }
58
59 @Test
60 public void testGetSystemTimeZone() throws Exception {
61 Assert.assertEquals("Wrong TimeZone", TimeZone.getDefault().getID(),TKUtils.getSystemTimeZone());
62 }
63
64 @Test
65 public void testGetSystemDateTimeZone() throws Exception {
66 Assert.assertEquals("Wrong DateTimeZone",DateTimeZone.forID(TimeZone.getDefault().getID()),TKUtils.getSystemDateTimeZone());
67 }
68
69 @Test
70 public void testGetDaysBetween() throws Exception {
71 LocalDate startDate = new LocalDate(2013,1,1);
72 LocalDate endDate = new LocalDate(2013,1,15);
73 Long days = TKUtils.getDaysBetween(startDate,endDate);
74 Assert.assertEquals("Wrong Days",14,days.intValue());
75
76
77 days = TKUtils.getDaysBetween(startDate,endDate.plusDays(1));
78 Assert.assertEquals("Wrong Days",15,days.intValue());
79
80
81 days = TKUtils.getDaysBetween(startDate,endDate.minusDays(1));
82 Assert.assertEquals("Wrong Days",13,days.intValue());
83 }
84
85 @Test
86 public void testGetHoursBetween() throws Exception {
87 DateTime beginDateTime = new DateTime(2010, 10, 16, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone());
88 DateTime endDateTime = new DateTime(2010, 10, 17, 12, 3, 0, 0, TKUtils.getSystemDateTimeZone());
89 BigDecimal hours = TKUtils.getHoursBetween(beginDateTime.getMillis(), endDateTime.getMillis());
90 Assert.assertEquals("Wrong hours", 24, hours.intValue());
91
92 endDateTime = new DateTime(2010, 10, 16, 18, 3, 0, 0, TKUtils.getSystemDateTimeZone());
93 hours = TKUtils.getHoursBetween(beginDateTime.getMillis(), endDateTime.getMillis());
94 Assert.assertEquals("Wrong hours", 6, hours.intValue());
95
96 endDateTime = new DateTime(2010, 10, 16, 18, 0, 0, 0, TKUtils.getSystemDateTimeZone());
97 hours = TKUtils.getHoursBetween(beginDateTime.getMillis(), endDateTime.getMillis());
98 Assert.assertEquals("Wrong hours", 5, hours.intValue());
99
100 }
101
102 @Test
103 public void testFormatAssignmentKey() throws Exception {
104 String assignmentKey = KpmeUtils.formatAssignmentKey("GK", 1L, 2L, 3L);
105 Assert.assertEquals("Assignment Key should be GK_1_2_3","GK_1_2_3",assignmentKey);
106 }
107
108 @Test
109 public void testFormatAssignmentDescription() throws Exception {
110 Assignment.Builder testAssignment = Assignment.Builder.create("admin", "GK", 30L, 30L, 30L);
111 testAssignment.setEffectiveLocalDate(new LocalDate(2010,1,1));
112
113 Map<String,String> formattedAssignmentDescription = TKUtils.formatAssignmentDescription(testAssignment.build());
114
115 assertTrue(formattedAssignmentDescription.containsKey("GK_30_30_30"));
116 Assert.assertEquals("Description is not correct","SDR1 Work Area : $20.00 Rcd 30 TEST-DEPT SDR1 task",formattedAssignmentDescription.get("GK_30_30_30"));
117
118 testAssignment.setGroupKeyCode("GK");
119 testAssignment.setJobNumber(999L);
120 testAssignment.setWorkArea(999L);
121 testAssignment.setTask(999L);
122 testAssignment.setPrincipalId("principalId");
123 testAssignment.setEffectiveLocalDate(new LocalDate(2013,1,1));
124
125 formattedAssignmentDescription = TKUtils.formatAssignmentDescription(testAssignment.build());
126 assertTrue(formattedAssignmentDescription.containsKey("GK_999_999_999"));
127 Assert.assertEquals("Description is not correct"," : $0.00 Rcd 999 ",formattedAssignmentDescription.get("GK_999_999_999"));
128 }
129
130 @Test
131 public void testGetAssignmentString() throws Exception {
132 String testAssignmentString = HrServiceLocator.getAssignmentService().getAssignmentDescription("admin", "IU-BL", 30L, 30L, 30L, new LocalDate(2010, 1, 1));
133 Assert.assertEquals("Assignment String is wrong","SDR1 Work Area : $20.00 Rcd 30 TEST-DEPT SDR1 task",testAssignmentString);
134 }
135
136 @Test
137 public void testGetDaySpanForCalendarEntry() throws Exception {
138 CalendarEntry.Builder payCalendarEntry = CalendarEntry.Builder.create();
139 payCalendarEntry.setBeginPeriodFullDateTime(new DateTime(2013, 1, 1, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
140 payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2013, 1, 4, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
141 List<Interval> daySpanList = TKUtils.getDaySpanForCalendarEntry(payCalendarEntry.build(),TKUtils.getSystemDateTimeZone());
142 Assert.assertEquals("List Size should be 3",3,daySpanList.size());
143 Assert.assertEquals("Start Date should be 01/01/2013",daySpanList.get(0).getStart(),
144 new DateTime(2013,1,1,0,0,0,TKUtils.getSystemDateTimeZone()));
145 Assert.assertEquals("End Date should be 01/04/2013",daySpanList.get(daySpanList.size()-1).getEnd(),
146 new DateTime(2013,1,4,0,0,0,TKUtils.getSystemDateTimeZone()));
147
148
149 payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2013, 1, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
150 List<Interval> daySpanList1 = TKUtils.getDaySpanForCalendarEntry(payCalendarEntry.build(),TKUtils.getSystemDateTimeZone());
151 Assert.assertEquals("List Size should be 4",4,daySpanList1.size());
152 Assert.assertEquals("Start Date should be 01/01/2013",daySpanList1.get(0).getStart(),
153 new DateTime(2013,1,1,0,0,0,TKUtils.getSystemDateTimeZone()));
154 Assert.assertEquals("End Date should be 01/05/2013",daySpanList1.get(daySpanList1.size()-1).getEnd(),
155 new DateTime(2013,1,5,0,0,0,TKUtils.getSystemDateTimeZone()));
156
157
158 payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2013, 1, 3, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
159 List<Interval> daySpanList2 = TKUtils.getDaySpanForCalendarEntry(payCalendarEntry.build(),TKUtils.getSystemDateTimeZone());
160 Assert.assertEquals("List Size should be 2",2,daySpanList2.size());
161 Assert.assertEquals("Start Date should be 01/01/2013",daySpanList2.get(0).getStart(),
162 new DateTime(2013,1,1,0,0,0,TKUtils.getSystemDateTimeZone()));
163 Assert.assertEquals("End Date should be 01/03/2013",daySpanList2.get(daySpanList2.size()-1).getEnd(),
164 new DateTime(2013,1,3,0,0,0,TKUtils.getSystemDateTimeZone()));
165
166
167 List<Interval> daySpanList3 = TKUtils.getDaySpanForCalendarEntry(payCalendarEntry.build());
168 Assert.assertEquals("List Size should be 2",2,daySpanList3.size());
169
170
171 Assert.assertEquals("Start Date should be 01/01/2013",daySpanList3.get(0).getStart().getMillis(),
172 new DateTime(2013,1,1,0,0,0,HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback()).getMillis());
173 Assert.assertTrue("Start Date should be 01/01/2013",daySpanList3.get(0).getStart().isEqual(new DateTime(2013,1,1,0,0,0,HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback())) );
174
175
176
177
178
179 Assert.assertEquals("End Date should be 01/03/2013",daySpanList3.get(daySpanList3.size()-1).getEnd().getMillis(),
180 new DateTime(2013,1,3,0,0,0,HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback()).getMillis());
181
182
183
184
185
186
187 }
188
189 @Test
190 public void testConvertHoursToMillis() throws Exception {
191 BigDecimal hours = new BigDecimal(1);
192 Long millis = TKUtils.convertHoursToMillis(hours);
193 Assert.assertEquals("Wrong Millis", 3600000, millis.intValue());
194
195 millis = TKUtils.convertHoursToMillis(hours.add(new BigDecimal(.5)));
196 Assert.assertEquals("Wrong Millis", 5400000, millis.intValue());
197
198
199 }
200
201 @Test
202 public void testConvertMillisToHours() throws Exception {
203 BigDecimal hours = TKUtils.convertMillisToHours(3600000L);
204 Assert.assertEquals("Wrong Hours", new BigDecimal(1), hours);
205
206 hours = TKUtils.convertMillisToHours(5400000L);
207 Assert.assertEquals("Wrong Hours",new BigDecimal(1.5),hours);
208
209
210 }
211
212 @Test
213 public void testConvertMillisToMinutes() {
214 BigDecimal mins = TKUtils.convertMillisToMinutes(380000);
215 assertTrue("Minutes should be between 6 and 7", mins.compareTo(new BigDecimal(6)) > 0 && mins.compareTo(new BigDecimal(7)) < 0);
216 mins = TKUtils.convertMillisToMinutes(240000);
217 assertTrue("Minutes should be 4", mins.compareTo(new BigDecimal(4)) == 0);
218 }
219
220 @Test
221 public void testConvertMillisToDays() throws Exception {
222 Long millis = 86400000L;
223 BigDecimal days = TKUtils.convertMillisToDays(millis);
224 Assert.assertEquals("Wrong Days",1,days.intValue());
225 }
226
227 @Test
228 public void testConvertMinutesToHours() throws Exception {
229 BigDecimal minutes = new BigDecimal(60);
230 BigDecimal hours = TKUtils.convertMinutesToHours(minutes);
231 Assert.assertEquals("Wrong Hours",1,hours.intValue());
232 }
233
234 @Test
235 public void testConvertMillisToWholeDays() throws Exception {
236 Long millis = 1L;
237 int days = TKUtils.convertMillisToWholeDays(millis);
238 Assert.assertEquals("Wrong Whole Days", 1, days);
239
240 millis = 86410000L;
241 days = TKUtils.convertMillisToWholeDays(millis);
242 Assert.assertEquals("Wrong Whole Days", 2, days);
243 }
244
245 @Test
246 public void testIsVirtualWorkDay() throws Exception {
247 Assert.assertEquals("Expected False",false,TKUtils.isVirtualWorkDay(new DateTime(2013,1,1,0,0,0)));
248 Assert.assertEquals("Expected True",true,TKUtils.isVirtualWorkDay(new DateTime(2013,1,1,1,0,0)));
249 Assert.assertEquals("Expected False",false,TKUtils.isVirtualWorkDay(new DateTime(2013,1,1,0,30,0)));
250 Assert.assertEquals("Expected true",true,TKUtils.isVirtualWorkDay(new DateTime(2013,1,1,13,30,0)));
251 }
252
253 @Test
254 public void testConvertDateStringToDateTime() throws Exception {
255 String dateString = "01/01/2013";
256 String timeString = "9:0";
257 DateTime dateTime = TKUtils.convertDateStringToDateTime(dateString, timeString);
258 DateTime equalsDateTime = new DateTime(2013, 1, 1, 9, 0, 0, dateTime.getZone());
259 Assert.assertTrue("Date Time Not Correct " + equalsDateTime, dateTime.isEqual(equalsDateTime));
260 Assert.assertEquals("Date Time Not Correct", dateTime.toString(), equalsDateTime.toString());
261 }
262
263
264 @Test
265 public void testConvertDateStringToDateTimeWithoutZone() {
266 String dateString = "01/01/2013";
267 String timeString = "9:0";
268 DateTime dateTime = TKUtils.convertDateStringToDateTimeWithoutZone(dateString, timeString);
269 DateTime equalsDateTime = new DateTime(2013, 1, 1, 9, 0, 0);
270 Assert.assertEquals("Date Time Not Correct", dateTime, equalsDateTime);
271 }
272
273 @Test
274 public void testGetIPAddressFromRequest() throws Exception {
275 Assert.assertEquals("IP Address is Not Correct","192.168.1.1",TKUtils.getIPAddressFromRequest("192.168.1.1"));
276 Assert.assertEquals("IP Address is Not Correct","",TKUtils.getIPAddressFromRequest("FE80:0000:0000:0000:0202:B3FF:FE1E:8329"));
277 }
278
279 @Test
280 public void testGetIPNumber() throws Exception {
281 Assert.assertEquals("Wrong IP Adress",java.net.InetAddress.getLocalHost().getHostAddress(),TKUtils.getIPNumber());
282 }
283 @Test
284 public void testSubtractOneSecondFromTimestamp() throws Exception {
285 DateTime testDate1 = new DateTime(2013, 1, 1, 9, 0 , 1);
286 DateTime testDate2 = new DateTime(2013, 1, 1, 9, 0 , 0);
287 Timestamp originalTimeStamp = new Timestamp(testDate1.getMillis());
288 Timestamp compareTimeStamp = new Timestamp(testDate2.getMillis());
289 Timestamp newTimeStamp = TKUtils.subtractOneSecondFromTimestamp(originalTimeStamp);
290 Assert.assertEquals("TimeStamp is Not Correct",compareTimeStamp,newTimeStamp);
291 }
292
293 @Test
294 public void testFormatDate() throws Exception {
295 LocalDate localDate = new LocalDate(2013,1,1);
296 String formattedDate = TKUtils.formatDate(localDate);
297 Assert.assertEquals("Date Format is not correct", "01/01/2013", formattedDate);
298 }
299
300 @Test
301 public void testFormatDateTimeShort() throws Exception {
302 DateTime dateTime = new DateTime(2013, 1 , 1, 0 , 0, 0);
303 String formattedDate = TKUtils.formatDateTimeShort(dateTime);
304 Assert.assertEquals("Date Format is not correct", "01/01/2013", formattedDate);
305 }
306
307 @Test
308 public void testFormatDateTimeLong() throws Exception {
309 DateTime dateTime = new DateTime(2013, 1 , 1, 9 , 30, 15);
310 String formattedDate = TKUtils.formatDateTimeLong(dateTime);
311 Assert.assertEquals("Date Format is not correct", "01/01/2013 09:30:15", formattedDate);
312 }
313
314 @Test
315 public void testFormatDateString() throws Exception {
316 String date = "01/01/2013";
317 LocalDate compareDate = new LocalDate(2013,1,1);
318 LocalDate localDate = TKUtils.formatDateString(date);
319 Assert.assertEquals("Date is not correct",localDate,compareDate);
320 }
321
322 @Test
323 public void testFormatTimeShort() throws Exception {
324 String dateString = "01/01/2013 09:30:15";
325 String timeShortString = TKUtils.formatTimeShort(dateString);
326 Assert.assertEquals("Time Format is not correct","09:30",timeShortString);
327
328 }
329 @Test
330 public void testFormatDateTimeString() throws Exception {
331 String dateTimeString = "01/01/2013";
332 DateTime compareDateTime = new DateTime(2013,1,1,0,0,0);
333 DateTime dateTime = TKUtils.formatDateTimeString(dateTimeString);
334
335 Assert.assertTrue("DateTime is not Correct", compareDateTime.isEqual(dateTime));
336
337 compareDateTime = compareDateTime.withZone(dateTime.getZone());
338 Assert.assertEquals("DateTime is not Correct", compareDateTime.toString(), dateTime.toString());
339 }
340
341 @Test
342 public void testFormatDateTimeStringNoTimeZone() throws Exception {
343 String dateTimeString = "01/01/2013";
344 DateTime compareDateTime = new DateTime(2013,1,1,0,0,0);
345 DateTime dateTime = TKUtils.formatDateTimeStringNoTimezone(dateTimeString);
346 Assert.assertEquals("DateTime is not Correct",compareDateTime,dateTime);
347 }
348
349 @Test
350 public void testGetTimezoneOffset() throws Exception {
351 DateTime date = new DateTime(DateTimeZone.forID("Etc/GMT+5"));
352 String timeZoneOffset = TKUtils.getTimezoneOffset(date);
353 Assert.assertEquals("wrong offset","-0500",timeZoneOffset);
354 }
355
356 @Test
357 public void testArrayToString() throws Exception {
358 String[] stringArray = {"one","two","three"};
359 String arrayString = TKUtils.arrayToString(stringArray);
360 Assert.assertEquals("String is not correct","onetwothree",arrayString);
361 }
362
363 @Test
364 public void testGetSessionTimeoutTime() throws Exception {
365 int timeOut;
366 if(StringUtils.isBlank(ConfigContext.getCurrentContextConfig().getProperty(KPMEConstants.ConfigSettings.SESSION_TIMEOUT))) {
367 timeOut = 2700;
368 } else {
369 timeOut = Integer.parseInt(ConfigContext.getCurrentContextConfig().getProperty(KPMEConstants.ConfigSettings.SESSION_TIMEOUT));
370 }
371 Assert.assertEquals("Session Time Out is Incorrect",timeOut,TKUtils.getSessionTimeoutTime());
372 }
373
374 @Test
375 public void testCreateDaySpan() throws Exception {
376 DateTime beginDateTime = new DateTime(2013,1,1,0,0,0,TKUtils.getSystemDateTimeZone());
377 DateTime endDateTime = new DateTime(2013,1,4,0,0,0,TKUtils.getSystemDateTimeZone());
378 List<Interval> daySpanList = TKUtils.createDaySpan(beginDateTime,endDateTime,TKUtils.getSystemDateTimeZone());
379 Assert.assertEquals("List Size should be 3",3,daySpanList.size());
380 Assert.assertEquals("Start Date should be 01/01/2013",daySpanList.get(0).getStart(),new DateTime(2013,1,1,0,0,0,TKUtils.getSystemDateTimeZone()));
381 Assert.assertEquals("End Date should be 01/04/2013",daySpanList.get(daySpanList.size()-1).getEnd(),new DateTime(2013,1,4,0,0,0,TKUtils.getSystemDateTimeZone()));
382 }
383
384 @Test
385 public void testGetFullWeekDaySpanForPayCalendarEntry() {
386 CalendarEntry.Builder payCalendarEntry = CalendarEntry.Builder.create();
387
388 payCalendarEntry.setBeginPeriodFullDateTime(new DateTime(2011, 8, 8, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
389
390 payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
391 List<Interval> intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry.build(), TKUtils.getSystemDateTimeZone() );
392 SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
393 Assert.assertEquals("First Interval should be 08/07/2011", "08/07/2011", format.format(intervals.get(0).getStart().toDate()));
394 Assert.assertEquals("Last Interval should be 08/26/2011", "08/26/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
395
396
397 payCalendarEntry.setBeginPeriodFullDateTime(new DateTime(2011, 8, 14, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
398 intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry.build(), TKUtils.getSystemDateTimeZone() );
399 Assert.assertEquals("First Interval should be 08/14/2011", "08/14/2011", format.format(intervals.get(0).getStart().toDate()));
400 Assert.assertEquals("Last Interval should be 08/26/2011", "08/26/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
401
402
403 payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 28, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
404 intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry.build(), TKUtils.getSystemDateTimeZone() );
405 Assert.assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
406 Assert.assertEquals("Last Interval should be 09/02/2011", "09/02/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
407
408
409 payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 28, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
410 intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry.build(), TKUtils.getSystemDateTimeZone() );
411 Assert.assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
412 Assert.assertEquals("Last Interval should be 08/27/2011", "08/27/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
413
414
415 payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 29, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
416 intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry.build(), TKUtils.getSystemDateTimeZone() );
417 Assert.assertEquals("First Interval should be 08/14/2011", "08/14/2011",format.format(intervals.get(0).getStart().toDate()));
418 Assert.assertEquals("Last Interval should be 09/03/2011", "09/03/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
419
420
421 payCalendarEntry.setBeginPeriodFullDateTime(new DateTime(2011, 8, 8, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
422 payCalendarEntry.setEndPeriodFullDateTime(new DateTime(2011, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
423 intervals = TKUtils.getFullWeekDaySpanForCalendarEntry(payCalendarEntry.build());
424 Assert.assertEquals("First Interval should be 08/07/2011", "08/07/2011", format.format(intervals.get(0).getStart().toDate()));
425 Assert.assertEquals("Last Interval should be 08/26/2011", "08/26/2011", format.format(intervals.get(intervals.size()-1).getStart().toDate()));
426
427 }
428
429 @Test
430 public void testisWeekend() throws Exception {
431 DateTime saturday = new DateTime(2013,10,26,0,0,0);
432 DateTime sunday = new DateTime(2013,10,27,0,0,0);
433 DateTime monday = new DateTime(2013,10,28,0,0,0);
434 Assert.assertEquals("This should be true",true,TKUtils.isWeekend(saturday));
435 Assert.assertEquals("This should be true",true,TKUtils.isWeekend(sunday));
436 Assert.assertEquals("This should be false",false,TKUtils.isWeekend(monday));
437 }
438
439 @Test
440 public void testFromDateString() {
441 String dateString = "01/01/2012..12/31/2012";
442 String fromDateString = TKUtils.getFromDateString(dateString);
443 assertTrue("fromDateString should be 01/01/2012, not " + fromDateString, fromDateString.equals("01/01/2012"));
444 Assert.assertNotNull(TKUtils.formatDateString(fromDateString));
445
446 dateString = ">=2/01/2012";
447 fromDateString = TKUtils.getFromDateString(dateString);
448 assertTrue("fromDateString should be 2/01/2012, not " + fromDateString, fromDateString.equals("2/01/2012"));
449 Assert.assertNotNull(TKUtils.formatDateString(fromDateString));
450 }
451
452 @Test
453 public void testToDateString() {
454 String dateString = "01/01/2012..12/31/2012";
455 String toDateString = TKUtils.getToDateString(dateString);
456 assertTrue("toDateString should be 12/31/2012, not " + toDateString, toDateString.equals("12/31/2012"));
457 Assert.assertNotNull(TKUtils.formatDateString(toDateString));
458
459 dateString = "<=2/01/2012";
460 toDateString = TKUtils.getToDateString(dateString);
461 assertTrue("toDateString should be 2/01/2012, not " + toDateString, toDateString.equals("2/01/2012"));
462 Assert.assertNotNull(TKUtils.formatDateString(toDateString));
463 }
464
465 @Test
466 public void testConvertTimeForDifferentTimeZone() {
467
468
469 DateTimeZone fromTimeZone = TKUtils.getSystemDateTimeZone();
470 DateTimeZone toTimeZone = DateTimeZone.forID("America/Anchorage");
471 DateTime originalDateTime = new DateTime(2011, 8, 28, 0, 0, 0, 0, fromTimeZone);
472
473 DateTime newDateTime = TKUtils.convertTimeForDifferentTimeZone(originalDateTime, fromTimeZone, toTimeZone);
474 assertNotNull(newDateTime);
475 assertTrue("newDateTime should be different than originalDateTime", !newDateTime.equals(originalDateTime));
476
477 newDateTime = TKUtils.convertTimeForDifferentTimeZone(originalDateTime, null, toTimeZone);
478 assertTrue("newDateTime should be the same as originalDateTime", newDateTime.equals(originalDateTime));
479
480 newDateTime = TKUtils.convertTimeForDifferentTimeZone(originalDateTime, null, null);
481 assertTrue("newDateTime should be the same as originalDateTime", newDateTime.equals(originalDateTime));
482
483 newDateTime = TKUtils.convertTimeForDifferentTimeZone(originalDateTime, fromTimeZone, null);
484 assertTrue("newDateTime should be the same as originalDateTime", newDateTime.equals(originalDateTime));
485 }
486
487
488 @Test
489 public void testisDateEqualOrBetween() throws Exception {
490
491
492
493 DateTime equalsDate = new DateTime(2013,1,1,0,0,0, TKUtils.getSystemDateTimeZone());
494
495 assertTrue("This should be true", TKUtils.isDateEqualOrBetween(equalsDate, "01/01/2013..01/07/2013"));
496
497
498
499 DateTime betweenDate = new DateTime(2013,1,4,0,0,0, DateTimeZone.forID("America/Indiana/Indianapolis"));
500 assertTrue("This should be true", TKUtils.isDateEqualOrBetween(betweenDate, "01/01/2013..01/07/2013"));
501
502 }
503 @Test
504 public void testgetRandomColor() throws Exception {
505 TreeSet<String> colorSet = new TreeSet<String>();
506 colorSet.add("#FFFFFF");
507 String color = TKUtils.getRandomColor(colorSet);
508 Pattern colorPattern = Pattern.compile("^#(([0-9a-fA-F]{2}){3}|([0-9a-fA-F]){3})$");
509 Matcher colorMatcher = colorPattern.matcher(color);
510 assertTrue(colorMatcher.find());
511 }
512
513 @Test
514 public void testcleanNumeric() throws Exception {
515 Assert.assertEquals("Expected 1234",new BigDecimal("1234"),TKUtils.cleanNumeric("1234"));
516 Assert.assertEquals("Expected 1234",new BigDecimal("1234"),TKUtils.cleanNumeric("12$34A"));
517 Assert.assertEquals("Expected 1234",new BigDecimal("1234.45"),TKUtils.cleanNumeric("12$34A..45"));
518 Assert.assertEquals("Expected 1234",new BigDecimal("-1234.45"),TKUtils.cleanNumeric("-12$-34A..45"));
519 }
520
521 @Test
522 public void testgetDocumentDescription() throws Exception {
523 String docDescription = TKUtils.getDocumentDescription("admin",new LocalDate(2010,1,1));
524 Assert.assertEquals("doc Description is wrong","admin, admin (admin) - 01/01/2010",docDescription);
525 }
526
527 @Test
528 public void testGetSingleGroupKey()
529 {
530 assertTrue(TKUtils.getSingleGroupKey(LocalDate.now()) == null);
531
532 String singleGroupKey =TKUtils.getSingleGroupKey(new LocalDate(1960, 1, 1) );
533 assertTrue(singleGroupKey == null);
534
535 singleGroupKey =TKUtils.getSingleGroupKey(new LocalDate(1970, 1, 1) );
536 assertTrue(singleGroupKey != null);
537 }
538 }