001: /*****************************************************************************************
002: *
003: * Copyright (C) 1996-2006, International Business Machines
004: * Corporation and others. All Rights Reserved.
005: **/
006:
007: /**
008: * Port From: JDK 1.4b1 : java.text.Format.IntlTestNumberFormatAPI
009: * Source File: java/text/format/IntlTestNumberFormatAPI.java
010: **/package com.ibm.icu.dev.test.format;
011:
012: import com.ibm.icu.text.*;
013: import com.ibm.icu.util.ULocale;
014:
015: import java.util.Locale;
016: import java.math.BigDecimal;
017: import java.math.BigInteger;
018: import java.text.FieldPosition;
019: import java.text.ParsePosition;
020: import java.text.ParseException;
021:
022: public class IntlTestNumberFormatAPI extends
023: com.ibm.icu.dev.test.TestFmwk {
024: public static void main(String[] args) throws Exception {
025: new IntlTestNumberFormatAPI().run(args);
026: }
027:
028: // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
029: public void TestAPI() {
030: logln("NumberFormat API test---");
031: logln("");
032: Locale.setDefault(Locale.ENGLISH);
033:
034: // ======= Test constructors
035:
036: logln("Testing NumberFormat constructors");
037:
038: NumberFormat def = NumberFormat.getInstance();
039:
040: NumberFormat fr = NumberFormat.getInstance(Locale.FRENCH);
041:
042: NumberFormat cur = NumberFormat.getCurrencyInstance();
043:
044: NumberFormat cur_fr = NumberFormat
045: .getCurrencyInstance(Locale.FRENCH);
046:
047: NumberFormat per = NumberFormat.getPercentInstance();
048:
049: NumberFormat per_fr = NumberFormat
050: .getPercentInstance(Locale.FRENCH);
051:
052: NumberFormat integer = NumberFormat.getIntegerInstance();
053:
054: NumberFormat int_fr = NumberFormat
055: .getIntegerInstance(Locale.FRENCH);
056:
057: //Fix "The variable is never used" compilation warnings
058: logln("Currency : " + cur.format(1234.5));
059: logln("Percent : " + per.format(1234.5));
060: logln("Integer : " + integer.format(1234.5));
061: logln("Int_fr : " + int_fr.format(1234.5));
062:
063: // ======= Test equality
064:
065: logln("Testing equality operator");
066:
067: if (per_fr.equals(cur_fr)) {
068: errln("ERROR: == failed");
069: }
070:
071: // ======= Test various format() methods
072:
073: logln("Testing various format() methods");
074:
075: // final double d = -10456.0037; // this appears as -10456.003700000001 on NT
076: // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
077: final double d = -10456.00370000000000; // this works!
078: final long l = 100000000;
079:
080: String res1 = new String();
081: String res2 = new String();
082: StringBuffer res3 = new StringBuffer();
083: StringBuffer res4 = new StringBuffer();
084: StringBuffer res5 = new StringBuffer();
085: StringBuffer res6 = new StringBuffer();
086: FieldPosition pos1 = new FieldPosition(0);
087: FieldPosition pos2 = new FieldPosition(0);
088: FieldPosition pos3 = new FieldPosition(0);
089: FieldPosition pos4 = new FieldPosition(0);
090:
091: res1 = cur_fr.format(d);
092: logln("" + d + " formatted to " + res1);
093:
094: res2 = cur_fr.format(l);
095: logln("" + l + " formatted to " + res2);
096:
097: res3 = cur_fr.format(d, res3, pos1);
098: logln("" + d + " formatted to " + res3);
099:
100: res4 = cur_fr.format(l, res4, pos2);
101: logln("" + l + " formatted to " + res4);
102:
103: res5 = cur_fr.format(d, res5, pos3);
104: logln("" + d + " formatted to " + res5);
105:
106: res6 = cur_fr.format(l, res6, pos4);
107: logln("" + l + " formatted to " + res6);
108:
109: // ======= Test parse()
110:
111: logln("Testing parse()");
112:
113: // String text = new String("-10,456.0037");
114: String text = new String("-10456,0037");
115: ParsePosition pos = new ParsePosition(0);
116: ParsePosition pos01 = new ParsePosition(0);
117: double d1 = ((Number) fr.parseObject(text, pos)).doubleValue();
118: if (d1 != d) {
119: errln("ERROR: Roundtrip failed (via parse()) for " + text);
120: }
121: logln(text + " parsed into " + d1);
122:
123: double d2 = fr.parse(text, pos01).doubleValue();
124: if (d2 != d) {
125: errln("ERROR: Roundtrip failed (via parse()) for " + text);
126: }
127: logln(text + " parsed into " + d2);
128:
129: double d3 = 0;
130: try {
131: d3 = fr.parse(text).doubleValue();
132: } catch (ParseException e) {
133: errln("ERROR: parse() failed");
134: }
135: if (d3 != d) {
136: errln("ERROR: Roundtrip failed (via parse()) for " + text);
137: }
138: logln(text + " parsed into " + d3);
139:
140: // ======= Test getters and setters
141:
142: logln("Testing getters and setters");
143:
144: final Locale[] locales = NumberFormat.getAvailableLocales();
145: long count = locales.length;
146: logln("Got " + count + " locales");
147: for (int i = 0; i < count; i++) {
148: String name;
149: name = locales[i].getDisplayName();
150: logln(name);
151: }
152:
153: fr.setParseIntegerOnly(def.isParseIntegerOnly());
154: if (fr.isParseIntegerOnly() != def.isParseIntegerOnly()) {
155: errln("ERROR: setParseIntegerOnly() failed");
156: }
157:
158: fr.setGroupingUsed(def.isGroupingUsed());
159: if (fr.isGroupingUsed() != def.isGroupingUsed()) {
160: errln("ERROR: setGroupingUsed() failed");
161: }
162:
163: fr.setMaximumIntegerDigits(def.getMaximumIntegerDigits());
164: if (fr.getMaximumIntegerDigits() != def
165: .getMaximumIntegerDigits()) {
166: errln("ERROR: setMaximumIntegerDigits() failed");
167: }
168:
169: fr.setMinimumIntegerDigits(def.getMinimumIntegerDigits());
170: if (fr.getMinimumIntegerDigits() != def
171: .getMinimumIntegerDigits()) {
172: errln("ERROR: setMinimumIntegerDigits() failed");
173: }
174:
175: fr.setMaximumFractionDigits(def.getMaximumFractionDigits());
176: if (fr.getMaximumFractionDigits() != def
177: .getMaximumFractionDigits()) {
178: errln("ERROR: setMaximumFractionDigits() failed");
179: }
180:
181: fr.setMinimumFractionDigits(def.getMinimumFractionDigits());
182: if (fr.getMinimumFractionDigits() != def
183: .getMinimumFractionDigits()) {
184: errln("ERROR: setMinimumFractionDigits() failed");
185: }
186:
187: // ======= Test getStaticClassID()
188:
189: // logln("Testing instanceof()");
190:
191: // try {
192: // NumberFormat test = new DecimalFormat();
193:
194: // if (! (test instanceof DecimalFormat)) {
195: // errln("ERROR: instanceof failed");
196: // }
197: // }
198: // catch (Exception e) {
199: // errln("ERROR: Couldn't create a DecimalFormat");
200: // }
201: }
202:
203: // Jitterbug 4451, for coverage
204: public void TestCoverage() {
205: class StubNumberFormat extends NumberFormat {
206: public void run() {
207: String p = NumberFormat.getPattern(ULocale.getDefault()
208: .toLocale(), 0);
209: if (!p.equals(NumberFormat.getPattern(ULocale
210: .getDefault(), 0))) {
211: errln("NumberFormat.getPattern(Locale, int) should delegate to (ULocale,)");
212: }
213: }
214:
215: public StringBuffer format(double number,
216: StringBuffer toAppendTo, FieldPosition pos) {
217: return null;
218: }
219:
220: public StringBuffer format(long number,
221: StringBuffer toAppendTo, FieldPosition pos) {
222: return null;
223: }
224:
225: public StringBuffer format(BigInteger number,
226: StringBuffer toAppendTo, FieldPosition pos) {
227: return null;
228: }
229:
230: public StringBuffer format(BigDecimal number,
231: StringBuffer toAppendTo, FieldPosition pos) {
232: return null;
233: }
234:
235: public StringBuffer format(
236: com.ibm.icu.math.BigDecimal number,
237: StringBuffer toAppendTo, FieldPosition pos) {
238: return null;
239: }
240:
241: public Number parse(String text, ParsePosition parsePosition) {
242: return null;
243: }
244: }
245: new StubNumberFormat().run();
246: }
247: }
|