test/jdk/java/util/Locale/Bug8179071.java
changeset 49918 8b9c78f0a712
child 50576 374bd919d8fe
equal deleted inserted replaced
49917:1871c5d07caf 49918:8b9c78f0a712
       
     1 /*
       
     2  * Copyright (c) 2018, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26  /*
       
    27  * @test
       
    28  * @bug 8179071
       
    29  * @summary Test that language aliases of CLDR supplemental metadata are handled correctly.
       
    30  * @modules jdk.localedata
       
    31  * @run main/othervm -Djava.locale.providers=CLDR Bug8179071
       
    32  */
       
    33 
       
    34 /**
       
    35  * This fix is dependent on a particular version of CLDR data.
       
    36  */
       
    37 
       
    38 import java.time.Month;
       
    39 import java.time.format.TextStyle;
       
    40 import java.util.Arrays;
       
    41 import java.util.HashSet;
       
    42 import java.util.Locale;
       
    43 import java.util.Map;
       
    44 import java.util.Set;
       
    45 
       
    46 public class Bug8179071 {
       
    47 
       
    48     // Deprecated and Legacy tags.
       
    49     private static final Set<String> LegacyAliases = Set.of("pa-PK", "ug-Arab-CN", "kk-Cyrl-KZ",
       
    50             "bs-BA", "ks-Arab-IN", "mn-Cyrl-MN", "ha-Latn-NE",
       
    51             "shi-MA", "ha-Latn-NG", "ms-Latn-BN","ms-Latn-SG",
       
    52             "ky-Cyrl-KG", "az-AZ", "zh-guoyu", "zh-min-nan", "i-klingon", "i-tsu",
       
    53             "sr-XK", "sgn-CH-DE", "mo", "i-tay", "scc", "uz-UZ", "uz-AF", "sr-RS",
       
    54             "i-hak", "sgn-BE-FR", "i-lux", "vai-LR", "tl", "zh-hakka", "i-ami", "aa-SAAHO", "ha-Latn-GH",
       
    55             "zh-xiang", "i-pwn", "sgn-BE-NL", "jw", "sh", "tzm-Latn-MA", "i-bnn");
       
    56     // expected month format data for  locales after language aliases replacement.
       
    57     private static Map<String, String> shortJanuaryNames = Map.of( "pa-PK", "\u062c\u0646\u0648\u0631\u06cc",
       
    58                                                           "uz-AF" , "\u062c\u0646\u0648",
       
    59                                                           "sr-ME", "jan",
       
    60                                                           "scc", "\u0458\u0430\u043d",
       
    61                                                           "sh", "jan",
       
    62                                                           "ha-Latn-NE", "Jan",
       
    63                                                           "i-lux", "Jan.");
       
    64 
       
    65 
       
    66     private static void test(String tag, String expected) {
       
    67         Locale target = Locale.forLanguageTag(tag);
       
    68         Month day = Month.JANUARY;
       
    69         TextStyle style = TextStyle.SHORT;
       
    70         String actual = day.getDisplayName(style, target);
       
    71         if (!actual.equals(expected)) {
       
    72             throw new RuntimeException("failed for locale  " + tag + " actual output " + actual +"  does not match with  " + expected);
       
    73         }
       
    74     }
       
    75 
       
    76     /**
       
    77      * getAvailableLocales() should not contain any deprecated or Legacy language tags
       
    78      */
       
    79     private static void checkInvalidTags() {
       
    80         Set<String> invalidTags = new HashSet<>();
       
    81         Arrays.asList(Locale.getAvailableLocales()).stream()
       
    82                 .map(loc -> loc.toLanguageTag())
       
    83                 .forEach( tag -> {if(LegacyAliases.contains(tag)) {invalidTags.add(tag);}});
       
    84         if (!invalidTags.isEmpty()) {
       
    85           throw new RuntimeException("failed: Deprecated and Legacy tags found  " + invalidTags  + " in AvailableLocales ");
       
    86         }
       
    87     }
       
    88 
       
    89     public static void main(String[] args) {
       
    90         shortJanuaryNames.forEach((key, value) -> test(key, value));
       
    91         checkInvalidTags();
       
    92     }
       
    93 }