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