]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/TimeZoneNameTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / localespi / src / com / ibm / icu / dev / test / localespi / TimeZoneNameTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2008-2009, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.localespi;\r
8 \r
9 import java.util.Locale;\r
10 import java.util.TimeZone;\r
11 \r
12 import com.ibm.icu.dev.test.TestFmwk;\r
13 import com.ibm.icu.lang.UCharacter;\r
14 import com.ibm.icu.util.ULocale;\r
15 \r
16 public class TimeZoneNameTest extends TestFmwk {\r
17     public static void main(String[] args) throws Exception {\r
18         new TimeZoneNameTest().run(args);\r
19     }\r
20 \r
21     public void TestTimeZoneNames() {\r
22         Locale[] locales = Locale.getAvailableLocales();\r
23         String[] tzids = TimeZone.getAvailableIDs();\r
24 \r
25         for (Locale loc : locales) {\r
26             boolean warningOnly = false;\r
27             if (TestUtil.isProblematicIBMLocale(loc)) {\r
28                 warningOnly = true;\r
29             }\r
30             else if ((TestUtil.isSUNJRE() || TestUtil.isIBMJRE()) && loc.toString().startsWith("eu")) {\r
31                 warningOnly = true;\r
32             }\r
33 \r
34             for (String tzid : tzids) {\r
35                 TimeZone tz = TimeZone.getTimeZone(tzid);\r
36                 com.ibm.icu.util.TimeZone tzIcu = com.ibm.icu.util.TimeZone.getTimeZone(tzid);\r
37                 checkDisplayNamePair(TimeZone.SHORT, tz, tzIcu, loc, warningOnly);\r
38                 checkDisplayNamePair(TimeZone.LONG, tz, tzIcu, loc, warningOnly);\r
39             }\r
40         }\r
41     }\r
42 \r
43     private void checkDisplayNamePair(int style, TimeZone tz, com.ibm.icu.util.TimeZone icuTz, Locale loc, boolean warnOnly) {\r
44         /* Note: There are two problems here.\r
45          * \r
46          * It looks Java 6 requires a TimeZoneNameProvider to return both standard name and daylight name\r
47          * for a zone.  If the provider implementation only returns either of them, Java 6 also ignore\r
48          * the other.  In ICU, there are zones which do not have daylight names, especially zones which\r
49          * do not use daylight time.  This test case does not check a standard name if its daylight name\r
50          * is not available because of the Java 6 implementation problem.\r
51          * \r
52          * Another problem is that ICU always use a standard name for a zone which does not use daylight\r
53          * saving time even daylight name is requested.\r
54          */\r
55 \r
56         String icuStdName = getIcuDisplayName(icuTz, false, style, loc);\r
57         String icuDstName = getIcuDisplayName(icuTz, true, style, loc);\r
58         if (icuStdName != null && icuDstName != null && !icuStdName.equals(icuDstName)) {\r
59             checkDisplayName(false, style, tz, loc, icuStdName, warnOnly);\r
60             checkDisplayName(true, style, tz, loc, icuDstName, warnOnly);\r
61         }\r
62     }\r
63 \r
64     private String getIcuDisplayName(com.ibm.icu.util.TimeZone icuTz, boolean daylight, int style, Locale loc) {\r
65         ULocale uloc = ULocale.forLocale(loc);\r
66         boolean shortStyle = (style == TimeZone.SHORT);\r
67         String icuname = icuTz.getDisplayName(daylight,\r
68                 (shortStyle ? com.ibm.icu.util.TimeZone.SHORT : com.ibm.icu.util.TimeZone.LONG),\r
69                 uloc);\r
70         int numDigits = 0;\r
71         for (int i = 0; i < icuname.length(); i++) {\r
72             if (UCharacter.isDigit(icuname.charAt(i))) {\r
73                 numDigits++;\r
74             }\r
75         }\r
76         if (numDigits >= 3) {\r
77             // ICU does not have the localized name\r
78             return null;\r
79         }\r
80         return icuname;\r
81     }\r
82 \r
83     private void checkDisplayName(boolean daylight, int style, TimeZone tz, Locale loc, String icuname, boolean warnOnly) {\r
84         String styleStr = (style == TimeZone.SHORT) ? "SHORT" : "LONG";\r
85 \r
86         String name = tz.getDisplayName(daylight, style, loc);\r
87         if (TestUtil.isICUExtendedLocale(loc)) {\r
88             // The name should be taken from ICU\r
89             if (!name.equals(icuname)) {\r
90                 if (warnOnly) {\r
91                     logln("WARNING: TimeZone name by ICU is " + icuname + ", but got " + name\r
92                             + " for time zone " + tz.getID() + " in locale " + loc\r
93                             + " (daylight=" + daylight + ", style=" + styleStr + ")");\r
94                     \r
95                 } else {\r
96                     errln("FAIL: TimeZone name by ICU is " + icuname + ", but got " + name\r
97                             + " for time zone " + tz.getID() + " in locale " + loc\r
98                             + " (daylight=" + daylight + ", style=" + styleStr + ")");\r
99                 }\r
100             }\r
101         } else {\r
102             if (!name.equals(icuname)) {\r
103                 logln("INFO: TimeZone name by ICU is " + icuname + ", but got " + name\r
104                         + " for time zone " + tz.getID() + " in locale " + loc\r
105                         + " (daylight=" + daylight + ", style=" + styleStr + ")");\r
106             }\r
107             // Try explicit ICU locale (xx_yy_ICU)\r
108             Locale icuLoc = TestUtil.toICUExtendedLocale(loc);\r
109             name = tz.getDisplayName(daylight, style, icuLoc);\r
110             if (!name.equals(icuname)) {\r
111                 if (warnOnly) {\r
112                     logln("WARNING: TimeZone name by ICU is " + icuname + ", but got " + name\r
113                             + " for time zone " + tz.getID() + " in locale " + icuLoc\r
114                             + " (daylight=" + daylight + ", style=" + styleStr + ")");\r
115                 } else {\r
116                     errln("FAIL: TimeZone name by ICU is " + icuname + ", but got " + name\r
117                             + " for time zone " + tz.getID() + " in locale " + icuLoc\r
118                             + " (daylight=" + daylight + ", style=" + styleStr + ")");\r
119                 }\r
120             }\r
121         }\r
122     }\r
123 }\r