2 *******************************************************************************
\r
3 * Copyright (C) 2003-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.format;
\r
9 import java.util.Arrays;
\r
11 import com.ibm.icu.text.NumberFormat;
\r
12 import com.ibm.icu.text.NumberFormat.SimpleNumberFormatFactory;
\r
13 import com.ibm.icu.util.ULocale;
\r
15 public class NumberFormatRegistrationTest extends com.ibm.icu.dev.test.TestFmwk {
\r
17 public static void main(String[] args) {
\r
18 new NumberFormatRegistrationTest().run(args);
\r
21 public void TestRegistration() {
\r
22 final ULocale SRC_LOC = ULocale.FRANCE;
\r
23 final ULocale SWAP_LOC = ULocale.US;
\r
25 class TestFactory extends SimpleNumberFormatFactory {
\r
26 NumberFormat currencyStyle;
\r
29 this(SRC_LOC, SWAP_LOC);
\r
32 TestFactory(ULocale srcLoc, ULocale swapLoc) {
\r
34 currencyStyle = NumberFormat.getIntegerInstance(swapLoc);
\r
37 public NumberFormat createFormat(ULocale loc, int formatType) {
\r
38 if (formatType == FORMAT_CURRENCY) {
\r
39 return currencyStyle;
\r
46 // coverage before registration
\r
49 NumberFormat.unregister(null);
\r
50 errln("did not throw exception on null unregister");
\r
52 catch (Exception e) {
\r
53 logln("PASS: null unregister failed as expected");
\r
57 NumberFormat.registerFactory(null);
\r
58 errln("did not throw exception on null register");
\r
60 catch (Exception e) {
\r
61 logln("PASS: null register failed as expected");
\r
65 // if no NF has been registered yet, shim is null, so this silently
\r
66 // returns false. if, on the other hand, a NF has been registered,
\r
67 // this will try to cast the argument to a Factory, and throw
\r
69 if (NumberFormat.unregister("")) {
\r
70 errln("unregister of empty string key succeeded");
\r
73 catch (Exception e) {
\r
77 ULocale fu_FU = new ULocale("fu_FU");
\r
78 NumberFormat f0 = NumberFormat.getIntegerInstance(SWAP_LOC);
\r
79 NumberFormat f1 = NumberFormat.getIntegerInstance(SRC_LOC);
\r
80 NumberFormat f2 = NumberFormat.getCurrencyInstance(SRC_LOC);
\r
81 Object key = NumberFormat.registerFactory(new TestFactory());
\r
82 Object key2 = NumberFormat.registerFactory(new TestFactory(fu_FU, ULocale.GERMANY));
\r
83 if (!Arrays.asList(NumberFormat.getAvailableULocales()).contains(fu_FU)) {
\r
84 errln("did not list fu_FU");
\r
86 NumberFormat f3 = NumberFormat.getCurrencyInstance(SRC_LOC);
\r
87 NumberFormat f4 = NumberFormat.getIntegerInstance(SRC_LOC);
\r
88 NumberFormat.unregister(key); // restore for other tests
\r
89 NumberFormat f5 = NumberFormat.getCurrencyInstance(SRC_LOC);
\r
91 NumberFormat.unregister(key2);
\r
93 float n = 1234.567f;
\r
94 logln("f0 swap int: " + f0.format(n));
\r
95 logln("f1 src int: " + f1.format(n));
\r
96 logln("f2 src cur: " + f2.format(n));
\r
97 logln("f3 reg cur: " + f3.format(n));
\r
98 logln("f4 reg int: " + f4.format(n));
\r
99 logln("f5 unreg cur: " + f5.format(n));
\r
101 if (!f3.format(n).equals(f0.format(n))) {
\r
102 errln("registered service did not match");
\r
104 if (!f4.format(n).equals(f1.format(n))) {
\r
105 errln("registered service did not inherit");
\r
107 if (!f5.format(n).equals(f2.format(n))) {
\r
108 errln("unregistered service did not match original");
\r
112 NumberFormat f6 = NumberFormat.getNumberInstance(fu_FU);
\r
114 errln("getNumberInstance(fu_FU) returned null");
\r