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.
   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 8179071 8202537
  27  * @summary Test that language aliases of CLDR supplemental metadata are handled correctly.
  28  * @modules jdk.localedata
  29  * @run main/othervm -Djava.locale.providers=CLDR Bug8179071
  30  */
  31 
  32 /**
  33  * This fix is dependent on a particular version of CLDR data.
  34  */
  35 
  36 import java.time.Month;
  37 import java.time.format.TextStyle;
  38 import java.util.Arrays;
  39 import java.util.HashSet;
  40 import java.util.Locale;
  41 import java.util.Map;
  42 import java.util.Set;
  43 
  44 public class Bug8179071 {
  45 
  46     // Deprecated and Legacy tags.
  47     private static final Set<String> LegacyAliases = Set.of("pa-PK", "ug-Arab-CN", "kk-Cyrl-KZ",
  48             "bs-BA", "ks-Arab-IN", "mn-Cyrl-MN", "ha-Latn-NE",
  49             "shi-MA", "ha-Latn-NG", "ms-Latn-BN","ms-Latn-SG",
  50             "ky-Cyrl-KG", "az-AZ", "zh-guoyu", "zh-min-nan", "i-klingon", "i-tsu",
  51             "sr-XK", "sgn-CH-DE", "mo", "i-tay", "scc", "uz-UZ", "uz-AF", "sr-RS",
  52             "i-hak", "sgn-BE-FR", "i-lux", "vai-LR", "tl", "zh-hakka", "i-ami", "aa-SAAHO", "ha-Latn-GH",
  53             "zh-xiang", "i-pwn", "sgn-BE-NL", "jw", "sh", "tzm-Latn-MA", "i-bnn");
  54     // expected month format data for  locales after language aliases replacement.
  55     private static Map<String, String> shortJanuaryNames = Map.of( "pa-PK", "\u062c\u0646\u0648\u0631\u06cc",
  56                                                           "uz-AF" , "\u062c\u0646\u0648",
  57                                                           "sr-ME", "jan.",
  58                                                           "scc", "\u0458\u0430\u043d",
  59                                                           "sh", "jan",
  60                                                           "ha-Latn-NE", "Jan",
  61                                                           "i-lux", "Jan.");
  62 
  63 
  64     private static void test(String tag, String expected) {
  65         Locale target = Locale.forLanguageTag(tag);
  66         Month day = Month.JANUARY;
  67         TextStyle style = TextStyle.SHORT;
  68         String actual = day.getDisplayName(style, target);
  69         if (!actual.equals(expected)) {
  70             throw new RuntimeException("failed for locale  " + tag + " actual output " + actual +"  does not match with  " + expected);
  71         }
  72     }
  73 
  74     /**
  75      * getAvailableLocales() should not contain any deprecated or Legacy language tags
  76      */
  77     private static void checkInvalidTags() {
  78         Set<String> invalidTags = new HashSet<>();
  79         Arrays.asList(Locale.getAvailableLocales()).stream()
  80                 .map(loc -> loc.toLanguageTag())
  81                 .forEach( tag -> {if(LegacyAliases.contains(tag)) {invalidTags.add(tag);}});
  82         if (!invalidTags.isEmpty()) {
  83           throw new RuntimeException("failed: Deprecated and Legacy tags found  " + invalidTags  + " in AvailableLocales ");
  84         }
  85     }
  86 
  87     public static void main(String[] args) {
  88         shortJanuaryNames.forEach((key, value) -> test(key, value));
  89         checkInvalidTags();
  90     }
  91 }