1 /*
   2  * Copyright (c) 2012, 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 import java.text.*;
  24 import java.text.spi.*;
  25 import java.util.*;
  26 import sun.util.locale.provider.LocaleProviderAdapter;
  27 
  28 public class LocaleProviders {
  29 
  30     public static void main(String[] args) {
  31         String methodName = args[0];
  32 
  33         switch (methodName) {
  34             case "getPlatformLocale":
  35                 if (args[1].equals("format")) {
  36                     getPlatformLocale(Locale.Category.FORMAT);
  37                 } else {
  38                     getPlatformLocale(Locale.Category.DISPLAY);
  39                 }
  40                 break;
  41 
  42             case "adapterTest":
  43                 adapterTest(args[1], args[2], (args.length >= 4 ? args[3] : ""));
  44                 break;
  45 
  46             case "bug7198834Test":
  47                 bug7198834Test();
  48                 break;
  49 
  50             default:
  51                 throw new RuntimeException("Test method '"+methodName+"' not found.");
  52         }
  53     }
  54 
  55     static void getPlatformLocale(Locale.Category cat) {
  56         Locale defloc = Locale.getDefault(cat);
  57         System.out.printf("%s,%s\n", defloc.getLanguage(), defloc.getCountry());
  58     }
  59 
  60     static void adapterTest(String expected, String lang, String ctry) {
  61         Locale testLocale = new Locale(lang, ctry);
  62         String preference = System.getProperty("java.locale.providers", "");
  63         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, testLocale);
  64         LocaleProviderAdapter.Type type = lda.getAdapterType();
  65         System.out.printf("testLocale: %s, got: %s, expected: %s\n", testLocale, type, expected);
  66         if (!type.toString().equals(expected)) {
  67             throw new RuntimeException("Returned locale data adapter is not correct.");
  68         }
  69     }
  70 
  71     static void bug7198834Test() {
  72         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, Locale.US);
  73         LocaleProviderAdapter.Type type = lda.getAdapterType();
  74         if (type == LocaleProviderAdapter.Type.HOST && System.getProperty("os.name").startsWith("Windows")) {
  75             DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
  76             String date = df.format(new Date());
  77             if (date.charAt(date.length()-1) == ' ') {
  78                 throw new RuntimeException("Windows Host Locale Provider returns a trailing space.");
  79             }
  80         } else {
  81             System.out.println("Windows HOST locale adapter not found. Ignoring this test.");
  82         }
  83     }
  84 }