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