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