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