|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
@@ -642,6 +642,31 @@ public void test52(long value, String ts) {
|
642 | 642 | assertEquals(ts1.toString(), ts, "ts1.toString() != ts");
|
643 | 643 | }
|
644 | 644 |
|
| 645 | + @Test |
| 646 | + public void test53() { |
| 647 | + // The latest Instant that can be converted to a Timestamp. |
| 648 | + Instant instant1 = Instant.ofEpochSecond(Long.MAX_VALUE / 1000, 999_999_999); |
| 649 | + assertEquals(Timestamp.from(instant1).toInstant(), instant1); |
| 650 | + |
| 651 | + // One nanosecond more, and converting it gets an overflow. |
| 652 | + Instant instant2 = instant1.plusNanos(1); |
| 653 | + expectThrows(IllegalArgumentException.class, () -> Timestamp.from(instant2)); |
| 654 | + |
| 655 | + // The earliest Instant that can be converted to a Timestamp. |
| 656 | + Instant instant3 = Instant.ofEpochSecond(Long.MIN_VALUE / 1000, 0); |
| 657 | + assertEquals(Timestamp.from(instant3).toInstant(), instant3); |
| 658 | + |
| 659 | + // One nanosecond less, and converting it gets an overflow. |
| 660 | + Instant instant4 = instant3.minusNanos(1); |
| 661 | + expectThrows(IllegalArgumentException.class, () -> Timestamp.from(instant4)); |
| 662 | + |
| 663 | + // The latest possible Instant will certainly overflow. |
| 664 | + expectThrows(IllegalArgumentException.class, () -> Timestamp.from(Instant.MAX)); |
| 665 | + |
| 666 | + // The earliest possible Instant will certainly overflow. |
| 667 | + expectThrows(IllegalArgumentException.class, () -> Timestamp.from(Instant.MIN)); |
| 668 | + } |
| 669 | + |
645 | 670 | /*
|
646 | 671 | * DataProvider used to provide Timestamps which are not valid and are used
|
647 | 672 | * to validate that an IllegalArgumentException will be thrown from the
|
|
0 commit comments