1 /* 2 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4052440 8003267 8062588 8210406 27 * @summary TimeZoneNameProvider tests 28 * @library providersrc/foobarutils 29 * providersrc/barprovider 30 * @modules java.base/sun.util.locale.provider 31 * java.base/sun.util.resources 32 * @build com.foobar.Utils 33 * com.bar.* 34 * @run main/othervm -Djava.locale.providers=JRE,SPI TimeZoneNameProviderTest 35 */ 36 37 import java.text.DateFormatSymbols; 38 import java.text.ParseException; 39 import java.text.SimpleDateFormat; 40 import java.time.format.TextStyle; 41 import java.util.Arrays; 42 import java.util.Calendar; 43 import java.util.Date; 44 import java.util.List; 45 import java.util.Locale; 46 import java.util.MissingResourceException; 47 import java.util.TimeZone; 48 49 import com.bar.TimeZoneNameProviderImpl; 50 51 import sun.util.locale.provider.LocaleProviderAdapter; 52 import sun.util.locale.provider.ResourceBundleBasedAdapter; 53 import sun.util.resources.OpenListResourceBundle; 54 55 public class TimeZoneNameProviderTest extends ProviderTest { 56 57 TimeZoneNameProviderImpl tznp = new TimeZoneNameProviderImpl(); 58 59 public static void main(String[] s) { 60 new TimeZoneNameProviderTest(); 61 } 62 63 TimeZoneNameProviderTest() { 64 test1(); 65 test2(); 66 test3(); 67 aliasTest(); 68 genericFallbackTest(); 69 } 70 71 void test1() { 72 Locale[] available = Locale.getAvailableLocales(); 73 List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getTimeZoneNameProvider().getAvailableLocales()); 74 List<Locale> providerLocales = Arrays.asList(tznp.getAvailableLocales()); 75 String[] ids = TimeZone.getAvailableIDs(); 76 77 for (Locale target: available) { 78 // pure JRE implementation 79 OpenListResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getTimeZoneNames(target); 80 boolean jreSupportsTarget = jreimplloc.contains(target); 81 82 for (String id: ids) { 83 // the time zone 84 TimeZone tz = TimeZone.getTimeZone(id); 85 86 // JRE string array for the id 87 String[] jrearray = null; 88 if (jreSupportsTarget) { 89 try { 90 jrearray = rb.getStringArray(id); 91 } catch (MissingResourceException mre) {} 92 } 93 94 for (int i = 1; i <=(tz.useDaylightTime()?4:2); i++) { 95 // the localized name 96 String name = tz.getDisplayName(i>=3, i%2, target); 97 98 // provider's name (if any) 99 String providersname = null; 100 if (providerLocales.contains(target)) { 101 providersname = tznp.getDisplayName(id, i>=3, i%2, target); 102 } 103 104 // JRE's name 105 String jresname = null; 106 if (jrearray != null) { 107 jresname = jrearray[i]; 108 } 109 110 checkValidity(target, jresname, providersname, name, 111 jreSupportsTarget && jresname != null); 112 } 113 } 114 } 115 } 116 117 final String pattern = "z"; 118 final Locale OSAKA = new Locale("ja", "JP", "osaka"); 119 final Locale KYOTO = new Locale("ja", "JP", "kyoto"); 120 final Locale GENERIC = new Locale("ja", "JP", "generic"); 121 122 final String[] TIMEZONES = { 123 "GMT", "America/Los_Angeles", "SystemV/PST8", 124 "SystemV/PST8PDT", "PST8PDT", 125 }; 126 final String[] DISPLAY_NAMES_OSAKA = { 127 tznp.getDisplayName(TIMEZONES[0], false, TimeZone.SHORT, OSAKA), 128 tznp.getDisplayName(TIMEZONES[1], false, TimeZone.SHORT, OSAKA), 129 tznp.getDisplayName(TIMEZONES[2], false, TimeZone.SHORT, OSAKA), 130 tznp.getDisplayName(TIMEZONES[3], false, TimeZone.SHORT, OSAKA), 131 tznp.getDisplayName(TIMEZONES[4], false, TimeZone.SHORT, OSAKA) 132 }; 133 final String[] DISPLAY_NAMES_KYOTO = { 134 tznp.getDisplayName(TIMEZONES[0], false, TimeZone.SHORT, KYOTO), 135 tznp.getDisplayName(TIMEZONES[1], false, TimeZone.SHORT, KYOTO), 136 tznp.getDisplayName(TIMEZONES[2], false, TimeZone.SHORT, KYOTO), 137 tznp.getDisplayName(TIMEZONES[3], false, TimeZone.SHORT, KYOTO), 138 tznp.getDisplayName(TIMEZONES[4], false, TimeZone.SHORT, KYOTO) 139 }; 140 141 void test2() { 142 Locale defaultLocale = Locale.getDefault(); 143 TimeZone reservedTimeZone = TimeZone.getDefault(); 144 Date d = new Date(2005-1900, Calendar.DECEMBER, 22); 145 String formatted; 146 147 TimeZone tz; 148 SimpleDateFormat df; 149 150 try { 151 for (int i = 0; i < TIMEZONES.length; i++) { 152 tz = TimeZone.getTimeZone(TIMEZONES[i]); 153 TimeZone.setDefault(tz); 154 df = new SimpleDateFormat(pattern, DateFormatSymbols.getInstance(OSAKA)); 155 Locale.setDefault(defaultLocale); 156 System.out.println(formatted = df.format(d)); 157 if(!formatted.equals(DISPLAY_NAMES_OSAKA[i])) { 158 throw new RuntimeException("TimeZone " + TIMEZONES[i] + 159 ": formatted zone names mismatch. " + 160 formatted + " should match with " + 161 DISPLAY_NAMES_OSAKA[i]); 162 } 163 164 df.parse(DISPLAY_NAMES_OSAKA[i]); 165 166 Locale.setDefault(KYOTO); 167 df = new SimpleDateFormat(pattern, DateFormatSymbols.getInstance()); 168 System.out.println(formatted = df.format(d)); 169 if(!formatted.equals(DISPLAY_NAMES_KYOTO[i])) { 170 throw new RuntimeException("Timezone " + TIMEZONES[i] + 171 ": formatted zone names mismatch. " + 172 formatted + " should match with " + 173 DISPLAY_NAMES_KYOTO[i]); 174 } 175 df.parse(DISPLAY_NAMES_KYOTO[i]); 176 } 177 } catch (ParseException pe) { 178 throw new RuntimeException("parse error occured" + pe); 179 } finally { 180 // restore the reserved locale and time zone 181 Locale.setDefault(defaultLocale); 182 TimeZone.setDefault(reservedTimeZone); 183 } 184 } 185 186 void test3() { 187 final String[] TZNAMES = { 188 LATIME, PST, PST8PDT, US_PACIFIC, 189 TOKYOTIME, JST, JAPAN, 190 }; 191 for (String tzname : TZNAMES) { 192 TimeZone tz = TimeZone.getTimeZone(tzname); 193 for (int style : new int[] { TimeZone.LONG, TimeZone.SHORT }) { 194 String osakaStd = tz.getDisplayName(false, style, OSAKA); 195 if (osakaStd != null) { 196 String generic = tz.toZoneId().getDisplayName( 197 style == TimeZone.LONG ? TextStyle.FULL : TextStyle.SHORT, 198 GENERIC); 199 String expected = "Generic " + osakaStd; 200 if (!expected.equals(generic)) { 201 throw new RuntimeException("Wrong generic name: got=\"" + generic 202 + "\", expected=\"" + expected + "\""); 203 } 204 } 205 } 206 } 207 } 208 209 final String LATIME = "America/Los_Angeles"; 210 final String PST = "PST"; 211 final String PST8PDT = "PST8PDT"; 212 final String US_PACIFIC = "US/Pacific"; 213 final String LATIME_IN_OSAKA = 214 tznp.getDisplayName(LATIME, false, TimeZone.LONG, OSAKA); 215 216 final String TOKYOTIME = "Asia/Tokyo"; 217 final String JST = "JST"; 218 final String JAPAN = "Japan"; 219 final String JST_IN_OSAKA = 220 tznp.getDisplayName(JST, false, TimeZone.LONG, OSAKA); 221 222 void aliasTest() { 223 // Check that provider's name for a standard id (America/Los_Angeles) is 224 // propagated to its aliases 225 String latime = TimeZone.getTimeZone(LATIME).getDisplayName(OSAKA); 226 if (!LATIME_IN_OSAKA.equals(latime)) { 227 throw new RuntimeException("Could not get provider's localized name. result: "+latime+" expected: "+LATIME_IN_OSAKA); 228 } 229 230 String pst = TimeZone.getTimeZone(PST).getDisplayName(OSAKA); 231 if (!LATIME_IN_OSAKA.equals(pst)) { 232 throw new RuntimeException("Provider's localized name is not available for an alias ID: "+PST+". result: "+pst+" expected: "+LATIME_IN_OSAKA); 233 } 234 235 String us_pacific = TimeZone.getTimeZone(US_PACIFIC).getDisplayName(OSAKA); 236 if (!LATIME_IN_OSAKA.equals(us_pacific)) { 237 throw new RuntimeException("Provider's localized name is not available for an alias ID: "+US_PACIFIC+". result: "+us_pacific+" expected: "+LATIME_IN_OSAKA); 238 } 239 240 // Check that provider's name for an alias id (JST) is 241 // propagated to its standard id and alias ids. 242 String jstime = TimeZone.getTimeZone(JST).getDisplayName(OSAKA); 243 if (!JST_IN_OSAKA.equals(jstime)) { 244 throw new RuntimeException("Could not get provider's localized name. result: "+jstime+" expected: "+JST_IN_OSAKA); 245 } 246 247 String tokyotime = TimeZone.getTimeZone(TOKYOTIME).getDisplayName(OSAKA); 248 if (!JST_IN_OSAKA.equals(tokyotime)) { 249 throw new RuntimeException("Provider's localized name is not available for a standard ID: "+TOKYOTIME+". result: "+tokyotime+" expected: "+JST_IN_OSAKA); 250 } 251 252 String japan = TimeZone.getTimeZone(JAPAN).getDisplayName(OSAKA); 253 if (!JST_IN_OSAKA.equals(japan)) { 254 throw new RuntimeException("Provider's localized name is not available for an alias ID: "+JAPAN+". result: "+japan+" expected: "+JST_IN_OSAKA); 255 } 256 } 257 258 /* 259 * Tests whether generic names can be retrieved through fallback. 260 * The test assumes the provider impl for OSAKA locale does NOT 261 * provide generic names. 262 */ 263 final String PT = "PT"; // SHORT generic name for "America/Los_Angeles" 264 void genericFallbackTest() { 265 String generic = 266 TimeZone.getTimeZone(LATIME) 267 .toZoneId() 268 .getDisplayName(TextStyle.SHORT, OSAKA); 269 if (!PT.equals(generic)) { 270 throw new RuntimeException("Generic name fallback failed. got: "+generic); 271 } 272 } 273 }