2 *******************************************************************************
\r
3 * Copyright (C) 2001-2004, 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 com.ibm.icu.text.*;
\r
16 import java.util.Date;
\r
17 import java.text.FieldPosition;
\r
18 import java.text.ParsePosition;
\r
21 * This is an API test, not a unit test. It doesn't test very many cases, and doesn't
\r
22 * try to test the full functionality. It just calls each function in the class and
\r
23 * verifies that it works on a basic level.
\r
25 public class IntlTestDateFormatAPIC extends com.ibm.icu.dev.test.TestFmwk {
\r
27 public static void main(String[] args) throws Exception {
\r
28 new IntlTestDateFormatAPIC().run(args);
\r
32 * Test hiding of parse() and format() APIs in the Format hierarchy.
\r
33 * We test the entire hierarchy, even though this test is located in
\r
34 * the DateFormat API test.
\r
36 public void TestNameHiding() {
\r
38 // N.B.: This test passes if it COMPILES, since it's a test of
\r
39 // compile-time name hiding.
\r
41 Date dateObj = new Date(0);
\r
42 Number numObj = new Double(3.1415926535897932384626433832795);
\r
43 StringBuffer strBuffer = new StringBuffer("");
\r
45 FieldPosition fpos = new FieldPosition(0);
\r
46 ParsePosition ppos = new ParsePosition(0);
\r
48 // DateFormat calling Format API
\r
50 logln("DateFormat");
\r
51 DateFormat dateFmt = DateFormat.getInstance();
\r
52 if (dateFmt != null) {
\r
53 str = dateFmt.format(dateObj);
\r
54 strBuffer = dateFmt.format(dateObj, strBuffer, fpos);
\r
56 errln("FAIL: Can't create DateFormat");
\r
60 // SimpleDateFormat calling Format & DateFormat API
\r
62 logln("SimpleDateFormat");
\r
63 SimpleDateFormat sdf = new SimpleDateFormat();
\r
65 str = sdf.format(dateObj);
\r
66 strBuffer = sdf.format(dateObj, strBuffer, fpos);
\r
68 strBuffer = sdf.format(new Date(0), strBuffer, fpos);
\r
69 str = sdf.format(new Date(0));
\r
72 sdf.parse(str, ppos);
\r
73 } catch (java.text.ParseException pe) {
\r
74 System.out.println(pe);
\r
78 // NumberFormat calling Format API
\r
80 logln("NumberFormat");
\r
81 NumberFormat fmt = NumberFormat.getInstance();
\r
83 str = fmt.format(numObj);
\r
84 strBuffer = fmt.format(numObj, strBuffer, fpos);
\r
86 errln("FAIL: Can't create NumberFormat");
\r
90 // DecimalFormat calling Format & NumberFormat API
\r
92 logln("DecimalFormat");
\r
93 DecimalFormat fmt = new DecimalFormat();
\r
95 str = fmt.format(numObj);
\r
96 strBuffer = fmt.format(numObj, strBuffer, fpos);
\r
98 str = fmt.format(2.71828);
\r
99 str = fmt.format(1234567);
\r
100 strBuffer = fmt.format(1.41421, strBuffer, fpos);
\r
101 strBuffer = fmt.format(9876543, strBuffer, fpos);
\r
102 Number obj = fmt.parse(str, ppos);
\r
104 obj = fmt.parse(str);
\r
106 errln("FAIL: The format object could not parse the string : "+str);
\r
108 } catch (java.text.ParseException pe) {
\r
109 System.out.println(pe);
\r
113 //ICU4J have not the classes ChoiceFormat and MessageFormat
\r
115 // ChoiceFormat calling Format & NumberFormat API
\r
117 logln("ChoiceFormat");
\r
118 ChoiceFormat fmt = new ChoiceFormat("0#foo|1#foos|2#foos");
\r
120 str = fmt.format(numObj);
\r
121 strBuffer = fmt.format(numObj, strBuffer, fpos);
\r
122 // NumberFormat API
\r
123 str = fmt.format(2.71828);
\r
124 str = fmt.format(1234567);
\r
125 strBuffer = fmt.format(1.41421, strBuffer, fpos);
\r
126 strBuffer = fmt.format(9876543, strBuffer, fpos);
\r
127 Number obj = fmt.parse(str, ppos);
\r
129 obj = fmt.parse(str);
\r
130 } catch (java.text.ParseException pe) {
\r
131 System.out.println(pe);
\r
136 // MessageFormat calling Format API
\r
138 logln("MessageFormat");
\r
139 MessageFormat fmt = new MessageFormat("");
\r
141 // We use dateObj, which MessageFormat should reject.
\r
142 // We're testing name hiding, not the format method.
\r
144 str = fmt.format(dateObj);
\r
145 } catch (Exception e) {
\r
146 //e.printStackTrace();
\r
149 strBuffer = fmt.format(dateObj, strBuffer, fpos);
\r
150 } catch (Exception e) {
\r
151 //e.printStackTrace();
\r