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>(jreloc); 55 localesExpected.addAll(providerloc); 56 if (localesFromAPI.equals(localesExpected)) { 57 System.out.println("availableLocalesTest passed."); 58 } else { 59 throw new RuntimeException("availableLocalesTest failed"); 60 } 61 } 62 63 void objectValidityTest() { 64 65 for (Locale target: availloc) { 66 // pure JRE implementation 67 ResourceBundle rb = LocaleProviderAdapter.forJRE().getLocaleData().getNumberFormatData(target); 68 boolean jreSupportsLocale = jreimplloc.contains(target); 69 70 // JRE string arrays 71 String[] jreNumberPatterns = null; 72 if (jreSupportsLocale) { 73 try { 74 jreNumberPatterns = rb.getStringArray("NumberPatterns"); 75 } catch (MissingResourceException mre) {} 76 } 77 78 // result object 79 String resultCur = getPattern(NumberFormat.getCurrencyInstance(target)); 80 String resultInt = getPattern(NumberFormat.getIntegerInstance(target)); 81 String resultNum = getPattern(NumberFormat.getNumberInstance(target)); 82 String resultPer = getPattern(NumberFormat.getPercentInstance(target)); 83 84 // provider's object (if any) 85 String providersCur = null; 86 String providersInt = null; 87 String providersNum = null; 88 String providersPer = null; 89 if (providerloc.contains(target)) { 90 NumberFormat dfCur = nfp.getCurrencyInstance(target); 91 if (dfCur != null) { 92 providersCur = getPattern(dfCur); 93 } 94 NumberFormat dfInt = nfp.getIntegerInstance(target); 95 if (dfInt != null) { 96 providersInt = getPattern(dfInt); 97 } 98 NumberFormat dfNum = nfp.getNumberInstance(target); 99 if (dfNum != null) { 100 providersNum = getPattern(dfNum); 101 } 102 NumberFormat dfPer = nfp.getPercentInstance(target); 103 if (dfPer != null) { 104 providersPer = getPattern(dfPer); 105 } 106 } 107 108 // JRE's object (if any) 109 // note that this totally depends on the current implementation 110 String jresCur = null; 111 String jresInt = null; 112 String jresNum = null; 113 String jresPer = null; 114 if (jreSupportsLocale) { 115 DecimalFormat dfCur = new DecimalFormat(jreNumberPatterns[1], 116 DecimalFormatSymbols.getInstance(target)); 117 if (dfCur != null) { 118 adjustForCurrencyDefaultFractionDigits(dfCur); 119 jresCur = dfCur.toPattern(); 120 } 121 DecimalFormat dfInt = new DecimalFormat(jreNumberPatterns[0], 122 DecimalFormatSymbols.getInstance(target)); 123 if (dfInt != null) { 124 dfInt.setMaximumFractionDigits(0); 125 dfInt.setDecimalSeparatorAlwaysShown(false); 126 dfInt.setParseIntegerOnly(true); 127 jresInt = dfInt.toPattern(); 128 } 129 DecimalFormat dfNum = new DecimalFormat(jreNumberPatterns[0], 130 DecimalFormatSymbols.getInstance(target)); 131 if (dfNum != null) { 132 jresNum = dfNum.toPattern(); 133 } 134 DecimalFormat dfPer = new DecimalFormat(jreNumberPatterns[2], 135 DecimalFormatSymbols.getInstance(target)); 136 if (dfPer != null) { 137 jresPer = dfPer.toPattern(); 138 } 139 } 140 141 checkValidity(target, jresCur, providersCur, resultCur, jreSupportsLocale); 142 checkValidity(target, jresInt, providersInt, resultInt, jreSupportsLocale); 143 checkValidity(target, jresNum, providersNum, resultNum, jreSupportsLocale); 144 checkValidity(target, jresPer, providersPer, resultPer, jreSupportsLocale); 145 } 146 } 147 148 /** 149 * Adjusts the minimum and maximum fraction digits to values that 150 * are reasonable for the currency's default fraction digits. 151 */ 152 void adjustForCurrencyDefaultFractionDigits(DecimalFormat df) { 153 DecimalFormatSymbols dfs = df.getDecimalFormatSymbols(); 154 Currency currency = dfs.getCurrency(); 155 if (currency == null) { 156 try { 157 currency = Currency.getInstance(dfs.getInternationalCurrencySymbol()); 158 } catch (IllegalArgumentException e) { 159 } 160 } 161 if (currency != null) { 162 int digits = currency.getDefaultFractionDigits(); 163 if (digits != -1) { 164 int oldMinDigits = df.getMinimumFractionDigits(); 165 // Common patterns are "#.##", "#.00", "#". 166 // Try to adjust all of them in a reasonable way. 167 if (oldMinDigits == df.getMaximumFractionDigits()) { 168 df.setMinimumFractionDigits(digits); 169 df.setMaximumFractionDigits(digits); 170 } else { 171 df.setMinimumFractionDigits(Math.min(digits, oldMinDigits)); 172 df.setMaximumFractionDigits(digits); 173 } 174 } 175 } 176 } 177 178 private static String getPattern(NumberFormat nf) { 179 if (nf instanceof DecimalFormat) { 180 return ((DecimalFormat)nf).toPattern(); 181 } 182 if (nf instanceof FooNumberFormat) { 183 return ((FooNumberFormat)nf).toPattern(); 184 } 185 return null; 186 } 187 188 private static final String[] NUMBER_PATTERNS = { 189 "num={0,number}", 190 "num={0,number,currency}", 191 "num={0,number,percent}", 192 "num={0,number,integer}" 193 }; 194 195 void messageFormatTest() { 196 for (Locale target : providerloc) { 197 for (String pattern : NUMBER_PATTERNS) { 198 MessageFormat mf = new MessageFormat(pattern, target); 199 String toPattern = mf.toPattern(); 200 if (!pattern.equals(toPattern)) { 201 throw new RuntimeException("MessageFormat.toPattern: got '" 202 + toPattern 203 + "', expected '" + pattern + "'"); 204 } 205 } 206 } 207 } 208 }