]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/format/IntlTestDateFormatAPI.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / format / IntlTestDateFormatAPI.java
1 /*****************************************************************************************\r
2  *\r
3  *   Copyright (C) 1996-2008, International Business Machines\r
4  *   Corporation and others.  All Rights Reserved.\r
5  **/\r
6 \r
7 /** \r
8  * Port From:   JDK 1.4b1 : java.text.Format.IntlTestDateFormatAPI\r
9  * Source File: java/text/format/IntlTestDateFormatAPI.java\r
10  **/\r
11 \r
12 /*\r
13     @test 1.4 98/03/06\r
14     @summary test International Date Format API\r
15 */\r
16 \r
17 package com.ibm.icu.dev.test.format;\r
18 \r
19 import com.ibm.icu.util.*;\r
20 import com.ibm.icu.text.*;\r
21 import java.util.Locale;\r
22 import java.util.Date;\r
23 import java.text.ParsePosition;\r
24 import java.text.FieldPosition;\r
25 import java.text.ParseException;\r
26 \r
27 public class IntlTestDateFormatAPI extends com.ibm.icu.dev.test.TestFmwk\r
28 {\r
29     public static void main(String[] args) throws Exception {\r
30         new IntlTestDateFormatAPI().run(args);\r
31     }\r
32 \r
33     // Test that the equals method works correctly.\r
34     public void TestEquals()\r
35     {\r
36         // Create two objects at different system times\r
37         DateFormat a = DateFormat.getInstance();\r
38         Date start = Calendar.getInstance().getTime();\r
39         while (true) {\r
40             // changed to remove compiler warnings.\r
41             if (!start.equals(Calendar.getInstance().getTime())) {\r
42                 break; // Wait for time to change\r
43             }\r
44         }\r
45         DateFormat b = DateFormat.getInstance();\r
46 \r
47         if (!(a.equals(b)))\r
48             errln("FAIL: DateFormat objects created at different times are unequal.");\r
49 \r
50         // Why has this test been disabled??? - aliu\r
51 //        if (b instanceof SimpleDateFormat)\r
52 //        {\r
53 //            //double ONE_YEAR = 365*24*60*60*1000.0; //The variable is never used\r
54 //            try {\r
55 //                ((SimpleDateFormat)b).setTwoDigitStartDate(start.getTime() + 50*ONE_YEAR);\r
56 //                if (a.equals(b))\r
57 //                    errln("FAIL: DateFormat objects with different two digit start dates are equal.");\r
58 //            }\r
59 //            catch (Exception e) {\r
60 //                errln("FAIL: setTwoDigitStartDate failed.");\r
61 //            }\r
62 //        }\r
63     }\r
64 \r
65     // This test checks various generic API methods in DateFormat to achieve 100% API coverage.\r
66     public void TestAPI()\r
67     {\r
68         logln("DateFormat API test---"); logln("");\r
69         Locale.setDefault(Locale.ENGLISH);\r
70 \r
71 \r
72         // ======= Test constructors\r
73 \r
74         logln("Testing DateFormat constructors");\r
75 \r
76         DateFormat def = DateFormat.getInstance();\r
77         DateFormat fr = DateFormat.getTimeInstance(DateFormat.FULL, Locale.FRENCH);\r
78         DateFormat it = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALIAN);\r
79         DateFormat de = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.GERMAN);\r
80 \r
81         // ======= Test equality\r
82 \r
83         logln("Testing equality operator");\r
84 \r
85         if( fr.equals(it) ) {\r
86             errln("ERROR: equals failed");\r
87         }\r
88 \r
89         // ======= Test various format() methods\r
90 \r
91         logln("Testing various format() methods");\r
92 \r
93         Date d = new Date((long)837039928046.0);\r
94 \r
95         StringBuffer res1 = new StringBuffer();\r
96         StringBuffer res2 = new StringBuffer();\r
97         String res3 = new String();\r
98         FieldPosition pos1 = new FieldPosition(0);\r
99         FieldPosition pos2 = new FieldPosition(0);\r
100 \r
101         res1 = fr.format(d, res1, pos1);\r
102         logln("" + d.getTime() + " formatted to " + res1);\r
103 \r
104         res2 = it.format(d, res2, pos2);\r
105         logln("" + d.getTime() + " formatted to " + res2);\r
106 \r
107         res3 = de.format(d);\r
108         logln("" + d.getTime() + " formatted to " + res3);\r
109 \r
110         // ======= Test parse()\r
111 \r
112         logln("Testing parse()");\r
113 \r
114         String text = new String("02/03/76 2:50 AM, CST");\r
115         Object result1 = new Date();\r
116         Date result2 = new Date();\r
117         Date result3 = new Date();\r
118         ParsePosition pos = new ParsePosition(0);\r
119         ParsePosition pos01 = new ParsePosition(0);\r
120 \r
121         result1 = def.parseObject(text, pos);\r
122         if (result1 == null) {\r
123             errln("ERROR: parseObject() failed for " + text);\r
124         }\r
125         logln(text + " parsed into " + ((Date)result1).getTime());\r
126 \r
127         try {\r
128             result2 = def.parse(text);\r
129         }\r
130         catch (ParseException e) {\r
131             errln("ERROR: parse() failed");\r
132         }\r
133         logln(text + " parsed into " + result2.getTime());\r
134 \r
135         result3 = def.parse(text, pos01);\r
136         if (result3 == null) {\r
137             errln("ERROR: parse() failed for " + text);\r
138         }\r
139         logln(text + " parsed into " + result3.getTime());\r
140 \r
141 \r
142         // ======= Test getters and setters\r
143 \r
144         logln("Testing getters and setters");\r
145 \r
146         final Locale[] locales = DateFormat.getAvailableLocales();\r
147         long count = locales.length;\r
148         logln("Got " + count + " locales" );\r
149 \r
150         // Ticket#6280\r
151         // These locales should be included in the result\r
152         final Locale[] samples = {\r
153                 new Locale("zh", "CN"),\r
154                 new Locale("zh", "TW"),\r
155                 new Locale("zh", "HK"),\r
156                 new Locale("sr", "RS"),\r
157         };\r
158         boolean[] available = new boolean[samples.length];\r
159         for(int i = 0; i < count; i++) {\r
160             String name;\r
161             name = locales[i].getDisplayName();\r
162             logln(name);\r
163             for (int j = 0; j < samples.length; j++) {\r
164                 if (locales[i].equals(samples[j])) {\r
165                     available[j] = true;\r
166                     break;\r
167                 }\r
168             }\r
169         }\r
170         for (int i = 0; i < available.length; i++) {\r
171             if (!available[i]) {\r
172                 errln("ERROR: missing Locale: " + samples[i]);\r
173             }\r
174         }\r
175 \r
176         fr.setLenient(it.isLenient());\r
177         if(fr.isLenient() != it.isLenient()) {\r
178             errln("ERROR: setLenient() failed");\r
179         }\r
180 \r
181         final Calendar cal = def.getCalendar();\r
182         Calendar newCal = (Calendar) cal.clone();\r
183         de.setCalendar(newCal);\r
184         it.setCalendar(newCal);\r
185         if( ! de.getCalendar().equals(it.getCalendar())) {\r
186             errln("ERROR: set Calendar() failed");\r
187         }\r
188 \r
189         final NumberFormat nf = def.getNumberFormat();\r
190         NumberFormat newNf = (NumberFormat) nf.clone();\r
191         de.setNumberFormat(newNf);\r
192         it.setNumberFormat(newNf);\r
193         if( ! de.getNumberFormat().equals(it.getNumberFormat())) {\r
194             errln("ERROR: set NumberFormat() failed");\r
195         }\r
196 \r
197         final TimeZone tz = def.getTimeZone();\r
198         TimeZone newTz = (TimeZone) tz.clone();\r
199         de.setTimeZone(newTz);\r
200         it.setTimeZone(newTz);\r
201         if( ! de.getTimeZone().equals(it.getTimeZone())) {\r
202             errln("ERROR: set TimeZone() failed");\r
203         }\r
204 \r
205         // ======= Test getStaticClassID()\r
206 \r
207 //        logln("Testing instanceof()");\r
208 \r
209 //        try {\r
210 //            DateFormat test = new SimpleDateFormat();\r
211 \r
212 //            if (! (test instanceof SimpleDateFormat)) {\r
213 //                errln("ERROR: instanceof failed");\r
214 //            }\r
215 //        }\r
216 //        catch (Exception e) {\r
217 //            errln("ERROR: Couldn't create a DateFormat");\r
218 //        }\r
219     }\r
220 }\r