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