|
1 | 1 | /* |
2 | | - * Copyright (c) 2014, 2021, 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 |
|
62 | 62 | import static java.time.temporal.ChronoField.AMPM_OF_DAY; |
63 | 63 | import static java.time.temporal.ChronoField.EPOCH_DAY; |
64 | 64 | import static java.time.temporal.ChronoField.HOUR_OF_AMPM; |
| 65 | +import static java.time.temporal.ChronoField.HOUR_OF_DAY; |
65 | 66 | import static java.time.temporal.ChronoField.INSTANT_SECONDS; |
66 | 67 | import static java.time.temporal.ChronoField.MICRO_OF_SECOND; |
67 | 68 | import static java.time.temporal.ChronoField.MILLI_OF_SECOND; |
| 69 | +import static java.time.temporal.ChronoField.MINUTE_OF_HOUR; |
68 | 70 | import static java.time.temporal.ChronoField.NANO_OF_SECOND; |
69 | 71 | import static java.time.temporal.ChronoField.OFFSET_SECONDS; |
70 | 72 | import static java.time.temporal.ChronoField.SECOND_OF_DAY; |
71 | 73 | import static java.util.Locale.US; |
72 | 74 | import static org.testng.Assert.assertEquals; |
| 75 | +import static org.testng.Assert.assertNull; |
73 | 76 |
|
| 77 | +import java.text.ParsePosition; |
74 | 78 | import java.time.DateTimeException; |
75 | 79 | import java.time.Instant; |
76 | 80 | import java.time.LocalDateTime; |
|
80 | 84 | import java.time.format.DateTimeFormatter; |
81 | 85 | import java.time.format.DateTimeFormatterBuilder; |
82 | 86 | import java.time.format.DateTimeParseException; |
| 87 | +import java.time.format.SignStyle; |
83 | 88 | import java.time.temporal.TemporalAccessor; |
| 89 | +import java.util.Locale; |
84 | 90 |
|
85 | 91 | import org.testng.annotations.DataProvider; |
86 | 92 | import org.testng.annotations.Test; |
87 | 93 |
|
88 | 94 | /** |
89 | 95 | * @test |
90 | 96 | * @summary Test parsing of edge cases. |
91 | | - * @bug 8223773 8272473 |
| 97 | + * @bug 8223773 8272473 8319640 |
92 | 98 | */ |
93 | 99 | public class TestDateTimeParsing { |
94 | 100 |
|
@@ -237,4 +243,30 @@ public void test_validateHourOfAmPm() { |
237 | 243 | } |
238 | 244 | } |
239 | 245 | } |
| 246 | + |
| 247 | + // Checks ::toFormat().parseObject(text, pos) do not throw DateTimeException |
| 248 | + @Test |
| 249 | + public void test_toFormat_2arg_null_return_on_DateTimeException() { |
| 250 | + var f = new DateTimeFormatterBuilder() |
| 251 | + .appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE) |
| 252 | + .optionalStart() |
| 253 | + .appendLiteral(':') |
| 254 | + .appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE) |
| 255 | + .optionalEnd() |
| 256 | + .optionalStart() |
| 257 | + .appendOffset("+HHmm", "Z") |
| 258 | + .optionalEnd() |
| 259 | + .toFormatter(Locale.ROOT) |
| 260 | + .toFormat(); |
| 261 | + assertNull(f.parseObject("17-30", new ParsePosition(0))); |
| 262 | + } |
| 263 | + |
| 264 | + // Checks ::toFormat().parseObject(text, pos) do not throw IOOBE |
| 265 | + @Test |
| 266 | + public void test_toFormat_2arg_null_return_on_IOOBE() { |
| 267 | + var date = "2023-11-13"; |
| 268 | + assertNull(DateTimeFormatter.ISO_LOCAL_DATE |
| 269 | + .toFormat() |
| 270 | + .parseObject(date, new ParsePosition(date.length() + 1))); |
| 271 | + } |
240 | 272 | } |
0 commit comments