1/*
2 * Copyright (c) 2013, 2016, 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 8004489 8006509 8008577 8145136
27 * @summary Unit test for CLDR FormatData resources
28 * @modules java.base/sun.util.locale.provider
29 *          jdk.localedata
30 * @compile -XDignore.symbol.file CldrFormatNamesTest.java
31 * @run main/othervm -Djava.locale.providers=CLDR CldrFormatNamesTest
32 */
33
34import java.util.*;
35import static java.util.Calendar.*;
36import sun.util.locale.provider.*;
37
38public class CldrFormatNamesTest {
39    private static final Locale ARABIC = new Locale("ar");
40    private static final Locale ZH_HANT = Locale.forLanguageTag("zh-Hant");
41
42    /*
43     * The first element is a Locale followed by key-value pairs
44     * in a FormatData resource bundle. The value type is either
45     * String or String[].
46     */
47    static final Object[][] CLDR_DATA = {
48        {
49            Locale.JAPAN,
50            "field.zone", "\u30bf\u30a4\u30e0\u30be\u30fc\u30f3",
51            "java.time.japanese.DatePatterns", new String[] {
52                "Gy\u5e74M\u6708d\u65e5EEEE",
53                "Gy\u5e74M\u6708d\u65e5",
54                "Gy\u5e74M\u6708d\u65e5",
55                "GGGGGy/M/d",
56            },
57            "java.time.roc.DatePatterns", new String[] {
58                "Gy\u5e74M\u6708d\u65e5EEEE",
59                "Gy\u5e74M\u6708d\u65e5",
60                "Gy/MM/dd",
61                "Gy/MM/dd",
62            },
63            "calendarname.buddhist", "\u30bf\u30a4\u4ecf\u6559\u66a6",
64        },
65        {
66            Locale.PRC,
67            "field.zone", "\u65f6\u533a",
68            "java.time.islamic.DatePatterns", new String[] {
69                "Gy\u5e74M\u6708d\u65e5EEEE",
70                "Gy\u5e74M\u6708d\u65e5",
71                "Gy\u5e74M\u6708d\u65e5",
72                "Gy/M/d",
73            },
74            "calendarname.islamic", "\u4f0a\u65af\u5170\u65e5\u5386",
75        },
76        {
77            Locale.GERMANY,
78            "field.dayperiod", "Tagesh\u00e4lfte",
79            "java.time.islamic.DatePatterns", new String[] {
80                "EEEE, d. MMMM y G",
81                "d. MMMM y G",
82                "dd.MM.y G",
83                "dd.MM.yy GGGGG",
84            },
85            "calendarname.islamic", "Islamischer Kalender",
86        },
87        {
88            Locale.FRANCE,
89            "field.dayperiod", "cadran",
90            "java.time.islamic.DatePatterns", new String[] {
91                "EEEE d MMMM y G",
92                "d MMMM y G",
93                "d MMM y G",
94                "dd/MM/y GGGGG",
95            },
96            "calendarname.islamic", "calendrier musulman",
97        },
98    };
99
100    // Islamic calendar symbol names in ar
101    private static final String[] ISLAMIC_MONTH_NAMES = {
102        "\u0645\u062d\u0631\u0645",
103        "\u0635\u0641\u0631",
104        "\u0631\u0628\u064a\u0639 \u0627\u0644\u0623\u0648\u0644",
105        "\u0631\u0628\u064a\u0639 \u0627\u0644\u0622\u062e\u0631",
106        "\u062c\u0645\u0627\u062f\u0649 \u0627\u0644\u0623\u0648\u0644\u0649",
107        "\u062c\u0645\u0627\u062f\u0649 \u0627\u0644\u0622\u062e\u0631\u0629",
108        "\u0631\u062c\u0628",
109        "\u0634\u0639\u0628\u0627\u0646",
110        "\u0631\u0645\u0636\u0627\u0646",
111        "\u0634\u0648\u0627\u0644",
112        "\u0630\u0648 \u0627\u0644\u0642\u0639\u062f\u0629",
113        "\u0630\u0648 \u0627\u0644\u062d\u062c\u0629",
114    };
115    private static final String[] ISLAMIC_ERA_NAMES = {
116        "",
117        "\u0647\u0640",
118    };
119
120    // Minguo calendar symbol names in zh_Hant
121    private static final String[] ROC_ERA_NAMES = {
122        "\u6c11\u570b\u524d",
123        "\u6c11\u570b",
124    };
125
126    private static int errors = 0;
127
128    // This test is CLDR data dependent.
129    public static void main(String[] args) {
130        for (Object[] data : CLDR_DATA) {
131            Locale locale = (Locale) data[0];
132            ResourceBundle rb = LocaleProviderAdapter.getResourceBundleBased()
133                                    .getLocaleResources(locale).getJavaTimeFormatData();
134            for (int i = 1; i < data.length; ) {
135                String key = (String) data[i++];
136                Object expected = data[i++];
137                if (rb.containsKey(key)) {
138                    Object value = rb.getObject(key);
139                    if (expected instanceof String) {
140                        if (!expected.equals(value)) {
141                            errors++;
142                            System.err.printf("error: key='%s', got '%s' expected '%s', rb: %s%n",
143                                              key, value, expected, rb);
144                        }
145                    } else if (expected instanceof String[]) {
146                        try {
147                            if (!Arrays.equals((Object[]) value, (Object[]) expected)) {
148                                errors++;
149                                System.err.printf("error: key='%s', got '%s' expected '%s', rb: %s%n",
150                                                  key, Arrays.asList((Object[])value),
151                                                  Arrays.asList((Object[])expected), rb);
152                            }
153                        } catch (Exception e) {
154                            errors++;
155                            e.printStackTrace();
156                        }
157                    }
158                } else {
159                    errors++;
160                    System.err.println("No resource for " + key+", rb: "+rb);
161                }
162            }
163        }
164
165        // test Islamic calendar names in Arabic
166        testSymbolNames(ARABIC, "islamic", ISLAMIC_MONTH_NAMES, MONTH, LONG, "month");
167        testSymbolNames(ARABIC, "islamic", ISLAMIC_ERA_NAMES, ERA, SHORT, "era");
168
169        // test ROC (Minguo) calendar names in zh-Hant
170        testSymbolNames(ZH_HANT, "roc", ROC_ERA_NAMES, ERA, SHORT, "era");
171
172        if (errors > 0) {
173            throw new RuntimeException("test failed");
174        }
175    }
176
177    private static void testSymbolNames(Locale locale, String calType, String[] expected,
178                                        int field, int style, String fieldName) {
179        for (int i = 0; i < expected.length; i++) {
180            String expt = expected[i];
181            String name = CalendarDataUtility.retrieveJavaTimeFieldValueName(calType, field, i, style, locale);
182            if (!expt.equals(name)) {
183                errors++;
184                System.err.printf("error: wrong %s %s name in %s: value=%d, got='%s', expected='%s'%n",
185                                  calType, fieldName, locale, i, name, expt);
186            }
187        }
188    }
189}
190