1 /* 2 ******************************************************************************* 3 * Copyright (C) 2008-2015, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7 package com.ibm.icu.dev.test.localespi; 8 9 import java.text.DateFormatSymbols; 10 import java.util.Locale; 11 12 import com.ibm.icu.dev.test.TestFmwk; 13 import com.ibm.icu.util.ULocale; 14 15 public class DateFormatSymbolsTest extends TestFmwk { main(String[] args)16 public static void main(String[] args) throws Exception { 17 new DateFormatSymbolsTest().run(args); 18 } 19 20 /* 21 * Check if getInstance returns the ICU implementation. 22 */ TestGetInstance()23 public void TestGetInstance() { 24 for (Locale loc : DateFormatSymbols.getAvailableLocales()) { 25 if (TestUtil.isExcluded(loc)) { 26 logln("Skipped " + loc); 27 continue; 28 } 29 30 DateFormatSymbols dfs = DateFormatSymbols.getInstance(loc); 31 32 boolean isIcuImpl = (dfs instanceof com.ibm.icu.impl.jdkadapter.DateFormatSymbolsICU); 33 34 if (TestUtil.isICUExtendedLocale(loc)) { 35 if (!isIcuImpl) { 36 errln("FAIL: getInstance returned JDK DateFormatSymbols for locale " + loc); 37 } 38 } else { 39 if (isIcuImpl) { 40 logln("INFO: getInstance returned ICU DateFormatSymbols for locale " + loc); 41 } 42 Locale iculoc = TestUtil.toICUExtendedLocale(loc); 43 DateFormatSymbols dfsIcu = DateFormatSymbols.getInstance(iculoc); 44 if (isIcuImpl) { 45 if (!dfs.equals(dfsIcu)) { 46 errln("FAIL: getInstance returned ICU DateFormatSymbols for locale " + loc 47 + ", but different from the one for locale " + iculoc); 48 } 49 } else { 50 if (!(dfsIcu instanceof com.ibm.icu.impl.jdkadapter.DateFormatSymbolsICU)) { 51 errln("FAIL: getInstance returned JDK DateFormatSymbols for locale " + iculoc); 52 } 53 } 54 } 55 } 56 } 57 58 /* 59 * Testing the contents of DateFormatSymbols between ICU instance and its 60 * equivalent created via the Locale SPI framework. 61 */ TestICUEquivalent()62 public void TestICUEquivalent() { 63 Locale[] TEST_LOCALES = { 64 new Locale("en", "US"), 65 new Locale("es", "ES"), 66 new Locale("ja", "JP", "JP"), 67 new Locale("th", "TH"), 68 }; 69 70 for (Locale loc : TEST_LOCALES) { 71 Locale iculoc = TestUtil.toICUExtendedLocale(loc); 72 DateFormatSymbols jdkDfs = DateFormatSymbols.getInstance(iculoc); 73 com.ibm.icu.text.DateFormatSymbols icuDfs = com.ibm.icu.text.DateFormatSymbols.getInstance(loc); 74 75 compareArrays(jdkDfs.getAmPmStrings(), icuDfs.getAmPmStrings(), loc, "getAmPmStrings"); 76 compareArrays(jdkDfs.getEras(), icuDfs.getEras(), loc, "getEras"); 77 compareArrays(jdkDfs.getMonths(), icuDfs.getMonths(), loc, "getMonths"); 78 compareArrays(jdkDfs.getShortMonths(), icuDfs.getShortMonths(), loc, "getShortMonths"); 79 compareArrays(jdkDfs.getShortWeekdays(), icuDfs.getShortWeekdays(), loc, "getShortWeekdays"); 80 compareArrays(jdkDfs.getWeekdays(), icuDfs.getWeekdays(), loc, "getWeekdays"); 81 compareArrays(jdkDfs.getZoneStrings(), icuDfs.getZoneStrings(), loc, "getZoneStrings"); 82 } 83 } 84 85 /* 86 * Testing setters 87 */ TestSetSymbols()88 public void TestSetSymbols() { 89 // ICU's JDK DateFormatSymbols implementation for ja_JP locale 90 DateFormatSymbols dfs = DateFormatSymbols.getInstance(new Locale("ja", "JP", "ICU")); 91 92 // en_US is supported by JDK, so this is the JDK's own DateFormatSymbols 93 Locale loc = new Locale("en", "US"); 94 DateFormatSymbols dfsEnUS = DateFormatSymbols.getInstance(loc); 95 96 // Copying over all symbols 97 dfs.setAmPmStrings(dfsEnUS.getAmPmStrings()); 98 dfs.setEras(dfsEnUS.getEras()); 99 dfs.setMonths(dfsEnUS.getMonths()); 100 dfs.setShortMonths(dfsEnUS.getShortMonths()); 101 dfs.setShortWeekdays(dfsEnUS.getShortWeekdays()); 102 dfs.setWeekdays(dfsEnUS.getWeekdays()); 103 dfs.setZoneStrings(dfsEnUS.getZoneStrings()); 104 105 compareArrays(dfs.getAmPmStrings(), dfsEnUS.getAmPmStrings(), loc, "getAmPmStrings"); 106 compareArrays(dfs.getEras(), dfsEnUS.getEras(), loc, "getEras"); 107 compareArrays(dfs.getMonths(), dfsEnUS.getMonths(), loc, "getMonths"); 108 compareArrays(dfs.getShortMonths(), dfsEnUS.getShortMonths(), loc, "getShortMonths"); 109 compareArrays(dfs.getShortWeekdays(), dfsEnUS.getShortWeekdays(), loc, "getShortWeekdays"); 110 compareArrays(dfs.getWeekdays(), dfsEnUS.getWeekdays(), loc, "getWeekdays"); 111 compareArrays(dfs.getZoneStrings(), dfsEnUS.getZoneStrings(), loc, "getZoneStrings"); 112 } 113 compareArrays(Object jarray, Object iarray, Locale loc, String method)114 private void compareArrays(Object jarray, Object iarray, Locale loc, String method) { 115 if (jarray instanceof String[][]) { 116 String[][] jaa = (String[][])jarray; 117 String[][] iaa = (String[][])iarray; 118 119 if (jaa.length != iaa.length || jaa[0].length != iaa[0].length) { 120 errln("FAIL: Different array size returned by " + method + "for locale " 121 + loc + "(jdksize=" + jaa.length + "x" + jaa[0].length 122 + ",icusize=" + iaa.length + "x" + iaa[0].length + ")"); 123 } 124 125 for (int i = 0; i < jaa.length; i++) { 126 for (int j = 0; j < jaa[i].length; j++) { 127 if (!TestUtil.equals(jaa[i][j], iaa[i][j])) { 128 errln("FAIL: Different symbols returned by " + method + "for locale " 129 + loc + " at index " + i + "," + j 130 + " (jdk=" + jaa[i][j] + ",icu=" + iaa[i][j] + ")"); 131 } 132 } 133 } 134 135 } else { 136 String[] ja = (String[])jarray; 137 String[] ia = (String[])iarray; 138 139 if (ja.length != ia.length) { 140 errln("FAIL: Different array size returned by " + method + "for locale " 141 + loc + "(jdksize=" + ja.length 142 + ",icusize=" + ia.length + ")"); 143 } else { 144 for (int i = 0; i < ja.length; i++) { 145 if (!TestUtil.equals(ja[i], ia[i])) { 146 errln("FAIL: Different symbols returned by " + method + "for locale " 147 + loc + " at index " + i + " (jdk=" + ja[i] + ",icu=" + ia[i] + ")"); 148 } 149 } 150 } 151 } 152 } 153 154 /* 155 * Testing Nynorsk locales 156 */ TestNynorsk()157 public void TestNynorsk() { 158 Locale nnNO = new Locale("nn", "NO"); 159 Locale noNONY = new Locale("no", "NO", "NY"); 160 161 DateFormatSymbols dfs_nnNO = DateFormatSymbols.getInstance(nnNO); 162 DateFormatSymbols dfs_nnNO_ICU = DateFormatSymbols.getInstance(TestUtil.toICUExtendedLocale(nnNO)); 163 DateFormatSymbols dfs_noNONY_ICU = DateFormatSymbols.getInstance(TestUtil.toICUExtendedLocale(noNONY)); 164 165 // Weekday names should be identical for these three. 166 // If data is taken from no/nb, then this check will fail. 167 String[] dow_nnNO = dfs_nnNO.getWeekdays(); 168 String[] dow_nnNO_ICU = dfs_nnNO_ICU.getWeekdays(); 169 String[] dow_noNONY_ICU = dfs_noNONY_ICU.getWeekdays(); 170 171 for (int i = 1; i < dow_nnNO.length; i++) { 172 if (!dow_nnNO[i].equals(dow_nnNO_ICU[i])) { 173 errln("FAIL: Different weekday name - index=" + i 174 + ", nn_NO:" + dow_nnNO[i] + ", nn_NO_ICU:" + dow_nnNO_ICU[i]); 175 } 176 } 177 for (int i = 1; i < dow_nnNO.length; i++) { 178 if (!dow_nnNO[i].equals(dow_noNONY_ICU[i])) { 179 errln("FAIL: Different weekday name - index=" + i 180 + ", nn_NO:" + dow_nnNO[i] + ", no_NO_NY_ICU:" + dow_nnNO_ICU[i]); 181 } 182 } 183 } 184 TestCalendarKeyword()185 public void TestCalendarKeyword() { 186 // ICU provider variant is appended 187 ULocale uloc0 = new ULocale("en_US_" + TestUtil.ICU_VARIANT + "@calendar=japanese"); 188 Locale loc = uloc0.toLocale(); 189 // On Java 7+, locale extension is preserved 190 ULocale uloc = ULocale.forLocale(loc); 191 String calType = uloc.getKeywordValue("calendar"); 192 if (calType == null) { 193 // Java 6 - skip this test 194 return; 195 } 196 197 DateFormatSymbols jdkDfs = DateFormatSymbols.getInstance(loc); 198 com.ibm.icu.text.DateFormatSymbols icuDfs = com.ibm.icu.text.DateFormatSymbols.getInstance(uloc); 199 200 // Check the length of era, so we can check if Japanese calendar is picked up 201 if (jdkDfs.getEras().length != icuDfs.getEras().length) { 202 errln("FAIL: Calendar keyword was ignored"); 203 } 204 } 205 } 206