|
1 | 1 | /* |
2 | | - * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2012, 2025, 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 |
|
61 | 61 |
|
62 | 62 | import java.time.Duration; |
63 | 63 | import java.time.Instant; |
| 64 | +import java.time.OffsetDateTime; |
| 65 | +import java.time.ZoneOffset; |
| 66 | +import java.time.format.DateTimeParseException; |
64 | 67 | import java.time.temporal.ChronoUnit; |
65 | 68 |
|
66 | 69 | import org.testng.annotations.Test; |
|
70 | 73 |
|
71 | 74 | /** |
72 | 75 | * Test Instant. |
73 | | - * @bug 8273369 8331202 |
| 76 | + * @bug 8273369 8331202 8364752 |
74 | 77 | */ |
75 | 78 | @Test |
76 | 79 | public class TestInstant extends AbstractTest { |
@@ -151,4 +154,58 @@ public void test_until_1arg_NPE() { |
151 | 154 | Instant.now().until(null); |
152 | 155 | }); |
153 | 156 | } |
| 157 | + |
| 158 | + @DataProvider |
| 159 | + private Object[][] valid_instants() { |
| 160 | + var I1 = OffsetDateTime.of(2017, 1, 1, 0, 0, 0, 0, ZoneOffset.of("+02")).toInstant(); |
| 161 | + var I2 = OffsetDateTime.of(2017, 1, 1, 0, 0, 0, 0, ZoneOffset.of("+02:02")).toInstant(); |
| 162 | + var I3 = OffsetDateTime.of(2017, 1, 1, 0, 0, 0, 0, ZoneOffset.of("+02:02:02")).toInstant(); |
| 163 | + var I4 = OffsetDateTime.of(2017, 1, 1, 0, 0, 0, 0, ZoneOffset.of("Z")).toInstant(); |
| 164 | + return new Object[][] { |
| 165 | + {"2017-01-01T00:00:00.000+02", I1}, |
| 166 | + {"2017-01-01T00:00:00.000+0200", I1}, |
| 167 | + {"2017-01-01T00:00:00.000+02:00", I1}, |
| 168 | + {"2017-01-01T00:00:00.000+020000", I1}, |
| 169 | + {"2017-01-01T00:00:00.000+02:00:00", I1}, |
| 170 | + |
| 171 | + {"2017-01-01T00:00:00.000+0202", I2}, |
| 172 | + {"2017-01-01T00:00:00.000+02:02", I2}, |
| 173 | + |
| 174 | + {"2017-01-01T00:00:00.000+020202", I3}, |
| 175 | + {"2017-01-01T00:00:00.000+02:02:02", I3}, |
| 176 | + |
| 177 | + {"2017-01-01T00:00:00.000Z", I4}, |
| 178 | + }; |
| 179 | + } |
| 180 | + |
| 181 | + @Test(dataProvider = "valid_instants") |
| 182 | + public void test_parse_valid(String instant, Instant expected) { |
| 183 | + assertEquals(Instant.parse(instant), expected); |
| 184 | + } |
| 185 | + |
| 186 | + @DataProvider |
| 187 | + private Object[][] invalid_instants() { |
| 188 | + return new Object[][] { |
| 189 | + {"2017-01-01T00:00:00.000"}, |
| 190 | + {"2017-01-01T00:00:00.000+0"}, |
| 191 | + {"2017-01-01T00:00:00.000+0:"}, |
| 192 | + {"2017-01-01T00:00:00.000+02:"}, |
| 193 | + {"2017-01-01T00:00:00.000+020"}, |
| 194 | + {"2017-01-01T00:00:00.000+02:0"}, |
| 195 | + {"2017-01-01T00:00:00.000+02:0:"}, |
| 196 | + {"2017-01-01T00:00:00.000+02:00:"}, |
| 197 | + {"2017-01-01T00:00:00.000+02:000"}, |
| 198 | + {"2017-01-01T00:00:00.000+02:00:0"}, |
| 199 | + {"2017-01-01T00:00:00.000+02:00:0:"}, |
| 200 | + {"2017-01-01T00:00:00.000+0200000"}, |
| 201 | + {"2017-01-01T00:00:00.000+02:00:000"}, |
| 202 | + {"2017-01-01T00:00:00.000+02:00:00:"}, |
| 203 | + {"2017-01-01T00:00:00.000UTC"}, |
| 204 | + }; |
| 205 | + } |
| 206 | + |
| 207 | + @Test(dataProvider = "invalid_instants") |
| 208 | + public void test_parse_invalid(String instant) { |
| 209 | + assertThrows(DateTimeParseException.class, () -> Instant.parse(instant)); |
| 210 | + } |
154 | 211 | } |
0 commit comments