8206120: Add test cases for lenient Japanese era parsing
authornaoto
Tue, 03 Jul 2018 14:42:13 -0700
changeset 50932 6d03b1ea636b
parent 50931 d93bba067334
child 50933 76b5ee99ffc0
8206120: Add test cases for lenient Japanese era parsing Reviewed-by: rriggs
test/jdk/java/time/test/java/time/format/TestNonIsoFormatter.java
test/jdk/java/util/Calendar/JapaneseLenientEraTest.java
--- a/test/jdk/java/time/test/java/time/format/TestNonIsoFormatter.java	Tue Jul 03 15:40:08 2018 -0400
+++ b/test/jdk/java/time/test/java/time/format/TestNonIsoFormatter.java	Tue Jul 03 14:42:13 2018 -0700
@@ -24,6 +24,7 @@
 /*
  *
  * @test
+ * @bug 8206120
  * @modules jdk.localedata
  */
 
@@ -44,6 +45,7 @@
 import java.time.format.DateTimeFormatterBuilder;
 import java.time.format.DateTimeParseException;
 import java.time.format.FormatStyle;
+import java.time.format.ResolverStyle;
 import java.time.format.TextStyle;
 import java.time.temporal.TemporalAccessor;
 import java.time.temporal.TemporalQueries;
@@ -135,6 +137,16 @@
         };
     }
 
+    @DataProvider(name="lenient_eraYear")
+    Object[][] lenientEraYear() {
+        return new Object[][] {
+            // Chronology, lenient era/year, strict era/year
+            { JAPANESE, "Meiji 123", "Heisei 2" },
+            { JAPANESE, "Showa 65", "Heisei 2" },
+            { JAPANESE, "Heisei 32", "NewEra 2" }, // NewEra
+        };
+    }
+
     @Test(dataProvider="format_data")
     public void test_formatLocalizedDate(Chronology chrono, Locale formatLocale, Locale numberingLocale,
                                          ChronoLocalDate date, String expected) {
@@ -173,4 +185,15 @@
         Chronology cal = ta.query(TemporalQueries.chronology());
         assertEquals(cal, chrono);
     }
+
+    @Test(dataProvider="lenient_eraYear")
+    public void test_lenientEraYear(Chronology chrono, String lenient, String strict) {
+        String mdStr = "-01-01";
+        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
+            .appendPattern("GGGG y-M-d")
+            .toFormatter()
+            .withChronology(chrono);
+        DateTimeFormatter dtfLenient = dtf.withResolverStyle(ResolverStyle.LENIENT);
+        assertEquals(LocalDate.parse(lenient+mdStr, dtfLenient), LocalDate.parse(strict+mdStr, dtf));
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/util/Calendar/JapaneseLenientEraTest.java	Tue Jul 03 14:42:13 2018 -0700
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2018, 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8206120
+ * @summary Test whether lenient era is accepted in JapaneseImperialCalendar
+ * @run testng/othervm JapaneseLenientEraTest
+ */
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import static org.testng.Assert.assertEquals;
+
+@Test
+public class JapaneseLenientEraTest {
+
+    @DataProvider(name="lenientEra")
+    Object[][] names() {
+        return new Object[][] {
+            // lenient era/year, strict era/year
+            { "Meiji 123", "Heisei 2" },
+            { "Sh\u014dwa 65", "Heisei 2" },
+            { "Heisei 32", "NewEra 2" }, // NewEra
+        };
+    }
+
+    @Test(dataProvider="lenientEra")
+    public void testLenientEra(String lenient, String strict) throws Exception {
+        Calendar c = new Calendar.Builder()
+            .setCalendarType("japanese")
+            .build();
+        DateFormat df = new SimpleDateFormat("GGGG y-M-d", Locale.ROOT);
+        df.setCalendar(c);
+        Date lenDate = df.parse(lenient + "-01-01");
+        df.setLenient(false);
+        Date strDate = df.parse(strict + "-01-01");
+        assertEquals(lenDate, strDate);
+    }
+}