< prev index next >

test/jdk/java/util/Locale/LocaleProviders.java

Print this page
rev 59374 : [mq]: 8245241


   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 import java.text.*;
  24 import java.text.spi.*;
  25 import java.util.*;




  26 import java.util.spi.*;
  27 import java.util.stream.IntStream;
  28 import sun.util.locale.provider.LocaleProviderAdapter;
  29 


  30 public class LocaleProviders {
  31 
  32     private static final boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");
  33     private static final boolean IS_MAC = System.getProperty("os.name").startsWith("Mac");
  34 
  35     public static void main(String[] args) {
  36         String methodName = args[0];
  37 
  38         switch (methodName) {
  39             case "getPlatformLocale":
  40                 if (args[1].equals("format")) {
  41                     getPlatformLocale(Locale.Category.FORMAT);
  42                 } else {
  43                     getPlatformLocale(Locale.Category.DISPLAY);
  44                 }
  45                 break;
  46 
  47             case "adapterTest":
  48                 adapterTest(args[1], args[2], (args.length >= 4 ? args[3] : ""));
  49                 break;


  75             case "bug8027289Test":
  76                 bug8027289Test(args[1]);
  77                 break;
  78 
  79             case "bug8220227Test":
  80                 bug8220227Test();
  81                 break;
  82 
  83             case "bug8228465Test":
  84                 bug8228465Test();
  85                 break;
  86 
  87             case "bug8232871Test":
  88                 bug8232871Test();
  89                 break;
  90 
  91             case "bug8232860Test":
  92                 bug8232860Test();
  93                 break;
  94 




  95             default:
  96                 throw new RuntimeException("Test method '"+methodName+"' not found.");
  97         }
  98     }
  99 
 100     static void getPlatformLocale(Locale.Category cat) {
 101         Locale defloc = Locale.getDefault(cat);
 102         System.out.printf("%s,%s\n", defloc.getLanguage(), defloc.getCountry());
 103     }
 104 
 105     static void adapterTest(String expected, String lang, String ctry) {
 106         Locale testLocale = new Locale(lang, ctry);
 107         LocaleProviderAdapter ldaExpected =
 108             LocaleProviderAdapter.forType(LocaleProviderAdapter.Type.valueOf(expected));
 109         if (!ldaExpected.getDateFormatProvider().isSupportedLocale(testLocale)) {
 110             System.out.println("test locale: "+testLocale+" is not supported by the expected provider: "+ldaExpected+". Ignoring the test.");
 111             return;
 112         }
 113         String preference = System.getProperty("java.locale.providers", "");
 114         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, testLocale);


 210                             }
 211                         }
 212                     } else {
 213                         throw new RuntimeException("Windows Host" +
 214                             " LocaleProviderAdapter was not selected for" +
 215                             " English locale.");
 216                     }
 217                 }
 218             } catch (ParseException pe) {
 219                 throw new RuntimeException("Parsing Windows version failed: "+pe.toString());
 220             }
 221         }
 222     }
 223 
 224     static void bug8013086Test(String lang, String ctry) {
 225         try {
 226             // Throws a NullPointerException if the test fails.
 227             System.out.println(new SimpleDateFormat("z", new Locale(lang, ctry)).parse("UTC"));
 228         } catch (ParseException pe) {
 229             // ParseException is fine in this test, as it's not "UTC"
 230 }
 231     }
 232 
 233     static void bug8013903Test() {
 234         if (IS_WINDOWS) {
 235             Date sampleDate = new Date(0x10000000000L);
 236             String hostResult = "\u5e73\u6210 16.11.03 (Wed) AM 11:53:47";
 237             String jreResult = "\u5e73\u6210 16.11.03 (\u6c34) \u5348\u524d 11:53:47";
 238             Locale l = new Locale("ja", "JP", "JP");
 239             SimpleDateFormat sdf = new SimpleDateFormat("GGGG yyyy.MMM.dd '('E')' a hh:mm:ss", l);
 240             sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
 241             String result = sdf.format(sampleDate);
 242             System.out.println(result);
 243             if (LocaleProviderAdapter.getAdapterPreference()
 244                 .contains(LocaleProviderAdapter.Type.JRE)) {
 245                 if (!jreResult.equals(result)) {
 246                     throw new RuntimeException("Format failed. result: \"" +
 247                         result + "\", expected: \"" + jreResult);
 248                 }
 249             } else {
 250                 // Windows display names. Subject to change if Windows changes its format.


 357                             "input: " + input + ", expected: " +
 358                             nfExpected + ", result: " + result);
 359                     }
 360 
 361                     var ifExpected = ifExpectedList.get(i);
 362                     result = intf.format(input);
 363                     if (!result.equals(ifExpected)) {
 364                         throw new RuntimeException("Incorrect integer format. " +
 365                             "input: " + input + ", expected: " +
 366                             ifExpected + ", result: " + result);
 367                     }
 368                 });
 369             System.out.println("bug8232860Test succeeded.");
 370         } else {
 371             System.out.println("Test ignored. Either :-\n" +
 372                 "Default format locale is not Locale.US: " + defLoc + ", or\n" +
 373                 "OS is neither macOS/Windows, or\n" +
 374                 "provider is not HOST: " + type);
 375         }
 376     }




















 377 }


   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 import java.text.*;
  24 import java.text.spi.*;
  25 import java.util.*;
  26 import java.util.logging.Level;
  27 import java.util.logging.LogManager;
  28 import java.util.logging.LogRecord;
  29 import java.util.logging.StreamHandler;
  30 import java.util.spi.*;
  31 import java.util.stream.IntStream;
  32 import sun.util.locale.provider.LocaleProviderAdapter;
  33 
  34 import static java.util.logging.LogManager.*;
  35 
  36 public class LocaleProviders {
  37 
  38     private static final boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");
  39     private static final boolean IS_MAC = System.getProperty("os.name").startsWith("Mac");
  40 
  41     public static void main(String[] args) {
  42         String methodName = args[0];
  43 
  44         switch (methodName) {
  45             case "getPlatformLocale":
  46                 if (args[1].equals("format")) {
  47                     getPlatformLocale(Locale.Category.FORMAT);
  48                 } else {
  49                     getPlatformLocale(Locale.Category.DISPLAY);
  50                 }
  51                 break;
  52 
  53             case "adapterTest":
  54                 adapterTest(args[1], args[2], (args.length >= 4 ? args[3] : ""));
  55                 break;


  81             case "bug8027289Test":
  82                 bug8027289Test(args[1]);
  83                 break;
  84 
  85             case "bug8220227Test":
  86                 bug8220227Test();
  87                 break;
  88 
  89             case "bug8228465Test":
  90                 bug8228465Test();
  91                 break;
  92 
  93             case "bug8232871Test":
  94                 bug8232871Test();
  95                 break;
  96 
  97             case "bug8232860Test":
  98                 bug8232860Test();
  99                 break;
 100 
 101             case "bug8245241Test":
 102                 bug8245241Test(args[1]);
 103                 break;
 104 
 105             default:
 106                 throw new RuntimeException("Test method '"+methodName+"' not found.");
 107         }
 108     }
 109 
 110     static void getPlatformLocale(Locale.Category cat) {
 111         Locale defloc = Locale.getDefault(cat);
 112         System.out.printf("%s,%s\n", defloc.getLanguage(), defloc.getCountry());
 113     }
 114 
 115     static void adapterTest(String expected, String lang, String ctry) {
 116         Locale testLocale = new Locale(lang, ctry);
 117         LocaleProviderAdapter ldaExpected =
 118             LocaleProviderAdapter.forType(LocaleProviderAdapter.Type.valueOf(expected));
 119         if (!ldaExpected.getDateFormatProvider().isSupportedLocale(testLocale)) {
 120             System.out.println("test locale: "+testLocale+" is not supported by the expected provider: "+ldaExpected+". Ignoring the test.");
 121             return;
 122         }
 123         String preference = System.getProperty("java.locale.providers", "");
 124         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, testLocale);


 220                             }
 221                         }
 222                     } else {
 223                         throw new RuntimeException("Windows Host" +
 224                             " LocaleProviderAdapter was not selected for" +
 225                             " English locale.");
 226                     }
 227                 }
 228             } catch (ParseException pe) {
 229                 throw new RuntimeException("Parsing Windows version failed: "+pe.toString());
 230             }
 231         }
 232     }
 233 
 234     static void bug8013086Test(String lang, String ctry) {
 235         try {
 236             // Throws a NullPointerException if the test fails.
 237             System.out.println(new SimpleDateFormat("z", new Locale(lang, ctry)).parse("UTC"));
 238         } catch (ParseException pe) {
 239             // ParseException is fine in this test, as it's not "UTC"
 240         }
 241     }
 242 
 243     static void bug8013903Test() {
 244         if (IS_WINDOWS) {
 245             Date sampleDate = new Date(0x10000000000L);
 246             String hostResult = "\u5e73\u6210 16.11.03 (Wed) AM 11:53:47";
 247             String jreResult = "\u5e73\u6210 16.11.03 (\u6c34) \u5348\u524d 11:53:47";
 248             Locale l = new Locale("ja", "JP", "JP");
 249             SimpleDateFormat sdf = new SimpleDateFormat("GGGG yyyy.MMM.dd '('E')' a hh:mm:ss", l);
 250             sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
 251             String result = sdf.format(sampleDate);
 252             System.out.println(result);
 253             if (LocaleProviderAdapter.getAdapterPreference()
 254                 .contains(LocaleProviderAdapter.Type.JRE)) {
 255                 if (!jreResult.equals(result)) {
 256                     throw new RuntimeException("Format failed. result: \"" +
 257                         result + "\", expected: \"" + jreResult);
 258                 }
 259             } else {
 260                 // Windows display names. Subject to change if Windows changes its format.


 367                             "input: " + input + ", expected: " +
 368                             nfExpected + ", result: " + result);
 369                     }
 370 
 371                     var ifExpected = ifExpectedList.get(i);
 372                     result = intf.format(input);
 373                     if (!result.equals(ifExpected)) {
 374                         throw new RuntimeException("Incorrect integer format. " +
 375                             "input: " + input + ", expected: " +
 376                             ifExpected + ", result: " + result);
 377                     }
 378                 });
 379             System.out.println("bug8232860Test succeeded.");
 380         } else {
 381             System.out.println("Test ignored. Either :-\n" +
 382                 "Default format locale is not Locale.US: " + defLoc + ", or\n" +
 383                 "OS is neither macOS/Windows, or\n" +
 384                 "provider is not HOST: " + type);
 385         }
 386     }
 387 
 388     static void bug8245241Test(String expected) {
 389         LogRecord[] lra = new LogRecord[1];
 390         StreamHandler handler = new StreamHandler() {
 391             @Override
 392             public void publish(LogRecord record) {
 393                 lra[0] = record;
 394             }
 395         };
 396         getLogManager().getLogger("").addHandler(handler);
 397 
 398         DateFormat.getDateInstance(); // this will init LocaleProviderAdapter
 399         handler.flush();
 400 
 401         if (lra[0] == null ||
 402             lra[0].getLevel() != Level.INFO ||
 403             !lra[0].getMessage().equals(expected)) {
 404             throw new RuntimeException("Expected log was not emitted. LogRecord: " + lra[0]);
 405         }
 406     }
 407 }
< prev index next >