001: /*
002: *******************************************************************************
003: * Copyright (C) 2003-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.format;
008:
009: import com.ibm.icu.text.*;
010: import com.ibm.icu.text.NumberFormat.*;
011: import com.ibm.icu.util.ULocale;
012:
013: import java.util.Arrays;
014:
015: public class NumberFormatRegistrationTest extends
016: com.ibm.icu.dev.test.TestFmwk {
017:
018: public static void main(String[] args) {
019: new NumberFormatRegistrationTest().run(args);
020: }
021:
022: public void TestRegistration() {
023: final ULocale SRC_LOC = ULocale.FRANCE;
024: final ULocale SWAP_LOC = ULocale.US;
025:
026: class TestFactory extends SimpleNumberFormatFactory {
027: NumberFormat currencyStyle;
028:
029: TestFactory() {
030: this (SRC_LOC, SWAP_LOC);
031: }
032:
033: TestFactory(ULocale srcLoc, ULocale swapLoc) {
034: super (srcLoc);
035: currencyStyle = NumberFormat
036: .getIntegerInstance(swapLoc);
037: }
038:
039: public NumberFormat createFormat(ULocale loc, int formatType) {
040: if (formatType == FORMAT_CURRENCY) {
041: return currencyStyle;
042: }
043: return null;
044: }
045: }
046:
047: {
048: // coverage before registration
049:
050: try {
051: NumberFormat.unregister(null);
052: errln("did not throw exception on null unregister");
053: } catch (Exception e) {
054: logln("PASS: null unregister failed as expected");
055: }
056:
057: try {
058: NumberFormat.registerFactory(null);
059: errln("did not throw exception on null register");
060: } catch (Exception e) {
061: logln("PASS: null register failed as expected");
062: }
063:
064: try {
065: // if no NF has been registered yet, shim is null, so this silently
066: // returns false. if, on the other hand, a NF has been registered,
067: // this will try to cast the argument to a Factory, and throw
068: // an exception.
069: if (NumberFormat.unregister("")) {
070: errln("unregister of empty string key succeeded");
071: }
072: } catch (Exception e) {
073: }
074: }
075:
076: ULocale fu_FU = new ULocale("fu_FU");
077: NumberFormat f0 = NumberFormat.getIntegerInstance(SWAP_LOC);
078: NumberFormat f1 = NumberFormat.getIntegerInstance(SRC_LOC);
079: NumberFormat f2 = NumberFormat.getCurrencyInstance(SRC_LOC);
080: Object key = NumberFormat.registerFactory(new TestFactory());
081: Object key2 = NumberFormat.registerFactory(new TestFactory(
082: fu_FU, ULocale.GERMANY));
083: if (!Arrays.asList(NumberFormat.getAvailableULocales())
084: .contains(fu_FU)) {
085: errln("did not list fu_FU");
086: }
087: NumberFormat f3 = NumberFormat.getCurrencyInstance(SRC_LOC);
088: NumberFormat f4 = NumberFormat.getIntegerInstance(SRC_LOC);
089: NumberFormat.unregister(key); // restore for other tests
090: NumberFormat f5 = NumberFormat.getCurrencyInstance(SRC_LOC);
091:
092: NumberFormat.unregister(key2);
093:
094: float n = 1234.567f;
095: logln("f0 swap int: " + f0.format(n));
096: logln("f1 src int: " + f1.format(n));
097: logln("f2 src cur: " + f2.format(n));
098: logln("f3 reg cur: " + f3.format(n));
099: logln("f4 reg int: " + f4.format(n));
100: logln("f5 unreg cur: " + f5.format(n));
101:
102: if (!f3.format(n).equals(f0.format(n))) {
103: errln("registered service did not match");
104: }
105: if (!f4.format(n).equals(f1.format(n))) {
106: errln("registered service did not inherit");
107: }
108: if (!f5.format(n).equals(f2.format(n))) {
109: errln("unregistered service did not match original");
110: }
111:
112: // coverage
113: NumberFormat f6 = NumberFormat.getNumberInstance(fu_FU);
114: }
115: }
|