8223773: DateTimeFormatter Fails to throw an Exception on Invalid CLOCK_HOUR_OF_AMPM and HOUR_OF_AMPM
Reviewed-by: lancea, scolebourne, rriggs
--- a/src/java.base/share/classes/java/time/format/Parsed.java Fri May 31 13:37:58 2019 -0700
+++ b/src/java.base/share/classes/java/time/format/Parsed.java Fri May 31 13:49:35 2019 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -399,7 +399,7 @@
updateCheckConflict(AMPM_OF_DAY, HOUR_OF_DAY, Math.addExact(Math.multiplyExact(ap, 12), hap));
} else { // STRICT or SMART
AMPM_OF_DAY.checkValidValue(ap);
- HOUR_OF_AMPM.checkValidValue(ap);
+ HOUR_OF_AMPM.checkValidValue(hap);
updateCheckConflict(AMPM_OF_DAY, HOUR_OF_DAY, ap * 12 + hap);
}
}
--- a/test/jdk/java/time/test/java/time/format/TestDateTimeParsing.java Fri May 31 13:37:58 2019 -0700
+++ b/test/jdk/java/time/test/java/time/format/TestDateTimeParsing.java Fri May 31 13:49:35 2019 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -59,13 +59,16 @@
*/
package test.java.time.format;
+import static java.time.temporal.ChronoField.AMPM_OF_DAY;
import static java.time.temporal.ChronoField.EPOCH_DAY;
+import static java.time.temporal.ChronoField.HOUR_OF_AMPM;
import static java.time.temporal.ChronoField.INSTANT_SECONDS;
import static java.time.temporal.ChronoField.MICRO_OF_SECOND;
import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
import static java.time.temporal.ChronoField.OFFSET_SECONDS;
import static java.time.temporal.ChronoField.SECOND_OF_DAY;
+import static java.util.Locale.US;
import static org.testng.Assert.assertEquals;
import java.time.DateTimeException;
@@ -76,15 +79,17 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
+import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
- * Test parsing of edge cases.
+ * @test
+ * @summary Test parsing of edge cases.
+ * @bug 8223773
*/
-@Test
public class TestDateTimeParsing {
private static final ZoneId PARIS = ZoneId.of("Europe/Paris");
@@ -115,6 +120,9 @@
private static final DateTimeFormatter INSTANTSECONDS_OFFSETSECONDS = new DateTimeFormatterBuilder()
.appendValue(INSTANT_SECONDS).appendLiteral(' ').appendValue(OFFSET_SECONDS).toFormatter();
+ private static final String DTPE_MESSAGE =
+ "Invalid value for HourOfAmPm (valid values 0 - 11): 12";
+
@DataProvider(name = "instantZones")
Object[][] data_instantZones() {
return new Object[][] {
@@ -201,4 +209,25 @@
assertEquals(actual.isSupported(MILLI_OF_SECOND), true);
}
+ // Bug 8223773: validation check for the range of HourOfAmPm in SMART mode.
+ // Should throw a DateTimeParseException, as 12 is out of range for HourOfAmPm.
+ @Test(expectedExceptions = DateTimeParseException.class)
+ public void test_validateHourOfAmPm() {
+ try {
+ new DateTimeFormatterBuilder()
+ .appendValue(HOUR_OF_AMPM,2)
+ .appendText(AMPM_OF_DAY)
+ .toFormatter(US)
+ .parse("12PM");
+ } catch (DateTimeParseException e) {
+ Throwable cause = e.getCause();
+ if (cause == null ||
+ !DTPE_MESSAGE.equals(cause.getMessage())) {
+ throw new RuntimeException(
+ "DateTimeParseException was thrown with different reason: " + e);
+ } else {
+ throw e;
+ }
+ }
+ }
}