CollatorProviderTest.java revision 5747:131a683a2ce0
1169240Sjfv/* 2169240Sjfv * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved. 3169240Sjfv * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4169240Sjfv * 5169240Sjfv * This code is free software; you can redistribute it and/or modify it 6169240Sjfv * under the terms of the GNU General Public License version 2 only, as 7169240Sjfv * published by the Free Software Foundation. 8169240Sjfv * 9169240Sjfv * This code is distributed in the hope that it will be useful, but WITHOUT 10169240Sjfv * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11169240Sjfv * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12169240Sjfv * version 2 for more details (a copy is included in the LICENSE file that 13169240Sjfv * accompanied this code). 14169240Sjfv * 15169240Sjfv * You should have received a copy of the GNU General Public License version 16169240Sjfv * 2 along with this work; if not, write to the Free Software Foundation, 17169240Sjfv * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18169240Sjfv * 19169240Sjfv * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20169240Sjfv * or visit www.oracle.com if you need additional information or have any 21169240Sjfv * questions. 22169240Sjfv */ 23169240Sjfv/* 24169240Sjfv * 25169240Sjfv */ 26169240Sjfv 27169240Sjfvimport java.text.*; 28169240Sjfvimport java.util.*; 29169240Sjfvimport sun.util.locale.provider.*; 30169240Sjfvimport sun.util.resources.*; 31169240Sjfv 32169240Sjfvpublic class CollatorProviderTest extends ProviderTest { 33169240Sjfv 34169240Sjfv com.foo.CollatorProviderImpl cp = new com.foo.CollatorProviderImpl(); 35169240Sjfv List<Locale> availloc = Arrays.asList(Collator.getAvailableLocales()); 36169240Sjfv List<Locale> providerloc = Arrays.asList(cp.getAvailableLocales()); 37169240Sjfv List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales()); 38169240Sjfv List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getCollatorProvider().getAvailableLocales()); 39169240Sjfv 40169240Sjfv public static void main(String[] s) { 41169240Sjfv new CollatorProviderTest(); 42169240Sjfv } 43169240Sjfv 44169240Sjfv CollatorProviderTest() { 45169240Sjfv availableLocalesTest(); 46169240Sjfv objectValidityTest(); 47169240Sjfv } 48169240Sjfv 49169240Sjfv void availableLocalesTest() { 50169240Sjfv Set<Locale> localesFromAPI = new HashSet<Locale>(availloc); 51169240Sjfv Set<Locale> localesExpected = new HashSet<Locale>(jreimplloc); 52169240Sjfv localesExpected.remove(Locale.ROOT); 53169240Sjfv localesExpected.addAll(providerloc); 54169240Sjfv if (localesFromAPI.equals(localesExpected)) { 55169240Sjfv System.out.println("availableLocalesTest passed."); 56169240Sjfv } else { 57169240Sjfv throw new RuntimeException("availableLocalesTest failed"); 58169240Sjfv } 59169240Sjfv } 60169240Sjfv 61169240Sjfv void objectValidityTest() { 62169240Sjfv Collator def = Collator.getInstance(new Locale("")); 63169240Sjfv String defrules = ((RuleBasedCollator)def).getRules(); 64169240Sjfv 65169240Sjfv for (Locale target: availloc) { 66169240Sjfv // pure JRE implementation 67169240Sjfv Set<Locale> jreimplloc = new HashSet<>(); 68169240Sjfv for (String tag : ((AvailableLanguageTags)LocaleProviderAdapter.forJRE().getCollatorProvider()).getAvailableLanguageTags()) { 69169240Sjfv jreimplloc.add(Locale.forLanguageTag(tag)); 70169240Sjfv } 71169240Sjfv ResourceBundle rb = LocaleProviderAdapter.forJRE().getLocaleData().getCollationData(target); 72169240Sjfv boolean jreSupportsLocale = jreimplloc.contains(target); 73169240Sjfv 74169240Sjfv // result object 75169240Sjfv Collator result = Collator.getInstance(target); 76169240Sjfv 77169240Sjfv // provider's object (if any) 78169240Sjfv Collator providersResult = null; 79169240Sjfv if (providerloc.contains(target)) { 80169240Sjfv providersResult = cp.getInstance(target); 81169240Sjfv } 82169240Sjfv 83169240Sjfv // JRE rule 84169240Sjfv Collator jresResult = null; 85169240Sjfv if (jreSupportsLocale) { 86169240Sjfv try { 87169240Sjfv String rules = rb.getString("Rule"); 88169240Sjfv jresResult = new RuleBasedCollator(defrules+rules); 89169240Sjfv jresResult.setDecomposition(Collator.NO_DECOMPOSITION); 90169240Sjfv } catch (MissingResourceException mre) { 91169240Sjfv } catch (ParseException pe) { 92169240Sjfv } 93169240Sjfv } 94169240Sjfv 95169240Sjfv checkValidity(target, jresResult, providersResult, result, jreSupportsLocale); 96169240Sjfv } 97169240Sjfv } 98169240Sjfv} 99169240Sjfv