]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatRegistrationTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / format / NumberFormatRegistrationTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2003-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.format;\r
8 \r
9 import java.util.Arrays;\r
10 \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
14 \r
15 public class NumberFormatRegistrationTest extends com.ibm.icu.dev.test.TestFmwk {\r
16 \r
17     public static void main(String[] args) {\r
18         new NumberFormatRegistrationTest().run(args);\r
19     }\r
20 \r
21     public void TestRegistration() {\r
22         final ULocale SRC_LOC = ULocale.FRANCE;\r
23         final ULocale SWAP_LOC = ULocale.US;\r
24 \r
25         class TestFactory extends SimpleNumberFormatFactory {\r
26             NumberFormat currencyStyle;\r
27 \r
28             TestFactory() {\r
29                 this(SRC_LOC, SWAP_LOC);\r
30             }\r
31 \r
32             TestFactory(ULocale srcLoc, ULocale swapLoc) {\r
33                 super(srcLoc);\r
34                 currencyStyle = NumberFormat.getIntegerInstance(swapLoc);\r
35             }\r
36 \r
37             public NumberFormat createFormat(ULocale loc, int formatType) {\r
38                 if (formatType == FORMAT_CURRENCY) {\r
39                     return currencyStyle;\r
40                 }\r
41                 return null;\r
42             }\r
43         }\r
44 \r
45         {\r
46             // coverage before registration\r
47 \r
48             try {\r
49                 NumberFormat.unregister(null);\r
50                 errln("did not throw exception on null unregister");\r
51             }\r
52             catch (Exception e) {\r
53                 logln("PASS: null unregister failed as expected");\r
54             }\r
55 \r
56             try {\r
57                 NumberFormat.registerFactory(null);\r
58                 errln("did not throw exception on null register");\r
59             }\r
60             catch (Exception e) {\r
61                 logln("PASS: null register failed as expected");\r
62             }\r
63 \r
64             try {\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
68                 // an exception.\r
69                 if (NumberFormat.unregister("")) {\r
70                     errln("unregister of empty string key succeeded");\r
71                 }\r
72             }\r
73             catch (Exception e) {\r
74             }\r
75         }\r
76 \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
85         }\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
90 \r
91         NumberFormat.unregister(key2);\r
92 \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
100 \r
101         if (!f3.format(n).equals(f0.format(n))) {\r
102             errln("registered service did not match");\r
103         }\r
104         if (!f4.format(n).equals(f1.format(n))) {\r
105             errln("registered service did not inherit");\r
106         }\r
107         if (!f5.format(n).equals(f2.format(n))) {\r
108             errln("unregistered service did not match original");\r
109         }\r
110 \r
111         // coverage\r
112         NumberFormat f6 = NumberFormat.getNumberInstance(fu_FU);\r
113         if (f6 == null) {\r
114             errln("getNumberInstance(fu_FU) returned null");\r
115         }\r
116     }\r
117 }\r