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