changeset 14336:4a3418a2c07f

8001440: CLDR adapter: Invalid number extension in language tag causes exception in NumberFormat.format() Reviewed-by: okutsu
author naoto
date Thu, 01 Nov 2012 13:28:47 -0700
parents 4314fe158bb9
children a003c6a54cb6
files jdk/src/share/classes/java/text/DecimalFormatSymbols.java jdk/test/java/util/Locale/LocaleProviders.java jdk/test/java/util/Locale/LocaleProviders.sh
diffstat 3 files changed, 42 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/src/share/classes/java/text/DecimalFormatSymbols.java	Thu Nov 01 10:33:32 2012 +0100
+++ b/jdk/src/share/classes/java/text/DecimalFormatSymbols.java	Thu Nov 01 13:28:47 2012 -0700
@@ -42,7 +42,9 @@
 import java.io.ObjectInputStream;
 import java.io.Serializable;
 import java.text.spi.DecimalFormatSymbolsProvider;
+import java.util.ArrayList;
 import java.util.Currency;
+import java.util.List;
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
@@ -554,20 +556,31 @@
             }
             ResourceBundle rb = adapter.getLocaleData().getNumberFormatData(locale);
             data = new Object[3];
+
+            // NumberElements look up. First, try the Unicode extension
+            String numElemKey;
             String numberType = locale.getUnicodeLocaleType("nu");
-            StringBuilder numElemKey =
-                new StringBuilder(numberType != null ?
-                                  numberType : rb.getString("DefaultNumberingSystem"));
-            if (numElemKey.length() != 0) {
-                numElemKey.append(".");
+            if (numberType != null) {
+                numElemKey = numberType + ".NumberElements";
+                if (rb.containsKey(numElemKey)) {
+                    data[0] = rb.getStringArray(numElemKey);
+                }
             }
-            numElemKey.append("NumberElements");
-            try {
-                data[0] = rb.getStringArray(numElemKey.toString());
-            } catch (MissingResourceException mre) {
-                // numberType must be bogus. Use the last resort numbering system.
+
+            // Next, try DefaultNumberingSystem value
+            if (data[0] == null && rb.containsKey("DefaultNumberingSystem")) {
+                numElemKey = rb.getString("DefaultNumberingSystem") + ".NumberElements";
+                if (rb.containsKey(numElemKey)) {
+                    data[0] = rb.getStringArray(numElemKey);
+                }
+            }
+
+            // Last resort. No need to check the availability.
+            // Just let it throw MissingResourceException when needed.
+            if (data[0] == null) {
                 data[0] = rb.getStringArray("NumberElements");
             }
+
             needCacheUpdate = true;
         }
 
--- a/jdk/test/java/util/Locale/LocaleProviders.java	Thu Nov 01 10:33:32 2012 +0100
+++ b/jdk/test/java/util/Locale/LocaleProviders.java	Thu Nov 01 13:28:47 2012 -0700
@@ -51,6 +51,10 @@
                 tzNameTest(args[1]);
                 break;
 
+            case "bug8001440Test":
+                bug8001440Test();
+                break;
+
             default:
                 throw new RuntimeException("Test method '"+methodName+"' not found.");
         }
@@ -93,4 +97,10 @@
             throw new RuntimeException("JRE's localized time zone name for "+id+" could not be retrieved. Returned name was: "+tzName);
         }
     }
+
+    static void bug8001440Test() {
+        Locale locale = Locale.forLanguageTag("th-TH-u-nu-hoge");
+        NumberFormat nf = NumberFormat.getInstance(locale);
+        String nu = nf.format(1234560);
+    }
 }
--- a/jdk/test/java/util/Locale/LocaleProviders.sh	Thu Nov 01 10:33:32 2012 +0100
+++ b/jdk/test/java/util/Locale/LocaleProviders.sh	Thu Nov 01 13:28:47 2012 -0700
@@ -23,7 +23,7 @@
 #!/bin/sh
 #
 # @test
-# @bug 6336885 7196799 7197573 7198834 8000245 8000615
+# @bug 6336885 7196799 7197573 7198834 8000245 8000615 8001440
 # @summary tests for "java.locale.providers" system property
 # @compile -XDignore.symbol.file LocaleProviders.java
 # @run shell/timeout=600 LocaleProviders.sh
@@ -245,4 +245,12 @@
 PARAM3=
 runTest
 
+# testing 8001440 fix.
+METHODNAME=bug8001440Test
+PREFLIST=CLDR
+PARAM1=
+PARAM2=
+PARAM3=
+runTest
+
 exit $result