2 *******************************************************************************
3 * Copyright (C) 2008, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.localespi;
9 import java.text.DecimalFormatSymbols;
10 import java.util.Currency;
11 import java.util.Locale;
13 import com.ibm.icu.dev.test.TestFmwk;
15 public class DecimalFormatSymbolsTest extends TestFmwk {
16 public static void main(String[] args) throws Exception {
17 new DecimalFormatSymbolsTest().run(args);
21 * Check if getInstance returns the ICU implementation.
23 public void TestGetInstance() {
24 for (Locale loc : DecimalFormatSymbols.getAvailableLocales()) {
25 if (TestUtil.isProblematicIBMLocale(loc)) {
26 logln("Skipped " + loc);
30 DecimalFormatSymbols decfs = DecimalFormatSymbols.getInstance(loc);
32 boolean isIcuImpl = (decfs instanceof com.ibm.icu.impl.jdkadapter.DecimalFormatSymbolsICU);
34 if (TestUtil.isICUExtendedLocale(loc)) {
36 errln("FAIL: getInstance returned JDK DecimalFormatSymbols for locale " + loc);
40 logln("INFO: getInstance returned ICU DecimalFormatSymbols for locale " + loc);
42 Locale iculoc = TestUtil.toICUExtendedLocale(loc);
43 DecimalFormatSymbols decfsIcu = DecimalFormatSymbols.getInstance(iculoc);
45 if (!decfs.equals(decfsIcu)) {
46 errln("FAIL: getInstance returned ICU DecimalFormatSymbols for locale " + loc
47 + ", but different from the one for locale " + iculoc);
50 if (!(decfsIcu instanceof com.ibm.icu.impl.jdkadapter.DecimalFormatSymbolsICU)) {
51 errln("FAIL: getInstance returned JDK DecimalFormatSymbols for locale " + iculoc);
59 * Testing the contents of DecimalFormatSymbols between ICU instance and its
60 * equivalent created via the Locale SPI framework.
62 public void TestICUEquivalent() {
63 Locale[] TEST_LOCALES = {
64 new Locale("en", "US"),
65 new Locale("pt", "BR"),
66 new Locale("ko", "KR"),
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);
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);
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");
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 + ")");
109 public void TestSetSymbols() {
110 // ICU's JDK DecimalFormatSymbols implementation for de_DE locale
111 DecimalFormatSymbols decfs = DecimalFormatSymbols.getInstance(new Locale("de", "DE", "ICU"));
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);
117 // Copying over all symbols
118 decfs.setCurrency(decfsEnUS.getCurrency());
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());
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");
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");