1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package test.sql; 24 25 import java.sql.Date; 26 import java.sql.Time; 27 import java.sql.Timestamp; 28 import java.time.Instant; 29 import java.time.LocalDateTime; 30 import java.util.Calendar; 31 import static org.testng.Assert.*; 32 import org.testng.annotations.AfterClass; 33 import org.testng.annotations.AfterMethod; 34 import org.testng.annotations.BeforeClass; 35 import org.testng.annotations.BeforeMethod; 36 import org.testng.annotations.Test; 37 38 public class TimestampTests { 39 40 public TimestampTests() { 41 } 42 43 @BeforeClass 44 public static void setUpClass() throws Exception { 45 } 46 47 @AfterClass 48 public static void tearDownClass() throws Exception { 49 } 50 51 @BeforeMethod 52 public void setUpMethod() throws Exception { 53 } 54 55 @AfterMethod 56 public void tearDownMethod() throws Exception { 57 } 58 59 /** 60 * Validate an IllegalArgumentException is thrown for an invalid Timestamp 61 */ 62 @Test(expectedExceptions = IllegalArgumentException.class) 63 public void testInvalid_timestamp() throws Exception { 64 String testTS = "2009-11-01-01 10:50"; 65 Timestamp.valueOf(testTS); 66 } 67 68 /** 69 * Validate an IllegalArgumentException is thrown for an invalid Timestamp 70 */ 71 @Test(expectedExceptions = IllegalArgumentException.class) 72 public void testInvalid_year2() throws Exception { 73 String testTS = "aaaa-11-01-01 10:50"; 74 Timestamp.valueOf(testTS); 75 } 76 77 /** 78 * Validate an IllegalArgumentException is thrown for an invalid Timestamp 79 */ 80 @Test(expectedExceptions = IllegalArgumentException.class) 81 public void testInvalid_year3() throws Exception { 82 String testTS = "aaaa-11-01 10:50"; 83 Timestamp.valueOf(testTS); 84 } 85 86 /** 87 * Validate that two Timestamp are equal when the leading 0 in seconds is 88 * omitted 89 */ 90 @Test 91 public void test1() throws Exception { 92 String testTS = "2009-01-01 10:50:00"; 93 String ExpectedTS = "2009-01-01 10:50:0"; 94 Timestamp ts = Timestamp.valueOf(testTS); 95 Timestamp ts2 = Timestamp.valueOf(ExpectedTS); 96 assertEquals(ts, ts2, "Error ts1 != ts2"); 97 } 98 99 /** 100 * Validate two Timestamps created from the same string are equal 101 */ 102 @Test 103 public void test2() throws Exception { 104 String testTS = "2009-01-01 10:50:0"; 105 Timestamp ts = Timestamp.valueOf(testTS); 106 Timestamp ts2 = Timestamp.valueOf(testTS); 107 assertEquals(ts, ts2, "Error ts1 != ts2"); 108 } 109 110 /** 111 * Validate that two Timestamp values one with leading 0s for month and day 112 * equals same string without the leading 0s. 113 */ 114 @Test 115 public void test3() throws Exception { 116 String testTS = "2009-1-1 10:50:0"; 117 String ExpectedTS = "2009-01-01 10:50:0"; 118 Timestamp ts = Timestamp.valueOf(testTS); 119 Timestamp ts2 = Timestamp.valueOf(ExpectedTS); 120 assertEquals(ts, ts2, "Error ts1 != ts2"); 121 } 122 123 /** 124 * Validate that two Timestamp values one with leading 0s for day omitted 125 * are equal 126 */ 127 @Test 128 public void test4() throws Exception { 129 String testTS = "2009-01-1 10:50:0"; 130 String ExpectedTS = "2009-01-01 10:50:0"; 131 Timestamp ts = Timestamp.valueOf(testTS); 132 Timestamp ts2 = Timestamp.valueOf(ExpectedTS); 133 assertEquals(ts, ts2, "Error ts1 != ts2"); 134 } 135 136 /** 137 * Validate that two Timestamp values one with leading 0s for month omitted 138 * and both with leading 0s for seconds omitted are equal 139 */ 140 @Test 141 public void test5() throws Exception { 142 String testTS = "2009-1-01 10:50:0"; 143 String ExpectedTS = "2009-01-01 10:50:0"; 144 Timestamp ts = Timestamp.valueOf(testTS); 145 Timestamp ts2 = Timestamp.valueOf(ExpectedTS); 146 assertEquals(ts, ts2, "Error ts1 != ts2"); 147 } 148 149 /** 150 * Validate that two Timestamp values one with leading 0s for month omitted 151 */ 152 @Test 153 public void test6() throws Exception { 154 String testTS = "2005-1-01 10:20:50.00"; 155 String ExpectedTS = "2005-01-01 10:20:50.00"; 156 Timestamp ts = Timestamp.valueOf(testTS); 157 Timestamp ts2 = Timestamp.valueOf(ExpectedTS); 158 assertEquals(ts, ts2, "Error ts1 != ts2"); 159 } 160 161 /** 162 * Validate that two Timestamp values one created using valueOf and another 163 * via a constructor are equal 164 */ 165 @Test 166 public void test7() { 167 168 Timestamp ts1 = Timestamp.valueOf("1996-12-13 14:15:25.001"); 169 Timestamp ts2 = new Timestamp(96, 11, 13, 14, 15, 25, 1000000); 170 assertTrue(ts1.equals(ts2), "Error ts1 != ts2"); 171 } 172 173 /** 174 * Validate that two Timestamp values one created using valueOf and another 175 * via a constructor are equal 176 */ 177 @Test 178 public void test8() { 179 Timestamp ts1 = Timestamp.valueOf("1996-12-13 14:15:25.001"); 180 Timestamp ts2 = new Timestamp(ts1.getTime()); 181 assertTrue(ts1.equals(ts2), "Error ts1 != ts2"); 182 } 183 184 /** 185 * Validate that two Timestamp values one created using valueOf and another 186 * via a constructor are equal 187 */ 188 @Test 189 public void test9() { 190 191 Timestamp ts1 = Timestamp.valueOf("1996-12-13 14:15:25.0"); 192 Timestamp ts2 = new Timestamp(96, 11, 13, 14, 15, 25, 0); 193 assertTrue(ts1.equals(ts2), "Error ts1 != ts2"); 194 } 195 196 /** 197 * Validate that a Timestamp cannot be equal to null 198 */ 199 @Test 200 public void test10() { 201 202 Timestamp ts1 = Timestamp.valueOf("1961-08-30 14:15:25.745634"); 203 assertFalse(ts1.equals(null), "Error ts1 == null"); 204 } 205 206 /** 207 * Validate that a Timestamp is equal to another timestamp created with the 208 * using the same value but not equal to a Timestamp which is one day later 209 */ 210 @Test 211 public void test11() { 212 213 Timestamp ts1 = Timestamp.valueOf("1996-12-10 12:26:19.12"); 214 Timestamp ts2 = Timestamp.valueOf("1996-12-10 12:26:19.12"); 215 Timestamp ts3 = Timestamp.valueOf("1996-12-11 12:24:19.12"); 216 assertTrue(ts1.equals(ts2) && ts2.equals(ts1), "Error ts1 != ts2"); 217 assertFalse(ts1.equals(ts3) && ts3.equals(ts1), "Error ts1 == ts3"); 218 219 } 220 221 /** 222 * Validate that a Timestamp is equal to itself 223 */ 224 @Test 225 public void test12() { 226 Timestamp ts1 = Timestamp.valueOf("1996-10-15 12:26:19.12"); 227 assertTrue(ts1.equals(ts1), "Error ts1 != ts1"); 228 } 229 230 /** 231 * Validate that two Timestamps are equal when one is created from the 232 * toString() of the other 233 */ 234 @Test 235 public void test13() { 236 Timestamp ts1 = Timestamp.valueOf("1996-12-10 12:26:19.12"); 237 Timestamp ts2 = Timestamp.valueOf(ts1.toString()); 238 assertTrue(ts1.equals(ts2) && ts2.equals(ts1), "Error ts1 != ts2"); 239 } 240 241 // Before Tests 242 /** 243 * Validate that Timestamp ts1 is before Timestamp ts2 244 */ 245 @Test 246 public void test14() { 247 Timestamp ts1 = Timestamp.valueOf("1996-12-13 14:15:25.745634"); 248 Timestamp ts2 = Timestamp.valueOf("1996-12-13 15:15:25.645634"); 249 assertTrue(ts1.before(ts2), "Error ts1 not before ts2"); 250 } 251 252 /** 253 * Validate that Timestamp ts1 is before Timestamp ts2 254 */ 255 @Test 256 public void test15() { 257 Timestamp ts1 = Timestamp.valueOf("1961-08-30 14:15:25"); 258 Timestamp ts2 = Timestamp.valueOf("1999-12-13 15:15:25"); 259 assertTrue(ts1.before(ts2), "Error ts1 not before ts2"); 260 } 261 262 /** 263 * Validate that Timestamp ts1 is before Timestamp ts2 264 */ 265 @Test 266 public void test16() { 267 268 Timestamp ts1 = Timestamp.valueOf("1999-12-13 14:15:25.745634"); 269 Timestamp ts2 = Timestamp.valueOf("1999-11-13 15:15:25.645634"); 270 assertFalse(ts1.before(ts2), "Error ts1 before ts2"); 271 } 272 273 /* 274 * Validate that a NullPointerException is thrown if a null is passed to 275 * the before method 276 */ 277 @Test(expectedExceptions = NullPointerException.class) 278 public void test17() throws Exception { 279 Timestamp ts1 = Timestamp.valueOf("1996-12-13 14:15:25.745634"); 280 ts1.before(null); 281 } 282 283 /* 284 * Validate a Timestamp cannot be before itself 285 */ 286 @Test 287 public void test18() { 288 Timestamp ts1 = Timestamp.valueOf("1999-11-10 12:26:19.3456543"); 289 assertFalse(ts1.before(ts1), "Error ts1 before ts1!"); 290 } 291 292 /** 293 * Create 3 Timestamps and make sure the 1st is before the other two 294 * Timestamps which are each greater than the one before it 295 */ 296 @Test 297 public void test19() { 298 299 Timestamp ts1 = new Timestamp(1234560000); 300 Timestamp ts2 = new Timestamp(1234567000); 301 Timestamp ts3 = new Timestamp(1234569000); 302 assertTrue(ts1.before(ts2) && ts2.before(ts3) && ts1.before(ts3)); 303 } 304 305 /** 306 * Validate that Timestamp ts1 is not after Timestamp ts2 307 */ 308 @Test 309 public void test20() { 310 Timestamp ts1 = Timestamp.valueOf("1999-12-13 14:15:25.745634"); 311 Timestamp ts2 = Timestamp.valueOf("1999-12-13 15:15:25.645634"); 312 assertFalse(ts1.after(ts2), "Error ts1 is after ts2"); 313 314 } 315 316 /** 317 * Validate that Timestamp ts1 is after Timestamp ts2 318 */ 319 @Test 320 public void test21() { 321 Timestamp ts1 = Timestamp.valueOf("1996-12-13 14:15:25.745634"); 322 Timestamp ts2 = Timestamp.valueOf("1996-11-13 15:15:25.645634"); 323 assertTrue(ts1.after(ts2), "Error ts1 not after ts2"); 324 } 325 326 /** 327 * Validate that a NullPointerException is thrown if a null is passed to the 328 * after method 329 */ 330 @Test(expectedExceptions = NullPointerException.class) 331 public void test22() throws Exception { 332 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 333 ts1.after(null); 334 } 335 336 /** 337 * Validate that a Timestamp cannot be after itself 338 */ 339 @Test 340 public void test23() { 341 Timestamp ts1 = Timestamp.valueOf("1999-11-10 12:26:19.3456543"); 342 assertFalse(ts1.after(ts1), "Error ts1 is after itself"); 343 } 344 /** 345 * Validate that a Timestamp after() works correctly with Timestamp 346 * created using milliseconds 347 */ 348 @Test 349 public void test24() { 350 351 Timestamp ts1 = new Timestamp(1234568000); 352 Timestamp ts2 = new Timestamp(1234565000); 353 Timestamp ts3 = new Timestamp(1234562000); 354 assertTrue(ts1.after(ts2) && ts2.after(ts3) && ts1.after(ts3)); 355 } 356 357 /** 358 * Validate compareTo returns 0 for Timestamps that are the same 359 */ 360 @Test 361 public void test25() { 362 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 363 Timestamp ts2 = new Timestamp(ts1.getTime()); 364 assertTrue(ts1.compareTo(ts2) == 0, "Error ts1 != ts2"); 365 } 366 367 /** 368 * Validate compareTo returns -1 for when the 1st Timestamp is earlier than 369 * the 2nd Timestamp 370 */ 371 @Test 372 public void test26() { 373 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 374 Timestamp ts2 = new Timestamp(ts1.getTime() + 1000); 375 assertTrue(ts1.compareTo(ts2) == -1, "Error ts1 not before ts2"); 376 assertTrue(ts2.compareTo(ts1) == 1, "Error ts1 is not before ts2"); 377 } 378 379 /** 380 * Validate compareTo returns 1 for when the 1st Timestamp is later than the 381 * 2nd Timestamp 382 */ 383 @Test 384 public void test27() { 385 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 386 Timestamp ts2 = new Timestamp(ts1.getTime() - 1000); 387 assertTrue(ts1.compareTo(ts2) == 1, "Error ts1 not after ts2"); 388 assertTrue(ts2.compareTo(ts1) == -1, "Error ts1 not after ts2"); 389 } 390 391 /** 392 * Validate compareTo returns 0 for Timestamps that are the same 393 */ 394 @Test 395 public void test28() { 396 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 397 java.util.Date ts2 = new java.util.Date(ts1.getTime()); 398 assertTrue(ts1.compareTo(ts2) == 0, "Error ts1 != ts2"); 399 } 400 401 /** 402 * Validate compareTo returns 0 for Timestamps that are the same 403 */ 404 @Test 405 public void test29() { 406 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 407 java.util.Date d = new java.util.Date(ts1.getTime()); 408 assertFalse(ts1.equals(d), "Error ts1 == d"); 409 } 410 411 /** 412 * Validate compareTo returns 0 for Timestamps that are the same 413 */ 414 @Test 415 public void test30() { 416 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 417 java.util.Date d = new Timestamp(ts1.getTime()); 418 assertTrue(ts1.equals(d), "Error ts1 != d"); 419 } 420 421 /** 422 * Validate equals returns false when a Date object is passed to equals 423 */ 424 @Test 425 public void test31() { 426 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 427 Date d = new Date(ts1.getTime()); 428 assertFalse(ts1.equals(d), "Error ts1 != d"); 429 } 430 431 /** 432 * Validate equals returns false when a Date object is passed to equals 433 */ 434 @Test 435 public void test32() { 436 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 437 java.util.Date d = new Date(ts1.getTime()); 438 assertFalse(ts1.equals(d), "Error ts1 != d"); 439 } 440 441 /** 442 * Validate equals returns false when a Time object is passed to equals 443 */ 444 @Test 445 public void test33() { 446 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 447 Time t1 = new Time(ts1.getTime()); 448 assertFalse(ts1.equals(t1), "Error ts1 == t1"); 449 } 450 451 /** 452 * Validate equals returns false when a String object is passed to equals 453 */ 454 @Test 455 public void test34() { 456 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 457 assertFalse(ts1.equals("1966-08-30 08:08:08"), "Error ts1 == a String"); 458 } 459 460 /** 461 * Validate getTime() returns the same value from 2 timeStamps created by 462 */ 463 @Test 464 public void test35() { 465 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 466 Timestamp ts2 = Timestamp.valueOf("1966-08-30 08:08:08"); 467 assertTrue(ts2.getTime() == ts1.getTime(), 468 "ts1.getTime() != ts2.getTime()"); 469 assertTrue(ts1.equals(ts2), "Error ts1 != ts2"); 470 } 471 472 /** 473 * Validate getTime() returns the same value from 2 timeStamps when 474 * setTime() is used to specify the same value for both Timestamps 475 */ 476 @Test 477 public void test36() { 478 Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08"); 479 Timestamp ts2 = Timestamp.valueOf("1961-08-30 00:00:00"); 480 ts2.setTime(ts1.getTime()); 481 assertTrue(ts2.getTime() == ts1.getTime(), 482 "ts1.getTime() != ts2.getTime()"); 483 assertTrue(ts1.equals(ts2), "Error ts1 != ts2"); 484 } 485 486 /** 487 * Validate an IllegalArgumentException is thrown for an invalid nanos value 488 */ 489 @Test(expectedExceptions = IllegalArgumentException.class) 490 public void test38() throws Exception { 491 Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00"); 492 ts1.setNanos(-1); 493 494 } 495 496 /** 497 * Validate an IllegalArgumentException is thrown for an invalid nanos value 498 */ 499 @Test(expectedExceptions = IllegalArgumentException.class) 500 public void test39() throws Exception { 501 int nanos = 999999999; 502 Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00"); 503 ts1.setNanos(nanos + 1); 504 505 } 506 507 /** 508 * Validate you can set nanos to 999999999 509 */ 510 @Test 511 public void test40() throws Exception { 512 int nanos = 999999999; 513 Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00"); 514 ts1.setNanos(nanos); 515 assertTrue(ts1.getNanos() == nanos, "Error Invalid Nanos value"); 516 517 } 518 519 /** 520 * Validate you can set nanos to 0 521 */ 522 @Test 523 public void test41() throws Exception { 524 int nanos = 0; 525 Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00"); 526 ts1.setNanos(nanos); 527 assertTrue(ts1.getNanos() == nanos, "Error Invalid Nanos value"); 528 529 } 530 531 /** 532 * Validate that a Timestamp made from a LocalDateTime are equal 533 */ 534 @Test 535 public void test42() throws Exception { 536 Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00"); 537 LocalDateTime ldt = ts1.toLocalDateTime(); 538 Timestamp ts2 = Timestamp.valueOf(ldt); 539 assertTrue(ts1.equals(ts2), "Error ts1 != ts2"); 540 } 541 542 /** 543 * Validate that a Timestamp LocalDateTime value, made from a LocalDateTime 544 * are equal 545 */ 546 @Test 547 public void test43() throws Exception { 548 LocalDateTime ldt = LocalDateTime.now(); 549 Timestamp ts2 = Timestamp.valueOf(ldt); 550 assertTrue(ldt.equals(ts2.toLocalDateTime()), 551 "Error LocalDateTime values are not equal"); 552 } 553 554 /** 555 * Validate an NPE occurs when a null LocalDateTime is passed to valueOF 556 */ 557 @Test(expectedExceptions = NullPointerException.class) 558 public void test44() throws Exception { 559 LocalDateTime ldt = null; 560 Timestamp.valueOf(ldt); 561 } 562 563 /** 564 * Validate that a Timestamp made from a Instant are equal 565 */ 566 @Test 567 public void test45() throws Exception { 568 Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00"); 569 Instant instant = ts1.toInstant(); 570 Timestamp ts2 = Timestamp.from(instant); 571 assertTrue(ts1.equals(ts2), "Error ts1 != ts2"); 572 } 573 574 /** 575 * Validate that a Timestamp made from a Instant are equal 576 */ 577 @Test 578 public void test46() throws Exception { 579 Instant instant = Instant.now(); 580 Timestamp ts2 = Timestamp.from(instant); 581 assertTrue(instant.equals(ts2.toInstant()), 582 "Error Instant values do not match"); 583 } 584 585 /** 586 * Validate an NPE occurs when a null instant is passed to from 587 */ 588 @Test(expectedExceptions = NullPointerException.class) 589 public void test47() throws Exception { 590 Instant instant = null; 591 Timestamp.from(instant); 592 } 593 594 // Added SQE tests 595 /** 596 * Create a Timestamp and a 2nd Timestamp that is 1 month earlier and 597 * validate that it is not before or after the original Timestamp 598 */ 599 @Test 600 public void test48() { 601 Calendar cal = Calendar.getInstance(); 602 Timestamp ts1 = new Timestamp(System.currentTimeMillis()); 603 cal.setTimeInMillis(ts1.getTime()); 604 cal.add(Calendar.MONTH, -1); 605 cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); 606 Timestamp ts2 = new Timestamp(cal.getTimeInMillis()); 607 assertFalse(ts1.before(ts2) || ts2.after(ts1)); 608 } 609 610 /** 611 * Create two Timestamps and validate that compareTo returns 1 to indicate 612 * the 1st Timestamp is greater than the 2nd Timestamp 613 */ 614 @Test 615 public void test49() { 616 Calendar cal = Calendar.getInstance(); 617 Timestamp ts1 = new Timestamp(System.currentTimeMillis()); 618 cal.setTimeInMillis(ts1.getTime()); 619 cal.add(Calendar.MONTH, -1); 620 cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); 621 Timestamp ts2 = new Timestamp(cal.getTimeInMillis()); 622 assertTrue(ts1.compareTo(ts2) == 1); 623 } 624 625 /** 626 * Create two Timestamps and validate that the 1st Timestamp is not equal to 627 * the 2nd Timestamp but equal to itself 628 */ 629 @Test 630 public void test50() { 631 Calendar cal = Calendar.getInstance(); 632 Timestamp ts1 = new Timestamp(System.currentTimeMillis()); 633 cal.setTimeInMillis(ts1.getTime()); 634 cal.add(Calendar.MONTH, -1); 635 cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); 636 Timestamp ts2 = new Timestamp(cal.getTimeInMillis()); 637 assertTrue(!ts1.equals(ts2) && ts1.equals(ts1)); 638 } 639 640 }