| /* |
| ********************************************************************** |
| * Copyright (c) 2002-2003, International Business Machines |
| * Corporation and others. All Rights Reserved. |
| ********************************************************************** |
| * Author: Alan Liu |
| * Created: December 18 2002 |
| * Since: ICU 2.4 |
| ********************************************************************** |
| */ |
| package com.ibm.icu.dev.test.util; |
| import com.ibm.icu.dev.test.TestFmwk; |
| import com.ibm.icu.util.*; |
| import java.util.Locale; |
| |
| /** |
| * @test |
| * @summary General test of Currency |
| */ |
| public class CurrencyTest extends TestFmwk { |
| |
| public static void main(String[] args) throws Exception { |
| new CurrencyTest().run(args); |
| } |
| |
| /** |
| * Test of basic API. |
| */ |
| public void TestAPI() { |
| Currency usd = Currency.getInstance("USD"); |
| /*int hash = */usd.hashCode(); |
| Currency jpy = Currency.getInstance("JPY"); |
| if (usd.equals(jpy)) { |
| errln("FAIL: USD == JPY"); |
| } |
| if (usd.equals("abc")) { |
| errln("FAIL: USD == (String)"); |
| } |
| if (usd.equals(null)) { |
| errln("FAIL: USD == (null)"); |
| } |
| if (!usd.equals(usd)) { |
| errln("FAIL: USD != USD"); |
| } |
| |
| Locale[] avail = Currency.getAvailableLocales(); |
| if(avail==null){ |
| errln("FAIL: getAvailableLocales returned null"); |
| } |
| |
| try { |
| usd.getName(Locale.US, 5, new boolean[1]); |
| errln("expected getName with invalid type parameter to throw exception"); |
| } |
| catch (Exception e) { |
| } |
| } |
| |
| /** |
| * Test registration. |
| */ |
| public void TestRegistration() { |
| final Currency jpy = Currency.getInstance("JPY"); |
| final Currency usd = Currency.getInstance(Locale.US); |
| |
| try { |
| Currency.unregister(null); // should fail, coverage |
| errln("expected unregister of null to throw exception"); |
| } |
| catch (Exception e) { |
| } |
| |
| if (Currency.unregister("")) { // coverage |
| errln("unregister before register erroneously succeeded"); |
| } |
| |
| Locale fu_FU = new Locale("fu", "FU", ""); |
| |
| Object key1 = Currency.registerInstance(jpy, Locale.US); |
| Object key2 = Currency.registerInstance(jpy, fu_FU); |
| |
| Currency nus = Currency.getInstance(Locale.US); |
| if (!nus.equals(jpy)) { |
| errln("expected " + jpy + " but got: " + nus); |
| } |
| |
| // converage, make sure default factory works |
| Currency nus1 = Currency.getInstance(Locale.JAPAN); |
| if (!nus1.equals(jpy)) { |
| errln("expected " + jpy + " but got: " + nus1); |
| } |
| |
| Locale[] locales = Currency.getAvailableLocales(); |
| boolean found = false; |
| for (int i = 0; i < locales.length; ++i) { |
| if (locales[i].equals(fu_FU)) { |
| found = true; |
| break; |
| } |
| } |
| if (!found) { |
| errln("did not find locale" + fu_FU + " in currency locales"); |
| } |
| |
| if (!Currency.unregister(key1)) { |
| errln("unable to unregister currency using key1"); |
| } |
| if (!Currency.unregister(key2)) { |
| errln("unable to unregister currency using key2"); |
| } |
| |
| Currency nus2 = Currency.getInstance(Locale.US); |
| if (!nus2.equals(usd)) { |
| errln("expected " + usd + " but got: " + nus2); |
| } |
| |
| locales = Currency.getAvailableLocales(); |
| found = false; |
| for (int i = 0; i < locales.length; ++i) { |
| if (locales[i].equals(fu_FU)) { |
| found = true; |
| break; |
| } |
| } |
| if (found) { |
| errln("found locale" + fu_FU + " in currency locales after unregister"); |
| } |
| } |
| |
| /** |
| * Test names. |
| */ |
| public void TestNames() { |
| // Do a basic check of getName() |
| // USD { "US$", "US Dollar" } // 04/04/1792- |
| Locale en = Locale.ENGLISH; |
| boolean[] isChoiceFormat = new boolean[1]; |
| Currency usd = Currency.getInstance("USD"); |
| // Warning: HARD-CODED LOCALE DATA in this test. If it fails, CHECK |
| // THE LOCALE DATA before diving into the code. |
| assertEquals("USD.getName(SYMBOL_NAME)", |
| "US$", |
| usd.getName(en, Currency.SYMBOL_NAME, isChoiceFormat)); |
| assertEquals("USD.getName(LONG_NAME)", |
| "US Dollar", |
| usd.getName(en, Currency.LONG_NAME, isChoiceFormat)); |
| |
| // TODO add more tests later |
| } |
| } |