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