1 /* 2 * Copyright (c) 2007, 2012, 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 */ 26 27 import java.text.*; 28 import java.util.*; 29 import sun.util.locale.provider.*; 30 import sun.util.resources.*; 31 32 public 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> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getTimeZoneNameProvider().getAvailableLocales()); 49 List<Locale> providerLocales = Arrays.asList(tznp.getAvailableLocales()); 50 String[] ids = TimeZone.getAvailableIDs(); 51 52 for (Locale target: available) { 53 // pure JRE implementation 54 OpenListResourceBundle rb = LocaleProviderAdapter.forJRE().getLocaleData().getTimeZoneNames(target); 55 boolean jreSupportsTarget = jreimplloc.contains(target); 56 57 for (String id: ids) { 58 // the time zone 59 TimeZone tz = TimeZone.getTimeZone(id); 60 61 // JRE string array for the id 62 String[] jrearray = null; 63 if (jreSupportsTarget) { 64 try { 65 jrearray = rb.getStringArray(id); 66 } catch (MissingResourceException mre) {} 67 } 68 69 for (int i = 1; i <=(tz.useDaylightTime()?4:2); i++) { 70 // the localized name 71 String name = tz.getDisplayName(i>=3, i%2, target); 72 73 // provider's name (if any) 74 String providersname = null; 75 if (providerLocales.contains(target)) { 76 providersname = tznp.getDisplayName(id, i>=3, i%2, target); 77 } 78 79 // JRE's name 80 String jresname = null; 81 if (jrearray != null) { 82 jresname = jrearray[i]; 83 } 84 85 checkValidity(target, jresname, providersname, name, 86 jreSupportsTarget && jresname != null); 87 } 88 } 89 } 90 } 91 92 final String pattern = "z"; 93 final Locale OSAKA = new Locale("ja", "JP", "osaka"); 94 final Locale KYOTO = new Locale("ja", "JP", "kyoto"); 95 96 final String[] TIMEZONES = { 97 "GMT", "America/Los_Angeles", "SystemV/PST8", 98 "SystemV/PST8PDT", "PST8PDT", 99 }; 100 final String[] DISPLAY_NAMES_OSAKA = { 101 tznp.getDisplayName(TIMEZONES[0], false, TimeZone.SHORT, OSAKA), 102 tznp.getDisplayName(TIMEZONES[1], false, TimeZone.SHORT, OSAKA), 103 tznp.getDisplayName(TIMEZONES[2], false, TimeZone.SHORT, OSAKA), 104 tznp.getDisplayName(TIMEZONES[3], false, TimeZone.SHORT, OSAKA), 105 tznp.getDisplayName(TIMEZONES[4], false, TimeZone.SHORT, OSAKA) 106 }; 107 final String[] DISPLAY_NAMES_KYOTO = { 108 tznp.getDisplayName(TIMEZONES[0], false, TimeZone.SHORT, KYOTO), 109 tznp.getDisplayName(TIMEZONES[1], false, TimeZone.SHORT, KYOTO), 110 tznp.getDisplayName(TIMEZONES[2], false, TimeZone.SHORT, KYOTO), 111 tznp.getDisplayName(TIMEZONES[3], false, TimeZone.SHORT, KYOTO), 112 tznp.getDisplayName(TIMEZONES[4], false, TimeZone.SHORT, KYOTO) 113 }; 114 115 void test2() { 116 Locale defaultLocale = Locale.getDefault(); 117 TimeZone reservedTimeZone = TimeZone.getDefault(); 118 Date d = new Date(2005-1900, Calendar.DECEMBER, 22); 119 String formatted; 120 121 TimeZone tz; 122 SimpleDateFormat df; 123 124 try { 125 for (int i = 0; i < TIMEZONES.length; i++) { 126 tz = TimeZone.getTimeZone(TIMEZONES[i]); 127 TimeZone.setDefault(tz); 128 df = new SimpleDateFormat(pattern, DateFormatSymbols.getInstance(OSAKA)); 129 Locale.setDefault(defaultLocale); 130 System.out.println(formatted = df.format(d)); 131 if(!formatted.equals(DISPLAY_NAMES_OSAKA[i])) { 132 throw new RuntimeException("TimeZone " + TIMEZONES[i] + 133 ": formatted zone names mismatch. " + 134 formatted + " should match with " + 135 DISPLAY_NAMES_OSAKA[i]); 136 } 137 138 df.parse(DISPLAY_NAMES_OSAKA[i]); 139 140 Locale.setDefault(KYOTO); 141 df = new SimpleDateFormat(pattern, DateFormatSymbols.getInstance()); 142 System.out.println(formatted = df.format(d)); 143 if(!formatted.equals(DISPLAY_NAMES_KYOTO[i])) { 144 throw new RuntimeException("Timezone " + TIMEZONES[i] + 145 ": formatted zone names mismatch. " + 146 formatted + " should match with " + 147 DISPLAY_NAMES_KYOTO[i]); 148 } 149 df.parse(DISPLAY_NAMES_KYOTO[i]); 150 } 151 } catch (ParseException pe) { 152 throw new RuntimeException("parse error occured" + pe); 153 } finally { 154 // restore the reserved locale and time zone 155 Locale.setDefault(defaultLocale); 156 TimeZone.setDefault(reservedTimeZone); 157 } 158 } 159 160 final String LATIME = "America/Los_Angeles"; 161 final String PST = "PST"; 162 final String PST8PDT = "PST8PDT"; 163 final String US_PACIFIC = "US/Pacific"; 164 final String LATIME_IN_OSAKA = 165 tznp.getDisplayName(LATIME, false, TimeZone.LONG, OSAKA); 166 167 final String TOKYOTIME = "Asia/Tokyo"; 168 final String JST = "JST"; 169 final String JAPAN = "Japan"; 170 final String JST_IN_OSAKA = 171 tznp.getDisplayName(JST, false, TimeZone.LONG, OSAKA); 172 173 void aliasTest() { 174 // Check that provider's name for a standard id (America/Los_Angeles) is 175 // propagated to its aliases 176 String latime = TimeZone.getTimeZone(LATIME).getDisplayName(OSAKA); 177 if (!LATIME_IN_OSAKA.equals(latime)) { 178 throw new RuntimeException("Could not get provider's localized name. result: "+latime+" expected: "+LATIME_IN_OSAKA); 179 } 180 181 String pst = TimeZone.getTimeZone(PST).getDisplayName(OSAKA); 182 if (!LATIME_IN_OSAKA.equals(pst)) { 183 throw new RuntimeException("Provider's localized name is not available for an alias ID: "+PST+". result: "+pst+" expected: "+LATIME_IN_OSAKA); 184 } 185 186 String us_pacific = TimeZone.getTimeZone(US_PACIFIC).getDisplayName(OSAKA); 187 if (!LATIME_IN_OSAKA.equals(us_pacific)) { 188 throw new RuntimeException("Provider's localized name is not available for an alias ID: "+US_PACIFIC+". result: "+us_pacific+" expected: "+LATIME_IN_OSAKA); 189 } 190 191 // Check that provider's name for an alias id (JST) is 192 // propagated to its standard id and alias ids. 193 String jstime = TimeZone.getTimeZone(JST).getDisplayName(OSAKA); 194 if (!JST_IN_OSAKA.equals(jstime)) { 195 throw new RuntimeException("Could not get provider's localized name. result: "+jstime+" expected: "+JST_IN_OSAKA); 196 } 197 198 String tokyotime = TimeZone.getTimeZone(TOKYOTIME).getDisplayName(OSAKA); 199 if (!JST_IN_OSAKA.equals(tokyotime)) { 200 throw new RuntimeException("Provider's localized name is not available for a standard ID: "+TOKYOTIME+". result: "+tokyotime+" expected: "+JST_IN_OSAKA); 201 } 202 203 String japan = TimeZone.getTimeZone(JAPAN).getDisplayName(OSAKA); 204 if (!JST_IN_OSAKA.equals(japan)) { 205 throw new RuntimeException("Provider's localized name is not available for an alias ID: "+JAPAN+". result: "+japan+" expected: "+JST_IN_OSAKA); 206 } 207 } 208 }