1/*
2 *******************************************************************************
3 * Copyright (C) 2008-2014, International Business Machines Corporation and    *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 */
7package com.ibm.icu.dev.test.localespi;
8
9import java.text.DecimalFormatSymbols;
10import java.util.Currency;
11import java.util.Locale;
12
13import com.ibm.icu.dev.test.TestFmwk;
14import com.ibm.icu.util.ULocale;
15
16public class DecimalFormatSymbolsTest extends TestFmwk {
17    public static void main(String[] args) throws Exception {
18        new DecimalFormatSymbolsTest().run(args);
19    }
20
21    /*
22     * Check if getInstance returns the ICU implementation.
23     */
24    public void TestGetInstance() {
25        for (Locale loc : DecimalFormatSymbols.getAvailableLocales()) {
26            if (TestUtil.isProblematicIBMLocale(loc)) {
27                logln("Skipped " + loc);
28                continue;
29            }
30
31            DecimalFormatSymbols decfs = DecimalFormatSymbols.getInstance(loc);
32
33            boolean isIcuImpl = (decfs instanceof com.ibm.icu.impl.jdkadapter.DecimalFormatSymbolsICU);
34
35            if (TestUtil.isICUExtendedLocale(loc)) {
36                if (!isIcuImpl) {
37                    errln("FAIL: getInstance returned JDK DecimalFormatSymbols for locale " + loc);
38                }
39            } else {
40                if (isIcuImpl) {
41                    logln("INFO: getInstance returned ICU DecimalFormatSymbols for locale " + loc);
42                }
43                Locale iculoc = TestUtil.toICUExtendedLocale(loc);
44                DecimalFormatSymbols decfsIcu = DecimalFormatSymbols.getInstance(iculoc);
45                if (isIcuImpl) {
46                    if (!decfs.equals(decfsIcu)) {
47                        errln("FAIL: getInstance returned ICU DecimalFormatSymbols for locale " + loc
48                                + ", but different from the one for locale " + iculoc);
49                    }
50                } else {
51                    if (!(decfsIcu instanceof com.ibm.icu.impl.jdkadapter.DecimalFormatSymbolsICU)) {
52                        errln("FAIL: getInstance returned JDK DecimalFormatSymbols for locale " + iculoc);
53                    }
54                }
55            }
56        }
57    }
58
59    /*
60     * Testing the contents of DecimalFormatSymbols between ICU instance and its
61     * equivalent created via the Locale SPI framework.
62     */
63    public void TestICUEquivalent() {
64        Locale[] TEST_LOCALES = {
65                new Locale("en", "US"),
66                new Locale("pt", "BR"),
67                new Locale("ko", "KR"),
68        };
69
70        for (Locale loc : TEST_LOCALES) {
71            Locale iculoc = TestUtil.toICUExtendedLocale(loc);
72            DecimalFormatSymbols jdkDecfs = DecimalFormatSymbols.getInstance(iculoc);
73            com.ibm.icu.text.DecimalFormatSymbols icuDecfs = com.ibm.icu.text.DecimalFormatSymbols.getInstance(loc);
74
75            Currency jdkCur = jdkDecfs.getCurrency();
76            com.ibm.icu.util.Currency icuCur = icuDecfs.getCurrency();
77            if ((jdkCur != null && icuCur == null)
78                    || (jdkCur == null && icuCur != null)
79                    || !jdkCur.getCurrencyCode().equals(icuCur.getCurrencyCode())) {
80                errln("FAIL: Different results returned by getCurrency for locale " + loc);
81            }
82
83            checkEquivalence(jdkDecfs.getCurrencySymbol(), icuDecfs.getCurrencySymbol(), loc, "getCurrencySymbol");
84            checkEquivalence(jdkDecfs.getDecimalSeparator(), icuDecfs.getDecimalSeparator(), loc, "getDecimalSeparator");
85            checkEquivalence(jdkDecfs.getDigit(), icuDecfs.getDigit(), loc, "getDigit");
86            checkEquivalence(jdkDecfs.getExponentSeparator(), icuDecfs.getExponentSeparator(), loc, "getExponentSeparator");
87            checkEquivalence(jdkDecfs.getGroupingSeparator(), icuDecfs.getGroupingSeparator(), loc, "getGroupingSeparator");
88            checkEquivalence(jdkDecfs.getInfinity(), icuDecfs.getInfinity(), loc, "getInfinity");
89            checkEquivalence(jdkDecfs.getInternationalCurrencySymbol(), icuDecfs.getInternationalCurrencySymbol(), loc, "getInternationalCurrencySymbol");
90            checkEquivalence(jdkDecfs.getMinusSign(), icuDecfs.getMinusSign(), loc, "getMinusSign");
91            checkEquivalence(jdkDecfs.getMonetaryDecimalSeparator(), icuDecfs.getMonetaryDecimalSeparator(), loc, "getMonetaryDecimalSeparator");
92            checkEquivalence(jdkDecfs.getNaN(), icuDecfs.getNaN(), loc, "getNaN");
93            checkEquivalence(jdkDecfs.getPatternSeparator(), icuDecfs.getPatternSeparator(), loc, "getPatternSeparator");
94            checkEquivalence(jdkDecfs.getPercent(), icuDecfs.getPercent(), loc, "getPercent");
95            checkEquivalence(jdkDecfs.getPerMill(), icuDecfs.getPerMill(), loc, "getPerMill");
96            checkEquivalence(jdkDecfs.getZeroDigit(), icuDecfs.getZeroDigit(), loc, "getZeroDigit");
97        }
98    }
99
100    private void checkEquivalence(Object jo, Object io, Locale loc, String method) {
101        if (!jo.equals(io)) {
102            errln("FAIL: Different results returned by " + method + " for locale "
103                    + loc + " (jdk=" + jo + ",icu=" + io + ")");
104        }
105    }
106
107    /*
108     * Testing setters
109     */
110    public void TestSetSymbols() {
111        // ICU's JDK DecimalFormatSymbols implementation for de_DE locale
112        DecimalFormatSymbols decfs = DecimalFormatSymbols.getInstance(TestUtil.toICUExtendedLocale(new Locale("de", "DE")));
113
114        // en_US is supported by JDK, so this is the JDK's own DecimalFormatSymbols
115        Locale loc = new Locale("en", "US");
116        DecimalFormatSymbols decfsEnUS = DecimalFormatSymbols.getInstance(loc);
117
118        // Copying over all symbols
119        decfs.setCurrency(decfsEnUS.getCurrency());
120
121        decfs.setCurrencySymbol(decfsEnUS.getCurrencySymbol());
122        decfs.setDecimalSeparator(decfsEnUS.getDecimalSeparator());
123        decfs.setDigit(decfsEnUS.getDigit());
124        decfs.setExponentSeparator(decfsEnUS.getExponentSeparator());
125        decfs.setGroupingSeparator(decfsEnUS.getGroupingSeparator());
126        decfs.setInfinity(decfsEnUS.getInfinity());
127        decfs.setInternationalCurrencySymbol(decfsEnUS.getInternationalCurrencySymbol());
128        decfs.setMinusSign(decfsEnUS.getMinusSign());
129        decfs.setMonetaryDecimalSeparator(decfsEnUS.getMonetaryDecimalSeparator());
130        decfs.setNaN(decfsEnUS.getNaN());
131        decfs.setPatternSeparator(decfsEnUS.getPatternSeparator());
132        decfs.setPercent(decfsEnUS.getPercent());
133        decfs.setPerMill(decfsEnUS.getPerMill());
134        decfs.setZeroDigit(decfsEnUS.getZeroDigit());
135
136        // Check
137        Currency cur = decfs.getCurrency();
138        Currency curEnUS = decfsEnUS.getCurrency();
139        if ((cur != null && curEnUS == null)
140                || (cur == null && curEnUS != null)
141                || !cur.equals(curEnUS)) {
142            errln("FAIL: Different results returned by getCurrency");
143        }
144
145        checkEquivalence(decfs.getCurrencySymbol(), decfsEnUS.getCurrencySymbol(), loc, "getCurrencySymbol");
146        checkEquivalence(decfs.getDecimalSeparator(), decfsEnUS.getDecimalSeparator(), loc, "getDecimalSeparator");
147        checkEquivalence(decfs.getDigit(), decfsEnUS.getDigit(), loc, "getDigit");
148        checkEquivalence(decfs.getExponentSeparator(), decfsEnUS.getExponentSeparator(), loc, "getExponentSeparator");
149        checkEquivalence(decfs.getGroupingSeparator(), decfsEnUS.getGroupingSeparator(), loc, "getGroupingSeparator");
150        checkEquivalence(decfs.getInfinity(), decfsEnUS.getInfinity(), loc, "getInfinity");
151        checkEquivalence(decfs.getInternationalCurrencySymbol(), decfsEnUS.getInternationalCurrencySymbol(), loc, "getInternationalCurrencySymbol");
152        checkEquivalence(decfs.getMinusSign(), decfsEnUS.getMinusSign(), loc, "getMinusSign");
153        checkEquivalence(decfs.getMonetaryDecimalSeparator(), decfsEnUS.getMonetaryDecimalSeparator(), loc, "getMonetaryDecimalSeparator");
154        checkEquivalence(decfs.getNaN(), decfsEnUS.getNaN(), loc, "getNaN");
155        checkEquivalence(decfs.getPatternSeparator(), decfsEnUS.getPatternSeparator(), loc, "getPatternSeparator");
156        checkEquivalence(decfs.getPercent(), decfsEnUS.getPercent(), loc, "getPercent");
157        checkEquivalence(decfs.getPerMill(), decfsEnUS.getPerMill(), loc, "getPerMill");
158        checkEquivalence(decfs.getZeroDigit(), decfsEnUS.getZeroDigit(), loc, "getZeroDigit");
159    }
160
161    public void TestKeywords() {
162        // ICU provider variant is appended
163        ULocale uloc0 = new ULocale("en_US_" + TestUtil.ICU_VARIANT + "@numbers=Arab;currency=EUR");
164        Locale loc = uloc0.toLocale();
165        // On Java 7+, locale extension is preserved
166        ULocale uloc = ULocale.forLocale(loc);
167        String nsType = uloc.getKeywordValue("numbers");
168        if (nsType == null) {
169            // Java 6 - skip this test
170            return;
171        }
172
173        DecimalFormatSymbols jdkDecfs = DecimalFormatSymbols.getInstance(loc);
174        com.ibm.icu.text.DecimalFormatSymbols icuDecfs = com.ibm.icu.text.DecimalFormatSymbols.getInstance(uloc);
175        // Check digit 0
176        if (jdkDecfs.getDigit() != icuDecfs.getDigit()) {
177            errln("FAIL: Different decimal digit - via JDK: " + jdkDecfs.getDigit() + ", with ICU: " + icuDecfs.getDigit());
178        }
179
180        String jdkCurrencyCode = jdkDecfs.getCurrency().getCurrencyCode();
181        String icuCurrencyCode = icuDecfs.getCurrency().getCurrencyCode();
182        if (!jdkCurrencyCode.equals(icuCurrencyCode)) {
183            errln("FAIL: Different currency code - via JDK: " + jdkCurrencyCode + ", with ICU: " + icuCurrencyCode);
184        }
185    }
186}
187