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