blob: 2c83e081f1d73770266fb55a399818d5ed9a2adb [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23/*
24 *
25 */
26
27import java.text.*;
28import java.util.*;
29import sun.util.*;
30import sun.util.resources.*;
31
32public class TimeZoneNameProviderTest extends ProviderTest {
33
34 com.bar.TimeZoneNameProviderImpl tznp = new com.bar.TimeZoneNameProviderImpl();
35
36 public static void main(String[] s) {
37 new TimeZoneNameProviderTest();
38 }
39
40 TimeZoneNameProviderTest() {
41 test1();
42 test2();
43 aliasTest();
44 }
45
46 void test1() {
47 Locale[] available = Locale.getAvailableLocales();
48 List<Locale> providerLocales = Arrays.asList(tznp.getAvailableLocales());
49 String[] ids = TimeZone.getAvailableIDs();
50
51 for (Locale target: available) {
52 // pure JRE implementation
53 OpenListResourceBundle rb = LocaleData.getTimeZoneNames(target);
54 boolean jreHasBundle = rb.getLocale().equals(target);
55
56 for (String id: ids) {
57 // the time zone
58 TimeZone tz = TimeZone.getTimeZone(id);
59
60 // JRE string array for the id
61 String[] jrearray = null;
62 if (jreHasBundle) {
63 try {
64 jrearray = rb.getStringArray(id);
65 } catch (MissingResourceException mre) {}
66 }
67
68 for (int i = 1; i <=(tz.useDaylightTime()?4:2); i++) {
69 // the localized name
70 String name = tz.getDisplayName(i>=3, i%2, target);
71
72 // provider's name (if any)
73 String providersname = null;
74 if (providerLocales.contains(target)) {
75 providersname = tznp.getDisplayName(id, i>=3, i%2, target);
76 }
77
78 // JRE's name (if any)
79 String jresname = null;
80 if (jrearray != null) {
81 jresname = jrearray[i];
82 }
83
84 checkValidity(target, jresname, providersname, name,
85 jreHasBundle && rb.handleGetKeys().contains(id));
86 }
87 }
88 }
89 }
90
91 final String pattern = "z";
92 final Locale OSAKA = new Locale("ja", "JP", "osaka");
93 final Locale KYOTO = new Locale("ja", "JP", "kyoto");
94
95 final String[] TIMEZONES = {
96 "GMT", "America/Los_Angeles", "SystemV/PST8",
97 "SystemV/PST8PDT", "PST8PDT",
98 };
99 final String[] DISPLAY_NAMES_OSAKA = {
100 tznp.getDisplayName(TIMEZONES[0], false, TimeZone.SHORT, OSAKA),
101 tznp.getDisplayName(TIMEZONES[1], false, TimeZone.SHORT, OSAKA),
102 tznp.getDisplayName(TIMEZONES[2], false, TimeZone.SHORT, OSAKA),
103 tznp.getDisplayName(TIMEZONES[3], false, TimeZone.SHORT, OSAKA),
104 tznp.getDisplayName(TIMEZONES[4], false, TimeZone.SHORT, OSAKA)
105 };
106 final String[] DISPLAY_NAMES_KYOTO = {
107 tznp.getDisplayName(TIMEZONES[0], false, TimeZone.SHORT, KYOTO),
108 tznp.getDisplayName(TIMEZONES[1], false, TimeZone.SHORT, KYOTO),
109 tznp.getDisplayName(TIMEZONES[2], false, TimeZone.SHORT, KYOTO),
110 tznp.getDisplayName(TIMEZONES[3], false, TimeZone.SHORT, KYOTO),
111 tznp.getDisplayName(TIMEZONES[4], false, TimeZone.SHORT, KYOTO)
112 };
113
114 void test2() {
115 Locale defaultLocale = Locale.getDefault();
116 Date d = new Date(2005-1900, Calendar.DECEMBER, 22);
117 String formatted;
118
119 TimeZone tz;
120 SimpleDateFormat df;
121
122 try {
123 for (int i = 0; i < TIMEZONES.length; i++) {
124 tz = TimeZone.getTimeZone(TIMEZONES[i]);
125 TimeZone.setDefault(tz);
126 df = new SimpleDateFormat(pattern, DateFormatSymbols.getInstance(OSAKA));
127 Locale.setDefault(defaultLocale);
128 System.out.println(formatted = df.format(d));
129 if(!formatted.equals(DISPLAY_NAMES_OSAKA[i])) {
130 throw new RuntimeException("TimeZone " + TIMEZONES[i] +
131 ": formatted zone names mismatch. " +
132 formatted + " should match with " +
133 DISPLAY_NAMES_OSAKA[i]);
134 }
135
136 df.parse(DISPLAY_NAMES_OSAKA[i]);
137
138 Locale.setDefault(KYOTO);
139 df = new SimpleDateFormat(pattern, DateFormatSymbols.getInstance());
140 System.out.println(formatted = df.format(d));
141 if(!formatted.equals(DISPLAY_NAMES_KYOTO[i])) {
142 Locale.setDefault(defaultLocale);
143 throw new RuntimeException("Timezone " + TIMEZONES[i] +
144 ": formatted zone names mismatch. " +
145 formatted + " should match with " +
146 DISPLAY_NAMES_KYOTO[i]);
147 }
148 df.parse(DISPLAY_NAMES_KYOTO[i]);
149 }
150 } catch (ParseException pe) {
151 Locale.setDefault(defaultLocale);
152 throw new RuntimeException("parse error occured" + pe);
153 }
154 Locale.setDefault(defaultLocale);
155 }
156
157 final String LATIME = "America/Los_Angeles";
158 final String PST = "PST";
159 final String PST8PDT = "PST8PDT";
160 final String US_PACIFIC = "US/Pacific";
161 final String LATIME_IN_OSAKA =
162 tznp.getDisplayName(LATIME, false, TimeZone.LONG, OSAKA);
163
164 final String TOKYOTIME = "Asia/Tokyo";
165 final String JST = "JST";
166 final String JAPAN = "Japan";
167 final String JST_IN_OSAKA =
168 tznp.getDisplayName(JST, false, TimeZone.LONG, OSAKA);
169
170 void aliasTest() {
171 // Check that provider's name for a standard id (America/Los_Angeles) is
172 // propagated to its aliases
173 String latime = TimeZone.getTimeZone(LATIME).getDisplayName(OSAKA);
174 if (!LATIME_IN_OSAKA.equals(latime)) {
175 throw new RuntimeException("Could not get provider's localized name. result: "+latime+" expected: "+LATIME_IN_OSAKA);
176 }
177
178 String pst = TimeZone.getTimeZone(PST).getDisplayName(OSAKA);
179 if (!LATIME_IN_OSAKA.equals(pst)) {
180 throw new RuntimeException("Provider's localized name is not available for an alias ID: "+PST+". result: "+pst+" expected: "+LATIME_IN_OSAKA);
181 }
182
183 String us_pacific = TimeZone.getTimeZone(US_PACIFIC).getDisplayName(OSAKA);
184 if (!LATIME_IN_OSAKA.equals(us_pacific)) {
185 throw new RuntimeException("Provider's localized name is not available for an alias ID: "+US_PACIFIC+". result: "+us_pacific+" expected: "+LATIME_IN_OSAKA);
186 }
187
188 // Check that provider's name for an alias id (JST) is
189 // propagated to its standard id and alias ids.
190 String jstime = TimeZone.getTimeZone(JST).getDisplayName(OSAKA);
191 if (!JST_IN_OSAKA.equals(jstime)) {
192 throw new RuntimeException("Could not get provider's localized name. result: "+jstime+" expected: "+JST_IN_OSAKA);
193 }
194
195 String tokyotime = TimeZone.getTimeZone(TOKYOTIME).getDisplayName(OSAKA);
196 if (!JST_IN_OSAKA.equals(tokyotime)) {
197 throw new RuntimeException("Provider's localized name is not available for a standard ID: "+TOKYOTIME+". result: "+tokyotime+" expected: "+JST_IN_OSAKA);
198 }
199
200 String japan = TimeZone.getTimeZone(JAPAN).getDisplayName(OSAKA);
201 if (!JST_IN_OSAKA.equals(japan)) {
202 throw new RuntimeException("Provider's localized name is not available for an alias ID: "+JAPAN+". result: "+japan+" expected: "+JST_IN_OSAKA);
203 }
204 }
205}