From: Yoshito Umaoka Date: Thu, 28 Jul 2011 16:40:37 +0000 (+0000) Subject: ICU-8518 Fixed a potential bug (toLowerCase, Calendar class name/type mismatch) in... X-Git-Tag: milestone-59-0-1~4611 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=09ac0d986bfb043e39873e78b96f384cd704705e;p=icu ICU-8518 Fixed a potential bug (toLowerCase, Calendar class name/type mismatch) in the DateFormatSymbols constructor taking a Calendar class as an argument. X-SVN-Rev: 30439 --- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DateFormatSymbols.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DateFormatSymbols.java index ef13b000223..94e4b32e950 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/DateFormatSymbols.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DateFormatSymbols.java @@ -464,6 +464,21 @@ public class DateFormatSymbols implements Serializable, Cloneable { /* use serialVersionUID from JDK 1.1.4 for interoperability */ private static final long serialVersionUID = -5987973545549424702L; + private static final String[][] CALENDAR_CLASSES = { + {"GregorianCalendar", "gregorian"}, + {"JapaneseCalendar", "japanese"}, + {"BuddhistCalendar", "buddhist"}, + {"TaiwanCalendar", "roc"}, + {"PersianCalendar", "persian"}, + {"IslamicCalendar", "islamic"}, + {"HebrewCalendar", "hebrew"}, + {"ChineseCalendar", "chinese"}, + {"IndianCalendar", "indian"}, + {"CopticCalendar", "coptic"}, + {"EthiopicCalendar", "ethiopic"}, + }; + + /** * Returns era strings. For example: "AD" and "BC". * @return the era strings. @@ -1356,7 +1371,7 @@ public class DateFormatSymbols implements Serializable, Cloneable { /** * Variant of DateFormatSymbols(Calendar, ULocale) that takes the Calendar class - * instead of a Calandar instance. + * instead of a Calendar instance. * @see #DateFormatSymbols(Calendar, Locale) * @stable ICU 3.2 */ @@ -1364,7 +1379,16 @@ public class DateFormatSymbols implements Serializable, Cloneable { String fullName = calendarClass.getName(); int lastDot = fullName.lastIndexOf('.'); String className = fullName.substring(lastDot+1); - String calType = className.replaceAll("Calendar", "").toLowerCase(); + String calType = null; + for (String[] calClassInfo : CALENDAR_CLASSES) { + if (calClassInfo[0].equals(className)) { + calType = calClassInfo[1]; + break; + } + } + if (calType == null) { + calType = className.replaceAll("Calendar", "").toLowerCase(Locale.ENGLISH); + } initializeData(locale, calType); } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestDateFormatSymbols.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestDateFormatSymbols.java index a5f2ac40a72..0188651c905 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestDateFormatSymbols.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestDateFormatSymbols.java @@ -1,6 +1,6 @@ /***************************************************************************************** * - * Copyright (C) 1996-2010, International Business Machines + * Copyright (C) 1996-2011, International Business Machines * Corporation and others. All Rights Reserved. **/ @@ -19,6 +19,8 @@ package com.ibm.icu.dev.test.format; import java.util.Locale; import com.ibm.icu.text.DateFormatSymbols; +import com.ibm.icu.util.Calendar; +import com.ibm.icu.util.ULocale; public class IntlTestDateFormatSymbols extends com.ibm.icu.dev.test.TestFmwk { @@ -477,4 +479,46 @@ public class IntlTestDateFormatSymbols extends com.ibm.icu.dev.test.TestFmwk errln("ERROR: Clone failed"); } } + + public void TestConstructorWithCalendar() { + ULocale[] TestLocales = { + new ULocale("en_US@caleandar=gregorian"), + new ULocale("ja_JP@calendar=japanese"), + new ULocale("th_TH@calendar=buddhist"), + new ULocale("zh_TW@calendar=roc"), + new ULocale("ar_IR@calendar=persian"), + new ULocale("ar_EG@calendar=islamic"), + new ULocale("he_IL@calendar=hebrew"), + new ULocale("zh_CN@calendar=chinese"), + new ULocale("hi_IN@calendar=indian"), + new ULocale("ar_EG@calendar=coptic"), + new ULocale("am_ET@calendar=ethiopic"), + }; + + int i; + + // calendars + Calendar[] calendars = new Calendar[TestLocales.length]; + for (i = 0; i < TestLocales.length; i++) { + calendars[i] = Calendar.getInstance(TestLocales[i]); + } + + // Creates an instance from a base locale + calendar + DateFormatSymbols[] symbols = new DateFormatSymbols[TestLocales.length]; + for (i = 0; i < TestLocales.length; i++) { + symbols[i] = new DateFormatSymbols(calendars[i], new ULocale(TestLocales[i].getBaseName())); + } + + // Compare an instance created from a base locale + calendar + // with an instance created from its base locale + calendar class + for (i = 0; i < TestLocales.length; i++) { + DateFormatSymbols dfs = new DateFormatSymbols(calendars[i].getClass(), new ULocale(TestLocales[i].getBaseName())); + if (!dfs.equals(symbols[i])) { + errln("FAIL: DateFormatSymbols created from a base locale and calendar instance" + + " is different from one created from the same base locale and calendar class - " + + TestLocales[i]); + } + } + + } }