1 package org.jbpm.calendar; 2 3 import java.util.*; 4 import junit.framework.*; 5 6 public class BusinessCalendarTest extends TestCase { 7 8 static BusinessCalendar businessCalendar = new BusinessCalendar(); 9 10 public void testNonBusinessDurationAddition() throws Exception { 11 Calendar calendar = BusinessCalendar.getCalendar(); 12 calendar.set(Calendar.YEAR, 2005); 13 calendar.set(Calendar.MONTH, Calendar.APRIL); 14 calendar.set(Calendar.DAY_OF_MONTH, 7); 15 calendar.set(Calendar.HOUR_OF_DAY, 10); 16 calendar.set(Calendar.MINUTE, 30); 17 calendar.set(Calendar.SECOND, 0); 18 calendar.set(Calendar.MILLISECOND, 0); 19 Date start = calendar.getTime(); 20 21 Date twentySecondsLater = 22 businessCalendar.add(start, new Duration("20 seconds")); 23 24 calendar = BusinessCalendar.getCalendar(); 25 calendar.set(Calendar.YEAR, 2005); 26 calendar.set(Calendar.MONTH, Calendar.APRIL); 27 calendar.set(Calendar.DAY_OF_MONTH, 7); 28 calendar.set(Calendar.HOUR_OF_DAY, 10); 29 calendar.set(Calendar.MINUTE, 30); 30 calendar.set(Calendar.SECOND, 20); 31 calendar.set(Calendar.MILLISECOND, 0); 32 Date expected = calendar.getTime(); 33 34 assertEquals(expected, twentySecondsLater); 35 } 36 37 public void testNonBusinessDurationAdditionOverNonBusinessTime() throws Exception { 38 Calendar calendar = BusinessCalendar.getCalendar(); 39 calendar.set(Calendar.YEAR, 2005); 40 calendar.set(Calendar.MONTH, Calendar.APRIL); 41 calendar.set(Calendar.DAY_OF_MONTH, 7); 42 calendar.set(Calendar.HOUR_OF_DAY, 11); 43 calendar.set(Calendar.MINUTE, 55); 44 calendar.set(Calendar.SECOND, 0); 45 calendar.set(Calendar.MILLISECOND, 0); 46 Date fiveBeforeTwelve = calendar.getTime(); 47 48 Date tenMinutesLaterThenFiveBeforeTwelve = 49 businessCalendar.add(fiveBeforeTwelve, new Duration("10 minutes")); 50 51 calendar = BusinessCalendar.getCalendar(); 52 calendar.set(Calendar.YEAR, 2005); 53 calendar.set(Calendar.MONTH, Calendar.APRIL); 54 calendar.set(Calendar.DAY_OF_MONTH, 7); 55 calendar.set(Calendar.HOUR_OF_DAY, 12); 56 calendar.set(Calendar.MINUTE, 5); 57 calendar.set(Calendar.SECOND, 0); 58 calendar.set(Calendar.MILLISECOND, 0); 59 Date fiveAfterTwelve = calendar.getTime(); 60 61 assertEquals(fiveAfterTwelve, tenMinutesLaterThenFiveBeforeTwelve); 62 } 63 64 public void testBusinessDurationAdditionInBusinessTime() throws Exception { 65 Calendar calendar = BusinessCalendar.getCalendar(); 66 calendar.set(Calendar.YEAR, 2005); 67 calendar.set(Calendar.MONTH, Calendar.APRIL); 68 calendar.set(Calendar.DAY_OF_MONTH, 7); 69 calendar.set(Calendar.HOUR_OF_DAY, 11); 70 calendar.set(Calendar.MINUTE, 55); 71 calendar.set(Calendar.SECOND, 0); 72 calendar.set(Calendar.MILLISECOND, 0); 73 Date fiveBeforeTwelve = calendar.getTime(); 74 75 Date oneMinuteLaterThenFiveBeforeTwelve = 76 businessCalendar.add(fiveBeforeTwelve, new Duration("1 business minute")); 77 78 calendar = BusinessCalendar.getCalendar(); 79 calendar.set(Calendar.YEAR, 2005); 80 calendar.set(Calendar.MONTH, Calendar.APRIL); 81 calendar.set(Calendar.DAY_OF_MONTH, 7); 82 calendar.set(Calendar.HOUR_OF_DAY, 11); 83 calendar.set(Calendar.MINUTE, 56); 84 calendar.set(Calendar.SECOND, 0); 85 calendar.set(Calendar.MILLISECOND, 0); 86 Date fourBeforeTwelve = calendar.getTime(); 87 88 assertEquals(fourBeforeTwelve, oneMinuteLaterThenFiveBeforeTwelve); 89 } 90 91 public void testBusinessDurationAdditionOverLunchBreak() throws Exception { 92 Calendar calendar = BusinessCalendar.getCalendar(); 93 calendar.set(Calendar.YEAR, 2005); 94 calendar.set(Calendar.MONTH, Calendar.APRIL); 95 calendar.set(Calendar.DAY_OF_MONTH, 7); 96 calendar.set(Calendar.HOUR_OF_DAY, 11); 97 calendar.set(Calendar.MINUTE, 55); 98 calendar.set(Calendar.SECOND, 0); 99 calendar.set(Calendar.MILLISECOND, 0); 100 Date fiveBeforeTwelve = calendar.getTime(); 101 102 Date tenBusinessMinutesAfterFiveBeforeTwelve = 103 businessCalendar.add(fiveBeforeTwelve, new Duration("10 business minutes")); 104 105 calendar = BusinessCalendar.getCalendar(); 106 calendar.set(Calendar.YEAR, 2005); 107 calendar.set(Calendar.MONTH, Calendar.APRIL); 108 calendar.set(Calendar.DAY_OF_MONTH, 7); 109 calendar.set(Calendar.HOUR_OF_DAY, 12); 110 calendar.set(Calendar.MINUTE, 35); 111 calendar.set(Calendar.SECOND, 0); 112 calendar.set(Calendar.MILLISECOND, 0); 113 Date twelveThirtyfive = calendar.getTime(); 114 115 assertEquals(twelveThirtyfive, tenBusinessMinutesAfterFiveBeforeTwelve); 116 } 117 118 public void testBusinessDurationAdditionOverDayBreak() throws Exception { 119 Calendar calendar = BusinessCalendar.getCalendar(); 120 calendar.set(Calendar.YEAR, 2005); 121 calendar.set(Calendar.MONTH, Calendar.APRIL); 122 calendar.set(Calendar.DAY_OF_MONTH, 7); 123 calendar.set(Calendar.HOUR_OF_DAY, 16); 124 calendar.set(Calendar.MINUTE, 55); 125 calendar.set(Calendar.SECOND, 0); 126 calendar.set(Calendar.MILLISECOND, 0); 127 Date fiveBeforeFive = calendar.getTime(); 128 129 Date tenBusinessMinutesAfterFiveBeforeFive = 130 businessCalendar.add(fiveBeforeFive, new Duration("10 business minutes")); 131 132 calendar = BusinessCalendar.getCalendar(); 133 calendar.set(Calendar.YEAR, 2005); 134 calendar.set(Calendar.MONTH, Calendar.APRIL); 135 calendar.set(Calendar.DAY_OF_MONTH, 8); 136 calendar.set(Calendar.HOUR_OF_DAY, 9); 137 calendar.set(Calendar.MINUTE, 5); 138 calendar.set(Calendar.SECOND, 0); 139 calendar.set(Calendar.MILLISECOND, 0); 140 Date fiveAfterNineNextDay = calendar.getTime(); 141 142 assertEquals(fiveAfterNineNextDay, tenBusinessMinutesAfterFiveBeforeFive); 143 } 144 145 public void testBusinessDurationAdditionOverHolidayBreak() throws Exception { 146 Calendar calendar = BusinessCalendar.getCalendar(); 147 calendar.set(Calendar.YEAR, 2005); 148 calendar.set(Calendar.MONTH, Calendar.MAY); 149 calendar.set(Calendar.DAY_OF_MONTH, 4); 150 calendar.set(Calendar.HOUR_OF_DAY, 16); 151 calendar.set(Calendar.MINUTE, 55); 152 calendar.set(Calendar.SECOND, 0); 153 calendar.set(Calendar.MILLISECOND, 0); 154 Date fiveBeforeFive = calendar.getTime(); 155 156 Date tenBusinessMinutesAfterFiveBeforeFive = 157 businessCalendar.add(fiveBeforeFive, new Duration("10 business minutes")); 158 159 calendar = BusinessCalendar.getCalendar(); 160 calendar.set(Calendar.YEAR, 2005); 161 calendar.set(Calendar.MONTH, Calendar.MAY); 162 calendar.set(Calendar.DAY_OF_MONTH, 6); 163 calendar.set(Calendar.HOUR_OF_DAY, 9); 164 calendar.set(Calendar.MINUTE, 5); 165 calendar.set(Calendar.SECOND, 0); 166 calendar.set(Calendar.MILLISECOND, 0); 167 Date fiveAfterNineTwoDaysLater = calendar.getTime(); 168 169 assertEquals(fiveAfterNineTwoDaysLater, tenBusinessMinutesAfterFiveBeforeFive); 170 } 171 172 public void testBusinessDurationAdditionOverWeekendBreak() throws Exception { 173 Calendar calendar = BusinessCalendar.getCalendar(); 174 calendar.set(Calendar.YEAR, 2005); 175 calendar.set(Calendar.MONTH, Calendar.APRIL); 176 calendar.set(Calendar.DAY_OF_MONTH, 8); 177 calendar.set(Calendar.HOUR_OF_DAY, 16); 178 calendar.set(Calendar.MINUTE, 55); 179 calendar.set(Calendar.SECOND, 0); 180 calendar.set(Calendar.MILLISECOND, 0); 181 Date fiveBeforeFive = calendar.getTime(); 182 183 Date tenBusinessMinutesAfterFiveBeforeFive = 184 businessCalendar.add(fiveBeforeFive, new Duration("10 business minutes")); 185 186 calendar = BusinessCalendar.getCalendar(); 187 calendar.set(Calendar.YEAR, 2005); 188 calendar.set(Calendar.MONTH, Calendar.APRIL); 189 calendar.set(Calendar.DAY_OF_MONTH, 11); 190 calendar.set(Calendar.HOUR_OF_DAY, 9); 191 calendar.set(Calendar.MINUTE, 5); 192 calendar.set(Calendar.SECOND, 0); 193 calendar.set(Calendar.MILLISECOND, 0); 194 Date fiveAfterNineAfterTheWeekend = calendar.getTime(); 195 196 assertEquals(fiveAfterNineAfterTheWeekend, tenBusinessMinutesAfterFiveBeforeFive); 197 } 198 199 public void testBusinessDurationAdditionStartingOutsideBusinessHours() throws Exception { 200 Calendar calendar = BusinessCalendar.getCalendar(); 201 calendar.set(Calendar.YEAR, 2005); 202 calendar.set(Calendar.MONTH, Calendar.APRIL); 203 calendar.set(Calendar.DAY_OF_MONTH, 8); 204 calendar.set(Calendar.HOUR_OF_DAY, 12); 205 calendar.set(Calendar.MINUTE, 15); 206 calendar.set(Calendar.SECOND, 0); 207 calendar.set(Calendar.MILLISECOND, 0); 208 Date quarterPastTwelve = calendar.getTime(); 209 210 Date tenBusinessMinutesAfterQuarterPastTwelve = 211 businessCalendar.add(quarterPastTwelve, new Duration("30 business minutes")); 212 213 calendar = BusinessCalendar.getCalendar(); 214 calendar.set(Calendar.YEAR, 2005); 215 calendar.set(Calendar.MONTH, Calendar.APRIL); 216 calendar.set(Calendar.DAY_OF_MONTH, 8); 217 calendar.set(Calendar.HOUR_OF_DAY, 13); 218 calendar.set(Calendar.MINUTE, 0); 219 calendar.set(Calendar.SECOND, 0); 220 calendar.set(Calendar.MILLISECOND, 0); 221 Date oneOClock = calendar.getTime(); 222 223 assertEquals(oneOClock, tenBusinessMinutesAfterQuarterPastTwelve); 224 } 225 226 public void testBusinessDurationAdditionStartingOutsideBusinessHoursAndOverWeekend() throws Exception { 227 Calendar calendar = BusinessCalendar.getCalendar(); 228 calendar.set(Calendar.YEAR, 2005); 229 calendar.set(Calendar.MONTH, Calendar.APRIL); 230 calendar.set(Calendar.DAY_OF_MONTH, 8); 231 calendar.set(Calendar.HOUR_OF_DAY, 12); 232 calendar.set(Calendar.MINUTE, 15); 233 calendar.set(Calendar.SECOND, 0); 234 calendar.set(Calendar.MILLISECOND, 0); 235 Date quarterPastTwelve = calendar.getTime(); 236 237 Date fiveBusinessHoursAfterQuarterPastTwelve = 238 businessCalendar.add(quarterPastTwelve, new Duration("5 business hours")); 239 240 calendar = BusinessCalendar.getCalendar(); 241 calendar.set(Calendar.YEAR, 2005); 242 calendar.set(Calendar.MONTH, Calendar.APRIL); 243 calendar.set(Calendar.DAY_OF_MONTH, 11); 244 calendar.set(Calendar.HOUR_OF_DAY, 9); 245 calendar.set(Calendar.MINUTE, 30); 246 calendar.set(Calendar.SECOND, 0); 247 calendar.set(Calendar.MILLISECOND, 0); 248 Date halfPastNineAfterTheWeekend = calendar.getTime(); 249 250 assertEquals(halfPastNineAfterTheWeekend, fiveBusinessHoursAfterQuarterPastTwelve); 251 } 252 253 public void testNextDayStart() { 254 Calendar calendar = BusinessCalendar.getCalendar(); 255 calendar.set(Calendar.YEAR, 2005); 256 calendar.set(Calendar.MONTH, Calendar.APRIL); 257 calendar.set(Calendar.DAY_OF_MONTH, 8); 258 calendar.set(Calendar.HOUR_OF_DAY, 13); 259 calendar.set(Calendar.MINUTE, 15); 260 Date justAfterLunchOnAprilEight2005 = calendar.getTime(); 261 Date startOfNextDay = businessCalendar.findStartOfNextDay(justAfterLunchOnAprilEight2005); 262 263 calendar = BusinessCalendar.getCalendar(); 264 calendar.set(Calendar.YEAR, 2005); 265 calendar.set(Calendar.MONTH, Calendar.APRIL); 266 calendar.set(Calendar.DAY_OF_MONTH, 9); 267 calendar.set(Calendar.HOUR_OF_DAY, 0); 268 calendar.set(Calendar.MINUTE, 0); 269 calendar.set(Calendar.SECOND, 0); 270 calendar.set(Calendar.MILLISECOND, 0); 271 assertEquals(calendar.getTime(), startOfNextDay); 272 } 273 274 public void testDayOfWeek() { 275 Calendar calendar = BusinessCalendar.getCalendar(); 276 calendar.set(Calendar.YEAR, 2005); 277 calendar.set(Calendar.MONTH, Calendar.APRIL); 278 calendar.set(Calendar.DAY_OF_MONTH, 8); 279 calendar.set(Calendar.HOUR_OF_DAY, 13); 280 calendar.set(Calendar.MINUTE, 15); 281 Date justAfterLunchOnAprilEight2005 = calendar.getTime(); 282 283 Day day = businessCalendar.findDay(justAfterLunchOnAprilEight2005); 284 assertSame(businessCalendar.weekDays[Calendar.FRIDAY], day); 285 } 286 } 287 | Popular Tags |