From 0469f7b5caa17be89ee58850f96356aa5f6d0825 Mon Sep 17 00:00:00 2001 From: Alan Liu Date: Thu, 15 Jan 2004 22:18:50 +0000 Subject: [PATCH] ICU-2202 add basic test X-SVN-Rev: 14333 --- .../com/ibm/icu/dev/test/util/TestAll.java | 7 +- .../ibm/icu/dev/test/util/ULocaleTest.java | 152 ++++++++++++++++++ 2 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 icu4j/src/com/ibm/icu/dev/test/util/ULocaleTest.java diff --git a/icu4j/src/com/ibm/icu/dev/test/util/TestAll.java b/icu4j/src/com/ibm/icu/dev/test/util/TestAll.java index 5fcf9fc51b..09e81b9f87 100644 --- a/icu4j/src/com/ibm/icu/dev/test/util/TestAll.java +++ b/icu4j/src/com/ibm/icu/dev/test/util/TestAll.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/util/TestAll.java,v $ - * $Date: 2003/11/14 19:15:14 $ - * $Revision: 1.5 $ + * $Date: 2004/01/15 22:18:50 $ + * $Revision: 1.6 $ * ******************************************************************************* */ @@ -32,7 +32,8 @@ public class TestAll extends TestGroup { "CurrencyTest", "UtilityTest", "TrieTest", - "LocaleDataTest" + "LocaleDataTest", + "ULocaleTest" }, "Test miscellaneous public utilities"); } diff --git a/icu4j/src/com/ibm/icu/dev/test/util/ULocaleTest.java b/icu4j/src/com/ibm/icu/dev/test/util/ULocaleTest.java new file mode 100644 index 0000000000..f9e8a457e5 --- /dev/null +++ b/icu4j/src/com/ibm/icu/dev/test/util/ULocaleTest.java @@ -0,0 +1,152 @@ +/* +********************************************************************** +* Copyright (c) 2004, International Business Machines +* Corporation and others. All Rights Reserved. +********************************************************************** +* Author: Alan Liu +* Created: January 14 2004 +* Since: ICU 2.8 +********************************************************************** +*/ +package com.ibm.icu.dev.test.util; + +import com.ibm.icu.util.Currency; +import com.ibm.icu.util.ULocale; +import com.ibm.icu.util.VersionInfo; +import com.ibm.icu.dev.test.TestFmwk; +import java.util.Locale; + +public class ULocaleTest extends TestFmwk { + + public static void main(String[] args) throws Exception { + new ULocaleTest().run(args); + } + + public void TestCurrency() { + + String DATA[] = { + // The tests are data driven. Each line represents + // a test event. Order is important. + + // Possible tests (each test is specified by 4 strings; + // sometimes the later strings will be null): + + // 1. To test the result for a given locale: + // requested locale, exp. currency, exp. valid loc., exp. actual loc. + + // 2. To register a currency against a locale: + // "r", locale to register, currency to register, (ignored) + + // 3. To unregister the last currency registered: + // "u", (ignored), (ignored), (ignored) + + "en_US", "USD", "en_US", "", + + "en_US_CALIFORNIA", "USD", "en_US", "", + + "r", "en_US_CALIFORNIA", "USD", null, + + "en_US_CALIFORNIA", "USD", "en_US_CALIFORNIA", "en_US_CALIFORNIA", + + "u", null, null, null, + + "en_US_CALIFORNIA", "USD", "en_US", "", + }; + + Object regKeys[] = new Object[20]; // adjust len as needed + int regKeyCount = 0; + + try { + for (int i=0; i " + curname + ")"); + } else if (DATA[i].equals("u")) { + Currency.unregister(regKeys[--regKeyCount]); + regKeys[regKeyCount] = null; + logln("(unregistering)"); + } else { + Locale loc = getLocale(DATA[i]); + String curname = DATA[i+1]; + Locale expValid = getLocale(DATA[i+2]); + Locale expActual = getLocale(DATA[i+3]); + Currency cur = Currency.getInstance(loc); + + boolean ok = true; + + if (!cur.getCurrencyCode().equals(curname)) { + errln("FAIL: Currency.getInstance(" + DATA[i] + + ").getCurrencyCode() => " + cur.getCurrencyCode() + + ", exp. " + + curname); + ok = false; + } + + ULocale valid = cur.getLocale(ULocale.VALID_LOCALE); + if (doValidTest()) { + if (!valid.toLocale().equals(expValid)) { + errln("FAIL: Currency.getInstance(" + DATA[i] + + ").getLocale(VALID) => " + valid + ", exp. " + + expValid); + ok = false; + } + } + + ULocale actual = cur.getLocale(ULocale.ACTUAL_LOCALE); + if (!actual.toLocale().equals(expActual)) { + errln("FAIL: Currency.getInstance(" + DATA[i] + + ").getLocale(ACTUAL) => " + actual + ", exp. " + + expActual); + ok = false; + } + + if (ok) { + logln("Ok: Currency.getInstance(" + DATA[i] + + ") => " + cur.getCurrencyCode() + ", valid=" + + valid + ", actual=" + actual); + } + } + } + } finally { + for (int i=0; i=0) { + language = ID.substring(0, i); + int j = ID.indexOf('_', i+1); + if (j<0) { + country = ID.substring(i+1); + } else { + country = ID.substring(i+1, j); + variant = ID.substring(j+1); + } + } + return new Locale(language, country, variant); + } + + // Time bomb code to temporarily modify the behavior of this test + // to account for the fact that the valid locale is unavailable in + // ICU 2.8. + + static boolean IS_AFTER_2_8 = + VersionInfo.ICU_VERSION.compareTo(VersionInfo.getInstance(2,8,0,0)) > 0; + + static boolean doValidTest() { return IS_AFTER_2_8; } +}