1 /*
   2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4052440 8062588 8210406
  27  * @summary CollatorProvider tests
  28  * @library providersrc/foobarutils
  29  *          providersrc/fooprovider
  30  * @modules java.base/sun.util.locale.provider
  31  *          java.base/sun.util.resources
  32  * @build com.foobar.Utils
  33  *        com.foo.*
  34  * @run main/othervm -Djava.locale.providers=JRE,SPI CollatorProviderTest
  35  */
  36 
  37 import java.text.Collator;
  38 import java.text.ParseException;
  39 import java.text.RuleBasedCollator;
  40 import java.util.Arrays;
  41 import java.util.HashSet;
  42 import java.util.List;
  43 import java.util.Locale;
  44 import java.util.MissingResourceException;
  45 import java.util.ResourceBundle;
  46 import java.util.Set;
  47 
  48 import com.foo.CollatorProviderImpl;
  49 
  50 import sun.util.locale.provider.AvailableLanguageTags;
  51 import sun.util.locale.provider.LocaleProviderAdapter;
  52 import sun.util.locale.provider.ResourceBundleBasedAdapter;
  53 
  54 public class CollatorProviderTest extends ProviderTest {
  55 
  56     CollatorProviderImpl cp = new CollatorProviderImpl();
  57     List<Locale> availloc = Arrays.asList(Collator.getAvailableLocales());
  58     List<Locale> providerloc = Arrays.asList(cp.getAvailableLocales());
  59     List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales());
  60     List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getCollatorProvider().getAvailableLocales());
  61 
  62     public static void main(String[] s) {
  63         new CollatorProviderTest();
  64     }
  65 
  66     CollatorProviderTest() {
  67         availableLocalesTest();
  68         objectValidityTest();
  69     }
  70 
  71     void availableLocalesTest() {
  72         Set<Locale> localesFromAPI = new HashSet<>(availloc);
  73         Set<Locale> localesExpected = new HashSet<>(jreloc);
  74         localesExpected.addAll(providerloc);
  75         if (localesFromAPI.equals(localesExpected)) {
  76             System.out.println("availableLocalesTest passed.");
  77         } else {
  78             throw new RuntimeException("availableLocalesTest failed");
  79         }
  80     }
  81 
  82     void objectValidityTest() {
  83         Collator def = Collator.getInstance(new Locale(""));
  84         String defrules = ((RuleBasedCollator)def).getRules();
  85 
  86         for (Locale target: availloc) {
  87             // pure JRE implementation
  88             Set<Locale> jreimplloc = new HashSet<>();
  89             for (String tag : ((AvailableLanguageTags)LocaleProviderAdapter.forJRE().getCollatorProvider()).getAvailableLanguageTags()) {
  90                 jreimplloc.add(Locale.forLanguageTag(tag));
  91             }
  92             ResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getCollationData(target);
  93             boolean jreSupportsLocale = jreimplloc.contains(target);
  94 
  95             // result object
  96             Collator result = Collator.getInstance(target);
  97 
  98             // provider's object (if any)
  99             Collator providersResult = null;
 100             if (providerloc.contains(target)) {
 101                 providersResult = cp.getInstance(target);
 102             }
 103 
 104             // JRE rule
 105             Collator jresResult = null;
 106             if (jreSupportsLocale) {
 107                 try {
 108                     String rules = rb.getString("Rule");
 109                     jresResult = new RuleBasedCollator(defrules+rules);
 110                     jresResult.setDecomposition(Collator.NO_DECOMPOSITION);
 111                 } catch (MissingResourceException mre) {
 112                 } catch (ParseException pe) {
 113                 }
 114             }
 115 
 116             checkValidity(target, jresResult, providersResult, result, jreSupportsLocale);
 117         }
 118     }
 119 }