|
37 | 37 |
|
38 | 38 | public class TimeConversionUtilsTest {
|
39 | 39 |
|
| 40 | + // 0.999999999 seconds (1 second - 1 nano) - we need to subtract the nano or the Duration would |
| 41 | + // overflow otherwise |
| 42 | + final long MAX_DURATION_NANOS = |
| 43 | + 1 * 1000 /*=1 micro*/ * 1000 /*=1 milli*/ * 1000 /*=1 second*/ - 1; |
| 44 | + |
| 45 | + // Arbitrary durations/instants to confirm conversion works as expected |
40 | 46 | final org.threeten.bp.Duration ttDuration = org.threeten.bp.Duration.ofMillis(123);
|
41 | 47 | final org.threeten.bp.Instant ttInstant = org.threeten.bp.Instant.ofEpochMilli(123);
|
42 | 48 | final java.time.Duration jtDuration = java.time.Duration.ofMillis(345);
|
@@ -69,4 +75,44 @@ void testToThreetenTimeInstant_validInput_succeeds() {
|
69 | 75 | jtInstant.toEpochMilli(), TimeConversionUtils.toThreetenInstant(jtInstant).toEpochMilli());
|
70 | 76 | assertNull(TimeConversionUtils.toThreetenInstant(null));
|
71 | 77 | }
|
| 78 | + |
| 79 | + @Test |
| 80 | + void testToThreeteenInstant_bigInput_doesNotOverflow() { |
| 81 | + // defaults to MAX_SECONDS plus the max value of long for the nanos part |
| 82 | + java.time.Instant jtInstant = java.time.Instant.MAX; |
| 83 | + org.threeten.bp.Instant ttInstant = TimeConversionUtils.toThreetenInstant(jtInstant); |
| 84 | + assertEquals(jtInstant.getEpochSecond(), ttInstant.getEpochSecond()); |
| 85 | + assertEquals(jtInstant.getNano(), ttInstant.getNano()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testToJavaTimeInstant_bigInput_doesNotOverflow() { |
| 90 | + // defaults to MAX_SECONDS plus the max value of long for the nanos part |
| 91 | + org.threeten.bp.Instant ttInstant = org.threeten.bp.Instant.MAX; |
| 92 | + java.time.Instant jtInstant = TimeConversionUtils.toJavaTimeInstant(ttInstant); |
| 93 | + assertEquals(jtInstant.getEpochSecond(), ttInstant.getEpochSecond()); |
| 94 | + assertEquals(jtInstant.getNano(), ttInstant.getNano()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testToThreeteenDuration_bigInput_doesNotOverflow() { |
| 99 | + // we use the max long value for the seconds part and an arbitrary int for the nanos part, so we |
| 100 | + // can confirm that both components are preserved |
| 101 | + java.time.Duration jtDuration = |
| 102 | + java.time.Duration.ofSeconds(Long.MAX_VALUE, MAX_DURATION_NANOS); |
| 103 | + org.threeten.bp.Duration ttDuration = TimeConversionUtils.toThreetenDuration(jtDuration); |
| 104 | + assertEquals(jtDuration.getSeconds(), ttDuration.getSeconds()); |
| 105 | + assertEquals(jtDuration.getNano(), ttDuration.getNano()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void testToJavaTimeDuration_bigInput_doesNotOverflow() { |
| 110 | + // we use the max long value for the seconds part and an arbitrary int for the nanos part, so we |
| 111 | + // can confirm that both components are preserved |
| 112 | + org.threeten.bp.Duration ttDuration = |
| 113 | + org.threeten.bp.Duration.ofSeconds(Long.MAX_VALUE, MAX_DURATION_NANOS); |
| 114 | + java.time.Duration jtDuration = TimeConversionUtils.toJavaTimeDuration(ttDuration); |
| 115 | + assertEquals(jtDuration.getSeconds(), ttDuration.getSeconds()); |
| 116 | + assertEquals(jtDuration.getNano(), ttDuration.getNano()); |
| 117 | + } |
72 | 118 | }
|
0 commit comments