Path: blob/master/test/jdk/java/util/PluggableLocale/TimeZoneNameProviderTest.java
41149 views
/*1* Copyright (c) 2007, 2018, 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*/2223/*24* @test25* @bug 4052440 8003267 8062588 821040626* @summary TimeZoneNameProvider tests27* @library providersrc/foobarutils28* providersrc/barprovider29* @modules java.base/sun.util.locale.provider30* java.base/sun.util.resources31* @build com.foobar.Utils32* com.bar.*33* @run main/othervm -Djava.locale.providers=JRE,SPI TimeZoneNameProviderTest34*/3536import java.text.DateFormatSymbols;37import java.text.ParseException;38import java.text.SimpleDateFormat;39import java.time.format.TextStyle;40import java.util.Arrays;41import java.util.Calendar;42import java.util.Date;43import java.util.List;44import java.util.Locale;45import java.util.MissingResourceException;46import java.util.TimeZone;4748import com.bar.TimeZoneNameProviderImpl;4950import sun.util.locale.provider.LocaleProviderAdapter;51import sun.util.locale.provider.ResourceBundleBasedAdapter;52import sun.util.resources.OpenListResourceBundle;5354public class TimeZoneNameProviderTest extends ProviderTest {5556TimeZoneNameProviderImpl tznp = new TimeZoneNameProviderImpl();5758public static void main(String[] s) {59new TimeZoneNameProviderTest();60}6162TimeZoneNameProviderTest() {63test1();64test2();65test3();66aliasTest();67genericFallbackTest();68}6970void test1() {71Locale[] available = Locale.getAvailableLocales();72List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getTimeZoneNameProvider().getAvailableLocales());73List<Locale> providerLocales = Arrays.asList(tznp.getAvailableLocales());74String[] ids = TimeZone.getAvailableIDs();7576for (Locale target: available) {77// pure JRE implementation78OpenListResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getTimeZoneNames(target);79boolean jreSupportsTarget = jreimplloc.contains(target);8081for (String id: ids) {82// the time zone83TimeZone tz = TimeZone.getTimeZone(id);8485// JRE string array for the id86String[] jrearray = null;87if (jreSupportsTarget) {88try {89jrearray = rb.getStringArray(id);90} catch (MissingResourceException mre) {}91}9293for (int i = 1; i <=(tz.useDaylightTime()?4:2); i++) {94// the localized name95String name = tz.getDisplayName(i>=3, i%2, target);9697// provider's name (if any)98String providersname = null;99if (providerLocales.contains(target)) {100providersname = tznp.getDisplayName(id, i>=3, i%2, target);101}102103// JRE's name104String jresname = null;105if (jrearray != null) {106jresname = jrearray[i];107}108109checkValidity(target, jresname, providersname, name,110jreSupportsTarget && jresname != null);111}112}113}114}115116final String pattern = "z";117final Locale OSAKA = new Locale("ja", "JP", "osaka");118final Locale KYOTO = new Locale("ja", "JP", "kyoto");119final Locale GENERIC = new Locale("ja", "JP", "generic");120121final String[] TIMEZONES = {122"GMT", "America/Los_Angeles", "SystemV/PST8",123"SystemV/PST8PDT", "PST8PDT",124};125final String[] DISPLAY_NAMES_OSAKA = {126tznp.getDisplayName(TIMEZONES[0], false, TimeZone.SHORT, OSAKA),127tznp.getDisplayName(TIMEZONES[1], false, TimeZone.SHORT, OSAKA),128tznp.getDisplayName(TIMEZONES[2], false, TimeZone.SHORT, OSAKA),129tznp.getDisplayName(TIMEZONES[3], false, TimeZone.SHORT, OSAKA),130tznp.getDisplayName(TIMEZONES[4], false, TimeZone.SHORT, OSAKA)131};132final String[] DISPLAY_NAMES_KYOTO = {133tznp.getDisplayName(TIMEZONES[0], false, TimeZone.SHORT, KYOTO),134tznp.getDisplayName(TIMEZONES[1], false, TimeZone.SHORT, KYOTO),135tznp.getDisplayName(TIMEZONES[2], false, TimeZone.SHORT, KYOTO),136tznp.getDisplayName(TIMEZONES[3], false, TimeZone.SHORT, KYOTO),137tznp.getDisplayName(TIMEZONES[4], false, TimeZone.SHORT, KYOTO)138};139140void test2() {141Locale defaultLocale = Locale.getDefault();142TimeZone reservedTimeZone = TimeZone.getDefault();143Date d = new Date(2005-1900, Calendar.DECEMBER, 22);144String formatted;145146TimeZone tz;147SimpleDateFormat df;148149try {150for (int i = 0; i < TIMEZONES.length; i++) {151tz = TimeZone.getTimeZone(TIMEZONES[i]);152TimeZone.setDefault(tz);153df = new SimpleDateFormat(pattern, DateFormatSymbols.getInstance(OSAKA));154Locale.setDefault(defaultLocale);155System.out.println(formatted = df.format(d));156if(!formatted.equals(DISPLAY_NAMES_OSAKA[i])) {157throw new RuntimeException("TimeZone " + TIMEZONES[i] +158": formatted zone names mismatch. " +159formatted + " should match with " +160DISPLAY_NAMES_OSAKA[i]);161}162163df.parse(DISPLAY_NAMES_OSAKA[i]);164165Locale.setDefault(KYOTO);166df = new SimpleDateFormat(pattern, DateFormatSymbols.getInstance());167System.out.println(formatted = df.format(d));168if(!formatted.equals(DISPLAY_NAMES_KYOTO[i])) {169throw new RuntimeException("Timezone " + TIMEZONES[i] +170": formatted zone names mismatch. " +171formatted + " should match with " +172DISPLAY_NAMES_KYOTO[i]);173}174df.parse(DISPLAY_NAMES_KYOTO[i]);175}176} catch (ParseException pe) {177throw new RuntimeException("parse error occured" + pe);178} finally {179// restore the reserved locale and time zone180Locale.setDefault(defaultLocale);181TimeZone.setDefault(reservedTimeZone);182}183}184185void test3() {186final String[] TZNAMES = {187LATIME, PST, PST8PDT, US_PACIFIC,188TOKYOTIME, JST, JAPAN,189};190for (String tzname : TZNAMES) {191TimeZone tz = TimeZone.getTimeZone(tzname);192for (int style : new int[] { TimeZone.LONG, TimeZone.SHORT }) {193String osakaStd = tz.getDisplayName(false, style, OSAKA);194if (osakaStd != null) {195String generic = tz.toZoneId().getDisplayName(196style == TimeZone.LONG ? TextStyle.FULL : TextStyle.SHORT,197GENERIC);198String expected = "Generic " + osakaStd;199if (!expected.equals(generic)) {200throw new RuntimeException("Wrong generic name: got=\"" + generic201+ "\", expected=\"" + expected + "\"");202}203}204}205}206}207208final String LATIME = "America/Los_Angeles";209final String PST = "PST";210final String PST8PDT = "PST8PDT";211final String US_PACIFIC = "US/Pacific";212final String LATIME_IN_OSAKA =213tznp.getDisplayName(LATIME, false, TimeZone.LONG, OSAKA);214215final String TOKYOTIME = "Asia/Tokyo";216final String JST = "JST";217final String JAPAN = "Japan";218final String JST_IN_OSAKA =219tznp.getDisplayName(JST, false, TimeZone.LONG, OSAKA);220221void aliasTest() {222// Check that provider's name for a standard id (America/Los_Angeles) is223// propagated to its aliases224String latime = TimeZone.getTimeZone(LATIME).getDisplayName(OSAKA);225if (!LATIME_IN_OSAKA.equals(latime)) {226throw new RuntimeException("Could not get provider's localized name. result: "+latime+" expected: "+LATIME_IN_OSAKA);227}228229String pst = TimeZone.getTimeZone(PST).getDisplayName(OSAKA);230if (!LATIME_IN_OSAKA.equals(pst)) {231throw new RuntimeException("Provider's localized name is not available for an alias ID: "+PST+". result: "+pst+" expected: "+LATIME_IN_OSAKA);232}233234String us_pacific = TimeZone.getTimeZone(US_PACIFIC).getDisplayName(OSAKA);235if (!LATIME_IN_OSAKA.equals(us_pacific)) {236throw new RuntimeException("Provider's localized name is not available for an alias ID: "+US_PACIFIC+". result: "+us_pacific+" expected: "+LATIME_IN_OSAKA);237}238239// Check that provider's name for an alias id (JST) is240// propagated to its standard id and alias ids.241String jstime = TimeZone.getTimeZone(JST).getDisplayName(OSAKA);242if (!JST_IN_OSAKA.equals(jstime)) {243throw new RuntimeException("Could not get provider's localized name. result: "+jstime+" expected: "+JST_IN_OSAKA);244}245246String tokyotime = TimeZone.getTimeZone(TOKYOTIME).getDisplayName(OSAKA);247if (!JST_IN_OSAKA.equals(tokyotime)) {248throw new RuntimeException("Provider's localized name is not available for a standard ID: "+TOKYOTIME+". result: "+tokyotime+" expected: "+JST_IN_OSAKA);249}250251String japan = TimeZone.getTimeZone(JAPAN).getDisplayName(OSAKA);252if (!JST_IN_OSAKA.equals(japan)) {253throw new RuntimeException("Provider's localized name is not available for an alias ID: "+JAPAN+". result: "+japan+" expected: "+JST_IN_OSAKA);254}255}256257/*258* Tests whether generic names can be retrieved through fallback.259* The test assumes the provider impl for OSAKA locale does NOT260* provide generic names.261*/262final String PT = "PT"; // SHORT generic name for "America/Los_Angeles"263void genericFallbackTest() {264String generic =265TimeZone.getTimeZone(LATIME)266.toZoneId()267.getDisplayName(TextStyle.SHORT, OSAKA);268if (!PT.equals(generic)) {269throw new RuntimeException("Generic name fallback failed. got: "+generic);270}271}272}273274