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