~ubuntu-branches/ubuntu/vivid/icu4j-4.4/vivid

« back to all changes in this revision

Viewing changes to main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatRegistrationTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Niels Thykier
  • Date: 2011-08-02 15:50:33 UTC
  • Revision ID: james.westby@ubuntu.com-20110802155033-itjzsl21y2lqdonn
Tags: upstream-4.4.2
ImportĀ upstreamĀ versionĀ 4.4.2

Show diffs side-by-side

added added

removed removed

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