]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/DateFormatTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / localespi / src / com / ibm / icu / dev / test / localespi / DateFormatTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2008, International Business Machines Corporation and         *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.localespi;\r
8 \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
13 \r
14 import com.ibm.icu.dev.test.TestFmwk;\r
15 \r
16 public class DateFormatTest extends TestFmwk {\r
17 \r
18     public static void main(String[] args) throws Exception {\r
19         new DateFormatTest().run(args);\r
20     }\r
21 \r
22     /*\r
23      * Check if getInstance returns the ICU implementation.\r
24      */\r
25     public void TestGetInstance() {\r
26         for (Locale loc : DateFormat.getAvailableLocales()) {\r
27             if (TestUtil.isProblematicIBMLocale(loc)) {\r
28                 logln("Skipped " + loc);\r
29                 continue;\r
30             }\r
31             checkGetInstance(DateFormat.FULL, DateFormat.LONG, loc);\r
32             checkGetInstance(DateFormat.MEDIUM, -1, loc);\r
33             checkGetInstance(1, DateFormat.SHORT, loc);\r
34         }\r
35     }\r
36 \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
40 \r
41         boolean isIcuImpl = (df instanceof com.ibm.icu.impl.jdkadapter.SimpleDateFormatICU);\r
42 \r
43         if (TestUtil.isICUExtendedLocale(loc)) {\r
44             if (!isIcuImpl) {\r
45                 errln("FAIL: " + method[0] + " returned JDK DateFormat for locale " + loc);\r
46             }\r
47         } else {\r
48             if (isIcuImpl) {\r
49                 logln("INFO: " + method[0] + " returned ICU DateFormat for locale " + loc);\r
50             }\r
51             Locale iculoc = TestUtil.toICUExtendedLocale(loc);\r
52             DateFormat dfIcu = getJDKInstance(dstyle, tstyle, iculoc, null);\r
53             if (isIcuImpl) {\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
57                 }\r
58             } else {\r
59                 if (!(dfIcu instanceof com.ibm.icu.impl.jdkadapter.SimpleDateFormatICU)) {\r
60                     errln("FAIL: " + method[0] + " returned JDK DateFormat for locale " + iculoc);\r
61                 }\r
62             }\r
63         }\r
64     }\r
65 \r
66     private DateFormat getJDKInstance(int dstyle, int tstyle, Locale loc, String[] methodName) {\r
67         DateFormat df;\r
68         String method;\r
69         if (dstyle < 0) {\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
75         } else {\r
76             df = DateFormat.getDateTimeInstance(dstyle, tstyle, loc);\r
77             method = "getDateTimeInstance";\r
78         }\r
79         if (methodName != null) {\r
80             methodName[0] = method;\r
81         }\r
82         return df;\r
83     }\r
84 \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
87         String method;\r
88         if (dstyle < 0) {\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
94         } else {\r
95             icudf = com.ibm.icu.text.DateFormat.getDateTimeInstance(dstyle, tstyle, loc);\r
96             method = "getDateTimeInstance";\r
97         }\r
98         if (methodName != null) {\r
99             methodName[0] = method;\r
100         }\r
101         return icudf;\r
102     }\r
103 \r
104     /*\r
105      * Testing the behavior of date format between ICU instance and its\r
106      * equivalent created via the Locale SPI framework.\r
107      */\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
116         };\r
117 \r
118         long[] TEST_DATES = {\r
119                 1199499330543L, // 2008-01-05T02:15:30.543Z\r
120                 1217001308085L, // 2008-07-25T15:55:08.085Z\r
121         };\r
122 \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
127                         continue;\r
128                     }\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
132 \r
133                     for (long t : TEST_DATES) {\r
134                         // Format\r
135                         Date d = new Date(t);\r
136                         String dstr1 = df.format(d);\r
137                         String dstr2 = icudf.format(d);\r
138 \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
143                             continue;\r
144                         }\r
145 \r
146                         // Parse\r
147                         Date d1, d2;\r
148                         try {\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
153                             continue;\r
154                         }\r
155                         try {\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
160                             continue;\r
161                         }\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
167                         }\r
168                     }\r
169                 }\r
170             }\r
171         }\r
172     }\r
173 \r
174     /*\r
175      * Check if ICU DateFormatProvider uses Thai native digit for Locale\r
176      * th_TH_TH.\r
177      */\r
178     public void TestThaiDigit() {\r
179         Locale thTHTH = new Locale("th", "TH", "TH");\r
180         String pattern = "yyyy-MM-dd";\r
181 \r
182         DateFormat dfmt = DateFormat.getDateInstance(DateFormat.FULL, thTHTH);\r
183         DateFormat dfmtIcu = DateFormat.getDateInstance(DateFormat.FULL, TestUtil.toICUExtendedLocale(thTHTH));\r
184 \r
185         ((java.text.SimpleDateFormat)dfmt).applyPattern(pattern);\r
186         ((java.text.SimpleDateFormat)dfmtIcu).applyPattern(pattern);\r
187 \r
188         Date d = new Date();\r
189         String str1 = dfmt.format(d);\r
190         String str2 = dfmtIcu.format(d);\r
191 \r
192         if (!str1.equals(str2)) {\r
193             errln("FAIL: ICU DateFormat returned a result different from JDK for th_TH_TH");\r
194         }\r
195     }\r
196 }\r