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