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