Path: blob/master/test/jdk/java/util/PluggableLocale/NumberFormatProviderTest.java
41149 views
/*1* Copyright (c) 2007, 2018, 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*/2223/*24* @test25* @bug 4052440 7003643 8062588 821040626* @summary NumberFormatProvider tests27* @library providersrc/foobarutils28* providersrc/fooprovider29* @modules java.base/sun.util.locale.provider30* @build com.foobar.Utils31* com.foo.*32* @run main/othervm -Djava.locale.providers=JRE,SPI NumberFormatProviderTest33*/3435import java.text.DecimalFormat;36import java.text.DecimalFormatSymbols;37import java.text.MessageFormat;38import java.text.NumberFormat;39import java.util.Arrays;40import java.util.Currency;41import java.util.HashSet;42import java.util.List;43import java.util.Locale;44import java.util.Set;4546import com.foo.FooNumberFormat;47import com.foo.NumberFormatProviderImpl;4849import sun.util.locale.provider.LocaleProviderAdapter;5051public class NumberFormatProviderTest extends ProviderTest {5253NumberFormatProviderImpl nfp = new NumberFormatProviderImpl();54List<Locale> availloc = Arrays.asList(NumberFormat.getAvailableLocales());55List<Locale> providerloc = Arrays.asList(nfp.getAvailableLocales());56List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales());57List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getNumberFormatProvider().getAvailableLocales());5859public static void main(String[] s) {60new NumberFormatProviderTest();61}6263NumberFormatProviderTest() {64availableLocalesTest();65objectValidityTest();66messageFormatTest();67}6869void availableLocalesTest() {70Set<Locale> localesFromAPI = new HashSet<>(availloc);71Set<Locale> localesExpected = new HashSet<>(jreloc);72localesExpected.addAll(providerloc);73if (localesFromAPI.equals(localesExpected)) {74System.out.println("availableLocalesTest passed.");75} else {76throw new RuntimeException("availableLocalesTest failed");77}78}7980void objectValidityTest() {8182for (Locale target: availloc) {83boolean jreSupportsLocale = jreimplloc.contains(target);8485// JRE string arrays86String[] jreNumberPatterns = null;87if (jreSupportsLocale) {88jreNumberPatterns = LocaleProviderAdapter.forJRE().getLocaleResources(target).getNumberPatterns();89}9091// result object92String resultCur = getPattern(NumberFormat.getCurrencyInstance(target));93String resultInt = getPattern(NumberFormat.getIntegerInstance(target));94String resultNum = getPattern(NumberFormat.getNumberInstance(target));95String resultPer = getPattern(NumberFormat.getPercentInstance(target));9697// provider's object (if any)98String providersCur = null;99String providersInt = null;100String providersNum = null;101String providersPer = null;102if (providerloc.contains(target)) {103NumberFormat dfCur = nfp.getCurrencyInstance(target);104if (dfCur != null) {105providersCur = getPattern(dfCur);106}107NumberFormat dfInt = nfp.getIntegerInstance(target);108if (dfInt != null) {109providersInt = getPattern(dfInt);110}111NumberFormat dfNum = nfp.getNumberInstance(target);112if (dfNum != null) {113providersNum = getPattern(dfNum);114}115NumberFormat dfPer = nfp.getPercentInstance(target);116if (dfPer != null) {117providersPer = getPattern(dfPer);118}119}120121// JRE's object (if any)122// note that this totally depends on the current implementation123String jresCur = null;124String jresInt = null;125String jresNum = null;126String jresPer = null;127if (jreSupportsLocale) {128DecimalFormat dfCur = new DecimalFormat(jreNumberPatterns[1],129DecimalFormatSymbols.getInstance(target));130if (dfCur != null) {131adjustForCurrencyDefaultFractionDigits(dfCur);132jresCur = dfCur.toPattern();133}134DecimalFormat dfInt = new DecimalFormat(jreNumberPatterns[0],135DecimalFormatSymbols.getInstance(target));136if (dfInt != null) {137dfInt.setMaximumFractionDigits(0);138dfInt.setDecimalSeparatorAlwaysShown(false);139dfInt.setParseIntegerOnly(true);140jresInt = dfInt.toPattern();141}142DecimalFormat dfNum = new DecimalFormat(jreNumberPatterns[0],143DecimalFormatSymbols.getInstance(target));144if (dfNum != null) {145jresNum = dfNum.toPattern();146}147DecimalFormat dfPer = new DecimalFormat(jreNumberPatterns[2],148DecimalFormatSymbols.getInstance(target));149if (dfPer != null) {150jresPer = dfPer.toPattern();151}152}153154checkValidity(target, jresCur, providersCur, resultCur, jreSupportsLocale);155checkValidity(target, jresInt, providersInt, resultInt, jreSupportsLocale);156checkValidity(target, jresNum, providersNum, resultNum, jreSupportsLocale);157checkValidity(target, jresPer, providersPer, resultPer, jreSupportsLocale);158}159}160161/**162* Adjusts the minimum and maximum fraction digits to values that163* are reasonable for the currency's default fraction digits.164*/165void adjustForCurrencyDefaultFractionDigits(DecimalFormat df) {166DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();167Currency currency = dfs.getCurrency();168if (currency == null) {169try {170currency = Currency.getInstance(dfs.getInternationalCurrencySymbol());171} catch (IllegalArgumentException e) {172}173}174if (currency != null) {175int digits = currency.getDefaultFractionDigits();176if (digits != -1) {177int oldMinDigits = df.getMinimumFractionDigits();178// Common patterns are "#.##", "#.00", "#".179// Try to adjust all of them in a reasonable way.180if (oldMinDigits == df.getMaximumFractionDigits()) {181df.setMinimumFractionDigits(digits);182df.setMaximumFractionDigits(digits);183} else {184df.setMinimumFractionDigits(Math.min(digits, oldMinDigits));185df.setMaximumFractionDigits(digits);186}187}188}189}190191private static String getPattern(NumberFormat nf) {192if (nf instanceof DecimalFormat) {193return ((DecimalFormat)nf).toPattern();194}195if (nf instanceof FooNumberFormat) {196return ((FooNumberFormat)nf).toPattern();197}198return null;199}200201private static final String[] NUMBER_PATTERNS = {202"num={0,number}",203"num={0,number,currency}",204"num={0,number,percent}",205"num={0,number,integer}"206};207208void messageFormatTest() {209for (Locale target : providerloc) {210for (String pattern : NUMBER_PATTERNS) {211MessageFormat mf = new MessageFormat(pattern, target);212String toPattern = mf.toPattern();213if (!pattern.equals(toPattern)) {214throw new RuntimeException("MessageFormat.toPattern: got '"215+ toPattern216+ "', expected '" + pattern + "'");217}218}219}220}221}222223