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 8204603
29  * @summary Test that correct data is retrieved for zh_CN and zh_TW locales
30  * and CLDR provider supports all locales for which aliases exist.
31  * @modules java.base/sun.util.locale.provider
32  *          jdk.localedata
33  * @run main Bug8204603
34  */
35 
36 import java.text.DateFormatSymbols;
37 import java.text.DecimalFormatSymbols;
38 import java.util.Calendar;
39 import java.util.HashSet;
40 import java.util.List;
41 import java.util.Locale;
42 import java.util.Map;
43 import java.util.Set;
44 import sun.util.locale.provider.LocaleProviderAdapter;
45 
46 /**
47  * This test is dependent on a particular version of CLDR data.
48  */
49 public class Bug8204603 {
50 
51     /**
52      * List of all locales for which CLDR provides alias Mappings. e.g alias of
53      * zh-HK is zh-Hant-HK
54      */
55     private static final List<Locale> ALIAS_LOCALES
56             = List.of(Locale.forLanguageTag("az-AZ"), Locale.forLanguageTag("bs-BA"),
57                     Locale.forLanguageTag("ha-Latn-GH"), Locale.forLanguageTag("ha-Latn-NE"),
58                     Locale.forLanguageTag("ha-Latn-NG"), Locale.forLanguageTag("i-lux"),
59                     Locale.forLanguageTag("kk-Cyrl-KZ"), Locale.forLanguageTag("ks-Arab-IN"),
60                     Locale.forLanguageTag("ky-Cyrl-KG"), Locale.forLanguageTag("lb"),
61                     Locale.forLanguageTag("lb"), Locale.forLanguageTag("mn-Cyrl-MN"),
62                     Locale.forLanguageTag("mo"), Locale.forLanguageTag("ms-Latn-BN"),
63                     Locale.forLanguageTag("ms-Latn-MY"), Locale.forLanguageTag("ms-Latn-SG"),
64                     Locale.forLanguageTag("pa-IN"), Locale.forLanguageTag("pa-PK"),
65                     Locale.forLanguageTag("scc"), Locale.forLanguageTag("scr"),
66                     Locale.forLanguageTag("sh"), Locale.forLanguageTag("shi-MA"),
67                     Locale.forLanguageTag("sr-BA"), Locale.forLanguageTag("sr-RS"),
68                     Locale.forLanguageTag("sr-XK"), Locale.forLanguageTag("tl"),
69                     Locale.forLanguageTag("tzm-Latn-MA"), Locale.forLanguageTag("ug-Arab-CN"),
70                     Locale.forLanguageTag("uz-AF"), Locale.forLanguageTag("uz-UZ"),
71                     Locale.forLanguageTag("vai-LR"), Locale.forLanguageTag("vai-LR"),
72                     Locale.forLanguageTag("yue-CN"), Locale.forLanguageTag("yue-HK"),
73                     Locale.forLanguageTag("zh-CN"), Locale.forLanguageTag("zh-HK"),
74                     Locale.forLanguageTag("zh-MO"), Locale.forLanguageTag("zh-SG"),
75                     Locale.forLanguageTag("zh-TW"));
76 
77     private static final Map<Locale, String> CALENDAR_DATA_MAP = Map.of(
78             Locale.forLanguageTag("zh-CN"), "\u5468\u65E5",
79             Locale.forLanguageTag("zh-TW"), "\u9031\u65E5");
80     private static final Map<Locale, String> NAN_DATA_MAP = Map.of(
81             Locale.forLanguageTag("zh-CN"), "NaN",
82             Locale.forLanguageTag("zh-TW"), "\u975E\u6578\u503C");
83 
main(String[] args)84     public static void main(String[] args) {
85         testCldrSupportedLocales();
86         CALENDAR_DATA_MAP.forEach((k, v) -> testCalendarData(k, v));
87         NAN_DATA_MAP.forEach((k, v) -> testNanData(k, v));
88     }
89 
90     /**
91      * tests that CLDR provider should return true for alias locales.
92      *
93      */
testCldrSupportedLocales()94     private static void testCldrSupportedLocales() {
95         LocaleProviderAdapter cldr = LocaleProviderAdapter.forType(LocaleProviderAdapter.Type.CLDR);
96         Set<Locale> availableLocs = Set.of(cldr.getAvailableLocales());
97         Set<String> langtags = new HashSet<>();
98         availableLocs.forEach(loc -> langtags.add(loc.toLanguageTag()));
99         ALIAS_LOCALES.stream().filter(loc -> !cldr.isSupportedProviderLocale(loc, langtags)).findAny()
100                 .ifPresent(l -> {
101                     throw new RuntimeException("Locale " + l
102                             + "  is not supported by CLDR locale provider");
103                 });
104     }
105 
testCalendarData(Locale loc, String expected)106     private static void testCalendarData(Locale loc, String expected) {
107         DateFormatSymbols dfs = DateFormatSymbols.getInstance(loc);
108         String[] shortDays = dfs.getShortWeekdays();
109         String actual = shortDays[Calendar.SUNDAY];
110         if (!actual.equals(expected)) {
111             throw new RuntimeException("Calendar data mismatch for locale: "
112                     + loc + ", expected  is: " + expected + ", actual is: " + actual);
113         }
114     }
115 
testNanData(Locale loc, String expected)116     private static void testNanData(Locale loc, String expected) {
117         DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(loc);
118         String actual = dfs.getNaN();
119         if (!actual.equals(expected)) {
120             throw new RuntimeException("NaN mismatch for locale: "
121                     + loc + ", expected  is: " + expected + ", actual is: " + actual);
122         }
123     }
124 }
125