2 *******************************************************************************
\r
3 * Copyright (C) 2008-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.localespi;
\r
9 import java.util.Locale;
\r
10 import java.util.TimeZone;
\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
16 public class TimeZoneNameTest extends TestFmwk {
\r
17 public static void main(String[] args) throws Exception {
\r
18 new TimeZoneNameTest().run(args);
\r
21 public void TestTimeZoneNames() {
\r
22 Locale[] locales = Locale.getAvailableLocales();
\r
23 String[] tzids = TimeZone.getAvailableIDs();
\r
25 for (Locale loc : locales) {
\r
26 boolean warningOnly = false;
\r
27 if (TestUtil.isProblematicIBMLocale(loc)) {
\r
30 else if ((TestUtil.isSUNJRE() || TestUtil.isIBMJRE()) && loc.toString().startsWith("eu")) {
\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
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
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
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
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
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
71 for (int i = 0; i < icuname.length(); i++) {
\r
72 if (UCharacter.isDigit(icuname.charAt(i))) {
\r
76 if (numDigits >= 3) {
\r
77 // ICU does not have the localized name
\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
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
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
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
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
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
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
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