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