Path: blob/master/test/jdk/java/util/PluggableLocale/GenericTest.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 8062588 821040626* @summary Generic tests for the pluggable locales feature27* @library providersrc/foobarutils28* providersrc/barprovider29* providersrc/fooprovider30* @modules java.base/sun.util.locale.provider31* @build com.foobar.Utils32* com.bar.*33* com.foo.*34* @run main/othervm -Djava.locale.providers=JRE,SPI GenericTest35*/3637import java.util.Arrays;38import java.util.HashSet;39import java.util.Locale;40import java.util.Set;4142import com.bar.CalendarDataProviderImpl;43import com.bar.CalendarNameProviderImpl;44import com.bar.CurrencyNameProviderImpl;45import com.bar.CurrencyNameProviderImpl2;46import com.bar.GenericTimeZoneNameProviderImpl;47import com.bar.LocaleNameProviderImpl;48import com.bar.TimeZoneNameProviderImpl;49import com.foo.BreakIteratorProviderImpl;50import com.foo.CollatorProviderImpl;51import com.foo.DateFormatProviderImpl;52import com.foo.DateFormatSymbolsProviderImpl;53import com.foo.DecimalFormatSymbolsProviderImpl;54import com.foo.NumberFormatProviderImpl;5556import sun.util.locale.provider.LocaleProviderAdapter;5758public class GenericTest {5960// test providers61BreakIteratorProviderImpl breakIP = new BreakIteratorProviderImpl();62CollatorProviderImpl collatorP = new CollatorProviderImpl();63DateFormatProviderImpl dateFP = new DateFormatProviderImpl();64DateFormatSymbolsProviderImpl dateFSP = new DateFormatSymbolsProviderImpl();65DecimalFormatSymbolsProviderImpl decimalFSP = new DecimalFormatSymbolsProviderImpl();66NumberFormatProviderImpl numberFP = new NumberFormatProviderImpl();67CurrencyNameProviderImpl currencyNP = new CurrencyNameProviderImpl();68CurrencyNameProviderImpl2 currencyNP2 = new CurrencyNameProviderImpl2();69LocaleNameProviderImpl localeNP = new LocaleNameProviderImpl();70TimeZoneNameProviderImpl tzNP = new TimeZoneNameProviderImpl();71GenericTimeZoneNameProviderImpl tzGenNP = new GenericTimeZoneNameProviderImpl();72CalendarDataProviderImpl calDataP = new CalendarDataProviderImpl();73CalendarNameProviderImpl calNameP = new CalendarNameProviderImpl();7475public static void main(String[] s) {76new GenericTest();77}7879GenericTest() {80availableLocalesTest();81localeFallbackTest();82}8384/**85* Make sure that all the locales are available from the existing providers86*/87void availableLocalesTest() {88// Check that Locale.getAvailableLocales() returns the union of the JRE supported89// locales and providers' locales90HashSet<Locale> result =91new HashSet<>(Arrays.asList(Locale.getAvailableLocales()));92HashSet<Locale> expected =93new HashSet<>(Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales()));94expected.addAll(Arrays.asList(breakIP.getAvailableLocales()));95expected.addAll(Arrays.asList(collatorP.getAvailableLocales()));96expected.addAll(Arrays.asList(dateFP.getAvailableLocales()));97expected.addAll(Arrays.asList(dateFSP.getAvailableLocales()));98expected.addAll(Arrays.asList(decimalFSP.getAvailableLocales()));99expected.addAll(Arrays.asList(numberFP.getAvailableLocales()));100expected.addAll(Arrays.asList(currencyNP.getAvailableLocales()));101expected.addAll(Arrays.asList(currencyNP2.getAvailableLocales()));102expected.addAll(Arrays.asList(localeNP.getAvailableLocales()));103expected.addAll(Arrays.asList(tzNP.getAvailableLocales()));104expected.addAll(Arrays.asList(tzGenNP.getAvailableLocales()));105expected.addAll(Arrays.asList(calDataP.getAvailableLocales()));106expected.addAll(Arrays.asList(calNameP.getAvailableLocales()));107if (!result.equals(expected)) {108throw new RuntimeException("Locale.getAvailableLocales() does not return the union of locales: diff="109+ getDiff(result, expected));110}111}112113/**114* test with "xx_YY_ZZ", which is an example locale not contained115* in Locale.getAvailableLocales(). Fallback tests for supported locales116* are done in each xxxProviderTest test cases.117*/118void localeFallbackTest() {119Locale xx = new Locale("xx");120Locale dispLocale = new Locale ("xx", "YY", "ZZ");121122String xxname = xx.getDisplayLanguage(xx);123String expected = localeNP.getDisplayLanguage(xx.getLanguage(), dispLocale);124if (!xxname.equals(expected)) {125throw new RuntimeException("Locale fallback did not perform correctly. got: "+xxname+" expected: "+expected);126}127}128129private static String getDiff(Set set1, Set set2) {130Set s1 = (Set)((HashSet)set1).clone();131s1.removeAll(set2);132133Set s2 = (Set)((HashSet)set2).clone();134s2.removeAll(set1);135s2.addAll(s1);136return s2.toString();137}138}139140