Path: blob/master/test/jdk/java/util/Locale/LocaleCategory.java
41149 views
/*1* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4700857 6997928 707948626* @summary tests for Locale.getDefault(Locale.Category) and27* Locale.setDefault(Locale.Category, Locale)28* @library /java/text/testlib29* @build TestUtils LocaleCategory30* @comment test user.xxx.display user.xxx.format properties31* @run main/othervm -Duser.language.display=ja32* -Duser.language.format=zh LocaleCategory33* @comment test user.xxx properties overriding user.xxx.display/format34* @run main/othervm -Duser.language=en35* -Duser.language.display=ja36* -Duser.language.format=zh LocaleCategory37*/3839import java.util.Locale;4041public class LocaleCategory {42private static Locale base = null;43private static Locale disp = null;44private static Locale fmt = null;4546public static void main(String[] args) {47Locale reservedLocale = Locale.getDefault();48if (TestUtils.hasSpecialVariant(reservedLocale)) {49System.out.println("Skipping this test because locale is " + reservedLocale);50return;51}5253try {54Locale.Builder builder = new Locale.Builder();55base = builder.setLanguage(System.getProperty("user.language", ""))56.setScript(System.getProperty("user.script", ""))57.setRegion(System.getProperty("user.country", ""))58.setVariant(System.getProperty("user.variant", "")).build();59disp = builder.setLanguage(60System.getProperty("user.language.display",61Locale.getDefault().getLanguage()))62.setScript(System.getProperty("user.script.display",63Locale.getDefault().getScript()))64.setRegion(System.getProperty("user.country.display",65Locale.getDefault().getCountry()))66.setVariant(System.getProperty("user.variant.display",67Locale.getDefault().getVariant())).build();68fmt = builder.setLanguage(System.getProperty("user.language.format",69Locale.getDefault().getLanguage()))70.setScript(System.getProperty("user.script.format",71Locale.getDefault().getScript()))72.setRegion(System.getProperty("user.country.format",73Locale.getDefault().getCountry()))74.setVariant(System.getProperty("user.variant.format",75Locale.getDefault().getVariant())).build();76checkDefault();77testGetSetDefault();78testBug7079486();79} finally {80// restore the reserved locale81Locale.setDefault(reservedLocale);82}83}8485private static void checkDefault() {86if (!base.equals(Locale.getDefault()) ||87!disp.equals(Locale.getDefault(Locale.Category.DISPLAY)) ||88!fmt.equals(Locale.getDefault(Locale.Category.FORMAT))) {89throw new RuntimeException("Some of the return values from "90+ "getDefault() do not agree with the locale derived "91+ "from \"user.xxxx\" system properties");92}93}9495private static void testGetSetDefault() {96try {97Locale.setDefault(null, null);98throw new RuntimeException("setDefault(null, null) should throw a "99+ "NullPointerException");100} catch (NullPointerException npe) {}101102Locale.setDefault(Locale.CHINA);103if (!Locale.CHINA.equals(Locale.getDefault(Locale.Category.DISPLAY)) ||104!Locale.CHINA.equals(Locale.getDefault(Locale.Category.FORMAT))) {105throw new RuntimeException("setDefault() should set all default "106+ "locales for all categories");107}108}109110private static void testBug7079486() {111Locale zh_Hans_CN = Locale.forLanguageTag("zh-Hans-CN");112113// make sure JRE has zh_Hans_CN localized string114if (zh_Hans_CN.getDisplayScript(Locale.US)115.equals(zh_Hans_CN.getDisplayScript(zh_Hans_CN))) {116return;117}118119Locale.setDefault(Locale.US);120String en_script = zh_Hans_CN.getDisplayScript();121122Locale.setDefault(Locale.Category.DISPLAY, zh_Hans_CN);123String zh_script = zh_Hans_CN.getDisplayScript();124125if (en_script.equals(zh_script)) {126throw new RuntimeException("Locale.getDisplayScript() (no args) "127+ "does not honor default DISPLAY locale");128}129}130}131132133134