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.time.ZoneId;
  31 import java.util.Calendar;
  32 import static org.testng.Assert.*;
  33 import org.testng.annotations.DataProvider;
  34 import org.testng.annotations.Test;
  35 import util.BaseTest;
  36 
  37 public class TimestampTests extends BaseTest {
  38 
  39     /*
  40      * Validate an IllegalArgumentException is thrown for an invalid Timestamp
  41      */
  42     @Test(dataProvider = "invalidTimestampValues",
  43             expectedExceptions = IllegalArgumentException.class)
  44     public void test(String ts) throws Exception {
  45         Timestamp.valueOf(ts);
  46     }
  47 
  48     /*
  49      * Validate that two Timestamp are equal when the leading 0 in seconds is
  50      * omitted
  51      */
  52     @Test
  53     public void test01() throws Exception {
  54         String testTS = "2009-01-01 10:50:00";
  55         String ExpectedTS = "2009-01-01 10:50:0";
  56         Timestamp ts = Timestamp.valueOf(testTS);
  57         Timestamp ts2 = Timestamp.valueOf(ExpectedTS);
  58         assertEquals(ts, ts2, "Error ts1 != ts2");
  59     }
  60 
  61     /*
  62      * Validate two Timestamps created from the same string are equal
  63      */
  64     @Test
  65     public void test02() throws Exception {
  66         String testTS = "2009-01-01 10:50:0";
  67         Timestamp ts = Timestamp.valueOf(testTS);
  68         Timestamp ts2 = Timestamp.valueOf(testTS);
  69         assertEquals(ts, ts2, "Error ts1 != ts2");
  70     }
  71 
  72     /*
  73      * Validate that two Timestamp values one with leading 0s for month and day
  74      * equals same string without the leading 0s.
  75      */
  76     @Test
  77     public void test03() throws Exception {
  78         String testTS = "2009-1-1 10:50:0";
  79         String ExpectedTS = "2009-01-01 10:50:0";
  80         Timestamp ts = Timestamp.valueOf(testTS);
  81         Timestamp ts2 = Timestamp.valueOf(ExpectedTS);
  82         assertEquals(ts, ts2, "Error ts1 != ts2");
  83     }
  84 
  85     /*
  86      * Validate that two Timestamp values one with leading 0s for day omitted
  87      * are equal
  88      */
  89     @Test
  90     public void test04() throws Exception {
  91         String testTS = "2009-01-1 10:50:0";
  92         String ExpectedTS = "2009-01-01 10:50:0";
  93         Timestamp ts = Timestamp.valueOf(testTS);
  94         Timestamp ts2 = Timestamp.valueOf(ExpectedTS);
  95         assertEquals(ts, ts2, "Error ts1 != ts2");
  96     }
  97 
  98     /*
  99      * Validate that two Timestamp values one with leading 0s for month omitted
 100      * and both with leading 0s for seconds omitted are equal
 101      */
 102     @Test
 103     public void test05() throws Exception {
 104         String testTS = "2009-1-01 10:50:0";
 105         String ExpectedTS = "2009-01-01 10:50:0";
 106         Timestamp ts = Timestamp.valueOf(testTS);
 107         Timestamp ts2 = Timestamp.valueOf(ExpectedTS);
 108         assertEquals(ts, ts2, "Error ts1 != ts2");
 109     }
 110 
 111     /*
 112      * Validate that two Timestamp values one with leading 0s for month omitted
 113      */
 114     @Test
 115     public void test06() throws Exception {
 116         String testTS = "2005-1-01 10:20:50.00";
 117         String ExpectedTS = "2005-01-01 10:20:50.00";
 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 created using valueOf and another
 125      * via a constructor are equal
 126      */
 127     @Test
 128     public void test07() {
 129 
 130         Timestamp ts1 = Timestamp.valueOf("1996-12-13 14:15:25.001");
 131         Timestamp ts2 = new Timestamp(96, 11, 13, 14, 15, 25, 1000000);
 132         assertTrue(ts1.equals(ts2), "Error ts1 != ts2");
 133     }
 134 
 135     /*
 136      * Validate that two Timestamp values one created using valueOf and another
 137      * via a constructor are equal
 138      */
 139     @Test
 140     public void test08() {
 141         Timestamp ts1 = Timestamp.valueOf("1996-12-13 14:15:25.001");
 142         Timestamp ts2 = new Timestamp(ts1.getTime());
 143         assertTrue(ts1.equals(ts2), "Error ts1 != ts2");
 144     }
 145 
 146     /*
 147      * Validate that two Timestamp values one created using valueOf and another
 148      * via a constructor are equal
 149      */
 150     @Test
 151     public void test09() {
 152 
 153         Timestamp ts1 = Timestamp.valueOf("1996-12-13 14:15:25.0");
 154         Timestamp ts2 = new Timestamp(96, 11, 13, 14, 15, 25, 0);
 155         assertTrue(ts1.equals(ts2), "Error ts1 != ts2");
 156     }
 157 
 158     /*
 159      * Validate that a Timestamp cannot be equal to null
 160      */
 161     @Test
 162     public void test10() {
 163 
 164         Timestamp ts1 = Timestamp.valueOf("1961-08-30 14:15:25.745634");
 165         Timestamp ts2 = null;
 166         assertFalse(ts1.equals(ts2), "Error ts1 == null");
 167     }
 168 
 169     /*
 170      * Validate that a Timestamp is equal to another timestamp created with the
 171      * using the same value but not equal to a Timestamp which is one day later
 172      */
 173     @Test
 174     public void test11() {
 175 
 176         Timestamp ts1 = Timestamp.valueOf("1996-12-10 12:26:19.12");
 177         Timestamp ts2 = Timestamp.valueOf("1996-12-10 12:26:19.12");
 178         Timestamp ts3 = Timestamp.valueOf("1996-12-11 12:24:19.12");
 179         assertTrue(ts1.equals(ts2) && ts2.equals(ts1), "Error ts1 != ts2");
 180         assertFalse(ts1.equals(ts3) && ts3.equals(ts1), "Error ts1 == ts3");
 181 
 182     }
 183 
 184     /*
 185      * Validate that a Timestamp is equal to itself
 186      */
 187     @Test
 188     public void test12() {
 189         Timestamp ts1 = Timestamp.valueOf("1996-10-15 12:26:19.12");
 190         assertTrue(ts1.equals(ts1), "Error ts1 != ts1");
 191     }
 192 
 193     /*
 194      * Validate that two Timestamps are equal when one is created from the
 195      * toString() of the other
 196      */
 197     @Test(dataProvider = "validTimestampValues")
 198     public void test13(String ts, String expectedTS) {
 199         Timestamp ts1 = Timestamp.valueOf(ts);
 200         Timestamp ts2 = Timestamp.valueOf(ts1.toString());
 201         assertTrue(ts1.equals(ts2) && ts2.equals(ts1)
 202                 && ts1.toString().equals(expectedTS), "Error ts1 != ts2");
 203     }
 204 
 205     // Before Tests
 206     /*
 207      * Validate that Timestamp ts1 is before Timestamp ts2
 208      */
 209     @Test
 210     public void test14() {
 211         Timestamp ts1 = Timestamp.valueOf("1996-12-13 14:15:25.745634");
 212         Timestamp ts2 = Timestamp.valueOf("1996-12-13 15:15:25.645634");
 213         assertTrue(ts1.before(ts2), "Error ts1 not before ts2");
 214     }
 215 
 216     /*
 217      * Validate that Timestamp ts1 is before Timestamp ts2
 218      */
 219     @Test
 220     public void test15() {
 221         Timestamp ts1 = Timestamp.valueOf("1961-08-30 14:15:25");
 222         Timestamp ts2 = Timestamp.valueOf("1999-12-13 15:15:25");
 223         assertTrue(ts1.before(ts2), "Error ts1 not before ts2");
 224     }
 225 
 226     /*
 227      * Validate that Timestamp ts1 is before Timestamp ts2
 228      */
 229     @Test
 230     public void test16() {
 231 
 232         Timestamp ts1 = Timestamp.valueOf("1999-12-13 14:15:25.745634");
 233         Timestamp ts2 = Timestamp.valueOf("1999-11-13 15:15:25.645634");
 234         assertFalse(ts1.before(ts2), "Error ts1 before ts2");
 235     }
 236 
 237     /*
 238      * Validate that a NullPointerException is thrown if a null is passed to
 239      * the before method
 240      */
 241     @Test(expectedExceptions = NullPointerException.class)
 242     public void test17() throws Exception {
 243         Timestamp ts1 = Timestamp.valueOf("1996-12-13 14:15:25.745634");
 244         ts1.before(null);
 245     }
 246 
 247     /*
 248      * Validate a Timestamp cannot be before itself
 249      */
 250     @Test
 251     public void test18() {
 252         Timestamp ts1 = Timestamp.valueOf("1999-11-10 12:26:19.3456543");
 253         assertFalse(ts1.before(ts1), "Error ts1 before ts1!");
 254     }
 255 
 256     /*
 257      * Create 3 Timestamps and make sure the 1st is before the other two
 258      * Timestamps which are each greater than the one before it
 259      */
 260     @Test
 261     public void test19() {
 262 
 263         Timestamp ts1 = new Timestamp(1234560000);
 264         Timestamp ts2 = new Timestamp(1234567000);
 265         Timestamp ts3 = new Timestamp(1234569000);
 266         assertTrue(ts1.before(ts2) && ts2.before(ts3) && ts1.before(ts3));
 267     }
 268 
 269     /*
 270      * Validate that Timestamp ts1 is not after Timestamp ts2
 271      */
 272     @Test
 273     public void test20() {
 274         Timestamp ts1 = Timestamp.valueOf("1999-12-13 14:15:25.745634");
 275         Timestamp ts2 = Timestamp.valueOf("1999-12-13 15:15:25.645634");
 276         assertFalse(ts1.after(ts2), "Error ts1 is after ts2");
 277 
 278     }
 279 
 280     /*
 281      * Validate that Timestamp ts1 is after Timestamp ts2
 282      */
 283     @Test
 284     public void test21() {
 285         Timestamp ts1 = Timestamp.valueOf("1996-12-13 14:15:25.745634");
 286         Timestamp ts2 = Timestamp.valueOf("1996-11-13 15:15:25.645634");
 287         assertTrue(ts1.after(ts2), "Error ts1 not after ts2");
 288     }
 289 
 290     /*
 291      * Validate that a NullPointerException is thrown if a null is passed to the
 292      * after method
 293      */
 294     @Test(expectedExceptions = NullPointerException.class)
 295     public void test22() throws Exception {
 296         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 297         ts1.after(null);
 298     }
 299 
 300     /*
 301      * Validate that a Timestamp cannot be after itself
 302      */
 303     @Test
 304     public void test23() {
 305         Timestamp ts1 = Timestamp.valueOf("1999-11-10 12:26:19.3456543");
 306         assertFalse(ts1.after(ts1), "Error ts1 is after itself");
 307     }
 308 
 309     /*
 310      * Validate that a Timestamp after() works correctly with Timestamp created
 311      * using milliseconds
 312      */
 313     @Test
 314     public void test24() {
 315 
 316         Timestamp ts1 = new Timestamp(1234568000);
 317         Timestamp ts2 = new Timestamp(1234565000);
 318         Timestamp ts3 = new Timestamp(1234562000);
 319         assertTrue(ts1.after(ts2) && ts2.after(ts3) && ts1.after(ts3));
 320     }
 321 
 322     /*
 323      * Validate compareTo returns 0 for Timestamps that are the same
 324      */
 325     @Test
 326     public void test25() {
 327         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 328         Timestamp ts2 = new Timestamp(ts1.getTime());
 329         assertTrue(ts1.compareTo(ts2) == 0, "Error ts1 != ts2");
 330     }
 331 
 332     /*
 333      * Validate compareTo returns -1 for when the 1st Timestamp is earlier than
 334      * the 2nd Timestamp
 335      */
 336     @Test
 337     public void test26() {
 338         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 339         Timestamp ts2 = new Timestamp(ts1.getTime() + 1000);
 340         assertTrue(ts1.compareTo(ts2) == -1, "Error ts1 not before ts2");
 341         assertTrue(ts2.compareTo(ts1) == 1, "Error ts1 is not before ts2");
 342     }
 343 
 344     /*
 345      * Validate compareTo returns 1 for when the 1st Timestamp is later than the
 346      * 2nd Timestamp
 347      */
 348     @Test
 349     public void test27() {
 350         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 351         Timestamp ts2 = new Timestamp(ts1.getTime() - 1000);
 352         assertTrue(ts1.compareTo(ts2) == 1, "Error ts1 not after ts2");
 353         assertTrue(ts2.compareTo(ts1) == -1, "Error ts1 not after ts2");
 354     }
 355 
 356     /*
 357      * Validate compareTo returns 0 for Timestamps that are the same
 358      */
 359     @Test
 360     public void test28() {
 361         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 362         java.util.Date ts2 = new java.util.Date(ts1.getTime());
 363         assertTrue(ts1.compareTo(ts2) == 0, "Error ts1 != ts2");
 364     }
 365 
 366     /*
 367      * Validate compareTo returns 0 for Timestamps that are the same
 368      */
 369     @Test
 370     public void test29() {
 371         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 372         java.util.Date d = new java.util.Date(ts1.getTime());
 373         assertFalse(ts1.equals(d), "Error ts1 == d");
 374     }
 375 
 376     /*
 377      * Validate compareTo returns 0 for Timestamps that are the same
 378      */
 379     @Test
 380     public void test30() {
 381         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 382         java.util.Date d = new Timestamp(ts1.getTime());
 383         assertTrue(ts1.equals(d), "Error ts1 != d");
 384     }
 385 
 386     /*
 387      * Validate equals returns false when a Date object is passed to equals
 388      */
 389     @Test
 390     public void test31() {
 391         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 392         Date d = new Date(ts1.getTime());
 393         assertFalse(ts1.equals(d), "Error ts1 != d");
 394     }
 395 
 396     /*
 397      * Validate equals returns false when a Date object is passed to equals
 398      */
 399     @Test
 400     public void test32() {
 401         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 402         java.util.Date d = new Date(ts1.getTime());
 403         assertFalse(ts1.equals(d), "Error ts1 != d");
 404     }
 405 
 406     /*
 407      * Validate equals returns false when a Time object is passed to equals
 408      */
 409     @Test
 410     public void test33() {
 411         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 412         Time t1 = new Time(ts1.getTime());
 413         assertFalse(ts1.equals(t1), "Error ts1 == t1");
 414     }
 415 
 416     /*
 417      * Validate equals returns false when a String object is passed to equals
 418      */
 419     @Test
 420     public void test34() {
 421         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 422         assertFalse(ts1.equals("1966-08-30 08:08:08"), "Error ts1 == a String");
 423     }
 424 
 425     /*
 426      * Validate getTime() returns the same value from 2 timeStamps created by
 427      */
 428     @Test
 429     public void test35() {
 430         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 431         Timestamp ts2 = Timestamp.valueOf("1966-08-30 08:08:08");
 432         assertTrue(ts2.getTime() == ts1.getTime(),
 433                 "ts1.getTime() != ts2.getTime()");
 434         assertTrue(ts1.equals(ts2), "Error ts1 != ts2");
 435     }
 436 
 437     /*
 438      * Validate getTime() returns the same value from 2 timeStamps when
 439      * setTime() is used to specify the same value for both Timestamps
 440      */
 441     @Test
 442     public void test36() {
 443         Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
 444         Timestamp ts2 = Timestamp.valueOf("1961-08-30 00:00:00");
 445         ts2.setTime(ts1.getTime());
 446         assertTrue(ts2.getTime() == ts1.getTime(),
 447                 "ts1.getTime() != ts2.getTime()");
 448         assertTrue(ts1.equals(ts2), "Error ts1 != ts2");
 449     }
 450 
 451     /*
 452      * Validate an IllegalArgumentException is thrown for an invalid nanos value
 453      */
 454     @Test(expectedExceptions = IllegalArgumentException.class)
 455     public void test38() throws Exception {
 456         Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
 457         ts1.setNanos(-1);
 458 
 459     }
 460 
 461     /*
 462      * Validate an IllegalArgumentException is thrown for an invalid nanos value
 463      */
 464     @Test(expectedExceptions = IllegalArgumentException.class)
 465     public void test39() throws Exception {
 466         int nanos = 999999999;
 467         Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
 468         ts1.setNanos(nanos + 1);
 469     }
 470 
 471     /*
 472      * Validate you can set nanos to 999999999
 473      */
 474     @Test
 475     public void test40() throws Exception {
 476         int nanos = 999999999;
 477         Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
 478         ts1.setNanos(nanos);
 479         assertTrue(ts1.getNanos() == nanos, "Error Invalid Nanos value");
 480     }
 481 
 482     /*
 483      * Validate you can set nanos to 0
 484      */
 485     @Test
 486     public void test41() throws Exception {
 487         int nanos = 0;
 488         Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
 489         ts1.setNanos(nanos);
 490         assertTrue(ts1.getNanos() == nanos, "Error Invalid Nanos value");
 491     }
 492 
 493     /*
 494      * Validate that a Timestamp made from a LocalDateTime are equal
 495      */
 496     @Test
 497     public void test42() throws Exception {
 498         Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
 499         LocalDateTime ldt = ts1.toLocalDateTime();
 500         Timestamp ts2 = Timestamp.valueOf(ldt);
 501         assertTrue(ts1.equals(ts2), "Error ts1 != ts2");
 502     }
 503 
 504     /*
 505      * Validate that a Timestamp LocalDateTime value, made from a LocalDateTime
 506      * are equal
 507      */
 508     @Test
 509     public void test43() throws Exception {
 510         LocalDateTime ldt = LocalDateTime.now();
 511         Timestamp ts2 = Timestamp.valueOf(ldt);
 512         assertTrue(ldt.equals(ts2.toLocalDateTime()),
 513                 "Error LocalDateTime values are not equal");
 514     }
 515 
 516     /*
 517      * Validate an NPE occurs when a null LocalDateTime is passed to valueOF
 518      */
 519     @Test(expectedExceptions = NullPointerException.class)
 520     public void test44() throws Exception {
 521         LocalDateTime ldt = null;
 522         Timestamp.valueOf(ldt);
 523     }
 524 
 525     /*
 526      * Validate that a Timestamp made from a Instant are equal
 527      */
 528     @Test
 529     public void test45() throws Exception {
 530         Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
 531         Instant instant = ts1.toInstant();
 532         Timestamp ts2 = Timestamp.from(instant);
 533         assertTrue(ts1.equals(ts2), "Error ts1 != ts2");
 534     }
 535 
 536     /*
 537      * Validate that a Timestamp made from a Instant are equal
 538      */
 539     @Test
 540     public void test46() throws Exception {
 541         Instant instant = Instant.now();
 542         Timestamp ts2 = Timestamp.from(instant);
 543         assertTrue(instant.equals(ts2.toInstant()),
 544                 "Error Instant values do not match");
 545     }
 546 
 547     /*
 548      * Validate an NPE occurs when a null instant is passed to from
 549      */
 550     @Test(expectedExceptions = NullPointerException.class)
 551     public void test47() throws Exception {
 552         Instant instant = null;
 553         Timestamp.from(instant);
 554     }
 555 
 556     // Added SQE tests
 557     /*
 558      * Create a Timestamp and a 2nd Timestamp that is 1 month earlier and
 559      * validate that it is not before or after the original Timestamp
 560      */
 561     @Test
 562     public void test48() {
 563         Calendar cal = Calendar.getInstance();
 564         Timestamp ts1 = new Timestamp(System.currentTimeMillis());
 565         cal.setTimeInMillis(ts1.getTime());
 566         cal.add(Calendar.MONTH, -1);
 567         cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
 568         Timestamp ts2 = new Timestamp(cal.getTimeInMillis());
 569         assertFalse(ts1.before(ts2) || ts2.after(ts1));
 570     }
 571 
 572     /*
 573      * Create two Timestamps and validate that compareTo returns 1 to indicate
 574      * the 1st Timestamp is greater than the 2nd Timestamp
 575      */
 576     @Test
 577     public void test49() {
 578         Calendar cal = Calendar.getInstance();
 579         Timestamp ts1 = new Timestamp(System.currentTimeMillis());
 580         cal.setTimeInMillis(ts1.getTime());
 581         cal.add(Calendar.MONTH, -1);
 582         cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
 583         Timestamp ts2 = new Timestamp(cal.getTimeInMillis());
 584         assertTrue(ts1.compareTo(ts2) == 1);
 585     }
 586 
 587     /*
 588      * Create two Timestamps and validate that the 1st Timestamp is not equal to
 589      * the 2nd Timestamp but equal to itself
 590      */
 591     @Test
 592     public void test50() {
 593         Calendar cal = Calendar.getInstance();
 594         Timestamp ts1 = new Timestamp(System.currentTimeMillis());
 595         cal.setTimeInMillis(ts1.getTime());
 596         cal.add(Calendar.MONTH, -1);
 597         cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
 598         Timestamp ts2 = new Timestamp(cal.getTimeInMillis());
 599         assertTrue(!ts1.equals(ts2) && ts1.equals(ts1));
 600     }
 601 
 602     /*
 603      * Validate that two Timestamps are equal when one is created from the
 604      * toString() of the other
 605      */
 606     @Test(dataProvider = "validateNanos")
 607     public void test51(String ts, int nanos) {
 608         Timestamp ts1 = Timestamp.valueOf(ts);
 609         Timestamp ts2 = Timestamp.valueOf(ts1.toString());
 610         assertTrue(ts1.getNanos() == nanos && ts1.equals(ts2),
 611                 "Error with Nanos");
 612     }
 613 
 614     @Test(dataProvider = "validTimestampLongValues")
 615     public void test52(long value, String ts) {
 616         Timestamp ts1 = new Timestamp(value);
 617         assertEquals(ts1.toString(), ts, "ts1.toString() != ts");
 618     }
 619 
 620     /*
 621      * DataProvider used to provide Timestamps which are not valid and are used
 622      * to validate that an IllegalArgumentException will be thrown from the
 623      * valueOf method
 624      */
 625     @DataProvider(name = "invalidTimestampValues")
 626     private Object[][] invalidTimestampValues() {
 627         return new Object[][]{
 628             {"2009-11-01-01 10:50:01"},
 629             {"aaaa-11-01-01 10:50"},
 630             {"aaaa-11-01 10:50"},
 631             {"1961--30 00:00:00"},
 632             {"--30 00:00:00"},
 633             {"-- 00:00:00"},
 634             {"1961-1- 00:00:00"},
 635             {"2009-11-01"},
 636             {"10:50:01"},
 637             {"1961-a-30 00:00:00"},
 638             {"1961-01-bb 00:00:00"},
 639             {"1961-08-30 00:00:00."},
 640             {"1961-08-30 :00:00"},
 641             {"1961-08-30 00::00"},
 642             {"1961-08-30 00:00:"},
 643             {"1961-08-30 ::"},
 644             {"1961-08-30 0a:00:00"},
 645             {"1961-08-30 00:bb:00"},
 646             {"1961-08-30 00:01:cc"},
 647             {"1961-08-30 00:00:00.01a"},
 648             {"1961-08-30 00:00:00.a"},
 649             {"1996-12-10 12:26:19.1234567890"},
 650             {null}
 651         };
 652     }
 653 
 654     /*
 655      * DataProvider used to provide Timestamps which are  valid and are used
 656      * to validate that an IllegalArgumentException will not be thrown from the
 657      * valueOf method and the corect value from toString() is returned
 658      */
 659     @DataProvider(name = "validTimestampValues")
 660     private Object[][] validTimestampValues() {
 661         return new Object[][]{
 662             {"1961-08-30 00:00:00", "1961-08-30 00:00:00.0"},
 663             {"1961-08-30 11:22:33", "1961-08-30 11:22:33.0"},
 664             {"1961-8-30 00:00:00", "1961-08-30 00:00:00.0"},
 665             {"1966-08-1 00:00:00", "1966-08-01 00:00:00.0"},
 666             {"1996-12-10 12:26:19.1", "1996-12-10 12:26:19.1"},
 667             {"1996-12-10 12:26:19.12", "1996-12-10 12:26:19.12"},
 668             {"1996-12-10 12:26:19.123", "1996-12-10 12:26:19.123"},
 669             {"1996-12-10 12:26:19.1234", "1996-12-10 12:26:19.1234"},
 670             {"1996-12-10 12:26:19.12345", "1996-12-10 12:26:19.12345"},
 671             {"1996-12-10 12:26:19.123456", "1996-12-10 12:26:19.123456"},
 672             {"1996-12-10 12:26:19.1234567", "1996-12-10 12:26:19.1234567"},
 673             {"1996-12-10 12:26:19.12345678", "1996-12-10 12:26:19.12345678"},
 674             {"1996-12-10 12:26:19.123456789", "1996-12-10 12:26:19.123456789"},
 675             {"1996-12-10 12:26:19.000000001", "1996-12-10 12:26:19.000000001"},
 676             {"1996-12-10 12:26:19.000000012", "1996-12-10 12:26:19.000000012"},
 677             {"1996-12-10 12:26:19.000000123", "1996-12-10 12:26:19.000000123"},
 678             {"1996-12-10 12:26:19.000001234", "1996-12-10 12:26:19.000001234"},
 679             {"1996-12-10 12:26:19.000012345", "1996-12-10 12:26:19.000012345"},
 680             {"1996-12-10 12:26:19.000123456", "1996-12-10 12:26:19.000123456"},
 681             {"1996-12-10 12:26:19.001234567", "1996-12-10 12:26:19.001234567"},
 682             {"1996-12-10 12:26:19.12345678", "1996-12-10 12:26:19.12345678"},
 683             {"1996-12-10 12:26:19.0", "1996-12-10 12:26:19.0"},
 684             {"1996-12-10 12:26:19.01230", "1996-12-10 12:26:19.0123"}
 685         };
 686     }
 687 
 688     @DataProvider(name = "validTimestampLongValues")
 689     private Object[][] validTimestampLongValues() {
 690         return new Object[][]{
 691             {1L, "1970-01-01 01:00:00.001"},
 692             {-3600*1000L - 1, "1969-12-31 23:59:59.999"},
 693             {-(20000L*365*24*60*60*1000), "18018-08-28 01:00:00.0"},
 694             {Timestamp.valueOf("1961-08-30 11:22:33").getTime(), "1961-08-30 11:22:33.0"},
 695             {Timestamp.valueOf("1961-08-30 11:22:33.54321000").getTime(), "1961-08-30 11:22:33.543"}, // nanoprecision lost
 696             {new Timestamp(114, 10, 10, 10, 10, 10, 100000000).getTime(), "2014-11-10 10:10:10.1"},
 697             {new Timestamp(0, 10, 10, 10, 10, 10, 100000).getTime(), "1900-11-10 10:10:10.0"}, // nanoprecision lost
 698             {new Date(114, 10, 10).getTime(), "2014-11-10 00:00:00.0"},
 699             {new Date(0, 10, 10).getTime(), "1900-11-10 00:00:00.0"},
 700             {LocalDateTime.of(1960, 10, 10, 10, 10, 10, 50000).atZone(ZoneId.of("America/Los_Angeles"))
 701                 .toInstant().toEpochMilli(), "1960-10-10 19:10:10.0"},
 702 
 703             // millisecond timestamps wraps around at year 1, so Long.MIN_VALUE looks similar
 704             // Long.MAX_VALUE, while actually representing 292278994 BCE
 705             {Long.MIN_VALUE, "292278994-08-17 08:12:55.192"},
 706             {Long.MAX_VALUE + 1, "292278994-08-17 08:12:55.192"},
 707             {Long.MAX_VALUE, "292278994-08-17 08:12:55.807"},
 708             {Long.MIN_VALUE - 1, "292278994-08-17 08:12:55.807"},
 709 
 710             // wrap around point near 0001-01-01, test that we never get a negative year:
 711             {-(1970L*365*24*60*60*1000), "0001-04-25 01:00:00.0"},
 712             {-(1970L*365*24*60*60*1000 + 115*24*60*60*1000L), "0001-12-31 01:00:00.0"},
 713             {-(1970L*365*24*60*60*1000 + 115*24*60*60*1000L - 23*60*60*1000L), "0001-01-01 00:00:00.0"},
 714 
 715             {LocalDateTime.of(0, 1, 1, 10, 10, 10, 50000).atZone(ZoneId.of("America/Los_Angeles"))
 716                 .toInstant().toEpochMilli() - 2*24*60*60*1000L, "0001-01-01 19:03:08.0"}, // 1 BCE
 717             {LocalDateTime.of(0, 1, 1, 10, 10, 10, 50000).atZone(ZoneId.of("America/Los_Angeles"))
 718                 .toInstant().toEpochMilli() - 3*24*60*60*1000L, "0002-12-31 19:03:08.0"} // 2 BCE
 719         };
 720     }
 721 
 722     /*
 723      * DataProvider used to provide Timestamp and Nanos values in order to
 724      * validate that the correct Nanos value is generated from the specified
 725      * Timestamp
 726      */
 727     @DataProvider(name = "validateNanos")
 728     private Object[][] validateNanos() {
 729         return new Object[][]{
 730             {"1961-08-30 00:00:00", 0},
 731             {"1996-12-10 12:26:19.1", 100000000},
 732             {"1996-12-10 12:26:19.12", 120000000},
 733             {"1996-12-10 12:26:19.123", 123000000},
 734             {"1996-12-10 12:26:19.1234", 123400000},
 735             {"1996-12-10 12:26:19.12345", 123450000},
 736             {"1996-12-10 12:26:19.123456", 123456000},
 737             {"1996-12-10 12:26:19.1234567", 123456700},
 738             {"1996-12-10 12:26:19.12345678", 123456780},
 739             {"1996-12-10 12:26:19.123456789", 123456789},
 740             {"1996-12-10 12:26:19.000000001", 1},
 741             {"1996-12-10 12:26:19.000000012", 12},
 742             {"1996-12-10 12:26:19.000000123", 123},
 743             {"1996-12-10 12:26:19.000001234", 1234},
 744             {"1996-12-10 12:26:19.000012345", 12345},
 745             {"1996-12-10 12:26:19.000123456", 123456},
 746             {"1996-12-10 12:26:19.001234567", 1234567},
 747             {"1996-12-10 12:26:19.012345678", 12345678},
 748             {"1996-12-10 12:26:19.0", 0},
 749             {"1996-12-10 12:26:19.01230", 12300000}
 750         };
 751     }
 752 }