From 799007c62d83a304d214be1f5e2f5cf66adbb074 Mon Sep 17 00:00:00 2001 From: Naoto Sato Date: Thu, 1 Nov 2012 13:28:47 -0700 Subject: [PATCH] 8001440: CLDR adapter: Invalid number extension in language tag causes exception in NumberFormat.format() Reviewed-by: okutsu --- .../java/text/DecimalFormatSymbols.java | 33 +++++++++++++------ .../java/util/Locale/LocaleProviders.java | 10 ++++++ jdk/test/java/util/Locale/LocaleProviders.sh | 10 +++++- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/jdk/src/share/classes/java/text/DecimalFormatSymbols.java b/jdk/src/share/classes/java/text/DecimalFormatSymbols.java index 3e79575c46c..81ab956a437 100644 --- a/jdk/src/share/classes/java/text/DecimalFormatSymbols.java +++ b/jdk/src/share/classes/java/text/DecimalFormatSymbols.java @@ -42,7 +42,9 @@ import java.io.IOException; 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 @@ public class DecimalFormatSymbols implements Cloneable, Serializable { } 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; } diff --git a/jdk/test/java/util/Locale/LocaleProviders.java b/jdk/test/java/util/Locale/LocaleProviders.java index 6bc499d2d38..b6320727943 100644 --- a/jdk/test/java/util/Locale/LocaleProviders.java +++ b/jdk/test/java/util/Locale/LocaleProviders.java @@ -51,6 +51,10 @@ public class LocaleProviders { tzNameTest(args[1]); break; + case "bug8001440Test": + bug8001440Test(); + break; + default: throw new RuntimeException("Test method '"+methodName+"' not found."); } @@ -93,4 +97,10 @@ public class LocaleProviders { 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); + } } diff --git a/jdk/test/java/util/Locale/LocaleProviders.sh b/jdk/test/java/util/Locale/LocaleProviders.sh index ab85ee72aa7..59e43fcff76 100644 --- a/jdk/test/java/util/Locale/LocaleProviders.sh +++ b/jdk/test/java/util/Locale/LocaleProviders.sh @@ -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 @@ PARAM2= PARAM3= runTest +# testing 8001440 fix. +METHODNAME=bug8001440Test +PREFLIST=CLDR +PARAM1= +PARAM2= +PARAM3= +runTest + exit $result