jdk/test/java/util/PluggableLocale/LocaleNameProviderTest.java
changeset 14185 916ec0a4d039
parent 13583 dc0017b1a452
child 15260 7af2d7a87806
equal deleted inserted replaced
14184:5553422ece67 14185:916ec0a4d039
    34     public static void main(String[] s) {
    34     public static void main(String[] s) {
    35         new LocaleNameProviderTest();
    35         new LocaleNameProviderTest();
    36     }
    36     }
    37 
    37 
    38     LocaleNameProviderTest() {
    38     LocaleNameProviderTest() {
       
    39         checkAvailLocValidityTest();
       
    40         variantFallbackTest();
       
    41     }
       
    42 
       
    43     void checkAvailLocValidityTest() {
    39         com.bar.LocaleNameProviderImpl lnp = new com.bar.LocaleNameProviderImpl();
    44         com.bar.LocaleNameProviderImpl lnp = new com.bar.LocaleNameProviderImpl();
    40         Locale[] availloc = Locale.getAvailableLocales();
    45         Locale[] availloc = Locale.getAvailableLocales();
    41         Locale[] testloc = availloc.clone();
    46         Locale[] testloc = availloc.clone();
       
    47         List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getLocaleNameProvider().getAvailableLocales());
    42         List<Locale> providerloc = Arrays.asList(lnp.getAvailableLocales());
    48         List<Locale> providerloc = Arrays.asList(lnp.getAvailableLocales());
    43 
    49 
    44         for (Locale target: availloc) {
    50         for (Locale target: availloc) {
    45             // pure JRE implementation
    51             // pure JRE implementation
    46             OpenListResourceBundle rb = LocaleProviderAdapter.forJRE().getLocaleData().getLocaleNames(target);
    52             OpenListResourceBundle rb = LocaleProviderAdapter.forJRE().getLocaleData().getLocaleNames(target);
    47             boolean jreHasBundle = rb.getLocale().equals(target);
    53             boolean jreSupportsTarget = jreimplloc.contains(target);
    48 
    54 
    49             for (Locale test: testloc) {
    55             for (Locale test: testloc) {
    50                 // codes
    56                 // codes
    51                 String lang = test.getLanguage();
    57                 String lang = test.getLanguage();
    52                 String ctry = test.getCountry();
    58                 String ctry = test.getCountry();
    65                     providerslang = lnp.getDisplayLanguage(lang, target);
    71                     providerslang = lnp.getDisplayLanguage(lang, target);
    66                     providersctry = lnp.getDisplayCountry(ctry, target);
    72                     providersctry = lnp.getDisplayCountry(ctry, target);
    67                     providersvrnt = lnp.getDisplayVariant(vrnt, target);
    73                     providersvrnt = lnp.getDisplayVariant(vrnt, target);
    68                 }
    74                 }
    69 
    75 
    70                 // JRE's name (if any)
    76                 // JRE's name
    71                 String jreslang = null;
    77                 String jreslang = null;
    72                 String jresctry = null;
    78                 String jresctry = null;
    73                 String jresvrnt = null;
    79                 String jresvrnt = null;
    74                 if (!lang.equals("")) {
    80                 if (!lang.equals("")) {
    75                     try {
    81                     try {
    82                     } catch (MissingResourceException mre) {}
    88                     } catch (MissingResourceException mre) {}
    83                 }
    89                 }
    84                 if (!vrnt.equals("")) {
    90                 if (!vrnt.equals("")) {
    85                     try {
    91                     try {
    86                         jresvrnt = rb.getString("%%"+vrnt);
    92                         jresvrnt = rb.getString("%%"+vrnt);
    87                     } catch (MissingResourceException mre) {
    93                     } catch (MissingResourceException mre) {}
    88                         jresvrnt = vrnt;
       
    89                     }
       
    90                 }
    94                 }
    91 
    95 
       
    96                 System.out.print("For key: "+lang+" ");
    92                 checkValidity(target, jreslang, providerslang, langresult,
    97                 checkValidity(target, jreslang, providerslang, langresult,
    93                     jreHasBundle && rb.handleGetKeys().contains(lang));
    98                     jreSupportsTarget && jreslang != null);
       
    99                 System.out.print("For key: "+ctry+" ");
    94                 checkValidity(target, jresctry, providersctry, ctryresult,
   100                 checkValidity(target, jresctry, providersctry, ctryresult,
    95                     jreHasBundle && rb.handleGetKeys().contains(ctry));
   101                     jreSupportsTarget && jresctry != null);
       
   102                 System.out.print("For key: "+vrnt+" ");
    96                 checkValidity(target, jresvrnt, providersvrnt, vrntresult,
   103                 checkValidity(target, jresvrnt, providersvrnt, vrntresult,
    97                     jreHasBundle && rb.handleGetKeys().contains("%%"+vrnt));
   104                     jreSupportsTarget && jresvrnt != null);
    98             }
   105             }
    99         }
   106         }
   100     }
   107     }
       
   108 
       
   109     void variantFallbackTest() {
       
   110         Locale YY = new Locale("yy", "YY", "YYYY");
       
   111         Locale YY_suffix = new Locale("yy", "YY", "YYYY_suffix");
       
   112         String retVrnt = null;
       
   113         String message = "variantFallbackTest() succeeded.";
       
   114 
       
   115 
       
   116         try {
       
   117             YY.getDisplayVariant(YY_suffix);
       
   118             message = "variantFallbackTest() failed. Either provider wasn't invoked, or invoked without suffix.";
       
   119         } catch (RuntimeException re) {
       
   120             retVrnt = re.getMessage();
       
   121             if (YY_suffix.getVariant().equals(retVrnt)) {
       
   122                 System.out.println(message);
       
   123                 return;
   101 }
   124 }
       
   125             message = "variantFallbackTest() failed. Returned variant: "+retVrnt;
       
   126         }
       
   127 
       
   128         throw new RuntimeException(message);
       
   129     }
       
   130 }