Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/PluggableLocale/TimeZoneNameProviderTest.java
47209 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23*24*/2526import java.text.*;27import java.time.format.TextStyle;28import java.util.*;29import sun.util.locale.provider.*;30import sun.util.resources.*;3132public class TimeZoneNameProviderTest extends ProviderTest {3334com.bar.TimeZoneNameProviderImpl tznp = new com.bar.TimeZoneNameProviderImpl();3536public static void main(String[] s) {37new TimeZoneNameProviderTest();38}3940TimeZoneNameProviderTest() {41test1();42test2();43test3();44aliasTest();45genericFallbackTest();46}4748void test1() {49Locale[] available = Locale.getAvailableLocales();50List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getTimeZoneNameProvider().getAvailableLocales());51List<Locale> providerLocales = Arrays.asList(tznp.getAvailableLocales());52String[] ids = TimeZone.getAvailableIDs();5354for (Locale target: available) {55// pure JRE implementation56OpenListResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getTimeZoneNames(target);57boolean jreSupportsTarget = jreimplloc.contains(target);5859for (String id: ids) {60// the time zone61TimeZone tz = TimeZone.getTimeZone(id);6263// JRE string array for the id64String[] jrearray = null;65if (jreSupportsTarget) {66try {67jrearray = rb.getStringArray(id);68} catch (MissingResourceException mre) {}69}7071for (int i = 1; i <=(tz.useDaylightTime()?4:2); i++) {72// the localized name73String name = tz.getDisplayName(i>=3, i%2, target);7475// provider's name (if any)76String providersname = null;77if (providerLocales.contains(target)) {78providersname = tznp.getDisplayName(id, i>=3, i%2, target);79}8081// JRE's name82String jresname = null;83if (jrearray != null) {84jresname = jrearray[i];85}8687checkValidity(target, jresname, providersname, name,88jreSupportsTarget && jresname != null);89}90}91}92}9394final String pattern = "z";95final Locale OSAKA = new Locale("ja", "JP", "osaka");96final Locale KYOTO = new Locale("ja", "JP", "kyoto");97final Locale GENERIC = new Locale("ja", "JP", "generic");9899final String[] TIMEZONES = {100"GMT", "America/Los_Angeles", "SystemV/PST8",101"SystemV/PST8PDT", "PST8PDT",102};103final String[] DISPLAY_NAMES_OSAKA = {104tznp.getDisplayName(TIMEZONES[0], false, TimeZone.SHORT, OSAKA),105tznp.getDisplayName(TIMEZONES[1], false, TimeZone.SHORT, OSAKA),106tznp.getDisplayName(TIMEZONES[2], false, TimeZone.SHORT, OSAKA),107tznp.getDisplayName(TIMEZONES[3], false, TimeZone.SHORT, OSAKA),108tznp.getDisplayName(TIMEZONES[4], false, TimeZone.SHORT, OSAKA)109};110final String[] DISPLAY_NAMES_KYOTO = {111tznp.getDisplayName(TIMEZONES[0], false, TimeZone.SHORT, KYOTO),112tznp.getDisplayName(TIMEZONES[1], false, TimeZone.SHORT, KYOTO),113tznp.getDisplayName(TIMEZONES[2], false, TimeZone.SHORT, KYOTO),114tznp.getDisplayName(TIMEZONES[3], false, TimeZone.SHORT, KYOTO),115tznp.getDisplayName(TIMEZONES[4], false, TimeZone.SHORT, KYOTO)116};117118void test2() {119Locale defaultLocale = Locale.getDefault();120TimeZone reservedTimeZone = TimeZone.getDefault();121Date d = new Date(2005-1900, Calendar.DECEMBER, 22);122String formatted;123124TimeZone tz;125SimpleDateFormat df;126127try {128for (int i = 0; i < TIMEZONES.length; i++) {129tz = TimeZone.getTimeZone(TIMEZONES[i]);130TimeZone.setDefault(tz);131df = new SimpleDateFormat(pattern, DateFormatSymbols.getInstance(OSAKA));132Locale.setDefault(defaultLocale);133System.out.println(formatted = df.format(d));134if(!formatted.equals(DISPLAY_NAMES_OSAKA[i])) {135throw new RuntimeException("TimeZone " + TIMEZONES[i] +136": formatted zone names mismatch. " +137formatted + " should match with " +138DISPLAY_NAMES_OSAKA[i]);139}140141df.parse(DISPLAY_NAMES_OSAKA[i]);142143Locale.setDefault(KYOTO);144df = new SimpleDateFormat(pattern, DateFormatSymbols.getInstance());145System.out.println(formatted = df.format(d));146if(!formatted.equals(DISPLAY_NAMES_KYOTO[i])) {147throw new RuntimeException("Timezone " + TIMEZONES[i] +148": formatted zone names mismatch. " +149formatted + " should match with " +150DISPLAY_NAMES_KYOTO[i]);151}152df.parse(DISPLAY_NAMES_KYOTO[i]);153}154} catch (ParseException pe) {155throw new RuntimeException("parse error occured" + pe);156} finally {157// restore the reserved locale and time zone158Locale.setDefault(defaultLocale);159TimeZone.setDefault(reservedTimeZone);160}161}162163void test3() {164final String[] TZNAMES = {165LATIME, PST, PST8PDT, US_PACIFIC,166TOKYOTIME, JST, JAPAN,167};168for (String tzname : TZNAMES) {169TimeZone tz = TimeZone.getTimeZone(tzname);170for (int style : new int[] { TimeZone.LONG, TimeZone.SHORT }) {171String osakaStd = tz.getDisplayName(false, style, OSAKA);172if (osakaStd != null) {173String generic = tz.toZoneId().getDisplayName(174style == TimeZone.LONG ? TextStyle.FULL : TextStyle.SHORT,175GENERIC);176String expected = "Generic " + osakaStd;177if (!expected.equals(generic)) {178throw new RuntimeException("Wrong generic name: got=\"" + generic179+ "\", expected=\"" + expected + "\"");180}181}182}183}184}185186final String LATIME = "America/Los_Angeles";187final String PST = "PST";188final String PST8PDT = "PST8PDT";189final String US_PACIFIC = "US/Pacific";190final String LATIME_IN_OSAKA =191tznp.getDisplayName(LATIME, false, TimeZone.LONG, OSAKA);192193final String TOKYOTIME = "Asia/Tokyo";194final String JST = "JST";195final String JAPAN = "Japan";196final String JST_IN_OSAKA =197tznp.getDisplayName(JST, false, TimeZone.LONG, OSAKA);198199void aliasTest() {200// Check that provider's name for a standard id (America/Los_Angeles) is201// propagated to its aliases202String latime = TimeZone.getTimeZone(LATIME).getDisplayName(OSAKA);203if (!LATIME_IN_OSAKA.equals(latime)) {204throw new RuntimeException("Could not get provider's localized name. result: "+latime+" expected: "+LATIME_IN_OSAKA);205}206207String pst = TimeZone.getTimeZone(PST).getDisplayName(OSAKA);208if (!LATIME_IN_OSAKA.equals(pst)) {209throw new RuntimeException("Provider's localized name is not available for an alias ID: "+PST+". result: "+pst+" expected: "+LATIME_IN_OSAKA);210}211212String us_pacific = TimeZone.getTimeZone(US_PACIFIC).getDisplayName(OSAKA);213if (!LATIME_IN_OSAKA.equals(us_pacific)) {214throw new RuntimeException("Provider's localized name is not available for an alias ID: "+US_PACIFIC+". result: "+us_pacific+" expected: "+LATIME_IN_OSAKA);215}216217// Check that provider's name for an alias id (JST) is218// propagated to its standard id and alias ids.219String jstime = TimeZone.getTimeZone(JST).getDisplayName(OSAKA);220if (!JST_IN_OSAKA.equals(jstime)) {221throw new RuntimeException("Could not get provider's localized name. result: "+jstime+" expected: "+JST_IN_OSAKA);222}223224String tokyotime = TimeZone.getTimeZone(TOKYOTIME).getDisplayName(OSAKA);225if (!JST_IN_OSAKA.equals(tokyotime)) {226throw new RuntimeException("Provider's localized name is not available for a standard ID: "+TOKYOTIME+". result: "+tokyotime+" expected: "+JST_IN_OSAKA);227}228229String japan = TimeZone.getTimeZone(JAPAN).getDisplayName(OSAKA);230if (!JST_IN_OSAKA.equals(japan)) {231throw new RuntimeException("Provider's localized name is not available for an alias ID: "+JAPAN+". result: "+japan+" expected: "+JST_IN_OSAKA);232}233}234235/*236* Tests whether generic names can be retrieved through fallback.237* The test assumes the provider impl for OSAKA locale does NOT238* provide generic names.239*/240final String PT = "PT"; // SHORT generic name for "America/Los_Angeles"241void genericFallbackTest() {242String generic =243TimeZone.getTimeZone(LATIME)244.toZoneId()245.getDisplayName(TextStyle.SHORT, OSAKA);246if (!PT.equals(generic)) {247throw new RuntimeException("Generic name fallback failed. got: "+generic);248}249}250}251252253