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