1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2008-2015, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.dev.test.localespi; 10 11 import java.text.DateFormat; 12 import java.text.ParseException; 13 import java.util.Calendar; 14 import java.util.Date; 15 import java.util.Locale; 16 17 import org.junit.Test; 18 import org.junit.runner.RunWith; 19 import org.junit.runners.JUnit4; 20 21 import com.ibm.icu.dev.test.TestFmwk; 22 import com.ibm.icu.impl.jdkadapter.CalendarICU; 23 import com.ibm.icu.util.ULocale; 24 25 @RunWith(JUnit4.class) 26 public class DateFormatTest extends TestFmwk { 27 /* 28 * Check if getInstance returns the ICU implementation. 29 */ 30 @Test TestGetInstance()31 public void TestGetInstance() { 32 for (Locale loc : DateFormat.getAvailableLocales()) { 33 if (TestUtil.isExcluded(loc)) { 34 logln("Skipped " + loc); 35 continue; 36 } 37 checkGetInstance(DateFormat.FULL, DateFormat.LONG, loc); 38 checkGetInstance(DateFormat.MEDIUM, -1, loc); 39 checkGetInstance(1, DateFormat.SHORT, loc); 40 } 41 } 42 checkGetInstance(int dstyle, int tstyle, Locale loc)43 private void checkGetInstance(int dstyle, int tstyle, Locale loc) { 44 String method[] = new String[1]; 45 DateFormat df = getJDKInstance(dstyle, tstyle, loc, method); 46 47 boolean isIcuImpl = (df instanceof com.ibm.icu.impl.jdkadapter.SimpleDateFormatICU); 48 49 if (TestUtil.isICUExtendedLocale(loc)) { 50 if (!isIcuImpl) { 51 errln("FAIL: " + method[0] + " returned JDK DateFormat for locale " + loc); 52 } 53 } else if (isIcuImpl) { 54 logln("INFO: " + method[0] + " returned ICU DateFormat for locale " + loc); 55 Locale iculoc = TestUtil.toICUExtendedLocale(loc); 56 DateFormat dfIcu = getJDKInstance(dstyle, tstyle, iculoc, null); 57 if (!df.equals(dfIcu)) { 58 errln("FAIL: " + method[0] + " returned ICU DateFormat for locale " + loc 59 + ", but different from the one for locale " + iculoc); 60 } 61 } 62 } 63 getJDKInstance(int dstyle, int tstyle, Locale loc, String[] methodName)64 private DateFormat getJDKInstance(int dstyle, int tstyle, Locale loc, String[] methodName) { 65 DateFormat df; 66 String method; 67 if (dstyle < 0) { 68 df = DateFormat.getTimeInstance(tstyle, loc); 69 method = "getTimeInstance"; 70 } else if (tstyle < 0) { 71 df = DateFormat.getDateInstance(dstyle, loc); 72 method = "getDateInstance"; 73 } else { 74 df = DateFormat.getDateTimeInstance(dstyle, tstyle, loc); 75 method = "getDateTimeInstance"; 76 } 77 if (methodName != null) { 78 methodName[0] = method; 79 } 80 return df; 81 } 82 getICUInstance(int dstyle, int tstyle, Locale loc, String[] methodName)83 private com.ibm.icu.text.DateFormat getICUInstance(int dstyle, int tstyle, Locale loc, String[] methodName) { 84 com.ibm.icu.text.DateFormat icudf; 85 String method; 86 if (dstyle < 0) { 87 icudf = com.ibm.icu.text.DateFormat.getTimeInstance(tstyle, loc); 88 method = "getTimeInstance"; 89 } else if (tstyle < 0) { 90 icudf = com.ibm.icu.text.DateFormat.getDateInstance(dstyle, loc); 91 method = "getDateInstance"; 92 } else { 93 icudf = com.ibm.icu.text.DateFormat.getDateTimeInstance(dstyle, tstyle, loc); 94 method = "getDateTimeInstance"; 95 } 96 if (methodName != null) { 97 methodName[0] = method; 98 } 99 return icudf; 100 } 101 102 /* 103 * Testing the behavior of date format between ICU instance and its 104 * equivalent created via the Locale SPI framework. 105 */ 106 @Test TestICUEquivalent()107 public void TestICUEquivalent() { 108 Locale[] TEST_LOCALES = { 109 new Locale("en", "US"), 110 new Locale("it", "IT"), 111 new Locale("iw", "IL"), 112 new Locale("ja", "JP", "JP"), 113 new Locale("th", "TH"), 114 new Locale("zh", "TW"), 115 }; 116 117 long[] TEST_DATES = { 118 1199499330543L, // 2008-01-05T02:15:30.543Z 119 1217001308085L, // 2008-07-25T15:55:08.085Z 120 }; 121 122 for (Locale loc : TEST_LOCALES) { 123 for (int dstyle = -1; dstyle <= 3; dstyle++) { 124 for (int tstyle = -1; tstyle <= 3; tstyle++) { 125 if (tstyle == -1 && dstyle == -1) { 126 continue; 127 } 128 Locale iculoc = TestUtil.toICUExtendedLocale(loc); 129 DateFormat df = getJDKInstance(dstyle, tstyle, iculoc, null); 130 com.ibm.icu.text.DateFormat icudf = getICUInstance(dstyle, tstyle, loc, null); 131 132 for (long t : TEST_DATES) { 133 // Format 134 Date d = new Date(t); 135 String dstr1 = df.format(d); 136 String dstr2 = icudf.format(d); 137 138 if (!dstr1.equals(dstr2)) { 139 errln("FAIL: Different format results for locale " + loc + " (dstyle=" + dstyle 140 + ",tstyle=" + tstyle + ") at time " + t + " - JDK:" + dstr1 141 + " ICU:" + dstr2); 142 continue; 143 } 144 145 // Parse 146 Date d1, d2; 147 try { 148 d1 = df.parse(dstr1); 149 } catch (ParseException e) { 150 errln("FAIL: ParseException thrown for JDK DateFormat for string " 151 + dstr1 + "(locale=" + iculoc + ",dstyle=" + dstyle + ",tstyle=" + tstyle + ")"); 152 continue; 153 } 154 try { 155 d2 = icudf.parse(dstr1); 156 } catch (ParseException e) { 157 errln("FAIL: ParseException thrown for ICU DateFormat for string " 158 + dstr1 + "(locale=" + loc + ",dstyle=" + dstyle + ",tstyle=" + tstyle + ")"); 159 continue; 160 } 161 if (!d1.equals(d2)) { 162 errln("FAIL: Different parse results for locale " + loc 163 + " for date string " + dstr1 + " (dstyle=" + dstyle 164 + ",tstyle=" + tstyle + ") at time " + t + " - JDK:" + dstr1 165 + " ICU:" + dstr2); 166 } 167 } 168 } 169 } 170 } 171 } 172 173 /* 174 * Check if ICU DateFormatProvider uses Thai native digit for Locale 175 * th_TH_TH. 176 */ 177 @Test TestThaiDigit()178 public void TestThaiDigit() { 179 Locale thTHTH = new Locale("th", "TH", "TH"); 180 String pattern = "yyyy-MM-dd"; 181 182 DateFormat dfmt = DateFormat.getDateInstance(DateFormat.FULL, thTHTH); 183 DateFormat dfmtIcu = DateFormat.getDateInstance(DateFormat.FULL, TestUtil.toICUExtendedLocale(thTHTH)); 184 185 ((java.text.SimpleDateFormat)dfmt).applyPattern(pattern); 186 ((java.text.SimpleDateFormat)dfmtIcu).applyPattern(pattern); 187 188 Date d = new Date(); 189 String str1 = dfmt.format(d); 190 String str2 = dfmtIcu.format(d); 191 192 if (!str1.equals(str2)) { 193 errln("FAIL: ICU DateFormat returned a result different from JDK for th_TH_TH"); 194 } 195 } 196 197 @Test TestCalendarKeyword()198 public void TestCalendarKeyword() { 199 // ICU provider variant is appended 200 ULocale uloc = new ULocale("en_US_" + TestUtil.ICU_VARIANT + "@calendar=buddhist"); 201 Locale loc = uloc.toLocale(); 202 DateFormat jdkDfmt = DateFormat.getDateInstance(DateFormat.FULL, loc); 203 Calendar cal = jdkDfmt.getCalendar(); 204 boolean isBuddhist = false; 205 if (cal instanceof CalendarICU) { 206 com.ibm.icu.util.Calendar icuCal = ((CalendarICU)cal).unwrap(); 207 isBuddhist = icuCal.getType().equals("buddhist"); 208 } 209 if (!isBuddhist) { 210 errln("FAIL: Calendar types is not Buddhist"); 211 } 212 } 213 } 214