< prev index next >

test/jdk/java/util/Locale/LocaleCategory.java

Print this page


   1 /*
   2  * Copyright (c) 2010, 2016, 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.util.Locale;
  24 
  25 public class LocaleCategory {
  26     private static Locale base = null;
  27     private static Locale disp = null;
  28     private static Locale fmt = null;
  29     private static String enc = null;
  30 
  31     public static void main(String[] args) {
  32         Locale reservedLocale = Locale.getDefault();
  33         if (TestUtils.hasSpecialVariant(reservedLocale)) {
  34             System.out.println("Skipping this test because locale is " + reservedLocale);
  35             return;
  36         }
  37 
  38         try {
  39             Locale.Builder builder = new Locale.Builder();
  40 
  41             base = builder.setLanguage(System.getProperty("user.language", ""))
  42                   .setScript(System.getProperty("user.script", ""))
  43                   .setRegion(System.getProperty("user.country", ""))
  44                   .setVariant(System.getProperty("user.variant", "")).build();
  45             disp = builder.setLanguage(
  46                     System.getProperty("user.language.display",
  47                                 Locale.getDefault().getLanguage()))
  48                         .setScript(System.getProperty("user.script.display",
  49                                 Locale.getDefault().getScript()))
  50                         .setRegion(System.getProperty("user.country.display",
  51                                 Locale.getDefault().getCountry()))
  52                         .setVariant(System.getProperty("user.variant.display",
  53                                 Locale.getDefault().getVariant())).build();
  54             fmt = builder.setLanguage(System.getProperty("user.language.format",
  55                                 Locale.getDefault().getLanguage()))
  56                        .setScript(System.getProperty("user.script.format",
  57                                 Locale.getDefault().getScript()))
  58                        .setRegion(System.getProperty("user.country.format",
  59                                 Locale.getDefault().getCountry()))
  60                        .setVariant(System.getProperty("user.variant.format",
  61                                   Locale.getDefault().getVariant())).build();
  62             checkDefault();
  63             testGetSetDefault();
  64             testBug7079486();
  65         } finally {
  66             // restore the reserved locale
  67             Locale.setDefault(reservedLocale);
  68         }
  69     }
  70 
  71     static void checkDefault() {
  72         if (!base.equals(Locale.getDefault()) ||
  73             !disp.equals(Locale.getDefault(Locale.Category.DISPLAY)) ||
  74             !fmt.equals(Locale.getDefault(Locale.Category.FORMAT))) {
  75             throw new RuntimeException("Some of the return values from getDefault() do not agree with the locale derived from \"user.xxxx\" system properties");


  76         }
  77     }
  78 
  79     static void testGetSetDefault() {
  80         try {
  81             Locale.setDefault(null, null);
  82             throw new RuntimeException("setDefault(null, null) should throw a NullPointerException");

  83         } catch (NullPointerException npe) {}
  84 
  85         Locale.setDefault(Locale.CHINA);
  86         if (!Locale.CHINA.equals(Locale.getDefault(Locale.Category.DISPLAY)) ||
  87             !Locale.CHINA.equals(Locale.getDefault(Locale.Category.FORMAT))) {
  88             throw new RuntimeException("setDefault() should set all default locales for all categories");

  89         }
  90     }
  91 
  92     static void testBug7079486() {
  93         Locale zh_Hans_CN = Locale.forLanguageTag("zh-Hans-CN");
  94 
  95         // make sure JRE has zh_Hans_CN localized string
  96         if (zh_Hans_CN.getDisplayScript(Locale.US).equals(zh_Hans_CN.getDisplayScript(zh_Hans_CN))) {

  97             return;
  98         }
  99 
 100         Locale.setDefault(Locale.US);
 101         String en_script = zh_Hans_CN.getDisplayScript();
 102 
 103         Locale.setDefault(Locale.Category.DISPLAY, zh_Hans_CN);
 104         String zh_script = zh_Hans_CN.getDisplayScript();
 105 
 106         if (en_script.equals(zh_script)) {
 107             throw new RuntimeException("Locale.getDisplayScript() (no args) does not honor default DISPLAY locale");

 108         }
 109     }
 110 }
 111 
   1 /*
   2  * Copyright (c) 2010, 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 4700857 6997928 7079486
  27  * @summary tests for Locale.getDefault(Locale.Category) and
  28  * Locale.setDefault(Locale.Category, Locale)
  29  * @library /java/text/testlib
  30  * @build TestUtils LocaleCategory
  31  * @comment test user.xxx.display user.xxx.format properties
  32  * @run main/othervm -Duser.language.display=ja
  33  *                   -Duser.language.format=zh LocaleCategory
  34  * @comment test user.xxx properties overriding user.xxx.display/format
  35  * @run main/othervm -Duser.language=en
  36  *                   -Duser.language.display=ja
  37  *                   -Duser.language.format=zh LocaleCategory
  38  */
  39 
  40 import java.util.Locale;
  41 
  42 public class LocaleCategory {
  43     private static Locale base = null;
  44     private static Locale disp = null;
  45     private static Locale fmt = null;

  46 
  47     public static void main(String[] args) {
  48         Locale reservedLocale = Locale.getDefault();
  49         if (TestUtils.hasSpecialVariant(reservedLocale)) {
  50             System.out.println("Skipping this test because locale is " + reservedLocale);
  51             return;
  52         }
  53 
  54         try {
  55             Locale.Builder builder = new Locale.Builder();

  56             base = builder.setLanguage(System.getProperty("user.language", ""))
  57                   .setScript(System.getProperty("user.script", ""))
  58                   .setRegion(System.getProperty("user.country", ""))
  59                   .setVariant(System.getProperty("user.variant", "")).build();
  60             disp = builder.setLanguage(
  61                     System.getProperty("user.language.display",
  62                                 Locale.getDefault().getLanguage()))
  63                         .setScript(System.getProperty("user.script.display",
  64                                 Locale.getDefault().getScript()))
  65                         .setRegion(System.getProperty("user.country.display",
  66                                 Locale.getDefault().getCountry()))
  67                         .setVariant(System.getProperty("user.variant.display",
  68                                 Locale.getDefault().getVariant())).build();
  69             fmt = builder.setLanguage(System.getProperty("user.language.format",
  70                                 Locale.getDefault().getLanguage()))
  71                        .setScript(System.getProperty("user.script.format",
  72                                 Locale.getDefault().getScript()))
  73                        .setRegion(System.getProperty("user.country.format",
  74                                 Locale.getDefault().getCountry()))
  75                        .setVariant(System.getProperty("user.variant.format",
  76                                   Locale.getDefault().getVariant())).build();
  77             checkDefault();
  78             testGetSetDefault();
  79             testBug7079486();
  80         } finally {
  81             // restore the reserved locale
  82             Locale.setDefault(reservedLocale);
  83         }
  84     }
  85 
  86     private static void checkDefault() {
  87         if (!base.equals(Locale.getDefault()) ||
  88             !disp.equals(Locale.getDefault(Locale.Category.DISPLAY)) ||
  89             !fmt.equals(Locale.getDefault(Locale.Category.FORMAT))) {
  90             throw new RuntimeException("Some of the return values from "
  91                     + "getDefault() do not agree with the locale derived "
  92                     + "from \"user.xxxx\" system properties");
  93         }
  94     }
  95 
  96     private static void testGetSetDefault() {
  97         try {
  98             Locale.setDefault(null, null);
  99             throw new RuntimeException("setDefault(null, null) should throw a "
 100                     + "NullPointerException");
 101         } catch (NullPointerException npe) {}
 102 
 103         Locale.setDefault(Locale.CHINA);
 104         if (!Locale.CHINA.equals(Locale.getDefault(Locale.Category.DISPLAY)) ||
 105             !Locale.CHINA.equals(Locale.getDefault(Locale.Category.FORMAT))) {
 106             throw new RuntimeException("setDefault() should set all default "
 107                     + "locales for all categories");
 108         }
 109     }
 110 
 111     private static void testBug7079486() {
 112         Locale zh_Hans_CN = Locale.forLanguageTag("zh-Hans-CN");
 113 
 114         // make sure JRE has zh_Hans_CN localized string
 115         if (zh_Hans_CN.getDisplayScript(Locale.US)
 116                 .equals(zh_Hans_CN.getDisplayScript(zh_Hans_CN))) {
 117             return;
 118         }
 119 
 120         Locale.setDefault(Locale.US);
 121         String en_script = zh_Hans_CN.getDisplayScript();
 122 
 123         Locale.setDefault(Locale.Category.DISPLAY, zh_Hans_CN);
 124         String zh_script = zh_Hans_CN.getDisplayScript();
 125 
 126         if (en_script.equals(zh_script)) {
 127             throw new RuntimeException("Locale.getDisplayScript() (no args) "
 128                     + "does not honor default DISPLAY locale");
 129         }
 130     }
 131 }
 132 
< prev index next >