| 1 | package net.sourceforge.calendardate; |
| 2 | |
| 3 | import java.io.ByteArrayInputStream; |
| 4 | import java.io.ByteArrayOutputStream; |
| 5 | import java.io.ObjectInputStream; |
| 6 | import java.io.ObjectOutputStream; |
| 7 | import java.util.Calendar; |
| 8 | import java.util.Date; |
| 9 | import java.util.GregorianCalendar; |
| 10 | import java.util.TimeZone; |
| 11 | |
| 12 | import net.sourceforge.calendardate.CalendarDate; |
| 13 | |
| 14 | import junit.framework.TestCase; |
| 15 | |
| 16 | public class CalendarDateTestCase extends TestCase { |
| 17 | |
| 18 | /** |
| 19 | * Constructor for TestDay. |
| 20 | * |
| 21 | * @param name |
| 22 | */ |
| 23 | public CalendarDateTestCase(String name) { |
| 24 | super(name); |
| 25 | } |
| 26 | |
| 27 | public void testGregorianCalendar() { |
| 28 | GregorianCalendar cal = new GregorianCalendar(2002, 0, 1); |
| 29 | assertEquals(0, cal.get(Calendar.MINUTE)); |
| 30 | assertEquals(0, cal.get(Calendar.HOUR)); |
| 31 | |
| 32 | } |
| 33 | |
| 34 | public void testToDate() { |
| 35 | TimeZone sydTimeZone = TimeZone.getTimeZone("Australia/Sydney"); |
| 36 | CalendarDate sep1 = new CalendarDate(2004, 9, 1); |
| 37 | GregorianCalendar cal = new GregorianCalendar(); |
| 38 | cal.clear(); |
| 39 | cal.set(2004, 8, 1); |
| 40 | cal.setTimeZone(sydTimeZone); |
| 41 | |
| 42 | assertEquals(cal.getTime(), sep1.toDate(sydTimeZone)); |
| 43 | |
| 44 | cal.set(2004, 8, 1, 20, 8, 30); // 8 minutes, 30 sec past 8pm |
| 45 | assertEquals(cal.getTime(), sep1.toDate(sydTimeZone, 20, 8, 30)); |
| 46 | } |
| 47 | |
| 48 | public void testSecondOccurrenceOftime() { |
| 49 | TimeZone sydTimeZone = TimeZone.getTimeZone("Australia/Sydney"); |
| 50 | CalendarDate mar28 = new CalendarDate(2004, 3, 28); |
| 51 | GregorianCalendar cal = new GregorianCalendar(); |
| 52 | cal.clear(); |
| 53 | cal.set(2004, 2, 28, 1, 30, 0); // 1:30am on 28 March 2004 |
| 54 | cal.setTimeZone(sydTimeZone); |
| 55 | |
| 56 | assertEquals("Expected the SECOND occurence to be returned by toDate()", addHours(cal, 2), |
| 57 | mar28.toDate(sydTimeZone, 2, 30, 0)); |
| 58 | assertFalse("Expected the SECOND occurence to be returned by toDate()", addHours(cal, 1) |
| 59 | .equals(mar28.toDate(sydTimeZone, 2, 30, 0))); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param cal |
| 64 | * @return |
| 65 | */ |
| 66 | private Date addHours(GregorianCalendar cal, int numHours) { |
| 67 | return new Date(cal.getTime().getTime() + numHours * 60 * 60 * 1000L); |
| 68 | } |
| 69 | |
| 70 | public void testNotLenient() { |
| 71 | checkConstructorThrows(2000, 4, 0); |
| 72 | checkConstructorThrows(2000, 4, 31); |
| 73 | checkConstructorThrows(2000, 0, 4); |
| 74 | checkConstructorThrows(2000, 13, 4); |
| 75 | new CalendarDate(2004, 2, 29); |
| 76 | } |
| 77 | |
| 78 | public void testOutOfRange() { |
| 79 | checkConstructorThrows(1599, 12, 31); |
| 80 | CalendarDate earliest = new CalendarDate(1600, 1, 1); |
| 81 | try { |
| 82 | earliest.addDays(-1); |
| 83 | fail("Was able to create date before earliest"); |
| 84 | } |
| 85 | catch (IllegalArgumentException e) { |
| 86 | // expected |
| 87 | } |
| 88 | |
| 89 | checkConstructorThrows(3000, 1, 1); |
| 90 | CalendarDate latest = new CalendarDate(2999, 12, 31); |
| 91 | try { |
| 92 | latest.addDays(1); |
| 93 | fail("Was able to created date after latest"); |
| 94 | } |
| 95 | catch (IllegalArgumentException e) { |
| 96 | // expected |
| 97 | } |
| 98 | assertEquals(CalendarDate.EARLIEST, earliest); |
| 99 | assertEquals(CalendarDate.LATEST, latest); |
| 100 | } |
| 101 | |
| 102 | private void checkConstructorThrows(int year, int month, int day) { |
| 103 | try { |
| 104 | new CalendarDate(year, month, day); |
| 105 | fail("Invalid year-month-day combination was accepted: " + year + "-" + month + "-" |
| 106 | + day); |
| 107 | } |
| 108 | catch (IllegalArgumentException e) { |
| 109 | // expected |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public void testToDateOutOfRangeThrowsException() { |
| 114 | CalendarDate sep1 = new CalendarDate(2004, 9, 1); |
| 115 | checkToDate(sep1, -1, 0, 0); |
| 116 | checkToDate(sep1, 0, -490, 0); |
| 117 | checkToDate(sep1, 0, 0, -1); |
| 118 | |
| 119 | checkToDate(sep1, 24, 0, 0); |
| 120 | checkToDate(sep1, 0, 60, 0); |
| 121 | checkToDate(sep1, 0, 0, 61); |
| 122 | } |
| 123 | |
| 124 | private void checkToDate(CalendarDate sep1, int hour, int min, int sec) { |
| 125 | TimeZone sydTimeZone = TimeZone.getTimeZone("Australia/Sydney"); |
| 126 | try { |
| 127 | sep1.toDate(sydTimeZone, hour, min, sec); |
| 128 | fail("Should have thrown IllegalArgumentException"); |
| 129 | } |
| 130 | catch (IllegalArgumentException e) { |
| 131 | // Expected |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | public void testSerializable() throws Exception { |
| 136 | CalendarDate sep1 = new CalendarDate(2004, 9, 1); |
| 137 | |
| 138 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 139 | ObjectOutputStream objOut = new ObjectOutputStream(out); |
| 140 | objOut.writeObject(sep1); |
| 141 | objOut.close(); |
| 142 | |
| 143 | byte[] array = out.toByteArray(); |
| 144 | |
| 145 | ByteArrayInputStream in = new ByteArrayInputStream(array); |
| 146 | ObjectInputStream objIn = new ObjectInputStream(in); |
| 147 | |
| 148 | CalendarDate outDate = (CalendarDate) objIn.readObject(); |
| 149 | assertEquals(sep1, outDate); |
| 150 | |
| 151 | } |
| 152 | |
| 153 | public void testDayOfWeek() { |
| 154 | CalendarDate d = new CalendarDate(2004, 6, 11); |
| 155 | assertEquals(6, d.getDayOfWeek()); |
| 156 | } |
| 157 | |
| 158 | public void testConstructor1() { |
| 159 | |
| 160 | CalendarDate d = new CalendarDate(TimeZone.getDefault(), new Date()); |
| 161 | |
| 162 | // What happens if it becomes a new day now? |
| 163 | |
| 164 | GregorianCalendar cal = new GregorianCalendar(); |
| 165 | |
| 166 | assertEquals(cal.get(Calendar.DAY_OF_MONTH), d.getDayOfMonth()); |
| 167 | assertEquals(cal.get(Calendar.DAY_OF_WEEK), d.getDayOfWeek()); |
| 168 | assertEquals(cal.get(Calendar.MONTH) + 1, d.getMonth()); |
| 169 | assertEquals(cal.get(Calendar.YEAR), d.getYear()); |
| 170 | |
| 171 | } |
| 172 | |
| 173 | public void testConstructor2() { |
| 174 | |
| 175 | CalendarDate d = new CalendarDate(TimeZone.getDefault()); |
| 176 | |
| 177 | // What happens if it becomes a new day now? |
| 178 | |
| 179 | GregorianCalendar cal = new GregorianCalendar(); |
| 180 | |
| 181 | assertEquals(cal.get(Calendar.DAY_OF_MONTH), d.getDayOfMonth()); |
| 182 | assertEquals(cal.get(Calendar.DAY_OF_WEEK), d.getDayOfWeek()); |
| 183 | assertEquals(cal.get(Calendar.MONTH) + 1, d.getMonth()); |
| 184 | assertEquals(cal.get(Calendar.YEAR), d.getYear()); |
| 185 | |
| 186 | } |
| 187 | |
| 188 | public void testConstructor3() { |
| 189 | new CalendarDate(2004, 2, 29); |
| 190 | } |
| 191 | |
| 192 | public void testEqualsAndHash() { |
| 193 | CalendarDate sep30 = new CalendarDate(2003, 9, 30); |
| 194 | CalendarDate oct1 = new CalendarDate(2003, 10, 1); |
| 195 | CalendarDate oct1again = new CalendarDate(2003, 10, 1); |
| 196 | CalendarDate oct1following = new CalendarDate(2004, 10, 1); |
| 197 | |
| 198 | assertFalse(sep30.equals(oct1)); |
| 199 | assertTrue(oct1.equals(oct1again)); |
| 200 | assertFalse(oct1following.equals(oct1)); |
| 201 | assertFalse(oct1.equals("Oct1 2004")); |
| 202 | |
| 203 | assertTrue(oct1.hashCode() == oct1again.hashCode()); |
| 204 | } |
| 205 | |
| 206 | public void testToString() { |
| 207 | CalendarDate sep1 = new CalendarDate(2003, 9, 1); |
| 208 | assertEquals("Unexpected String representation", "2003-9-1", sep1.toString()); |
| 209 | } |
| 210 | |
| 211 | public void testMonthsUntil() { |
| 212 | CalendarDate sep30 = new CalendarDate(2003, 9, 30); |
| 213 | CalendarDate oct1 = new CalendarDate(2003, 10, 1); |
| 214 | CalendarDate oct1following = new CalendarDate(2004, 10, 1); |
| 215 | |
| 216 | assertEquals(1, sep30.monthsUntil(oct1)); |
| 217 | assertEquals(-1, oct1.monthsUntil(sep30)); |
| 218 | assertEquals(12, oct1.monthsUntil(oct1following)); |
| 219 | |
| 220 | } |
| 221 | |
| 222 | public void testBefore() { |
| 223 | CalendarDate sep30 = new CalendarDate(2003, 9, 30); |
| 224 | CalendarDate oct1 = new CalendarDate(2003, 10, 1); |
| 225 | CalendarDate oct1again = new CalendarDate(2003, 10, 1); |
| 226 | |
| 227 | assertTrue(sep30.isBefore(oct1)); |
| 228 | assertFalse(oct1.isBefore(oct1again)); |
| 229 | } |
| 230 | |
| 231 | public void testAfter() { |
| 232 | CalendarDate sep30 = new CalendarDate(2003, 9, 30); |
| 233 | CalendarDate oct1 = new CalendarDate(2003, 10, 1); |
| 234 | CalendarDate oct1again = new CalendarDate(2003, 10, 1); |
| 235 | |
| 236 | assertTrue(oct1.isAfter(sep30)); |
| 237 | assertFalse(sep30.isAfter(oct1)); |
| 238 | assertFalse(oct1.isAfter(oct1again)); |
| 239 | } |
| 240 | |
| 241 | public void testDayCreate() { |
| 242 | CalendarDate sep1 = new CalendarDate(2004, 9, 1); |
| 243 | assertEquals(1, sep1.getDayOfMonth()); |
| 244 | assertEquals(9, sep1.getMonth()); |
| 245 | assertEquals(2004, sep1.getYear()); |
| 246 | |
| 247 | CalendarDate sep2 = sep1.addDays(1); |
| 248 | assertEquals(2, sep2.getDayOfMonth()); |
| 249 | assertEquals(9, sep2.getMonth()); |
| 250 | assertEquals(2004, sep2.getYear()); |
| 251 | |
| 252 | assertEquals(1, sep1.daysUntil(sep2)); |
| 253 | assertEquals(0, sep2.daysUntil(sep2)); |
| 254 | |
| 255 | CalendarDate feb1 = new CalendarDate(2000, 2, 1); |
| 256 | assertEquals(2, feb1.getMonth()); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Check our understanding of Calendar |
| 261 | * |
| 262 | */ |
| 263 | public void testCalendar() { |
| 264 | GregorianCalendar cal = new GregorianCalendar(2000, 0, 32); |
| 265 | assertEquals(1, cal.get(Calendar.MONTH)); |
| 266 | } |
| 267 | |
| 268 | public void testNumLeapsToYear() { |
| 269 | assertEquals(0, CalendarDate.numLeapsToYear(4)); |
| 270 | assertEquals(0, CalendarDate.numLeapsToYear(1)); |
| 271 | assertEquals(1, CalendarDate.numLeapsToYear(5)); |
| 272 | assertEquals(CalendarDate.numLeapsToYear(2000) + 1, CalendarDate.numLeapsToYear(2001)); |
| 273 | } |
| 274 | |
| 275 | public void testMany() { |
| 276 | |
| 277 | GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); |
| 278 | int startYear = 1890; |
| 279 | CalendarDate begin = new CalendarDate(startYear, 1, 1); |
| 280 | for (int i = 0; i < (365 * 250); i++) { |
| 281 | cal.set(startYear, 0, 1 + i); |
| 282 | |
| 283 | CalendarDate end = begin.addDays(i); |
| 284 | String detailStr = i + ": " + cal.getTime().toString(); |
| 285 | assertEquals(detailStr, cal.get(Calendar.YEAR), end.getYear()); |
| 286 | assertEquals(detailStr, cal.get(Calendar.MONTH) + 1, end.getMonth()); |
| 287 | assertEquals(detailStr, cal.get(Calendar.DAY_OF_MONTH), end.getDayOfMonth()); |
| 288 | |
| 289 | assertEquals(detailStr, i, begin.daysUntil(end)); |
| 290 | assertEquals(detailStr, -i, end.daysUntil(begin)); |
| 291 | |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | public void testDaysInMonth() { |
| 296 | assertEquals(31, CalendarDate.daysInMonth(2000, 1)); |
| 297 | assertEquals(29, CalendarDate.daysInMonth(2000, 2)); |
| 298 | assertEquals(28, CalendarDate.daysInMonth(2001, 2)); |
| 299 | assertEquals(30, CalendarDate.daysInMonth(2000, 9)); |
| 300 | } |
| 301 | |
| 302 | public void testIsLeapYear() { |
| 303 | assertTrue(CalendarDate.isLeapYear(2000)); |
| 304 | assertFalse(CalendarDate.isLeapYear(1900)); |
| 305 | assertTrue(CalendarDate.isLeapYear(1996)); |
| 306 | assertFalse(CalendarDate.isLeapYear(1997)); |
| 307 | } |
| 308 | |
| 309 | public void testIsOutOfRange() { |
| 310 | TimeZone sydneyTime = TimeZone.getTimeZone("Australia/Sydney"); |
| 311 | GregorianCalendar cal = new GregorianCalendar(sydneyTime); |
| 312 | cal.set(Calendar.YEAR, CalendarDate.EARLIEST.getYear()); |
| 313 | cal.set(Calendar.MONTH, CalendarDate.EARLIEST.getMonth() - 1); |
| 314 | cal.set(Calendar.DAY_OF_MONTH, CalendarDate.EARLIEST.getDayOfMonth()); |
| 315 | cal.set(Calendar.HOUR_OF_DAY, 0); |
| 316 | cal.set(Calendar.MINUTE, 0); |
| 317 | cal.set(Calendar.SECOND, 0); |
| 318 | cal.set(Calendar.MILLISECOND, 0); |
| 319 | |
| 320 | assertFalse(CalendarDate.isOutsideRange(sydneyTime, cal.getTime())); |
| 321 | assertTrue(CalendarDate.isOutsideRange(sydneyTime, millisecondBefore(cal.getTime()))); |
| 322 | |
| 323 | cal.set(Calendar.YEAR, CalendarDate.LATEST.getYear()); |
| 324 | cal.set(Calendar.MONTH, CalendarDate.LATEST.getMonth() - 1); |
| 325 | cal.set(Calendar.DAY_OF_MONTH, CalendarDate.LATEST.getDayOfMonth()); |
| 326 | cal.set(Calendar.HOUR_OF_DAY, 23); |
| 327 | cal.set(Calendar.MINUTE, 59); |
| 328 | cal.set(Calendar.SECOND, 59); // Assume platform doesn't say this year |
| 329 | // has a leap second |
| 330 | cal.set(Calendar.MILLISECOND, 999); |
| 331 | |
| 332 | assertFalse(CalendarDate.isOutsideRange(sydneyTime, cal.getTime())); |
| 333 | assertTrue(CalendarDate.isOutsideRange(sydneyTime, millisecondAfter(cal.getTime()))); |
| 334 | } |
| 335 | |
| 336 | private static Date millisecondBefore(Date d) { |
| 337 | return new Date(d.getTime() - 1); |
| 338 | } |
| 339 | |
| 340 | private static Date millisecondAfter(Date d) { |
| 341 | return new Date(d.getTime() + 1); |
| 342 | } |
| 343 | |
| 344 | public void testAddingMonthsAlsoAddsToYear() throws Exception { |
| 345 | assertEquals(new CalendarDate(2007, 8, 1), new CalendarDate(2005, 11, 1).addMonths(21)); |
| 346 | } |
| 347 | |
| 348 | public void testAddingMonthsReducesDayOfMonthIfNecessary() throws Exception { |
| 349 | assertEquals(new CalendarDate(2005, 2, 28), new CalendarDate(2005, 1, 31).addMonths(1)); |
| 350 | } |
| 351 | |
| 352 | public void testSubtractingOneMonthFromJanuaryGoesBackToDecemberOfPreviousYear() |
| 353 | throws Exception { |
| 354 | assertEquals(new CalendarDate(2004, 12, 1), new CalendarDate(2005, 1, 1).addMonths(-1)); |
| 355 | } |
| 356 | |
| 357 | public void testSubtracting24MonthsGoesBackTwoYears() |
| 358 | throws Exception { |
| 359 | assertEquals(new CalendarDate(2003, 1, 1), new CalendarDate(2005, 1, 1).addMonths(-24)); |
| 360 | } |
| 361 | |
| 362 | } |