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 8000273 8062588 8210406
  27  * @summary LocaleNameProvider tests
  28  * @library providersrc/foobarutils
  29  *          providersrc/barprovider
  30  * @modules java.base/sun.util.locale.provider
  31  *          java.base/sun.util.resources
  32  * @build com.foobar.Utils
  33  *        com.bar.*
  34  * @run main/othervm -Djava.locale.providers=JRE,SPI LocaleNameProviderTest
  35  */
  36 
  37 import java.util.Arrays;
  38 import java.util.List;
  39 import java.util.Locale;
  40 import java.util.MissingResourceException;
  41 
  42 import com.bar.LocaleNameProviderImpl;
  43 
  44 import sun.util.locale.provider.LocaleProviderAdapter;
  45 import sun.util.locale.provider.ResourceBundleBasedAdapter;
  46 import sun.util.resources.OpenListResourceBundle;
  47 
  48 public class LocaleNameProviderTest extends ProviderTest {
  49 
  50     public static void main(String[] s) {
  51         new LocaleNameProviderTest();
  52     }
  53 
  54     LocaleNameProviderTest() {
  55         checkAvailLocValidityTest();
  56         variantFallbackTest();
  57     }
  58 
  59     void checkAvailLocValidityTest() {
  60         LocaleNameProviderImpl lnp = new LocaleNameProviderImpl();
  61         Locale[] availloc = Locale.getAvailableLocales();
  62         Locale[] testloc = availloc.clone();
  63         List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getLocaleNameProvider().getAvailableLocales());
  64         List<Locale> providerloc = Arrays.asList(lnp.getAvailableLocales());
  65 
  66         for (Locale target: availloc) {
  67             // pure JRE implementation
  68             OpenListResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getLocaleNames(target);
  69             boolean jreSupportsTarget = jreimplloc.contains(target);
  70 
  71             for (Locale test: testloc) {
  72                 // codes
  73                 String lang = test.getLanguage();
  74                 String ctry = test.getCountry();
  75                 String vrnt = test.getVariant();
  76 
  77                 // the localized name
  78                 String langresult = test.getDisplayLanguage(target);
  79                 String ctryresult = test.getDisplayCountry(target);
  80                 String vrntresult = test.getDisplayVariant(target);
  81 
  82                 // provider's name (if any)
  83                 String providerslang = null;
  84                 String providersctry = null;
  85                 String providersvrnt = null;
  86                 if (providerloc.contains(target)) {
  87                     providerslang = lnp.getDisplayLanguage(lang, target);
  88                     providersctry = lnp.getDisplayCountry(ctry, target);
  89                     providersvrnt = lnp.getDisplayVariant(vrnt, target);
  90                 }
  91 
  92                 // JRE's name
  93                 String jreslang = null;
  94                 String jresctry = null;
  95                 String jresvrnt = null;
  96                 if (!lang.equals("")) {
  97                     try {
  98                         jreslang = rb.getString(lang);
  99                     } catch (MissingResourceException mre) {}
 100                 }
 101                 if (!ctry.equals("")) {
 102                     try {
 103                         jresctry = rb.getString(ctry);
 104                     } catch (MissingResourceException mre) {}
 105                 }
 106                 if (!vrnt.equals("")) {
 107                     try {
 108                         jresvrnt = rb.getString("%%"+vrnt);
 109                     } catch (MissingResourceException mre) {}
 110                 }
 111 
 112                 System.out.print("For key: "+lang+" ");
 113                 checkValidity(target, jreslang, providerslang, langresult,
 114                     jreSupportsTarget && jreslang != null);
 115                 System.out.print("For key: "+ctry+" ");
 116                 checkValidity(target, jresctry, providersctry, ctryresult,
 117                     jreSupportsTarget && jresctry != null);
 118                 System.out.print("For key: "+vrnt+" ");
 119                 checkValidity(target, jresvrnt, providersvrnt, vrntresult,
 120                     jreSupportsTarget && jresvrnt != null);
 121             }
 122         }
 123     }
 124 
 125     void variantFallbackTest() {
 126         Locale YY = new Locale("yy", "YY", "YYYY");
 127         Locale YY_suffix = new Locale("yy", "YY", "YYYY_suffix");
 128         String retVrnt = null;
 129         String message = "variantFallbackTest() succeeded.";
 130 
 131 
 132         try {
 133             YY.getDisplayVariant(YY_suffix);
 134             message = "variantFallbackTest() failed. Either provider wasn't invoked, or invoked without suffix.";
 135         } catch (RuntimeException re) {
 136             retVrnt = re.getMessage();
 137             if (YY_suffix.getVariant().equals(retVrnt)) {
 138                 System.out.println(message);
 139                 return;
 140             }
 141             message = "variantFallbackTest() failed. Returned variant: "+retVrnt;
 142         }
 143 
 144         throw new RuntimeException(message);
 145     }
 146 }
--- EOF ---