]> granicus.if.org Git - icu/commitdiff
ICU-8518 Fixed a potential bug (toLowerCase, Calendar class name/type mismatch) in...
authorYoshito Umaoka <y.umaoka@gmail.com>
Thu, 28 Jul 2011 16:40:37 +0000 (16:40 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Thu, 28 Jul 2011 16:40:37 +0000 (16:40 +0000)
X-SVN-Rev: 30439

icu4j/main/classes/core/src/com/ibm/icu/text/DateFormatSymbols.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestDateFormatSymbols.java

index ef13b000223ec05a44458ff21ac919b1e1ff1d4a..94e4b32e950864dc17bd8c202b857b102ad804be 100644 (file)
@@ -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);
     }
index a5f2ac40a72d65a293bdc8035360072e24d4a14a..0188651c9059c82fa2a86a3ec190dfd9815965b3 100644 (file)
@@ -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]);
+            }
+        }
+
+    }
 }