]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestDateFormatAPIC.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / format / IntlTestDateFormatAPIC.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2001-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 /** 
9  * Port From:   ICU4C v1.8.1 : format : IntlTestDateFormatAPI
10  * Source File: $ICU4CRoot/source/test/intltest/dtfmapts.cpp
11  **/
12
13 package com.ibm.icu.dev.test.format;
14
15 import java.text.FieldPosition;
16 import java.text.ParsePosition;
17 import java.util.Date;
18
19 import com.ibm.icu.text.DateFormat;
20 import com.ibm.icu.text.DecimalFormat;
21 import com.ibm.icu.text.NumberFormat;
22 import com.ibm.icu.text.SimpleDateFormat;
23
24 /*
25  * This is an API test, not a unit test.  It doesn't test very many cases, and doesn't
26  * try to test the full functionality.  It just calls each function in the class and
27  * verifies that it works on a basic level.
28  */
29 public class IntlTestDateFormatAPIC extends com.ibm.icu.dev.test.TestFmwk {
30     
31     public static void main(String[] args)  throws Exception {
32         new IntlTestDateFormatAPIC().run(args);
33     }
34     
35     /**
36      * Test hiding of parse() and format() APIs in the Format hierarchy.
37      * We test the entire hierarchy, even though this test is located in
38      * the DateFormat API test.
39      */
40     public void TestNameHiding() {
41     
42         // N.B.: This test passes if it COMPILES, since it's a test of
43         // compile-time name hiding.
44     
45         Date dateObj = new Date(0);
46         Number numObj = new Double(3.1415926535897932384626433832795);
47         StringBuffer strBuffer = new StringBuffer("");
48         String str;
49         FieldPosition fpos = new FieldPosition(0);
50         ParsePosition ppos = new ParsePosition(0);
51     
52         // DateFormat calling Format API
53         {
54             logln("DateFormat");
55             DateFormat dateFmt = DateFormat.getInstance();
56             if (dateFmt != null) {
57                 str = dateFmt.format(dateObj);
58                 strBuffer = dateFmt.format(dateObj, strBuffer, fpos);
59             } else {
60                 errln("FAIL: Can't create DateFormat");
61             }
62         }
63     
64         // SimpleDateFormat calling Format & DateFormat API
65         {
66             logln("SimpleDateFormat");
67             SimpleDateFormat sdf = new SimpleDateFormat();
68             // Format API
69             str = sdf.format(dateObj);
70             strBuffer = sdf.format(dateObj, strBuffer, fpos);
71             // DateFormat API
72             strBuffer = sdf.format(new Date(0), strBuffer, fpos);
73             str = sdf.format(new Date(0));
74             try {
75                 sdf.parse(str);
76                 sdf.parse(str, ppos);
77             } catch (java.text.ParseException pe) {
78                 System.out.println(pe);
79             }
80         }
81     
82         // NumberFormat calling Format API
83         {
84             logln("NumberFormat");
85             NumberFormat fmt = NumberFormat.getInstance();
86             if (fmt != null) {
87                 str = fmt.format(numObj);
88                 strBuffer = fmt.format(numObj, strBuffer, fpos);
89             } else {
90                 errln("FAIL: Can't create NumberFormat");
91             }
92         }
93     
94         // DecimalFormat calling Format & NumberFormat API
95         {
96             logln("DecimalFormat");
97             DecimalFormat fmt = new DecimalFormat();
98             // Format API
99             str = fmt.format(numObj);
100             strBuffer = fmt.format(numObj, strBuffer, fpos);
101             // NumberFormat API
102             str = fmt.format(2.71828);
103             str = fmt.format(1234567);
104             strBuffer = fmt.format(1.41421, strBuffer, fpos);
105             strBuffer = fmt.format(9876543, strBuffer, fpos);
106             Number obj = fmt.parse(str, ppos);
107             try {
108                 obj = fmt.parse(str);
109                 if(obj==null){
110                     errln("FAIL: The format object could not parse the string : "+str);
111                 }
112             } catch (java.text.ParseException pe) {
113                 System.out.println(pe);
114             }
115         }
116         
117         //ICU4J have not the classes ChoiceFormat and MessageFormat
118         /*
119         // ChoiceFormat calling Format & NumberFormat API
120         {
121             logln("ChoiceFormat");
122             ChoiceFormat fmt = new ChoiceFormat("0#foo|1#foos|2#foos");
123             // Format API
124             str = fmt.format(numObj);
125             strBuffer = fmt.format(numObj, strBuffer, fpos);
126             // NumberFormat API
127             str = fmt.format(2.71828);
128             str = fmt.format(1234567);
129             strBuffer = fmt.format(1.41421, strBuffer, fpos);
130             strBuffer = fmt.format(9876543, strBuffer, fpos);
131             Number obj = fmt.parse(str, ppos);
132             try {
133                 obj = fmt.parse(str);
134             } catch (java.text.ParseException pe) {
135                 System.out.println(pe);
136             }
137         }
138     
139         
140         // MessageFormat calling Format API
141         {
142             logln("MessageFormat");
143             MessageFormat fmt = new MessageFormat("");
144             // Format API
145             // We use dateObj, which MessageFormat should reject.
146             // We're testing name hiding, not the format method.
147             try {
148                 str = fmt.format(dateObj);
149             } catch (Exception e) {
150                 //e.printStackTrace();
151             }
152             try {
153                 strBuffer = fmt.format(dateObj, strBuffer, fpos);
154             } catch (Exception e) {
155                 //e.printStackTrace();
156             }
157         }
158         */
159     }
160 }