NumberFormatProviderTest.java revision 0
0N/A/*
2362N/A * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2362N/A * have any questions.
2362N/A */
2362N/A/*
0N/A *
0N/A */
0N/A
0N/Aimport java.text.*;
0N/Aimport java.util.*;
0N/Aimport sun.util.*;
0N/Aimport sun.util.resources.*;
0N/A
0N/Apublic class NumberFormatProviderTest extends ProviderTest {
0N/A
0N/A com.foo.NumberFormatProviderImpl nfp = new com.foo.NumberFormatProviderImpl();
0N/A List<Locale> availloc = Arrays.asList(NumberFormat.getAvailableLocales());
0N/A List<Locale> providerloc = Arrays.asList(nfp.getAvailableLocales());
0N/A List<Locale> jreloc = Arrays.asList(LocaleData.getAvailableLocales());
0N/A
0N/A public static void main(String[] s) {
0N/A new NumberFormatProviderTest();
0N/A }
0N/A
0N/A NumberFormatProviderTest() {
0N/A availableLocalesTest();
0N/A objectValidityTest();
0N/A }
0N/A
0N/A void availableLocalesTest() {
0N/A Set<Locale> localesFromAPI = new HashSet<Locale>(availloc);
0N/A Set<Locale> localesExpected = new HashSet<Locale>(jreloc);
0N/A localesExpected.addAll(providerloc);
0N/A if (localesFromAPI.equals(localesExpected)) {
0N/A System.out.println("availableLocalesTest passed.");
0N/A } else {
0N/A throw new RuntimeException("availableLocalesTest failed");
0N/A }
0N/A }
0N/A
0N/A void objectValidityTest() {
0N/A
0N/A for (Locale target: availloc) {
0N/A // pure JRE implementation
0N/A ResourceBundle rb = LocaleData.getNumberFormatData(target);
0N/A boolean jreSupportsLocale = jreloc.contains(target);
0N/A
0N/A // JRE string arrays
0N/A String[] jreNumberPatterns = null;
0N/A if (jreSupportsLocale) {
0N/A try {
0N/A jreNumberPatterns = rb.getStringArray("NumberPatterns");
0N/A } catch (MissingResourceException mre) {}
0N/A }
0N/A
0N/A // result object
0N/A String resultCur =
0N/A ((DecimalFormat)NumberFormat.getCurrencyInstance(target)).toPattern();
0N/A String resultInt =
0N/A ((DecimalFormat)NumberFormat.getIntegerInstance(target)).toPattern();
0N/A String resultNum =
0N/A ((DecimalFormat)NumberFormat.getNumberInstance(target)).toPattern();
0N/A String resultPer =
0N/A ((DecimalFormat)NumberFormat.getPercentInstance(target)).toPattern();
0N/A
0N/A // provider's object (if any)
0N/A String providersCur = null;
0N/A String providersInt = null;
0N/A String providersNum = null;
0N/A String providersPer = null;
0N/A if (providerloc.contains(target)) {
0N/A DecimalFormat dfCur = (DecimalFormat)nfp.getCurrencyInstance(target);
0N/A if (dfCur != null) {
0N/A providersCur = dfCur.toPattern();
0N/A }
0N/A DecimalFormat dfInt = (DecimalFormat)nfp.getIntegerInstance(target);
0N/A if (dfInt != null) {
0N/A providersInt = dfInt.toPattern();
0N/A }
0N/A DecimalFormat dfNum = (DecimalFormat)nfp.getNumberInstance(target);
0N/A if (dfNum != null) {
0N/A providersNum = dfNum.toPattern();
0N/A }
0N/A DecimalFormat dfPer = (DecimalFormat)nfp.getPercentInstance(target);
0N/A if (dfPer != null) {
0N/A providersPer = dfPer.toPattern();
0N/A }
0N/A }
0N/A
0N/A // JRE's object (if any)
0N/A // note that this totally depends on the current implementation
0N/A String jresCur = null;
0N/A String jresInt = null;
0N/A String jresNum = null;
0N/A String jresPer = null;
0N/A if (jreSupportsLocale) {
0N/A DecimalFormat dfCur = new DecimalFormat(jreNumberPatterns[1],
0N/A DecimalFormatSymbols.getInstance(target));
0N/A if (dfCur != null) {
0N/A adjustForCurrencyDefaultFractionDigits(dfCur);
0N/A jresCur = dfCur.toPattern();
0N/A }
0N/A DecimalFormat dfInt = new DecimalFormat(jreNumberPatterns[0],
0N/A DecimalFormatSymbols.getInstance(target));
0N/A if (dfInt != null) {
0N/A dfInt.setMaximumFractionDigits(0);
0N/A dfInt.setDecimalSeparatorAlwaysShown(false);
0N/A dfInt.setParseIntegerOnly(true);
0N/A jresInt = dfInt.toPattern();
0N/A }
0N/A DecimalFormat dfNum = new DecimalFormat(jreNumberPatterns[0],
0N/A DecimalFormatSymbols.getInstance(target));
0N/A if (dfNum != null) {
0N/A jresNum = dfNum.toPattern();
0N/A }
0N/A DecimalFormat dfPer = new DecimalFormat(jreNumberPatterns[2],
0N/A DecimalFormatSymbols.getInstance(target));
0N/A if (dfPer != null) {
0N/A jresPer = dfPer.toPattern();
0N/A }
0N/A }
0N/A
0N/A checkValidity(target, jresCur, providersCur, resultCur, jreSupportsLocale);
0N/A checkValidity(target, jresInt, providersInt, resultInt, jreSupportsLocale);
0N/A checkValidity(target, jresNum, providersNum, resultNum, jreSupportsLocale);
0N/A checkValidity(target, jresPer, providersPer, resultPer, jreSupportsLocale);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adjusts the minimum and maximum fraction digits to values that
0N/A * are reasonable for the currency's default fraction digits.
0N/A */
0N/A void adjustForCurrencyDefaultFractionDigits(DecimalFormat df) {
0N/A DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
0N/A Currency currency = dfs.getCurrency();
0N/A if (currency == null) {
0N/A try {
0N/A currency = Currency.getInstance(dfs.getInternationalCurrencySymbol());
0N/A } catch (IllegalArgumentException e) {
0N/A }
0N/A }
0N/A if (currency != null) {
0N/A int digits = currency.getDefaultFractionDigits();
0N/A if (digits != -1) {
0N/A int oldMinDigits = df.getMinimumFractionDigits();
0N/A // Common patterns are "#.##", "#.00", "#".
0N/A // Try to adjust all of them in a reasonable way.
0N/A if (oldMinDigits == df.getMaximumFractionDigits()) {
0N/A df.setMinimumFractionDigits(digits);
0N/A df.setMaximumFractionDigits(digits);
0N/A } else {
0N/A df.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
0N/A df.setMaximumFractionDigits(digits);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}
0N/A