Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/PluggableLocale/NumberFormatProviderTest.java
47209 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23*24*/2526import java.text.*;27import java.util.*;28import sun.util.locale.provider.*;29import sun.util.resources.*;3031import com.foo.FooNumberFormat;3233public class NumberFormatProviderTest extends ProviderTest {3435com.foo.NumberFormatProviderImpl nfp = new com.foo.NumberFormatProviderImpl();36List<Locale> availloc = Arrays.asList(NumberFormat.getAvailableLocales());37List<Locale> providerloc = Arrays.asList(nfp.getAvailableLocales());38List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales());39List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getNumberFormatProvider().getAvailableLocales());4041public static void main(String[] s) {42new NumberFormatProviderTest();43}4445NumberFormatProviderTest() {46availableLocalesTest();47objectValidityTest();48messageFormatTest();49}5051void availableLocalesTest() {52Set<Locale> localesFromAPI = new HashSet<>(availloc);53Set<Locale> localesExpected = new HashSet<>(jreloc);54localesExpected.addAll(providerloc);55if (localesFromAPI.equals(localesExpected)) {56System.out.println("availableLocalesTest passed.");57} else {58throw new RuntimeException("availableLocalesTest failed");59}60}6162void objectValidityTest() {6364for (Locale target: availloc) {65boolean jreSupportsLocale = jreimplloc.contains(target);6667// JRE string arrays68String[] jreNumberPatterns = null;69if (jreSupportsLocale) {70jreNumberPatterns = LocaleProviderAdapter.forJRE().getLocaleResources(target).getNumberPatterns();71}7273// result object74String resultCur = getPattern(NumberFormat.getCurrencyInstance(target));75String resultInt = getPattern(NumberFormat.getIntegerInstance(target));76String resultNum = getPattern(NumberFormat.getNumberInstance(target));77String resultPer = getPattern(NumberFormat.getPercentInstance(target));7879// provider's object (if any)80String providersCur = null;81String providersInt = null;82String providersNum = null;83String providersPer = null;84if (providerloc.contains(target)) {85NumberFormat dfCur = nfp.getCurrencyInstance(target);86if (dfCur != null) {87providersCur = getPattern(dfCur);88}89NumberFormat dfInt = nfp.getIntegerInstance(target);90if (dfInt != null) {91providersInt = getPattern(dfInt);92}93NumberFormat dfNum = nfp.getNumberInstance(target);94if (dfNum != null) {95providersNum = getPattern(dfNum);96}97NumberFormat dfPer = nfp.getPercentInstance(target);98if (dfPer != null) {99providersPer = getPattern(dfPer);100}101}102103// JRE's object (if any)104// note that this totally depends on the current implementation105String jresCur = null;106String jresInt = null;107String jresNum = null;108String jresPer = null;109if (jreSupportsLocale) {110DecimalFormat dfCur = new DecimalFormat(jreNumberPatterns[1],111DecimalFormatSymbols.getInstance(target));112if (dfCur != null) {113adjustForCurrencyDefaultFractionDigits(dfCur);114jresCur = dfCur.toPattern();115}116DecimalFormat dfInt = new DecimalFormat(jreNumberPatterns[0],117DecimalFormatSymbols.getInstance(target));118if (dfInt != null) {119dfInt.setMaximumFractionDigits(0);120dfInt.setDecimalSeparatorAlwaysShown(false);121dfInt.setParseIntegerOnly(true);122jresInt = dfInt.toPattern();123}124DecimalFormat dfNum = new DecimalFormat(jreNumberPatterns[0],125DecimalFormatSymbols.getInstance(target));126if (dfNum != null) {127jresNum = dfNum.toPattern();128}129DecimalFormat dfPer = new DecimalFormat(jreNumberPatterns[2],130DecimalFormatSymbols.getInstance(target));131if (dfPer != null) {132jresPer = dfPer.toPattern();133}134}135136checkValidity(target, jresCur, providersCur, resultCur, jreSupportsLocale);137checkValidity(target, jresInt, providersInt, resultInt, jreSupportsLocale);138checkValidity(target, jresNum, providersNum, resultNum, jreSupportsLocale);139checkValidity(target, jresPer, providersPer, resultPer, jreSupportsLocale);140}141}142143/**144* Adjusts the minimum and maximum fraction digits to values that145* are reasonable for the currency's default fraction digits.146*/147void adjustForCurrencyDefaultFractionDigits(DecimalFormat df) {148DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();149Currency currency = dfs.getCurrency();150if (currency == null) {151try {152currency = Currency.getInstance(dfs.getInternationalCurrencySymbol());153} catch (IllegalArgumentException e) {154}155}156if (currency != null) {157int digits = currency.getDefaultFractionDigits();158if (digits != -1) {159int oldMinDigits = df.getMinimumFractionDigits();160// Common patterns are "#.##", "#.00", "#".161// Try to adjust all of them in a reasonable way.162if (oldMinDigits == df.getMaximumFractionDigits()) {163df.setMinimumFractionDigits(digits);164df.setMaximumFractionDigits(digits);165} else {166df.setMinimumFractionDigits(Math.min(digits, oldMinDigits));167df.setMaximumFractionDigits(digits);168}169}170}171}172173private static String getPattern(NumberFormat nf) {174if (nf instanceof DecimalFormat) {175return ((DecimalFormat)nf).toPattern();176}177if (nf instanceof FooNumberFormat) {178return ((FooNumberFormat)nf).toPattern();179}180return null;181}182183private static final String[] NUMBER_PATTERNS = {184"num={0,number}",185"num={0,number,currency}",186"num={0,number,percent}",187"num={0,number,integer}"188};189190void messageFormatTest() {191for (Locale target : providerloc) {192for (String pattern : NUMBER_PATTERNS) {193MessageFormat mf = new MessageFormat(pattern, target);194String toPattern = mf.toPattern();195if (!pattern.equals(toPattern)) {196throw new RuntimeException("MessageFormat.toPattern: got '"197+ toPattern198+ "', expected '" + pattern + "'");199}200}201}202}203}204205206