From 757a7942eed2b0aa457f8517a0259d2ac82c5b18 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 16 Apr 2010 14:14:28 -0700 Subject: Merge LocaleData and Resources, rename Resources to ICU. Also move our ICU tests into our little tree of tests. Bug: 2596471 Change-Id: I73b53d74c26ef9bf670f12cac58b51ba61eefead --- luni/src/main/java/java/util/Calendar.java | 8 +- luni/src/main/java/java/util/Currency.java | 21 ++- luni/src/main/java/java/util/Formatter.java | 3 +- luni/src/main/java/java/util/Locale.java | 164 +++++++++------------ luni/src/main/java/java/util/ResourceBundle.java | 4 +- luni/src/main/java/java/util/TimeZone.java | 7 +- .../test/java/com/ibm/icu4jni/util/AllTests.java | 28 ++++ .../test/java/com/ibm/icu4jni/util/ICUTest.java | 74 ++++++++++ 8 files changed, 189 insertions(+), 120 deletions(-) create mode 100644 luni/src/test/java/com/ibm/icu4jni/util/AllTests.java create mode 100644 luni/src/test/java/com/ibm/icu4jni/util/ICUTest.java (limited to 'luni') diff --git a/luni/src/main/java/java/util/Calendar.java b/luni/src/main/java/java/util/Calendar.java index 6cb3ec7..3e5e43d 100644 --- a/luni/src/main/java/java/util/Calendar.java +++ b/luni/src/main/java/java/util/Calendar.java @@ -18,7 +18,7 @@ package java.util; import com.ibm.icu4jni.util.LocaleData; -import com.ibm.icu4jni.util.Resources; +import com.ibm.icu4jni.util.ICU; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; @@ -724,11 +724,9 @@ public abstract class Calendar implements Serializable, Cloneable, Comparable("user.language", "en")); //$NON-NLS-1$ //$NON-NLS-2$ - // BEGIN android-changed - String region = AccessController.doPrivileged(new PriviAction( - "user.region", "US")); //$NON-NLS-1$ //$NON-NLS-2$ - // END android-changed - String variant = AccessController.doPrivileged(new PriviAction( - "user.variant", "")); //$NON-NLS-1$ //$NON-NLS-2$ + String language = AccessController.doPrivileged(new PriviAction("user.language", "en")); + String region = AccessController.doPrivileged(new PriviAction("user.region", "US")); + String variant = AccessController.doPrivileged(new PriviAction("user.variant", "")); defaultLocale = new Locale(language, region, variant); } @@ -215,19 +210,15 @@ public final class Locale implements Cloneable, Serializable { private transient String variantCode; private transient String cachedToStringResult; - // BEGIN android-removed - // private transient ULocale uLocale; - // END android-removed - - /** - * Constructs a default which is used during static initialization of the - * default for the platform. - */ - private Locale() { - languageCode = "en"; //$NON-NLS-1$ - countryCode = "US"; //$NON-NLS-1$ - variantCode = ""; //$NON-NLS-1$ - } + /** + * Constructs a default which is used during static initialization of the + * default for the platform. + */ + private Locale() { + languageCode = "en"; + countryCode = "US"; + variantCode = ""; + } /** * Constructs a new {@code Locale} using the specified language. @@ -236,7 +227,7 @@ public final class Locale implements Cloneable, Serializable { * the language this {@code Locale} represents. */ public Locale(String language) { - this(language, "", ""); //$NON-NLS-1$//$NON-NLS-2$ + this(language, "", ""); } /** @@ -248,7 +239,7 @@ public final class Locale implements Cloneable, Serializable { * the country this {@code Locale} represents. */ public Locale(String language, String country) { - this(language, country, ""); //$NON-NLS-1$ + this(language, country, ""); } /** @@ -282,12 +273,12 @@ public final class Locale implements Cloneable, Serializable { // END android-changed // Map new language codes to the obsolete language // codes so the correct resource bundles will be used. - if (languageCode.equals("he")) {//$NON-NLS-1$ - languageCode = "iw"; //$NON-NLS-1$ - } else if (languageCode.equals("id")) {//$NON-NLS-1$ - languageCode = "in"; //$NON-NLS-1$ - } else if (languageCode.equals("yi")) {//$NON-NLS-1$ - languageCode = "ji"; //$NON-NLS-1$ + if (languageCode.equals("he")) { + languageCode = "iw"; + } else if (languageCode.equals("id")) { + languageCode = "in"; + } else if (languageCode.equals("yi")) { + languageCode = "ji"; } // countryCode is defined in ASCII character set @@ -348,7 +339,7 @@ public final class Locale implements Cloneable, Serializable { * @return an array of {@code Locale}s. */ public static Locale[] getAvailableLocales() { - return Resources.getAvailableLocales(); + return ICU.getAvailableLocales(); } /** @@ -391,16 +382,14 @@ public final class Locale implements Cloneable, Serializable { * @return a country name. */ public String getDisplayCountry(Locale locale) { - // BEGIN android-changed if (countryCode.length() == 0) { return countryCode; } - String result = Resources.getDisplayCountryNative(toString(), locale.toString()); + String result = ICU.getDisplayCountryNative(toString(), locale.toString()); if (result == null) { // TODO: do we need to do this, or does ICU do it for us? - result = Resources.getDisplayCountryNative(toString(), Locale.getDefault().toString()); + result = ICU.getDisplayCountryNative(toString(), Locale.getDefault().toString()); } return result; - // END android-changed } /** @@ -424,16 +413,14 @@ public final class Locale implements Cloneable, Serializable { * @return a language name. */ public String getDisplayLanguage(Locale locale) { - // BEGIN android-changed if (languageCode.length() == 0) { return languageCode; } - String result = Resources.getDisplayLanguageNative(toString(), locale.toString()); + String result = ICU.getDisplayLanguageNative(toString(), locale.toString()); if (result == null) { // TODO: do we need to do this, or does ICU do it for us? - result = Resources.getDisplayLanguageNative(toString(), Locale.getDefault().toString()); + result = ICU.getDisplayLanguageNative(toString(), Locale.getDefault().toString()); } return result; - // END android-changed } /** @@ -463,22 +450,22 @@ public final class Locale implements Cloneable, Serializable { } if (countryCode.length() > 0) { if (count == 1) { - buffer.append(" ("); //$NON-NLS-1$ + buffer.append(" ("); } buffer.append(getDisplayCountry(locale)); count++; } if (variantCode.length() > 0) { if (count == 1) { - buffer.append(" ("); //$NON-NLS-1$ + buffer.append(" ("); } else if (count == 2) { - buffer.append(","); //$NON-NLS-1$ + buffer.append(","); } buffer.append(getDisplayVariant(locale)); count++; } if (count > 1) { - buffer.append(")"); //$NON-NLS-1$ + buffer.append(")"); } return buffer.toString(); } @@ -504,16 +491,14 @@ public final class Locale implements Cloneable, Serializable { * @return a variant name. */ public String getDisplayVariant(Locale locale) { - // BEGIN android-changed if (variantCode.length() == 0) { return variantCode; } - String result = Resources.getDisplayVariantNative(toString(), locale.toString()); + String result = ICU.getDisplayVariantNative(toString(), locale.toString()); if (result == null) { // TODO: do we need to do this, or does ICU do it for us? - result = Resources.getDisplayVariantNative(toString(), Locale.getDefault().toString()); + result = ICU.getDisplayVariantNative(toString(), Locale.getDefault().toString()); } return result; - // END android-changed } /** @@ -525,12 +510,10 @@ public final class Locale implements Cloneable, Serializable { * if there is no matching three letter ISO country code. */ public String getISO3Country() throws MissingResourceException { - // BEGIN android-changed if (countryCode.length() == 0) { return countryCode; } - return Resources.getISO3CountryNative(toString()); - // END android-changed + return ICU.getISO3CountryNative(toString()); } /** @@ -542,12 +525,10 @@ public final class Locale implements Cloneable, Serializable { * if there is no matching three letter ISO language code. */ public String getISO3Language() throws MissingResourceException { - // BEGIN android-changed if (languageCode.length() == 0) { return languageCode; } - return Resources.getISO3LanguageNative(toString()); - // END android-changed + return ICU.getISO3LanguageNative(toString()); } /** @@ -557,9 +538,7 @@ public final class Locale implements Cloneable, Serializable { * @return an array of strings. */ public static String[] getISOCountries() { - // BEGIN android-changed - return Resources.getISOCountries(); - // END android-changed + return ICU.getISOCountries(); } /** @@ -569,9 +548,7 @@ public final class Locale implements Cloneable, Serializable { * @return an array of strings. */ public static String[] getISOLanguages() { - // BEGIN android-changed - return Resources.getISOLanguages(); - // END android-changed + return ICU.getISOLanguages(); } /** @@ -670,25 +647,24 @@ public final class Locale implements Cloneable, Serializable { } private static final ObjectStreamField[] serialPersistentFields = { - new ObjectStreamField("country", String.class), //$NON-NLS-1$ - new ObjectStreamField("hashcode", Integer.TYPE), //$NON-NLS-1$ - new ObjectStreamField("language", String.class), //$NON-NLS-1$ - new ObjectStreamField("variant", String.class) }; //$NON-NLS-1$ + new ObjectStreamField("country", String.class), + new ObjectStreamField("hashcode", Integer.TYPE), + new ObjectStreamField("language", String.class), + new ObjectStreamField("variant", String.class) }; private void writeObject(ObjectOutputStream stream) throws IOException { ObjectOutputStream.PutField fields = stream.putFields(); - fields.put("country", countryCode); //$NON-NLS-1$ - fields.put("hashcode", -1); //$NON-NLS-1$ - fields.put("language", languageCode); //$NON-NLS-1$ - fields.put("variant", variantCode); //$NON-NLS-1$ + fields.put("country", countryCode); + fields.put("hashcode", -1); + fields.put("language", languageCode); + fields.put("variant", variantCode); stream.writeFields(); } - private void readObject(ObjectInputStream stream) throws IOException, - ClassNotFoundException { + private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { ObjectInputStream.GetField fields = stream.readFields(); - countryCode = (String) fields.get("country", ""); //$NON-NLS-1$//$NON-NLS-2$ - languageCode = (String) fields.get("language", ""); //$NON-NLS-1$//$NON-NLS-2$ - variantCode = (String) fields.get("variant", ""); //$NON-NLS-1$//$NON-NLS-2$ + countryCode = (String) fields.get("country", ""); + languageCode = (String) fields.get("language", ""); + variantCode = (String) fields.get("variant", ""); } } diff --git a/luni/src/main/java/java/util/ResourceBundle.java b/luni/src/main/java/java/util/ResourceBundle.java index 8901e45..dda89b9 100644 --- a/luni/src/main/java/java/util/ResourceBundle.java +++ b/luni/src/main/java/java/util/ResourceBundle.java @@ -17,7 +17,7 @@ package java.util; -import com.ibm.icu4jni.util.Resources; +import com.ibm.icu4jni.util.ICU; import dalvik.system.VMStack; import java.io.File; import java.io.IOException; @@ -632,7 +632,7 @@ public abstract class ResourceBundle { } private void setLocale(String name) { - setLocale(Resources.localeFromString(name)); + setLocale(ICU.localeFromString(name)); } public static final void clearCache() { diff --git a/luni/src/main/java/java/util/TimeZone.java b/luni/src/main/java/java/util/TimeZone.java index dd320b8..a195045 100644 --- a/luni/src/main/java/java/util/TimeZone.java +++ b/luni/src/main/java/java/util/TimeZone.java @@ -17,13 +17,10 @@ package java.util; +import com.ibm.icu4jni.util.ICU; import java.io.Serializable; - -// BEGIN android-added import org.apache.harmony.luni.internal.util.ZoneInfo; import org.apache.harmony.luni.internal.util.ZoneInfoDB; -import com.ibm.icu4jni.util.Resources; -// END android-added /** * {@code TimeZone} represents a time zone offset, taking into account @@ -276,7 +273,7 @@ public abstract class TimeZone implements Serializable, Cloneable { if (style == SHORT || style == LONG) { boolean useDaylight = daylightTime && useDaylightTime(); - String result = Resources.getDisplayTimeZone(getID(), daylightTime, style, locale.toString()); + String result = ICU.getDisplayTimeZone(getID(), daylightTime, style, locale.toString()); if (result != null) { return result; } diff --git a/luni/src/test/java/com/ibm/icu4jni/util/AllTests.java b/luni/src/test/java/com/ibm/icu4jni/util/AllTests.java new file mode 100644 index 0000000..2db1dcc --- /dev/null +++ b/luni/src/test/java/com/ibm/icu4jni/util/AllTests.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ibm.icu4jni.util; + +import junit.framework.Test; +import junit.framework.TestSuite; + +public class AllTests { + public static final Test suite() { + TestSuite suite = new TestSuite(); + suite.addTestSuite(ICUTest.class); + return suite; + } +} diff --git a/luni/src/test/java/com/ibm/icu4jni/util/ICUTest.java b/luni/src/test/java/com/ibm/icu4jni/util/ICUTest.java new file mode 100644 index 0000000..f8797b3 --- /dev/null +++ b/luni/src/test/java/com/ibm/icu4jni/util/ICUTest.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ibm.icu4jni.util; + +import java.util.Locale; + +public class ICUTest extends junit.framework.TestCase { + public void test_getISOLanguages() throws Exception { + // Check that corrupting our array doesn't affect other callers. + assertNotNull(ICU.getISOLanguages()[0]); + ICU.getISOLanguages()[0] = null; + assertNotNull(ICU.getISOLanguages()[0]); + } + + public void test_getISOCountries() throws Exception { + // Check that corrupting our array doesn't affect other callers. + assertNotNull(ICU.getISOCountries()[0]); + ICU.getISOCountries()[0] = null; + assertNotNull(ICU.getISOCountries()[0]); + } + + public void test_getAvailableLocales() throws Exception { + // Check that corrupting our array doesn't affect other callers. + assertNotNull(ICU.getAvailableLocales()[0]); + ICU.getAvailableLocales()[0] = null; + assertNotNull(ICU.getAvailableLocales()[0]); + } + + public void test_getKnownTimezones() throws Exception { + // Check that corrupting our array doesn't affect other callers. + assertNotNull(ICU.getKnownTimezones()[0]); + ICU.getKnownTimezones()[0] = null; + assertNotNull(ICU.getKnownTimezones()[0]); + } + + public void test_getDisplayTimeZones() throws Exception { + // Check that corrupting our array doesn't affect other callers. + assertNotNull(ICU.getDisplayTimeZones(null)[0]); + ICU.getDisplayTimeZones(null)[0] = null; + assertNotNull(ICU.getDisplayTimeZones(null)[0]); + // getDisplayTimezones actually returns a String[][] rather than a String[]. + assertNotNull(ICU.getDisplayTimeZones(null)[0][0]); + ICU.getDisplayTimeZones(null)[0][0] = null; + assertNotNull(ICU.getDisplayTimeZones(null)[0][0]); + } + + public void test_localeFromString() throws Exception { + // localeFromString is pretty lenient. Some of these can't be round-tripped + // through Locale.toString. + assertEquals(Locale.ENGLISH, ICU.localeFromString("en")); + assertEquals(Locale.ENGLISH, ICU.localeFromString("en_")); + assertEquals(Locale.ENGLISH, ICU.localeFromString("en__")); + assertEquals(Locale.US, ICU.localeFromString("en_US")); + assertEquals(Locale.US, ICU.localeFromString("en_US_")); + assertEquals(new Locale("", "US", ""), ICU.localeFromString("_US")); + assertEquals(new Locale("", "US", ""), ICU.localeFromString("_US_")); + assertEquals(new Locale("", "", "POSIX"), ICU.localeFromString("__POSIX")); + assertEquals(new Locale("aa", "BB", "CC"), ICU.localeFromString("aa_BB_CC")); + } +} -- cgit v1.1