1 /* 2 * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /* 24 * 25 */ 26 27 import java.text.*; 28 import java.util.*; 29 import sun.util.locale.provider.*; 30 import sun.util.resources.*; 31 32 import com.foo.FooNumberFormat; 33 34 public class NumberFormatProviderTest extends ProviderTest { 35 36 com.foo.NumberFormatProviderImpl nfp = new com.foo.NumberFormatProviderImpl(); 37 List<Locale> availloc = Arrays.asList(NumberFormat.getAvailableLocales()); 38 List<Locale> providerloc = Arrays.asList(nfp.getAvailableLocales()); 39 List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales()); 40 List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getNumberFormatProvider().getAvailableLocales()); 41 42 public static void main(String[] s) { 43 new NumberFormatProviderTest(); 44 } 45 46 NumberFormatProviderTest() { 47 availableLocalesTest(); 48 objectValidityTest(); 49 messageFormatTest(); 50 } 51 52 void availableLocalesTest() { 53 Set<Locale> localesFromAPI = new HashSet<Locale>(availloc); 54 Set<Locale> localesExpected = new HashSet<Locale>(jreimplloc); 55 localesExpected.remove(Locale.ROOT); 56 localesExpected.addAll(providerloc); 57 if (localesFromAPI.equals(localesExpected)) { 58 System.out.println("availableLocalesTest passed."); 59 } else { 60 throw new RuntimeException("availableLocalesTest failed"); 61 } 62 } 63 64 void objectValidityTest() { 65 66 for (Locale target: availloc) { 67 // pure JRE implementation 68 ResourceBundle rb = LocaleProviderAdapter.forJRE().getLocaleData().getNumberFormatData(target); 69 boolean jreSupportsLocale = jreimplloc.contains(target); 70 71 // JRE string arrays 72 String[] jreNumberPatterns = null; 73 if (jreSupportsLocale) { 74 try { 75 jreNumberPatterns = rb.getStringArray("NumberPatterns"); 76 } catch (MissingResourceException mre) {} 77 } 78 79 // result object 80 String resultCur = getPattern(NumberFormat.getCurrencyInstance(target)); 81 String resultInt = getPattern(NumberFormat.getIntegerInstance(target)); 82 String resultNum = getPattern(NumberFormat.getNumberInstance(target)); 83 String resultPer = getPattern(NumberFormat.getPercentInstance(target)); 84 85 // provider's object (if any) 86 String providersCur = null; 87 String providersInt = null; 88 String providersNum = null; 89 String providersPer = null; 90 if (providerloc.contains(target)) { 91 NumberFormat dfCur = nfp.getCurrencyInstance(target); 92 if (dfCur != null) { 93 providersCur = getPattern(dfCur); 94 } 95 NumberFormat dfInt = nfp.getIntegerInstance(target); 96 if (dfInt != null) { 97 providersInt = getPattern(dfInt); 98 } 99 NumberFormat dfNum = nfp.getNumberInstance(target); 100 if (dfNum != null) { 101 providersNum = getPattern(dfNum); 102 } 103 NumberFormat dfPer = nfp.getPercentInstance(target); 104 if (dfPer != null) { 105 providersPer = getPattern(dfPer); 106 } 107 } 108 109 // JRE's object (if any) 110 // note that this totally depends on the current implementation 111 String jresCur = null; 112 String jresInt = null; 113 String jresNum = null; 114 String jresPer = null; 115 if (jreSupportsLocale) { 116 DecimalFormat dfCur = new DecimalFormat(jreNumberPatterns[1], 117 DecimalFormatSymbols.getInstance(target)); 118 if (dfCur != null) { 119 adjustForCurrencyDefaultFractionDigits(dfCur); 120 jresCur = dfCur.toPattern(); 121 } 122 DecimalFormat dfInt = new DecimalFormat(jreNumberPatterns[0], 123 DecimalFormatSymbols.getInstance(target)); 124 if (dfInt != null) { 125 dfInt.setMaximumFractionDigits(0); 126 dfInt.setDecimalSeparatorAlwaysShown(false); 127 dfInt.setParseIntegerOnly(true); 128 jresInt = dfInt.toPattern(); 129 } 130 DecimalFormat dfNum = new DecimalFormat(jreNumberPatterns[0], 131 DecimalFormatSymbols.getInstance(target)); 132 if (dfNum != null) { 133 jresNum = dfNum.toPattern(); 134 } 135 DecimalFormat dfPer = new DecimalFormat(jreNumberPatterns[2], 136 DecimalFormatSymbols.getInstance(target)); 137 if (dfPer != null) { 138 jresPer = dfPer.toPattern(); 139 } 140 } 141 142 checkValidity(target, jresCur, providersCur, resultCur, jreSupportsLocale); 143 checkValidity(target, jresInt, providersInt, resultInt, jreSupportsLocale); 144 checkValidity(target, jresNum, providersNum, resultNum, jreSupportsLocale); 145 checkValidity(target, jresPer, providersPer, resultPer, jreSupportsLocale); 146 } 147 } 148 149 /** 150 * Adjusts the minimum and maximum fraction digits to values that 151 * are reasonable for the currency's default fraction digits. 152 */ 153 void adjustForCurrencyDefaultFractionDigits(DecimalFormat df) { 154 DecimalFormatSymbols dfs = df.getDecimalFormatSymbols(); 155 Currency currency = dfs.getCurrency(); 156 if (currency == null) { 157 try { 158 currency = Currency.getInstance(dfs.getInternationalCurrencySymbol()); 159 } catch (IllegalArgumentException e) { 160 } 161 } 162 if (currency != null) { 163 int digits = currency.getDefaultFractionDigits(); 164 if (digits != -1) { 165 int oldMinDigits = df.getMinimumFractionDigits(); 166 // Common patterns are "#.##", "#.00", "#". 167 // Try to adjust all of them in a reasonable way. 168 if (oldMinDigits == df.getMaximumFractionDigits()) { 169 df.setMinimumFractionDigits(digits); 170 df.setMaximumFractionDigits(digits); 171 } else { 172 df.setMinimumFractionDigits(Math.min(digits, oldMinDigits)); 173 df.setMaximumFractionDigits(digits); 174 } 175 } 176 } 177 } 178 179 private static String getPattern(NumberFormat nf) { 180 if (nf instanceof DecimalFormat) { 181 return ((DecimalFormat)nf).toPattern(); 182 } 183 if (nf instanceof FooNumberFormat) { 184 return ((FooNumberFormat)nf).toPattern(); 185 } 186 return null; 187 } 188 189 private static final String[] NUMBER_PATTERNS = { 190 "num={0,number}", 191 "num={0,number,currency}", 192 "num={0,number,percent}", 193 "num={0,number,integer}" 194 }; 195 196 void messageFormatTest() { 197 for (Locale target : providerloc) { 198 for (String pattern : NUMBER_PATTERNS) { 199 MessageFormat mf = new MessageFormat(pattern, target); 200 String toPattern = mf.toPattern(); 201 if (!pattern.equals(toPattern)) { 202 throw new RuntimeException("MessageFormat.toPattern: got '" 203 + toPattern 204 + "', expected '" + pattern + "'"); 205 } 206 } 207 } 208 } 209 }