2 *******************************************************************************
\r
3 * Copyright (C) 2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.localespi;
\r
9 import java.text.DateFormat;
\r
10 import java.text.ParseException;
\r
11 import java.util.Date;
\r
12 import java.util.Locale;
\r
14 import com.ibm.icu.dev.test.TestFmwk;
\r
16 public class DateFormatTest extends TestFmwk {
\r
18 public static void main(String[] args) throws Exception {
\r
19 new DateFormatTest().run(args);
\r
23 * Check if getInstance returns the ICU implementation.
\r
25 public void TestGetInstance() {
\r
26 for (Locale loc : DateFormat.getAvailableLocales()) {
\r
27 if (TestUtil.isProblematicIBMLocale(loc)) {
\r
28 logln("Skipped " + loc);
\r
31 checkGetInstance(DateFormat.FULL, DateFormat.LONG, loc);
\r
32 checkGetInstance(DateFormat.MEDIUM, -1, loc);
\r
33 checkGetInstance(1, DateFormat.SHORT, loc);
\r
37 private void checkGetInstance(int dstyle, int tstyle, Locale loc) {
\r
38 String method[] = new String[1];
\r
39 DateFormat df = getJDKInstance(dstyle, tstyle, loc, method);
\r
41 boolean isIcuImpl = (df instanceof com.ibm.icu.impl.jdkadapter.SimpleDateFormatICU);
\r
43 if (TestUtil.isICUExtendedLocale(loc)) {
\r
45 errln("FAIL: " + method[0] + " returned JDK DateFormat for locale " + loc);
\r
49 logln("INFO: " + method[0] + " returned ICU DateFormat for locale " + loc);
\r
51 Locale iculoc = TestUtil.toICUExtendedLocale(loc);
\r
52 DateFormat dfIcu = getJDKInstance(dstyle, tstyle, iculoc, null);
\r
54 if (!df.equals(dfIcu)) {
\r
55 errln("FAIL: " + method[0] + " returned ICU DateFormat for locale " + loc
\r
56 + ", but different from the one for locale " + iculoc);
\r
59 if (!(dfIcu instanceof com.ibm.icu.impl.jdkadapter.SimpleDateFormatICU)) {
\r
60 errln("FAIL: " + method[0] + " returned JDK DateFormat for locale " + iculoc);
\r
66 private DateFormat getJDKInstance(int dstyle, int tstyle, Locale loc, String[] methodName) {
\r
70 df = DateFormat.getTimeInstance(tstyle, loc);
\r
71 method = "getTimeInstance";
\r
72 } else if (tstyle < 0) {
\r
73 df = DateFormat.getDateInstance(dstyle, loc);
\r
74 method = "getDateInstance";
\r
76 df = DateFormat.getDateTimeInstance(dstyle, tstyle, loc);
\r
77 method = "getDateTimeInstance";
\r
79 if (methodName != null) {
\r
80 methodName[0] = method;
\r
85 private com.ibm.icu.text.DateFormat getICUInstance(int dstyle, int tstyle, Locale loc, String[] methodName) {
\r
86 com.ibm.icu.text.DateFormat icudf;
\r
89 icudf = com.ibm.icu.text.DateFormat.getTimeInstance(tstyle, loc);
\r
90 method = "getTimeInstance";
\r
91 } else if (tstyle < 0) {
\r
92 icudf = com.ibm.icu.text.DateFormat.getDateInstance(dstyle, loc);
\r
93 method = "getDateInstance";
\r
95 icudf = com.ibm.icu.text.DateFormat.getDateTimeInstance(dstyle, tstyle, loc);
\r
96 method = "getDateTimeInstance";
\r
98 if (methodName != null) {
\r
99 methodName[0] = method;
\r
105 * Testing the behavior of date format between ICU instance and its
\r
106 * equivalent created via the Locale SPI framework.
\r
108 public void TestICUEquivalent() {
\r
109 Locale[] TEST_LOCALES = {
\r
110 new Locale("en", "US"),
\r
111 new Locale("it", "IT"),
\r
112 new Locale("iw", "IL"),
\r
113 new Locale("ja", "JP", "JP"),
\r
114 new Locale("th", "TH"),
\r
115 new Locale("zh", "TW"),
\r
118 long[] TEST_DATES = {
\r
119 1199499330543L, // 2008-01-05T02:15:30.543Z
\r
120 1217001308085L, // 2008-07-25T15:55:08.085Z
\r
123 for (Locale loc : TEST_LOCALES) {
\r
124 for (int dstyle = -1; dstyle <= 3; dstyle++) {
\r
125 for (int tstyle = -1; tstyle <= 3; tstyle++) {
\r
126 if (tstyle == -1 && dstyle == -1) {
\r
129 Locale iculoc = TestUtil.toICUExtendedLocale(loc);
\r
130 DateFormat df = getJDKInstance(dstyle, tstyle, iculoc, null);
\r
131 com.ibm.icu.text.DateFormat icudf = getICUInstance(dstyle, tstyle, loc, null);
\r
133 for (long t : TEST_DATES) {
\r
135 Date d = new Date(t);
\r
136 String dstr1 = df.format(d);
\r
137 String dstr2 = icudf.format(d);
\r
139 if (!dstr1.equals(dstr2)) {
\r
140 errln("FAIL: Different format results for locale " + loc + " (dstyle=" + dstyle
\r
141 + ",tstyle=" + tstyle + ") at time " + t + " - JDK:" + dstr1
\r
142 + " ICU:" + dstr2);
\r
149 d1 = df.parse(dstr1);
\r
150 } catch (ParseException e) {
\r
151 errln("FAIL: ParseException thrown for JDK DateFormat for string "
\r
152 + dstr1 + "(locale=" + iculoc + ",dstyle=" + dstyle + ",tstyle=" + tstyle + ")");
\r
156 d2 = icudf.parse(dstr1);
\r
157 } catch (ParseException e) {
\r
158 errln("FAIL: ParseException thrown for ICU DateFormat for string "
\r
159 + dstr1 + "(locale=" + loc + ",dstyle=" + dstyle + ",tstyle=" + tstyle + ")");
\r
162 if (!d1.equals(d2)) {
\r
163 errln("FAIL: Different parse results for locale " + loc
\r
164 + " for date string " + dstr1 + " (dstyle=" + dstyle
\r
165 + ",tstyle=" + tstyle + ") at time " + t + " - JDK:" + dstr1
\r
166 + " ICU:" + dstr2);
\r
175 * Check if ICU DateFormatProvider uses Thai native digit for Locale
\r
178 public void TestThaiDigit() {
\r
179 Locale thTHTH = new Locale("th", "TH", "TH");
\r
180 String pattern = "yyyy-MM-dd";
\r
182 DateFormat dfmt = DateFormat.getDateInstance(DateFormat.FULL, thTHTH);
\r
183 DateFormat dfmtIcu = DateFormat.getDateInstance(DateFormat.FULL, TestUtil.toICUExtendedLocale(thTHTH));
\r
185 ((java.text.SimpleDateFormat)dfmt).applyPattern(pattern);
\r
186 ((java.text.SimpleDateFormat)dfmtIcu).applyPattern(pattern);
\r
188 Date d = new Date();
\r
189 String str1 = dfmt.format(d);
\r
190 String str2 = dfmtIcu.format(d);
\r
192 if (!str1.equals(str2)) {
\r
193 errln("FAIL: ICU DateFormat returned a result different from JDK for th_TH_TH");
\r