test/java/util/PluggableLocale/TimeZoneNameProviderTest.java

Print this page




  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;


  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 


 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);




  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 = 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;


  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 


 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);