]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestSimpleDateFormatAPI.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / format / IntlTestSimpleDateFormatAPI.java
1 /*****************************************************************************************
2  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
3  * (C) Copyright IBM Corp. 1996-2012 - All Rights Reserved
4  *
5  *   The original version of this source code and documentation is copyrighted and
6  * owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
7  * provided under terms of a License Agreement between Taligent and Sun. This
8  * technology is protected by multiple US and International patents. This notice and
9  * attribution to Taligent may not be removed.
10  *   Taligent is a registered trademark of Taligent, Inc.
11  **/
12
13 /** 
14  * Port From:   JDK 1.4b1 : java.text.Format.IntlTestSimpleDateFormatAPI
15  * Source File: java/text/format/IntlTestSimpleDateFormatAPI.java
16  **/
17  
18 package com.ibm.icu.dev.test.format;
19
20 import java.text.FieldPosition;
21 import java.text.Format;
22 import java.text.ParseException;
23 import java.text.ParsePosition;
24 import java.util.Date;
25 import java.util.Locale;
26
27 import com.ibm.icu.text.DateFormatSymbols;
28 import com.ibm.icu.text.SimpleDateFormat;
29
30 /**
31 * @test 1.4 98/03/06
32 * @summary test International Simple Date Format API
33 */
34 public class IntlTestSimpleDateFormatAPI extends com.ibm.icu.dev.test.TestFmwk
35 {
36     public static void main(String[] args) throws Exception {
37         new IntlTestSimpleDateFormatAPI().run(args);
38     }
39
40     // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
41     public void TestAPI()
42     {
43         logln("SimpleDateFormat API test---"); logln("");
44
45         Locale.setDefault(Locale.ENGLISH);
46
47         // ======= Test constructors
48
49         logln("Testing SimpleDateFormat constructors");
50
51         SimpleDateFormat def = new SimpleDateFormat();
52
53         final String pattern = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
54         SimpleDateFormat pat = new SimpleDateFormat(pattern);
55
56         SimpleDateFormat pat_fr = new SimpleDateFormat(pattern, Locale.FRENCH);
57
58         DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRENCH);
59
60         SimpleDateFormat cust1 = new SimpleDateFormat(pattern, symbols);
61
62         // ======= Test clone() and equality
63
64         logln("Testing clone(), assignment and equality operators");
65
66         Format clone = (Format) def.clone();
67         if( ! clone.equals(def) ) {
68             errln("ERROR: Format clone or equals failed");
69         }
70
71         // ======= Test various format() methods
72
73         logln("Testing various format() methods");
74
75         Date d = new Date((long)837039928046.0);
76
77         StringBuffer res1 = new StringBuffer();
78         StringBuffer res2 = new StringBuffer();
79         FieldPosition pos1 = new FieldPosition(0);
80         FieldPosition pos2 = new FieldPosition(0);
81
82         res1 = def.format(d, res1, pos1);
83         logln( "" + d.getTime() + " formatted to " + res1);
84
85         res2 = cust1.format(d, res2, pos2);
86         logln("" + d.getTime() + " formatted to " + res2);
87
88         // ======= Test parse()
89
90         logln("Testing parse()");
91
92         String text = new String("02/03/76, 2:50 AM, CST");
93         Date result1 = new Date();
94         Date result2 = new Date();
95         ParsePosition pos= new ParsePosition(0);
96         result1 = def.parse(text, pos);
97         logln(text + " parsed into " + result1);
98
99         try {
100             result2 = def.parse(text);
101         }
102         catch (ParseException e) {
103             errln("ERROR: parse() failed");
104         }
105         logln(text + " parsed into " + result2);
106
107         // ======= Test getters and setters
108
109         logln("Testing getters and setters");
110
111         final DateFormatSymbols syms = pat.getDateFormatSymbols();
112         def.setDateFormatSymbols(syms);
113         pat_fr.setDateFormatSymbols(syms);
114         if( ! pat.getDateFormatSymbols().equals(def.getDateFormatSymbols()) ) {
115             errln("ERROR: set DateFormatSymbols() failed");
116         }
117
118         /*
119         DateFormatSymbols has not the method getTwoDigitStartDate();
120         //Date startDate = null; //The variable is never used
121         try {
122 //            startDate = pat.getTwoDigitStartDate();
123         }
124         catch (Exception e) {
125             errln("ERROR: getTwoDigitStartDate() failed");
126         }
127
128         try {
129 //            pat_fr.setTwoDigitStartDate(startDate);
130         }
131         catch (Exception e) {
132             errln("ERROR: setTwoDigitStartDate() failed");
133         }*/
134
135         // ======= Test applyPattern()
136
137         logln("Testing applyPattern()");
138
139         String p1 = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
140         logln("Applying pattern " + p1);
141         pat.applyPattern(p1);
142
143         String s2 = pat.toPattern();
144         logln("Extracted pattern is " + s2);
145         if( ! s2.equals(p1) ) {
146             errln("ERROR: toPattern() result did not match pattern applied");
147         }
148
149         logln("Applying pattern " + p1);
150         pat.applyLocalizedPattern(p1);
151         String s3 = pat.toLocalizedPattern();
152         logln("Extracted pattern is " + s3);
153         if( ! s3.equals(p1) ) {
154             errln("ERROR: toLocalizedPattern() result did not match pattern applied");
155         }
156         
157         // ======= Test for Ticket 5684 (Parsing patterns with 'Y' and 'e'
158         logln("Testing parse()");
159
160         String p2 = new String("YYYY'W'wwe");
161         logln("Applying pattern " + p2);
162         pat.applyPattern(p2);
163         Date dt = pat.parse("2007W014", new ParsePosition(0));
164         if (dt == null) {
165             errln("ERROR: Parsing failed using 'Y' and 'e'");
166         }
167
168
169         // ======= Test getStaticClassID()
170
171 //        logln("Testing instanceof");
172
173 //        try {
174 //            DateFormat test = new SimpleDateFormat();
175
176 //            if (! (test instanceof SimpleDateFormat)) {
177 //                errln("ERROR: instanceof failed");
178 //            }
179 //        }
180 //        catch (Exception e) {
181 //            errln("ERROR: Couldn't create a SimpleDateFormat");
182 //        }
183     }
184     
185     // Jitterbug 4451, for coverage
186     public void TestCoverage(){
187         class StubDateFormat extends SimpleDateFormat{
188             /**
189              * For serialization
190              */
191             private static final long serialVersionUID = 8460897119491427934L;
192
193             public void run(){
194                 if (!zeroPaddingNumber(12, 4, 6).equals("0012")){
195                     errln("SimpleDateFormat(zeroPaddingNumber(long , int , int )");
196                 }
197             }
198         }
199         new StubDateFormat().run();
200     }
201 }