0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A *
0N/A */
0N/A
0N/Aimport java.text.*;
0N/Aimport java.util.*;
0N/Aimport sun.util.*;
0N/Aimport sun.util.resources.*;
0N/A
0N/Apublic class TimeZoneNameProviderTest extends ProviderTest {
0N/A
0N/A com.bar.TimeZoneNameProviderImpl tznp = new com.bar.TimeZoneNameProviderImpl();
0N/A
0N/A public static void main(String[] s) {
0N/A new TimeZoneNameProviderTest();
0N/A }
0N/A
0N/A TimeZoneNameProviderTest() {
0N/A test1();
0N/A test2();
0N/A aliasTest();
0N/A }
0N/A
0N/A void test1() {
0N/A Locale[] available = Locale.getAvailableLocales();
0N/A List<Locale> providerLocales = Arrays.asList(tznp.getAvailableLocales());
0N/A String[] ids = TimeZone.getAvailableIDs();
0N/A
0N/A for (Locale target: available) {
0N/A // pure JRE implementation
0N/A OpenListResourceBundle rb = LocaleData.getTimeZoneNames(target);
0N/A boolean jreHasBundle = rb.getLocale().equals(target);
0N/A
0N/A for (String id: ids) {
0N/A // the time zone
0N/A TimeZone tz = TimeZone.getTimeZone(id);
0N/A
0N/A // JRE string array for the id
0N/A String[] jrearray = null;
0N/A if (jreHasBundle) {
0N/A try {
0N/A jrearray = rb.getStringArray(id);
0N/A } catch (MissingResourceException mre) {}
0N/A }
0N/A
0N/A for (int i = 1; i <=(tz.useDaylightTime()?4:2); i++) {
0N/A // the localized name
0N/A String name = tz.getDisplayName(i>=3, i%2, target);
0N/A
0N/A // provider's name (if any)
0N/A String providersname = null;
0N/A if (providerLocales.contains(target)) {
0N/A providersname = tznp.getDisplayName(id, i>=3, i%2, target);
0N/A }
0N/A
0N/A // JRE's name (if any)
0N/A String jresname = null;
0N/A if (jrearray != null) {
0N/A jresname = jrearray[i];
0N/A }
0N/A
0N/A checkValidity(target, jresname, providersname, name,
0N/A jreHasBundle && rb.handleGetKeys().contains(id));
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A final String pattern = "z";
0N/A final Locale OSAKA = new Locale("ja", "JP", "osaka");
0N/A final Locale KYOTO = new Locale("ja", "JP", "kyoto");
0N/A
0N/A final String[] TIMEZONES = {
0N/A "GMT", "America/Los_Angeles", "SystemV/PST8",
0N/A "SystemV/PST8PDT", "PST8PDT",
0N/A };
0N/A final String[] DISPLAY_NAMES_OSAKA = {
0N/A tznp.getDisplayName(TIMEZONES[0], false, TimeZone.SHORT, OSAKA),
0N/A tznp.getDisplayName(TIMEZONES[1], false, TimeZone.SHORT, OSAKA),
0N/A tznp.getDisplayName(TIMEZONES[2], false, TimeZone.SHORT, OSAKA),
0N/A tznp.getDisplayName(TIMEZONES[3], false, TimeZone.SHORT, OSAKA),
0N/A tznp.getDisplayName(TIMEZONES[4], false, TimeZone.SHORT, OSAKA)
0N/A };
0N/A final String[] DISPLAY_NAMES_KYOTO = {
0N/A tznp.getDisplayName(TIMEZONES[0], false, TimeZone.SHORT, KYOTO),
0N/A tznp.getDisplayName(TIMEZONES[1], false, TimeZone.SHORT, KYOTO),
0N/A tznp.getDisplayName(TIMEZONES[2], false, TimeZone.SHORT, KYOTO),
0N/A tznp.getDisplayName(TIMEZONES[3], false, TimeZone.SHORT, KYOTO),
0N/A tznp.getDisplayName(TIMEZONES[4], false, TimeZone.SHORT, KYOTO)
0N/A };
0N/A
0N/A void test2() {
0N/A Locale defaultLocale = Locale.getDefault();
0N/A Date d = new Date(2005-1900, Calendar.DECEMBER, 22);
0N/A String formatted;
0N/A
0N/A TimeZone tz;
0N/A SimpleDateFormat df;
0N/A
0N/A try {
0N/A for (int i = 0; i < TIMEZONES.length; i++) {
0N/A tz = TimeZone.getTimeZone(TIMEZONES[i]);
0N/A TimeZone.setDefault(tz);
0N/A df = new SimpleDateFormat(pattern, DateFormatSymbols.getInstance(OSAKA));
0N/A Locale.setDefault(defaultLocale);
0N/A System.out.println(formatted = df.format(d));
0N/A if(!formatted.equals(DISPLAY_NAMES_OSAKA[i])) {
0N/A throw new RuntimeException("TimeZone " + TIMEZONES[i] +
0N/A ": formatted zone names mismatch. " +
0N/A formatted + " should match with " +
0N/A DISPLAY_NAMES_OSAKA[i]);
0N/A }
0N/A
0N/A df.parse(DISPLAY_NAMES_OSAKA[i]);
0N/A
0N/A Locale.setDefault(KYOTO);
0N/A df = new SimpleDateFormat(pattern, DateFormatSymbols.getInstance());
0N/A System.out.println(formatted = df.format(d));
0N/A if(!formatted.equals(DISPLAY_NAMES_KYOTO[i])) {
0N/A Locale.setDefault(defaultLocale);
0N/A throw new RuntimeException("Timezone " + TIMEZONES[i] +
0N/A ": formatted zone names mismatch. " +
0N/A formatted + " should match with " +
0N/A DISPLAY_NAMES_KYOTO[i]);
0N/A }
0N/A df.parse(DISPLAY_NAMES_KYOTO[i]);
0N/A }
0N/A } catch (ParseException pe) {
0N/A Locale.setDefault(defaultLocale);
0N/A throw new RuntimeException("parse error occured" + pe);
0N/A }
0N/A Locale.setDefault(defaultLocale);
0N/A }
0N/A
0N/A final String LATIME = "America/Los_Angeles";
0N/A final String PST = "PST";
0N/A final String PST8PDT = "PST8PDT";
0N/A final String US_PACIFIC = "US/Pacific";
0N/A final String LATIME_IN_OSAKA =
0N/A tznp.getDisplayName(LATIME, false, TimeZone.LONG, OSAKA);
0N/A
0N/A final String TOKYOTIME = "Asia/Tokyo";
0N/A final String JST = "JST";
0N/A final String JAPAN = "Japan";
0N/A final String JST_IN_OSAKA =
0N/A tznp.getDisplayName(JST, false, TimeZone.LONG, OSAKA);
0N/A
0N/A void aliasTest() {
0N/A // Check that provider's name for a standard id (America/Los_Angeles) is
0N/A // propagated to its aliases
0N/A String latime = TimeZone.getTimeZone(LATIME).getDisplayName(OSAKA);
0N/A if (!LATIME_IN_OSAKA.equals(latime)) {
0N/A throw new RuntimeException("Could not get provider's localized name. result: "+latime+" expected: "+LATIME_IN_OSAKA);
0N/A }
0N/A
0N/A String pst = TimeZone.getTimeZone(PST).getDisplayName(OSAKA);
0N/A if (!LATIME_IN_OSAKA.equals(pst)) {
0N/A throw new RuntimeException("Provider's localized name is not available for an alias ID: "+PST+". result: "+pst+" expected: "+LATIME_IN_OSAKA);
0N/A }
0N/A
0N/A String us_pacific = TimeZone.getTimeZone(US_PACIFIC).getDisplayName(OSAKA);
0N/A if (!LATIME_IN_OSAKA.equals(us_pacific)) {
0N/A throw new RuntimeException("Provider's localized name is not available for an alias ID: "+US_PACIFIC+". result: "+us_pacific+" expected: "+LATIME_IN_OSAKA);
0N/A }
0N/A
0N/A // Check that provider's name for an alias id (JST) is
0N/A // propagated to its standard id and alias ids.
0N/A String jstime = TimeZone.getTimeZone(JST).getDisplayName(OSAKA);
0N/A if (!JST_IN_OSAKA.equals(jstime)) {
0N/A throw new RuntimeException("Could not get provider's localized name. result: "+jstime+" expected: "+JST_IN_OSAKA);
0N/A }
0N/A
0N/A String tokyotime = TimeZone.getTimeZone(TOKYOTIME).getDisplayName(OSAKA);
0N/A if (!JST_IN_OSAKA.equals(tokyotime)) {
0N/A throw new RuntimeException("Provider's localized name is not available for a standard ID: "+TOKYOTIME+". result: "+tokyotime+" expected: "+JST_IN_OSAKA);
0N/A }
0N/A
0N/A String japan = TimeZone.getTimeZone(JAPAN).getDisplayName(OSAKA);
0N/A if (!JST_IN_OSAKA.equals(japan)) {
0N/A throw new RuntimeException("Provider's localized name is not available for an alias ID: "+JAPAN+". result: "+japan+" expected: "+JST_IN_OSAKA);
0N/A }
0N/A }
0N/A}