001: /*
002: **********************************************************************
003: * Copyright (c) 2002-2005, International Business Machines
004: * Corporation and others. All Rights Reserved.
005: **********************************************************************
006: * Author: Alan Liu
007: * Created: December 18 2002
008: * Since: ICU 2.4
009: **********************************************************************
010: */
011: package com.ibm.icu.dev.test.util;
012:
013: import com.ibm.icu.dev.test.TestFmwk;
014: import com.ibm.icu.util.*;
015: import java.util.Locale;
016:
017: /**
018: * @test
019: * @summary General test of Currency
020: */
021: public class CurrencyTest extends TestFmwk {
022:
023: public static void main(String[] args) throws Exception {
024: new CurrencyTest().run(args);
025: }
026:
027: /**
028: * Test of basic API.
029: */
030: public void TestAPI() {
031: Currency usd = Currency.getInstance("USD");
032: /*int hash = */usd.hashCode();
033: Currency jpy = Currency.getInstance("JPY");
034: if (usd.equals(jpy)) {
035: errln("FAIL: USD == JPY");
036: }
037: if (usd.equals("abc")) {
038: errln("FAIL: USD == (String)");
039: }
040: if (usd.equals(null)) {
041: errln("FAIL: USD == (null)");
042: }
043: if (!usd.equals(usd)) {
044: errln("FAIL: USD != USD");
045: }
046:
047: Locale[] avail = Currency.getAvailableLocales();
048: if (avail == null) {
049: errln("FAIL: getAvailableLocales returned null");
050: }
051:
052: try {
053: usd.getName(ULocale.US, 5, new boolean[1]);
054: errln("expected getName with invalid type parameter to throw exception");
055: } catch (Exception e) {
056: logln("PASS: getName failed as expected");
057: }
058: }
059:
060: /**
061: * Test registration.
062: */
063: public void TestRegistration() {
064: final Currency jpy = Currency.getInstance("JPY");
065: final Currency usd = Currency.getInstance(Locale.US);
066:
067: try {
068: Currency.unregister(null); // should fail, coverage
069: errln("expected unregister of null to throw exception");
070: } catch (Exception e) {
071: logln("PASS: unregister of null failed as expected");
072: }
073:
074: if (Currency.unregister("")) { // coverage
075: errln("unregister before register erroneously succeeded");
076: }
077:
078: ULocale fu_FU = new ULocale("fu_FU");
079:
080: Object key1 = Currency.registerInstance(jpy, ULocale.US);
081: Object key2 = Currency.registerInstance(jpy, fu_FU);
082:
083: Currency nus = Currency.getInstance(Locale.US);
084: if (!nus.equals(jpy)) {
085: errln("expected " + jpy + " but got: " + nus);
086: }
087:
088: // converage, make sure default factory works
089: Currency nus1 = Currency.getInstance(Locale.JAPAN);
090: if (!nus1.equals(jpy)) {
091: errln("expected " + jpy + " but got: " + nus1);
092: }
093:
094: ULocale[] locales = Currency.getAvailableULocales();
095: boolean found = false;
096: for (int i = 0; i < locales.length; ++i) {
097: if (locales[i].equals(fu_FU)) {
098: found = true;
099: break;
100: }
101: }
102: if (!found) {
103: errln("did not find locale" + fu_FU
104: + " in currency locales");
105: }
106:
107: if (!Currency.unregister(key1)) {
108: errln("unable to unregister currency using key1");
109: }
110: if (!Currency.unregister(key2)) {
111: errln("unable to unregister currency using key2");
112: }
113:
114: Currency nus2 = Currency.getInstance(Locale.US);
115: if (!nus2.equals(usd)) {
116: errln("expected " + usd + " but got: " + nus2);
117: }
118:
119: locales = Currency.getAvailableULocales();
120: found = false;
121: for (int i = 0; i < locales.length; ++i) {
122: if (locales[i].equals(fu_FU)) {
123: found = true;
124: break;
125: }
126: }
127: if (found) {
128: errln("found locale" + fu_FU
129: + " in currency locales after unregister");
130: }
131: }
132:
133: /**
134: * Test names.
135: */
136: public void TestNames() {
137: // Do a basic check of getName()
138: // USD { "US$", "US Dollar" } // 04/04/1792-
139: ULocale en = ULocale.ENGLISH;
140: boolean[] isChoiceFormat = new boolean[1];
141: Currency usd = Currency.getInstance("USD");
142: // Warning: HARD-CODED LOCALE DATA in this test. If it fails, CHECK
143: // THE LOCALE DATA before diving into the code.
144: if (!noData()) {
145: assertEquals("USD.getName(SYMBOL_NAME)", "US$", usd
146: .getName(en, Currency.SYMBOL_NAME, isChoiceFormat));
147: assertEquals("USD.getName(LONG_NAME)", "US Dollar", usd
148: .getName(en, Currency.LONG_NAME, isChoiceFormat));
149: }
150: // TODO add more tests later
151: }
152:
153: public void TestCoverage() {
154: Currency usd = Currency.getInstance("USD");
155: if (!noData()) {
156: assertEquals("USD.getSymbol()", "$", usd.getSymbol());
157: }
158: assertEquals("USD.getLocale()", ULocale.ROOT, usd
159: .getLocale(null));
160: }
161:
162: public void TestCurrencyKeyword() {
163: ULocale locale = new ULocale(
164: "th_TH@collation=traditional;currency=QQQ");
165: Currency currency = Currency.getInstance(locale);
166: String result = currency.getCurrencyCode();
167: if (!"QQQ".equals(result)) {
168: errln("got unexpected currency: " + result);
169: }
170: }
171: }
|