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 8008577
  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", "\u65f6\u533a",
  67             "java.time.islamic.DatePatterns", new String[] {
  68                 "Gy\u5e74M\u6708d\u65e5EEEE",
  69                 "Gy\u5e74M\u6708d\u65e5",
  70                 "Gy\u5e74M\u6708d\u65e5",
  71                 "Gy/M/d",
  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                 "dd.MM.y G",
  82                 "dd.MM.yy GGGGG",
  83             },
  84             "calendarname.islamic", "Islamischer Kalender",
  85         },
  86         {
  87             Locale.FRANCE,
  88             "field.dayperiod", "cadran",
  89             "java.time.islamic.DatePatterns", new String[] {
  90                 "EEEE d MMMM y G",
  91                 "d MMMM y G",
  92                 "d MMM y G",
  93                 "dd/MM/y GGGGG",
  94             },
  95             "calendarname.islamic", "calendrier musulman",
  96         },
  97     };
  98 
  99     // Islamic calendar symbol names in ar
 100     private static final String[] ISLAMIC_MONTH_NAMES = {
 101         "\u0645\u062d\u0631\u0645",
 102         "\u0635\u0641\u0631",
 103         "\u0631\u0628\u064a\u0639 \u0627\u0644\u0623\u0648\u0644",
 104         "\u0631\u0628\u064a\u0639 \u0627\u0644\u0622\u062e\u0631",
 105         "\u062c\u0645\u0627\u062f\u0649 \u0627\u0644\u0623\u0648\u0644\u0649",
 106         "\u062c\u0645\u0627\u062f\u0649 \u0627\u0644\u0622\u062e\u0631\u0629",
 107         "\u0631\u062c\u0628",
 108         "\u0634\u0639\u0628\u0627\u0646",
 109         "\u0631\u0645\u0636\u0627\u0646",
 110         "\u0634\u0648\u0627\u0644",
 111         "\u0630\u0648 \u0627\u0644\u0642\u0639\u062f\u0629",
 112         "\u0630\u0648 \u0627\u0644\u062d\u062c\u0629",
 113     };
 114     private static final String[] ISLAMIC_ERA_NAMES = {
 115         "",
 116         "\u0647\u0640",
 117     };
 118 
 119     // Minguo calendar symbol names in zh_Hant
 120     private static final String[] ROC_ERA_NAMES = {
 121         "\u6c11\u570b\u524d",
 122         "\u6c11\u570b",
 123     };
 124 
 125     private static int errors = 0;
 126 
 127     // This test is CLDR data dependent.
 128     public static void main(String[] args) {
 129         for (Object[] data : CLDR_DATA) {
 130             Locale locale = (Locale) data[0];
 131             ResourceBundle rb = LocaleProviderAdapter.getResourceBundleBased()
 132                                     .getLocaleResources(locale).getJavaTimeFormatData();
 133             for (int i = 1; i < data.length; ) {
 134                 String key = (String) data[i++];
 135                 Object expected = data[i++];
 136                 if (rb.containsKey(key)) {
 137                     Object value = rb.getObject(key);
 138                     if (expected instanceof String) {
 139                         if (!expected.equals(value)) {
 140                             errors++;
 141                             System.err.printf("error: key='%s', got '%s' expected '%s', rb: %s%n",
 142                                               key, value, expected, rb);
 143                         }
 144                     } else if (expected instanceof String[]) {
 145                         try {
 146                             if (!Arrays.equals((Object[]) value, (Object[]) expected)) {
 147                                 errors++;
 148                                 System.err.printf("error: key='%s', got '%s' expected '%s', rb: %s%n",
 149                                                   key, Arrays.asList((Object[])value),
 150                                                   Arrays.asList((Object[])expected), rb);
 151                             }
 152                         } catch (Exception e) {
 153                             errors++;
 154                             e.printStackTrace();
 155                         }
 156                     }
 157                 } else {
 158                     errors++;
 159                     System.err.println("No resource for " + key+", rb: "+rb);
 160                 }
 161             }
 162         }
 163 
 164         // test Islamic calendar names in Arabic
 165         testSymbolNames(ARABIC, "islamic", ISLAMIC_MONTH_NAMES, MONTH, LONG, "month");
 166         testSymbolNames(ARABIC, "islamic", ISLAMIC_ERA_NAMES, ERA, SHORT, "era");
 167 
 168         // test ROC (Minguo) calendar names in zh-Hant
 169         testSymbolNames(ZH_HANT, "roc", ROC_ERA_NAMES, ERA, SHORT, "era");
 170 
 171         if (errors > 0) {
 172             throw new RuntimeException("test failed");
 173         }
 174     }
 175 
 176     private static void testSymbolNames(Locale locale, String calType, String[] expected,
 177                                         int field, int style, String fieldName) {
 178         for (int i = 0; i < expected.length; i++) {
 179             String expt = expected[i];
 180             String name = CalendarDataUtility.retrieveJavaTimeFieldValueName(calType, field, i, style, locale);
 181             if (!expt.equals(name)) {
 182                 errors++;
 183                 System.err.printf("error: wrong %s %s name in %s: value=%d, got='%s', expected='%s'%n",
 184                                   calType, fieldName, locale, i, name, expt);
 185             }
 186         }
 187     }
 188 }