changeset 59683:a99b2ab49b5b

8246662: Test java/time/test/java/time/format/TestUnicodeExtension.java failed on japanese locale. Reviewed-by: rriggs, joehw
author naoto
date Tue, 09 Jun 2020 14:46:08 -0700
parents 0de5bf3fd6b0
children 474709480635
files src/java.base/share/classes/java/time/format/DateTimeFormatter.java test/jdk/java/time/test/java/time/format/TestUnicodeExtension.java
diffstat 2 files changed, 33 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/time/format/DateTimeFormatter.java	Tue Jun 09 21:23:33 2020 +0000
+++ b/src/java.base/share/classes/java/time/format/DateTimeFormatter.java	Tue Jun 09 14:46:08 2020 -0700
@@ -1469,7 +1469,7 @@
 
     /**
      * Returns a copy of this formatter with localized values of the locale,
-     * calendar, region, decimal style and/or timezone, that supercede values in
+     * calendar, region, decimal style and/or timezone, that supersede values in
      * this formatter.
      * <p>
      * This is used to lookup any part of the formatter needing specific
@@ -1489,16 +1489,12 @@
      *
      * @param locale  the locale, not null
      * @return a formatter based on this formatter with localized values of
-     *      the calendar, decimal style and/or timezone, that supercede values in this
+     *      the calendar, decimal style and/or timezone, that supersede values in this
      *      formatter.
      * @see #withLocale(Locale)
      * @since 10
      */
     public DateTimeFormatter localizedBy(Locale locale) {
-        if (this.locale.equals(locale)) {
-            return this;
-        }
-
         // Override decimalStyle/chronology/timezone for the locale object
         String tzType = locale.getUnicodeLocaleType("tz");
         ZoneId z = tzType != null ?
@@ -1506,13 +1502,16 @@
                         .map(ZoneId::of)
                         .orElse(zone) :
                     zone;
-        return new DateTimeFormatter(printerParser,
-                locale,
-                DecimalStyle.of(locale),
-                resolverStyle,
-                resolverFields,
-                Chronology.ofLocale(locale),
-                z);
+        Chronology c = Chronology.ofLocale(locale);
+        DecimalStyle ds = DecimalStyle.of(locale);
+        if (this.locale.equals(locale) &&
+                c.equals(chrono) &&
+                ds.equals(decimalStyle) &&
+                Objects.equals(z, zone)) {
+            return this;
+        } else {
+            return new DateTimeFormatter(printerParser, locale, ds, resolverStyle, resolverFields, c, z);
+        }
     }
 
     //-----------------------------------------------------------------------
--- a/test/jdk/java/time/test/java/time/format/TestUnicodeExtension.java	Tue Jun 09 21:23:33 2020 +0000
+++ b/test/jdk/java/time/test/java/time/format/TestUnicodeExtension.java	Tue Jun 09 14:46:08 2020 -0700
@@ -47,6 +47,7 @@
 import java.time.temporal.WeekFields;
 import java.util.Locale;
 import java.util.TimeZone;
+import java.util.stream.Stream;
 
 import org.testng.annotations.AfterTest;
 import org.testng.annotations.BeforeTest;
@@ -825,15 +826,26 @@
     public void test_localizedBy(Locale locale, Chronology chrono, ZoneId zone,
                                 Chronology chronoExpected, ZoneId zoneExpected,
                                 String formatExpected) {
-        DateTimeFormatter dtf =
-            DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.FULL)
-                .withChronology(chrono).withZone(zone).localizedBy(locale);
-        assertEquals(dtf.getChronology(), chronoExpected);
-        assertEquals(dtf.getZone(), zoneExpected);
-        String formatted = dtf.format(ZDT);
-        assertEquals(formatted, formatExpected);
-        assertEquals(dtf.parse(formatted, ZonedDateTime::from),
-            zoneExpected != null ? ZDT.withZoneSameInstant(zoneExpected) : ZDT);
+        // try this test both with the implicit default locale, and explicit default locale ja-JP
+        Locale def = Locale.getDefault();
+        try {
+            Stream.of(def, Locale.JAPAN).forEach(l -> {
+                System.out.println("    Testing with the default locale: " + l);
+                Locale.setDefault(l);
+
+                DateTimeFormatter dtf =
+                        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.FULL)
+                                .withChronology(chrono).withZone(zone).localizedBy(locale);
+                assertEquals(dtf.getChronology(), chronoExpected);
+                assertEquals(dtf.getZone(), zoneExpected);
+                String formatted = dtf.format(ZDT);
+                assertEquals(formatted, formatExpected);
+                assertEquals(dtf.parse(formatted, ZonedDateTime::from),
+                        zoneExpected != null ? ZDT.withZoneSameInstant(zoneExpected) : ZDT);
+            });
+        } finally {
+            Locale.setDefault(def);
+        }
     }
 
     @Test(dataProvider="withLocale")