jdk/test/sun/util/resources/TimeZone/TimeZoneNames/TimeZoneNamesTest.java
changeset 22616 2b4c9efdf2d7
parent 22614 2e2f77dc2599
child 22617 dcd0c84f4b06
equal deleted inserted replaced
22614:2e2f77dc2599 22616:2b4c9efdf2d7
     1 /*
       
     2  * Copyright (c) 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  * @test
       
    26  * @bug 8025051
       
    27  * @summary Test time zone names across all locales
       
    28  * @run main TimeZoneNamesTest
       
    29  */
       
    30 
       
    31 import sun.util.locale.provider.TimeZoneNameUtility;
       
    32 import java.util.TimeZone;
       
    33 import java.util.Locale;
       
    34 import java.util.Properties;
       
    35 import java.io.IOException;
       
    36 import java.io.FileInputStream;
       
    37 
       
    38 public class TimeZoneNamesTest {
       
    39     // name type to test. Possible: long, short.
       
    40     static String requestedTestType = "long";
       
    41     // test Standard/DST (false) or Generic (true) TZ names
       
    42     static boolean testGeneric = false;
       
    43 
       
    44     public static void testGenericTZName( Locale locale, String timezoneName,
       
    45                                           int nameType, String expectedName ) throws RuntimeException {
       
    46         if (testGeneric) {
       
    47             String genericName = TimeZoneNameUtility.retrieveGenericDisplayName(timezoneName, nameType, locale);
       
    48             //Check for equality
       
    49             if (!genericName.equals(expectedName))
       
    50                 throw new RuntimeException( "Time zone ("+timezoneName+") name is incorrect for locale \""+locale.getDisplayName()
       
    51                                             +"\" nameType: Generic"+"("+nameType+") Should be: " +expectedName+" Observed: "+genericName);
       
    52         }
       
    53     }
       
    54 
       
    55     public static void testTZName( Locale locale, String timezoneName, boolean isDaylight,
       
    56                                    int nameType, String expectedName ) throws RuntimeException {
       
    57         if (!testGeneric) {
       
    58             //Construct time zone objects
       
    59             TimeZone zone = TimeZone.getTimeZone(timezoneName);
       
    60             //Get name from JDK
       
    61             String name = zone.getDisplayName(isDaylight, nameType, locale);
       
    62             //Check for equality
       
    63             if (!name.equals(expectedName))
       
    64                 throw new RuntimeException( "Time zone ("+timezoneName+") name is incorrect for locale: \""+locale.getDisplayName()
       
    65                                             +"\" nameType:"+requestedTestType+" DST:"+isDaylight+" Should be: " +expectedName+" Observed: "+name);
       
    66         }
       
    67     }
       
    68 
       
    69     public static boolean testPropertyEntry( Locale locale, String entry, String value ) {
       
    70         boolean result = true;
       
    71         String[] params = entry.split("\\.");
       
    72         if (params.length != 3) {
       
    73             System.out.println("Incorrect property file entry="+entry+" "+params.length);
       
    74             result = false;
       
    75         } else {
       
    76             boolean isDaylight = true;
       
    77             int nameType = TimeZone.LONG;
       
    78 
       
    79             if (params[2].equals("short"))
       
    80                 nameType = TimeZone.SHORT;
       
    81 
       
    82             if (params[1].equals("standard"))
       
    83                 isDaylight = false;
       
    84 
       
    85             // Names with non-requested tz name type are ignored
       
    86             if (requestedTestType.equals(params[2])) {
       
    87                 try {
       
    88                     if (params[1].equals("generic"))
       
    89                         testGenericTZName( locale, params[0], nameType, value );
       
    90                     else
       
    91                         testTZName( locale, params[0], isDaylight, nameType, value );
       
    92                 } catch (RuntimeException e) {
       
    93                     System.out.println( "Test FAILED: "+e );
       
    94                     result = false;
       
    95                 }
       
    96             }
       
    97         }
       
    98         return result;
       
    99     }
       
   100 
       
   101     public static boolean testPropertyFile( String propFile, String shortName, Locale locale ) throws RuntimeException {
       
   102         boolean result = true;
       
   103         Properties property = new Properties();
       
   104         try {
       
   105             property.load( new FileInputStream(propFile) );
       
   106         } catch (IOException e) {
       
   107             throw new RuntimeException("Property file "+propFile+" is not found", e);
       
   108         }
       
   109         for (String key: property.stringPropertyNames()) {
       
   110             result &= testPropertyEntry(locale, key, property.getProperty(key) );
       
   111         }
       
   112         return result;
       
   113     }
       
   114 
       
   115     // Locale to test, file with names data, test long/short names, test generic names (true/false)
       
   116     static Object[][] testTargets = {
       
   117         { Locale.ROOT,"TimeZoneNames.properties","long",false},
       
   118         { Locale.ROOT,"TimeZoneNames_short.properties","short",false},
       
   119         { Locale.ROOT,"TimeZoneNames.properties","long",true},
       
   120         { Locale.ROOT,"TimeZoneNames_short.properties","short",true},
       
   121 
       
   122         { new Locale("de"),"TimeZoneNames_de.properties","long",false},
       
   123         { new Locale("de"),"TimeZoneNames_de_short.properties","short",false},
       
   124         { new Locale("de"),"TimeZoneNames_de.properties","long",true},
       
   125         { new Locale("de"),"TimeZoneNames_de_short.properties","short",true},
       
   126 
       
   127         { new Locale("es"),"TimeZoneNames_es.properties","long",false},
       
   128         { new Locale("es"),"TimeZoneNames_es_short.properties","short",false},
       
   129         { new Locale("es"),"TimeZoneNames_es.properties","long",true},
       
   130         { new Locale("es"),"TimeZoneNames_es_short.properties","short",true},
       
   131 
       
   132         { new Locale("fr"),"TimeZoneNames_fr.properties","long",false},
       
   133         { new Locale("fr"),"TimeZoneNames_fr_short.properties","short",false},
       
   134         { new Locale("fr"),"TimeZoneNames_fr.properties","long",true},
       
   135         { new Locale("fr"),"TimeZoneNames_fr_short.properties","short",true},
       
   136 
       
   137         { new Locale("it"),"TimeZoneNames_it.properties","long",false},
       
   138         { new Locale("it"),"TimeZoneNames_it_short.properties","short",false},
       
   139         { new Locale("it"),"TimeZoneNames_it.properties","long",true},
       
   140         { new Locale("it"),"TimeZoneNames_it_short.properties","short",true},
       
   141 
       
   142         { new Locale("ja"),"TimeZoneNames_ja.properties","long",false},
       
   143         { new Locale("ja"),"TimeZoneNames_ja_short.properties","short",false},
       
   144         { new Locale("ja"),"TimeZoneNames_ja.properties","long",true},
       
   145         { new Locale("ja"),"TimeZoneNames_ja_short.properties","short",true},
       
   146 
       
   147         { new Locale("ko"),"TimeZoneNames_ko.properties","long",false},
       
   148         { new Locale("ko"),"TimeZoneNames_ko_short.properties","short",false},
       
   149         { new Locale("ko"),"TimeZoneNames_ko.properties","long",true},
       
   150         { new Locale("ko"),"TimeZoneNames_ko_short.properties","short",true},
       
   151 
       
   152         { new Locale("pt","BR"),"TimeZoneNames_pt_BR.properties","long",false},
       
   153         { new Locale("pt","BR"),"TimeZoneNames_pt_BR_short.properties","short",false},
       
   154         { new Locale("pt","BR"),"TimeZoneNames_pt_BR.properties","long",true},
       
   155         { new Locale("pt","BR"),"TimeZoneNames_pt_BR_short.properties","short",true},
       
   156 
       
   157         { new Locale("sv"),"TimeZoneNames_sv.properties","long",false},
       
   158         { new Locale("sv"),"TimeZoneNames_sv_short.properties","short",false},
       
   159         { new Locale("sv"),"TimeZoneNames_sv.properties","long",true},
       
   160         { new Locale("sv"),"TimeZoneNames_sv_short.properties","short",true},
       
   161 
       
   162         { new Locale("zh","CN"),"TimeZoneNames_zh_CN.properties","long",false},
       
   163         { new Locale("zh","CN"),"TimeZoneNames_zh_CN_short.properties","short",false},
       
   164         { new Locale("zh","CN"),"TimeZoneNames_zh_CN.properties","long",true},
       
   165         { new Locale("zh","CN"),"TimeZoneNames_zh_CN_short.properties","short",true},
       
   166 
       
   167         { new Locale("zh","TW"),"TimeZoneNames_zh_TW.properties","long",false},
       
   168         { new Locale("zh","TW"),"TimeZoneNames_zh_TW_short.properties","short",false},
       
   169         { new Locale("zh","TW"),"TimeZoneNames_zh_TW.properties","long",true},
       
   170         { new Locale("zh","TW"),"TimeZoneNames_zh_TW_short.properties","short",true}
       
   171     };
       
   172 
       
   173     public static void main(String[] args) {
       
   174         boolean result = true;
       
   175 
       
   176         for (Object [] test: testTargets) {
       
   177             Locale testLocale = (Locale) test[0];
       
   178             String testFile = (String) test[1];
       
   179             requestedTestType = (String) test[2];
       
   180             testGeneric = (Boolean) test[3];
       
   181             result &= testPropertyFile( System.getProperty("test.src",".")+"/"+testFile, testFile, testLocale);
       
   182         }
       
   183         if (!result)
       
   184             throw new RuntimeException("Some time zones has unexpected names. Please, check test output.");
       
   185     }
       
   186 }