8159548: Formatter returns unexpected strings if locale is null.
authornaoto
Tue, 21 Jun 2016 15:15:05 -0700
changeset 39134 be0fe7a4190a
parent 39133 b5641ce64cf7
child 39135 12b17886ae03
8159548: Formatter returns unexpected strings if locale is null. Reviewed-by: sherman
jdk/src/java.base/share/classes/java/util/Formatter.java
jdk/test/java/util/Formatter/FormatLocale.java
--- a/jdk/src/java.base/share/classes/java/util/Formatter.java	Tue Jun 21 18:51:18 2016 +0100
+++ b/jdk/src/java.base/share/classes/java/util/Formatter.java	Tue Jun 21 15:15:05 2016 -0700
@@ -3895,8 +3895,7 @@
                 TimeZone tz = t.getTimeZone();
                 sb.append(tz.getDisplayName((t.get(Calendar.DST_OFFSET) != 0),
                                            TimeZone.SHORT,
-                                           Objects.requireNonNullElse(l,
-                                               Locale.getDefault(Locale.Category.FORMAT))));
+                                           Objects.requireNonNullElse(l, Locale.US)));
                 break;
             }
 
@@ -3904,8 +3903,7 @@
             case DateTime.NAME_OF_DAY_ABBREV:     // 'a'
             case DateTime.NAME_OF_DAY:          { // 'A'
                 int i = t.get(Calendar.DAY_OF_WEEK);
-                Locale lt = Objects.requireNonNullElse(l,
-                                               Locale.getDefault(Locale.Category.FORMAT));
+                Locale lt = Objects.requireNonNullElse(l, Locale.US);
                 DateFormatSymbols dfs = DateFormatSymbols.getInstance(lt);
                 if (c == DateTime.NAME_OF_DAY)
                     sb.append(dfs.getWeekdays()[i]);
@@ -3917,8 +3915,7 @@
             case DateTime.NAME_OF_MONTH_ABBREV_X: // 'h' -- same b
             case DateTime.NAME_OF_MONTH:        { // 'B'
                 int i = t.get(Calendar.MONTH);
-                Locale lt = Objects.requireNonNullElse(l,
-                                               Locale.getDefault(Locale.Category.FORMAT));
+                Locale lt = Objects.requireNonNullElse(l, Locale.US);
                 DateFormatSymbols dfs = DateFormatSymbols.getInstance(lt);
                 if (c == DateTime.NAME_OF_MONTH)
                     sb.append(dfs.getMonths()[i]);
--- a/jdk/test/java/util/Formatter/FormatLocale.java	Tue Jun 21 18:51:18 2016 +0100
+++ b/jdk/test/java/util/Formatter/FormatLocale.java	Tue Jun 21 15:15:05 2016 -0700
@@ -23,17 +23,22 @@
 
 /**
  * @test
- * @bug 8146156
+ * @bug 8146156 8159548
  * @summary test whether uppercasing follows Locale.Category.FORMAT locale.
  * @run main/othervm FormatLocale
  */
 
 import java.time.LocalDate;
+import java.time.ZonedDateTime;
+import java.time.ZoneId;
 import java.time.Month;
+import java.util.Calendar;
 import java.util.Formatter;
+import java.util.GregorianCalendar;
 import java.util.List;
 import java.util.Locale;
 import java.util.Locale.Category;
+import java.util.TimeZone;
 import java.util.stream.IntStream;
 
 public class FormatLocale {
@@ -61,7 +66,7 @@
         "N\u0130SAN",
         "1,00000E+08");
 
-    public static void main(String [] args) {
+    static void formatLocaleTest() {
         StringBuilder sb = new StringBuilder();
 
         IntStream.range(0, src.size()).forEach(i -> {
@@ -79,4 +84,44 @@
             }
         });
     }
+
+    static void nullLocaleTest() {
+        String fmt = "%1$ta %1$tA %1$th %1$tB %1tZ";
+        String expected = "Fri Friday Jan January PST";
+        StringBuilder sb = new StringBuilder();
+        Locale orig = Locale.getDefault();
+
+        try {
+            Locale.setDefault(Locale.JAPAN);
+            Formatter f = new Formatter(sb, (Locale)null);
+            ZoneId zid = ZoneId.of("America/Los_Angeles");
+            Calendar c = new GregorianCalendar(TimeZone.getTimeZone(zid), Locale.US);
+            c.set(2016, 0, 1, 0, 0, 0);
+            f.format(fmt, c);
+            if (!sb.toString().equals(expected)) {
+                throw new RuntimeException(
+                    "Localized text returned with null locale.\n" +
+                    "    expected: " + expected + "\n" +
+                    "    returned: " + sb.toString());
+            }
+
+            sb.setLength(0);
+            ZonedDateTime zdt = ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, zid);
+            f.format(fmt, zdt);
+
+            if (!sb.toString().equals(expected)) {
+                throw new RuntimeException(
+                    "Localized text returned with null locale.\n" +
+                    "    expected: " + expected + "\n" +
+                    "    returned: " + sb.toString());
+            }
+        } finally {
+            Locale.setDefault(orig);
+        }
+    }
+
+    public static void main(String [] args) {
+        formatLocaleTest();
+        nullLocaleTest();
+    }
 }