001: /*
002: *******************************************************************************
003: * Copyright (C) 2001-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: /**
009: * Port From: ICU4C v1.8.1 : format : IntlTestDateFormatAPI
010: * Source File: $ICU4CRoot/source/test/intltest/dtfmapts.cpp
011: **/package com.ibm.icu.dev.test.format;
012:
013: import com.ibm.icu.text.*;
014: import java.util.Date;
015: import java.text.FieldPosition;
016: import java.text.ParsePosition;
017:
018: /*
019: * This is an API test, not a unit test. It doesn't test very many cases, and doesn't
020: * try to test the full functionality. It just calls each function in the class and
021: * verifies that it works on a basic level.
022: */
023: public class IntlTestDateFormatAPIC extends
024: com.ibm.icu.dev.test.TestFmwk {
025:
026: public static void main(String[] args) throws Exception {
027: new IntlTestDateFormatAPIC().run(args);
028: }
029:
030: /**
031: * Test hiding of parse() and format() APIs in the Format hierarchy.
032: * We test the entire hierarchy, even though this test is located in
033: * the DateFormat API test.
034: */
035: public void TestNameHiding() {
036:
037: // N.B.: This test passes if it COMPILES, since it's a test of
038: // compile-time name hiding.
039:
040: Date dateObj = new Date(0);
041: Number numObj = new Double(3.1415926535897932384626433832795);
042: StringBuffer strBuffer = new StringBuffer("");
043: String str;
044: FieldPosition fpos = new FieldPosition(0);
045: ParsePosition ppos = new ParsePosition(0);
046:
047: // DateFormat calling Format API
048: {
049: logln("DateFormat");
050: DateFormat dateFmt = DateFormat.getInstance();
051: if (dateFmt != null) {
052: str = dateFmt.format(dateObj);
053: strBuffer = dateFmt.format(dateObj, strBuffer, fpos);
054: } else {
055: errln("FAIL: Can't create DateFormat");
056: }
057: }
058:
059: // SimpleDateFormat calling Format & DateFormat API
060: {
061: logln("SimpleDateFormat");
062: SimpleDateFormat sdf = new SimpleDateFormat();
063: // Format API
064: str = sdf.format(dateObj);
065: strBuffer = sdf.format(dateObj, strBuffer, fpos);
066: // DateFormat API
067: strBuffer = sdf.format(new Date(0), strBuffer, fpos);
068: str = sdf.format(new Date(0));
069: try {
070: sdf.parse(str);
071: sdf.parse(str, ppos);
072: } catch (java.text.ParseException pe) {
073: System.out.println(pe);
074: }
075: }
076:
077: // NumberFormat calling Format API
078: {
079: logln("NumberFormat");
080: NumberFormat fmt = NumberFormat.getInstance();
081: if (fmt != null) {
082: str = fmt.format(numObj);
083: strBuffer = fmt.format(numObj, strBuffer, fpos);
084: } else {
085: errln("FAIL: Can't create NumberFormat");
086: }
087: }
088:
089: // DecimalFormat calling Format & NumberFormat API
090: {
091: logln("DecimalFormat");
092: DecimalFormat fmt = new DecimalFormat();
093: // Format API
094: str = fmt.format(numObj);
095: strBuffer = fmt.format(numObj, strBuffer, fpos);
096: // NumberFormat API
097: str = fmt.format(2.71828);
098: str = fmt.format(1234567);
099: strBuffer = fmt.format(1.41421, strBuffer, fpos);
100: strBuffer = fmt.format(9876543, strBuffer, fpos);
101: Number obj = fmt.parse(str, ppos);
102: try {
103: obj = fmt.parse(str);
104: if (obj == null) {
105: errln("FAIL: The format object could not parse the string : "
106: + str);
107: }
108: } catch (java.text.ParseException pe) {
109: System.out.println(pe);
110: }
111: }
112:
113: //ICU4J have not the classes ChoiceFormat and MessageFormat
114: /*
115: // ChoiceFormat calling Format & NumberFormat API
116: {
117: logln("ChoiceFormat");
118: ChoiceFormat fmt = new ChoiceFormat("0#foo|1#foos|2#foos");
119: // Format API
120: str = fmt.format(numObj);
121: strBuffer = fmt.format(numObj, strBuffer, fpos);
122: // NumberFormat API
123: str = fmt.format(2.71828);
124: str = fmt.format(1234567);
125: strBuffer = fmt.format(1.41421, strBuffer, fpos);
126: strBuffer = fmt.format(9876543, strBuffer, fpos);
127: Number obj = fmt.parse(str, ppos);
128: try {
129: obj = fmt.parse(str);
130: } catch (java.text.ParseException pe) {
131: System.out.println(pe);
132: }
133: }
134:
135:
136: // MessageFormat calling Format API
137: {
138: logln("MessageFormat");
139: MessageFormat fmt = new MessageFormat("");
140: // Format API
141: // We use dateObj, which MessageFormat should reject.
142: // We're testing name hiding, not the format method.
143: try {
144: str = fmt.format(dateObj);
145: } catch (Exception e) {
146: //e.printStackTrace();
147: }
148: try {
149: strBuffer = fmt.format(dateObj, strBuffer, fpos);
150: } catch (Exception e) {
151: //e.printStackTrace();
152: }
153: }
154: */
155: }
156: }
|