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