| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.types; |
| 34 | | | |
| 35 | | | |
| 36 | | | import java.io.ByteArrayInputStream; |
| 37 | | | import java.io.ByteArrayOutputStream; |
| 38 | | | import java.io.IOException; |
| 39 | | | import java.io.ObjectInputStream; |
| 40 | | | import java.io.ObjectOutputStream; |
| 41 | | | import java.sql.Timestamp; |
| 42 | | | import java.text.ParseException; |
| 43 | | | import java.text.SimpleDateFormat; |
| 44 | | | import junit.framework.TestCase; |
| 45 | | | import org.jcoderz.commons.util.Constants; |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | 100 | (1) | public class DateTest |
| 54 | | | extends TestCase |
| 55 | | | { |
| 56 | | | |
| 57 | | | static final long SOME_TIME_IN_MILLIES = 12345000001L; |
| 58 | | | |
| 59 | | | static final long MAX_DIFF_FOR_NOW = 500L; |
| 60 | | | |
| 61 | | | static final long SOME_TIME_IN_MILLIES_2 = 123456L; |
| 62 | | | |
| 63 | | | |
| 64 | | | public void testFromSqlDate () |
| 65 | | | { |
| 66 | 100 | | final java.sql.Date sqlDate = new java.sql.Date(SOME_TIME_IN_MILLIES); |
| 67 | 100 | | final Date date = new Date(SOME_TIME_IN_MILLIES); |
| 68 | 100 | | assertEquals("Value should not change when created via sql date.", |
| 69 | | | date, Date.fromSqlDate(sqlDate)); |
| 70 | 100 | | } |
| 71 | | | |
| 72 | | | |
| 73 | | | public void testFromSqlTimestamp () |
| 74 | | | { |
| 75 | 100 | | final Timestamp sqlTimestamp = new Timestamp(SOME_TIME_IN_MILLIES); |
| 76 | 100 | | final Date date = new Date(sqlTimestamp.getTime()); |
| 77 | 100 | | assertEquals("Value should not change when created via sql timestamp.", |
| 78 | | | date, Date.fromSqlTimestamp(sqlTimestamp)); |
| 79 | 100 | | } |
| 80 | | | |
| 81 | | | |
| 82 | | | public void testNow () |
| 83 | | | { |
| 84 | 100 | | final Date currentDate = new Date(System.currentTimeMillis()); |
| 85 | 100 | | final long diff = Date.now().getTime() - currentDate.getTime(); |
| 86 | 75 | | assertTrue("Now is to far from Date.now()", diff < MAX_DIFF_FOR_NOW); |
| 87 | 100 | | } |
| 88 | | | |
| 89 | | | |
| 90 | | | public void testNowPlus () |
| 91 | | | { |
| 92 | 100 | | final Date currentDate |
| 93 | | | = new Date(System.currentTimeMillis() + SOME_TIME_IN_MILLIES_2); |
| 94 | 100 | | final long diff |
| 95 | | | = Date.nowPlus(SOME_TIME_IN_MILLIES_2).getTime() |
| 96 | | | - currentDate.getTime(); |
| 97 | 75 | | assertTrue("NowPlus is to far from Date.now() + ...", |
| 98 | | | diff < MAX_DIFF_FOR_NOW); |
| 99 | 100 | | } |
| 100 | | | |
| 101 | | | |
| 102 | | | {@link } |
| 103 | | | |
| 104 | | | public void testPlus () |
| 105 | | | { |
| 106 | 100 | | final long time = System.currentTimeMillis(); |
| 107 | 100 | | final Date date = new Date(time); |
| 108 | 100 | | final Date plusDate = new Date(time + SOME_TIME_IN_MILLIES); |
| 109 | 100 | (2) | assertTrue("plusDate " + plusDate + " is not equals to date.plus() " |
| 110 | | | + date.plus(SOME_TIME_IN_MILLIES), |
| 111 | | | plusDate.equals(date.plus(SOME_TIME_IN_MILLIES))); |
| 112 | 100 | | } |
| 113 | | | |
| 114 | | | |
| 115 | | | {@link } |
| 116 | | | |
| 117 | | | public void testMinus () |
| 118 | | | { |
| 119 | 100 | | final long time = System.currentTimeMillis(); |
| 120 | 100 | | final Date date = new Date(time); |
| 121 | 100 | | final Date minusDate = new Date(time - SOME_TIME_IN_MILLIES); |
| 122 | 100 | (3) | assertTrue("minusDate " + minusDate + " is not equals to date.minus() " |
| 123 | | | + date.plus(SOME_TIME_IN_MILLIES), |
| 124 | | | minusDate.equals(date.minus(SOME_TIME_IN_MILLIES))); |
| 125 | 100 | | } |
| 126 | | | |
| 127 | | | |
| 128 | | | |
| 129 | | | @throws |
| 130 | | | |
| 131 | | | public void testFromString () |
| 132 | | | throws ParseException |
| 133 | | | { |
| 134 | | | |
| 135 | 100 | | final Date currentDate = new Date(System.currentTimeMillis()); |
| 136 | 100 | | final String pattern = "dd.MM.yyyy"; |
| 137 | 100 | | final String date = currentDate.toString(pattern); |
| 138 | 100 | | assertEquals("Empty string should produce null result.", |
| 139 | | | null, Date.fromString("", pattern)); |
| 140 | | | |
| 141 | | | |
| 142 | 100 | | final SimpleDateFormat dateFormat |
| 143 | | | = new SimpleDateFormat(pattern, Constants.SYSTEM_LOCALE); |
| 144 | 100 | | dateFormat.setTimeZone(Date.TIME_ZONE); |
| 145 | 100 | | final Date expected = Date.fromUtilDate(dateFormat.parse(date)); |
| 146 | 100 | | assertEquals("Valid date not parsed correctly.", expected, |
| 147 | | | Date.fromString(date, pattern)); |
| 148 | | | |
| 149 | 100 | | final String time = "2004-09-04T10:04:22.000Z"; |
| 150 | 100 | | final Date timeDate = Date.fromString(time); |
| 151 | 100 | | assertEquals("should be the same string representation", time, |
| 152 | | | timeDate.toString()); |
| 153 | | | |
| 154 | 100 | | assertNull("Should be null for null argument.", Date.fromString(null)); |
| 155 | 100 | | } |
| 156 | | | |
| 157 | | | |
| 158 | | | public void testToString () |
| 159 | | | { |
| 160 | 100 | | assertEquals("For day 0 string representation should be fix.", |
| 161 | | | "1970-01-01T00:00:00.000Z", Date.OLD_DATE.toString()); |
| 162 | 100 | | } |
| 163 | | | |
| 164 | | | |
| 165 | | | public void testToStringWithMillies () |
| 166 | | | { |
| 167 | 100 | | assertEquals("For day 0 string representation should be fix.", |
| 168 | | | "1970-01-01T00:00:00.001Z", new Date(1L).toString()); |
| 169 | 100 | | } |
| 170 | | | |
| 171 | | | |
| 172 | | | public void testDateString () |
| 173 | | | { |
| 174 | 100 | | assertEquals("String representation should be same for default pattern.", |
| 175 | | | "1970-01-01Z", Date.OLD_DATE.toDateString()); |
| 176 | 100 | | } |
| 177 | | | |
| 178 | | | |
| 179 | | | public void testToUtilDate () |
| 180 | | | { |
| 181 | 100 | | final long time = System.currentTimeMillis(); |
| 182 | 100 | | final Date date = new Date(time); |
| 183 | | | |
| 184 | 100 | | assertEquals("Util Date differs from Date.", |
| 185 | | | new java.util.Date(time).getTime(), date.toUtilDate().getTime()); |
| 186 | 100 | | } |
| 187 | | | |
| 188 | | | |
| 189 | | | public void testToSqlDate () |
| 190 | | | { |
| 191 | 100 | | final long time = System.currentTimeMillis(); |
| 192 | 100 | | final Date date = new Date(time); |
| 193 | | | |
| 194 | 100 | | assertEquals("Sql Date differs from Date.", |
| 195 | | | new java.sql.Date(time).getTime(), date.toSqlDate().getTime()); |
| 196 | 100 | | } |
| 197 | | | |
| 198 | | | |
| 199 | | | public void testToSqlTimestamp () |
| 200 | | | { |
| 201 | 100 | | final long time = System.currentTimeMillis(); |
| 202 | 100 | | final Date date = new Date(time); |
| 203 | | | |
| 204 | 100 | | assertEquals("Sql timestamp differs from Date.", |
| 205 | | | time, date.toSqlTimestamp().getTime()); |
| 206 | 100 | | } |
| 207 | | | |
| 208 | | | |
| 209 | | | public void testEquals () |
| 210 | | | { |
| 211 | 100 | | final long time = System.currentTimeMillis(); |
| 212 | 100 | | final Date date = new Date(time); |
| 213 | 100 | | Date date2 = new Date(time); |
| 214 | 100 | | final Date date3 = new Date(SOME_TIME_IN_MILLIES); |
| 215 | 100 | | assertEquals("Date equals created wrong result.", |
| 216 | | | true, date.equals(date2)); |
| 217 | 100 | | assertEquals("Date equals created wrong result.", |
| 218 | | | false, date.equals(date3)); |
| 219 | 100 | | date2 = new Date (SOME_TIME_IN_MILLIES - 1); |
| 220 | 100 | | assertEquals("Date equals created wrong result.", |
| 221 | | | false, date.equals(date2)); |
| 222 | 100 | | } |
| 223 | | | |
| 224 | | | |
| 225 | | | public void testCompareTo () |
| 226 | | | { |
| 227 | 100 | | final long time = System.currentTimeMillis(); |
| 228 | 100 | | final Date date = new Date(time); |
| 229 | 100 | | Date date2 = new Date(time); |
| 230 | 100 | | assertEquals("Date comparison created wrong result.", |
| 231 | | | 0, date.compareTo(date2)); |
| 232 | | | |
| 233 | 100 | | final Date date3 = new Date(time - SOME_TIME_IN_MILLIES_2); |
| 234 | 100 | | assertEquals("Date comparison created wrong result.", |
| 235 | | | 1, date.compareTo(date3)); |
| 236 | | | |
| 237 | 100 | | date2 = new Date (time + SOME_TIME_IN_MILLIES_2); |
| 238 | 100 | | assertEquals("Date comparison created wrong result.", |
| 239 | | | -1, date.compareTo(date2)); |
| 240 | 100 | | } |
| 241 | | | |
| 242 | | | |
| 243 | | | |
| 244 | | | @throws |
| 245 | | | @throws |
| 246 | | | |
| 247 | | | public void testSerialize () |
| 248 | | | throws IOException, ClassNotFoundException |
| 249 | | | { |
| 250 | 100 | | final ByteArrayOutputStream bOut = new ByteArrayOutputStream(); |
| 251 | 100 | | final ObjectOutputStream objOut = new ObjectOutputStream(bOut); |
| 252 | | | ByteArrayInputStream bIn; |
| 253 | | | ObjectInputStream objIn; |
| 254 | | | |
| 255 | 100 | | final Date date = Date.now(); |
| 256 | | | |
| 257 | 100 | | objOut.writeObject(date); |
| 258 | 100 | | objOut.flush(); |
| 259 | 100 | | bIn = new ByteArrayInputStream(bOut.toByteArray()); |
| 260 | 100 | | objIn = new ObjectInputStream(bIn); |
| 261 | 100 | | final Date dateRead = (Date) objIn.readObject(); |
| 262 | | | |
| 263 | 100 | | assertEquals("Value changed during serialization.", date, dateRead); |
| 264 | 100 | | } |
| 265 | | | |
| 266 | | | |
| 267 | | | public void testSqlTimestamp () |
| 268 | | | { |
| 269 | 100 | | final Date refDate = Date.now(); |
| 270 | 100 | | final Timestamp test = new Timestamp(refDate.getTime()); |
| 271 | | | |
| 272 | 100 | | assertEquals("Refdate changed in timestamp representation.", |
| 273 | | | refDate.getTime(), test.getTime()); |
| 274 | | | |
| 275 | 100 | | final Date testDate = Date.fromSqlTimestamp(test); |
| 276 | | | |
| 277 | 100 | | assertEquals("Timestamp Value changed within Date type conversion.", |
| 278 | | | test, testDate.toSqlTimestamp()); |
| 279 | 100 | | } |
| 280 | | | |
| 281 | | | |
| 282 | | | public void testSqlTimestampWithNanos () |
| 283 | | | { |
| 284 | 100 | | final Date refDate = Date.now(); |
| 285 | 100 | | final Timestamp test = new Timestamp(refDate.getTime()); |
| 286 | | | |
| 287 | 100 | | test.setNanos(test.getNanos() + 1); |
| 288 | | | |
| 289 | 100 | | assertEquals("Refdate changed in timestamp representation.", |
| 290 | | | refDate.getTime(), test.getTime()); |
| 291 | | | |
| 292 | 100 | | final Date testDate = Date.fromSqlTimestamp(test); |
| 293 | 100 | | test.setNanos(test.getNanos() - 1); |
| 294 | | | |
| 295 | 100 | | assertEquals("Timestamp Value changed within Date type conversion.", |
| 296 | | | test, testDate.toSqlTimestamp()); |
| 297 | 100 | | } |
| 298 | | | |
| 299 | | | {@link } |
| 300 | | | public void testGetDaysSinceEpoch () |
| 301 | | | { |
| 302 | 100 | | final int days = Date.getDaysSinceEpoch(); |
| 303 | 75 | | assertTrue("Result must be positive but was " + days, days > 0); |
| 304 | 100 | | } |
| 305 | | | |
| 306 | | | {@link } |
| 307 | | | public void testGetDaysSinceEpochDate () |
| 308 | | | { |
| 309 | 100 | | final int days = Date.getDaysSinceEpoch(Date.now()); |
| 310 | 75 | | assertTrue("Result must be positive but was " + days, days > 0); |
| 311 | 100 | | assertEquals("No days passed.", 0, Date.getDaysSinceEpoch(new Date(0))); |
| 312 | 100 | | } |
| 313 | | | |
| 314 | | | |
| 315 | | | {@link } |
| 316 | | | |
| 317 | | | public void testHashCode () |
| 318 | | | { |
| 319 | 100 | | assertEquals("two dates with the vaue should have the same " |
| 320 | | | + "hashCode", new Date(SOME_TIME_IN_MILLIES).hashCode(), |
| 321 | | | new Date(SOME_TIME_IN_MILLIES).hashCode()); |
| 322 | 100 | | } |
| 323 | | | |
| 324 | | | |
| 325 | | | {@link } |
| 326 | | | |
| 327 | | | public void testElapsed () |
| 328 | | | { |
| 329 | 100 | | final Date currentDate = new Date(System.currentTimeMillis()); |
| 330 | 100 | | final long diff = currentDate.elapsedMillis(); |
| 331 | 75 | | assertTrue("Diff is to far from Date.now()", diff < MAX_DIFF_FOR_NOW); |
| 332 | 75 | | assertTrue("Diff is negative.", diff >= 0); |
| 333 | 100 | | } |
| 334 | | | |
| 335 | | | |
| 336 | | | {@link } |
| 337 | | | |
| 338 | | | public void testElapsedDate () |
| 339 | | | { |
| 340 | 100 | | final Date currentDate = new Date(System.currentTimeMillis()); |
| 341 | 100 | | final long diff = currentDate.elapsedMillis( |
| 342 | | | new Date(currentDate.getTime() + SOME_TIME_IN_MILLIES_2)); |
| 343 | 100 | | assertEquals("Diff is wrong", SOME_TIME_IN_MILLIES_2, diff); |
| 344 | 100 | | } |
| 345 | | | |
| 346 | | | |
| 347 | | | public void testAfter () |
| 348 | | | { |
| 349 | 100 | | final long time = System.currentTimeMillis(); |
| 350 | 100 | | final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2); |
| 351 | 100 | | final Date date2 = new Date(time); |
| 352 | 100 | | final Date date3 = new Date(time); |
| 353 | 100 | | final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2); |
| 354 | 100 | | assertTrue("After comparison result unexpected (this < other)", |
| 355 | | | date2.after(date1)); |
| 356 | 100 | | assertFalse("After comparison result unexpected (this == other)", |
| 357 | | | date2.after(date3)); |
| 358 | 100 | | assertFalse("After comparison result unexpected (this > other)", |
| 359 | | | date2.after(date4)); |
| 360 | 100 | | } |
| 361 | | | |
| 362 | | | |
| 363 | | | public void testAfterOrEqual () |
| 364 | | | { |
| 365 | 100 | | final long time = System.currentTimeMillis(); |
| 366 | 100 | | final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2); |
| 367 | 100 | | final Date date2 = new Date(time); |
| 368 | 100 | | final Date date3 = new Date(time); |
| 369 | 100 | | final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2); |
| 370 | 100 | | assertTrue("AfterOrEqual comparison result unexpected (this < other)", |
| 371 | | | date2.afterOrEqual(date1)); |
| 372 | 100 | | assertTrue("AfterOrEqual comparison result unexpected (this == other)", |
| 373 | | | date2.afterOrEqual(date3)); |
| 374 | 100 | | assertFalse("AfterOrEqual comparison result unexpected (this > other)", |
| 375 | | | date2.afterOrEqual(date4)); |
| 376 | 100 | | } |
| 377 | | | |
| 378 | | | |
| 379 | | | public void testBefore () |
| 380 | | | { |
| 381 | 100 | | final long time = System.currentTimeMillis(); |
| 382 | 100 | | final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2); |
| 383 | 100 | | final Date date2 = new Date(time); |
| 384 | 100 | | final Date date3 = new Date(time); |
| 385 | 100 | | final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2); |
| 386 | 100 | | assertFalse("Before comparison result unexpected (this < other)", |
| 387 | | | date2.before(date1)); |
| 388 | 100 | | assertFalse("Before comparison result unexpected (this == other)", |
| 389 | | | date2.before(date3)); |
| 390 | 100 | | assertTrue("Before comparison result unexpected (this > other)", |
| 391 | | | date2.before(date4)); |
| 392 | 100 | | } |
| 393 | | | |
| 394 | | | |
| 395 | | | public void testBeforeOrEqual () |
| 396 | | | { |
| 397 | 100 | | final long time = System.currentTimeMillis(); |
| 398 | 100 | | final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2); |
| 399 | 100 | | final Date date2 = new Date(time); |
| 400 | 100 | | final Date date3 = new Date(time); |
| 401 | 100 | | final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2); |
| 402 | 100 | | assertFalse("BeforeOrEqual comparison result unexpected (this < other)", |
| 403 | | | date2.beforeOrEqual(date1)); |
| 404 | 100 | | assertTrue("BeforeOrEqual comparison result unexpected (this == other)", |
| 405 | | | date2.beforeOrEqual(date3)); |
| 406 | 100 | | assertTrue("BeforeOrEqual comparison result unexpected (this > other)", |
| 407 | | | date2.beforeOrEqual(date4)); |
| 408 | 100 | | } |
| 409 | | | |
| 410 | | | |
| 411 | | | public void testEarliest () |
| 412 | | | { |
| 413 | 100 | | final long time = System.currentTimeMillis(); |
| 414 | 100 | | final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2); |
| 415 | 100 | | final Date date2 = new Date(time); |
| 416 | 100 | | final Date date3 = new Date(time); |
| 417 | 100 | | final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2); |
| 418 | 100 | | assertEquals("Earliest result unexpected (a < b)", date1, |
| 419 | | | Date.earliest(date1, date2)); |
| 420 | 100 | | assertEquals("Earliest result unexpected (a == b)", date2, |
| 421 | | | Date.earliest(date2, date3)); |
| 422 | 100 | | assertEquals("Earliest result unexpected (a > b)", date2, |
| 423 | | | Date.earliest(date4, date2)); |
| 424 | 100 | | } |
| 425 | | | |
| 426 | | | |
| 427 | | | public void testLatest () |
| 428 | | | { |
| 429 | 100 | | final long time = System.currentTimeMillis(); |
| 430 | 100 | | final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2); |
| 431 | 100 | | final Date date2 = new Date(time); |
| 432 | 100 | | final Date date3 = new Date(time); |
| 433 | 100 | | final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2); |
| 434 | 100 | | assertEquals("Latest result unexpected (a < b)", date2, |
| 435 | | | Date.latest(date1, date2)); |
| 436 | 100 | | assertEquals("Latest result unexpected (a == b)", date2, |
| 437 | | | Date.latest(date2, date3)); |
| 438 | 100 | | assertEquals("Latest result unexpected (a > b)", date4, |
| 439 | | | Date.latest(date2, date4)); |
| 440 | 100 | | } |
| 441 | | | } |