2 *******************************************************************************
\r
3 * Copyright (C) 2001-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
9 * Port From: ICU4C v1.8.1 : format : IntlTestDateFormatAPI
\r
10 * Source File: $ICU4CRoot/source/test/intltest/dtfmapts.cpp
\r
13 package com.ibm.icu.dev.test.format;
\r
15 import java.text.FieldPosition;
\r
16 import java.text.ParsePosition;
\r
17 import java.util.Date;
\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
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
29 public class IntlTestDateFormatAPIC extends com.ibm.icu.dev.test.TestFmwk {
\r
31 public static void main(String[] args) throws Exception {
\r
32 new IntlTestDateFormatAPIC().run(args);
\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
40 public void TestNameHiding() {
\r
42 // N.B.: This test passes if it COMPILES, since it's a test of
\r
43 // compile-time name hiding.
\r
45 Date dateObj = new Date(0);
\r
46 Number numObj = new Double(3.1415926535897932384626433832795);
\r
47 StringBuffer strBuffer = new StringBuffer("");
\r
49 FieldPosition fpos = new FieldPosition(0);
\r
50 ParsePosition ppos = new ParsePosition(0);
\r
52 // DateFormat calling Format API
\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
60 errln("FAIL: Can't create DateFormat");
\r
64 // SimpleDateFormat calling Format & DateFormat API
\r
66 logln("SimpleDateFormat");
\r
67 SimpleDateFormat sdf = new SimpleDateFormat();
\r
69 str = sdf.format(dateObj);
\r
70 strBuffer = sdf.format(dateObj, strBuffer, fpos);
\r
72 strBuffer = sdf.format(new Date(0), strBuffer, fpos);
\r
73 str = sdf.format(new Date(0));
\r
76 sdf.parse(str, ppos);
\r
77 } catch (java.text.ParseException pe) {
\r
78 System.out.println(pe);
\r
82 // NumberFormat calling Format API
\r
84 logln("NumberFormat");
\r
85 NumberFormat fmt = NumberFormat.getInstance();
\r
87 str = fmt.format(numObj);
\r
88 strBuffer = fmt.format(numObj, strBuffer, fpos);
\r
90 errln("FAIL: Can't create NumberFormat");
\r
94 // DecimalFormat calling Format & NumberFormat API
\r
96 logln("DecimalFormat");
\r
97 DecimalFormat fmt = new DecimalFormat();
\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
108 obj = fmt.parse(str);
\r
110 errln("FAIL: The format object could not parse the string : "+str);
\r
112 } catch (java.text.ParseException pe) {
\r
113 System.out.println(pe);
\r
117 //ICU4J have not the classes ChoiceFormat and MessageFormat
\r
119 // ChoiceFormat calling Format & NumberFormat API
\r
121 logln("ChoiceFormat");
\r
122 ChoiceFormat fmt = new ChoiceFormat("0#foo|1#foos|2#foos");
\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
133 obj = fmt.parse(str);
\r
134 } catch (java.text.ParseException pe) {
\r
135 System.out.println(pe);
\r
140 // MessageFormat calling Format API
\r
142 logln("MessageFormat");
\r
143 MessageFormat fmt = new MessageFormat("");
\r
145 // We use dateObj, which MessageFormat should reject.
\r
146 // We're testing name hiding, not the format method.
\r
148 str = fmt.format(dateObj);
\r
149 } catch (Exception e) {
\r
150 //e.printStackTrace();
\r
153 strBuffer = fmt.format(dateObj, strBuffer, fpos);
\r
154 } catch (Exception e) {
\r
155 //e.printStackTrace();
\r