Path: blob/master/test/jdk/java/util/PluggableLocale/CollatorProviderTest.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 CollatorProvider tests27* @library providersrc/foobarutils28* providersrc/fooprovider29* @modules java.base/sun.util.locale.provider30* java.base/sun.util.resources31* @build com.foobar.Utils32* com.foo.*33* @run main/othervm -Djava.locale.providers=JRE,SPI CollatorProviderTest34*/3536import java.text.Collator;37import java.text.ParseException;38import java.text.RuleBasedCollator;39import java.util.Arrays;40import java.util.HashSet;41import java.util.List;42import java.util.Locale;43import java.util.MissingResourceException;44import java.util.ResourceBundle;45import java.util.Set;4647import com.foo.CollatorProviderImpl;4849import sun.util.locale.provider.AvailableLanguageTags;50import sun.util.locale.provider.LocaleProviderAdapter;51import sun.util.locale.provider.ResourceBundleBasedAdapter;5253public class CollatorProviderTest extends ProviderTest {5455CollatorProviderImpl cp = new CollatorProviderImpl();56List<Locale> availloc = Arrays.asList(Collator.getAvailableLocales());57List<Locale> providerloc = Arrays.asList(cp.getAvailableLocales());58List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales());59List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getCollatorProvider().getAvailableLocales());6061public static void main(String[] s) {62new CollatorProviderTest();63}6465CollatorProviderTest() {66availableLocalesTest();67objectValidityTest();68}6970void availableLocalesTest() {71Set<Locale> localesFromAPI = new HashSet<>(availloc);72Set<Locale> localesExpected = new HashSet<>(jreloc);73localesExpected.addAll(providerloc);74if (localesFromAPI.equals(localesExpected)) {75System.out.println("availableLocalesTest passed.");76} else {77throw new RuntimeException("availableLocalesTest failed");78}79}8081void objectValidityTest() {82Collator def = Collator.getInstance(new Locale(""));83String defrules = ((RuleBasedCollator)def).getRules();8485for (Locale target: availloc) {86// pure JRE implementation87Set<Locale> jreimplloc = new HashSet<>();88for (String tag : ((AvailableLanguageTags)LocaleProviderAdapter.forJRE().getCollatorProvider()).getAvailableLanguageTags()) {89jreimplloc.add(Locale.forLanguageTag(tag));90}91ResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getCollationData(target);92boolean jreSupportsLocale = jreimplloc.contains(target);9394// result object95Collator result = Collator.getInstance(target);9697// provider's object (if any)98Collator providersResult = null;99if (providerloc.contains(target)) {100providersResult = cp.getInstance(target);101}102103// JRE rule104Collator jresResult = null;105if (jreSupportsLocale) {106try {107String rules = rb.getString("Rule");108jresResult = new RuleBasedCollator(defrules+rules);109jresResult.setDecomposition(Collator.NO_DECOMPOSITION);110} catch (MissingResourceException mre) {111} catch (ParseException pe) {112}113}114115checkValidity(target, jresResult, providersResult, result, jreSupportsLocale);116}117}118}119120