< prev index next >

test/jdk/java/time/test/java/time/format/TestDateTimeParsing.java

Print this page
rev 55060 : [mq]: 8223773

@@ -1,7 +1,7 @@
 /*
- * 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -57,34 +57,40 @@
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 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;
 import java.time.Instant;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.time.ZoneOffset;
 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.
+ *
+ * @bug 8223773
  */
 @Test
 public class TestDateTimeParsing {
 
     private static final ZoneId PARIS = ZoneId.of("Europe/Paris");

@@ -113,10 +119,13 @@
         .appendValue(INSTANT_SECONDS).appendLiteral('.').appendValue(NANO_OF_SECOND).toFormatter();
     private static final DateTimeFormatter INSTANTSECONDS_NOS_WITH_PARIS = INSTANTSECONDS_NOS.withZone(PARIS);
     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[][] {
             {LOCALFIELDS_ZONEID, "2014-06-30 01:02:03 Europe/Paris", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, PARIS)},
             {LOCALFIELDS_ZONEID, "2014-06-30 01:02:03 +02:30", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, OFFSET_0230)},

@@ -199,6 +208,28 @@
         assertEquals(actual.isSupported(NANO_OF_SECOND), true);
         assertEquals(actual.isSupported(MICRO_OF_SECOND), true);
         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;
+            }
+        }
+    }
 }
< prev index next >