jdk/test/java/util/Locale/LocaleCategory.java
author duke
Wed, 05 Jul 2017 22:06:37 +0200
changeset 40471 ad45133ff99e
parent 40455 db40a6f6f274
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6490
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
     1
/*
40455
db40a6f6f274 7094818: closed/java/text/Format/DateFormat tests failed on Hindi
peytoia
parents: 10339
diff changeset
     2
 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
6490
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
     4
 *
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
     7
 * published by the Free Software Foundation.
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
     8
 *
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    13
 * accompanied this code).
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    14
 *
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    18
 *
6495
3cec0a02688b 6981759: copyright header fix for test/java/util/Locale/LocaleCategory.java
naoto
parents: 6490
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3cec0a02688b 6981759: copyright header fix for test/java/util/Locale/LocaleCategory.java
naoto
parents: 6490
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3cec0a02688b 6981759: copyright header fix for test/java/util/Locale/LocaleCategory.java
naoto
parents: 6490
diff changeset
    21
 * questions.
6490
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    22
 */
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    23
import java.util.Locale;
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    24
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    25
public class LocaleCategory {
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    26
    private static Locale base = null;
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    27
    private static Locale disp = null;
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    28
    private static Locale fmt = null;
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    29
    private static String enc = null;
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    30
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    31
    public static void main(String[] args) {
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    32
        Locale reservedLocale = Locale.getDefault();
40455
db40a6f6f274 7094818: closed/java/text/Format/DateFormat tests failed on Hindi
peytoia
parents: 10339
diff changeset
    33
        if (TestUtils.hasSpecialVariant(reservedLocale)) {
db40a6f6f274 7094818: closed/java/text/Format/DateFormat tests failed on Hindi
peytoia
parents: 10339
diff changeset
    34
            System.out.println("Skipping this test because locale is " + reservedLocale);
db40a6f6f274 7094818: closed/java/text/Format/DateFormat tests failed on Hindi
peytoia
parents: 10339
diff changeset
    35
            return;
db40a6f6f274 7094818: closed/java/text/Format/DateFormat tests failed on Hindi
peytoia
parents: 10339
diff changeset
    36
        }
db40a6f6f274 7094818: closed/java/text/Format/DateFormat tests failed on Hindi
peytoia
parents: 10339
diff changeset
    37
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    38
        try {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    39
            Locale.Builder builder = new Locale.Builder();
7049
f6ad89ce23f7 6997928: LocaleCategory test fails with b118 PIT
naoto
parents: 6495
diff changeset
    40
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    41
            base = builder.setLanguage(System.getProperty("user.language", ""))
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    42
                  .setScript(System.getProperty("user.script", ""))
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    43
                  .setRegion(System.getProperty("user.country", ""))
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    44
                  .setVariant(System.getProperty("user.variant", "")).build();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    45
            disp = builder.setLanguage(
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    46
                    System.getProperty("user.language.display",
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    47
                                Locale.getDefault().getLanguage()))
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    48
                        .setScript(System.getProperty("user.script.display",
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    49
                                Locale.getDefault().getScript()))
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    50
                        .setRegion(System.getProperty("user.country.display",
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    51
                                Locale.getDefault().getCountry()))
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    52
                        .setVariant(System.getProperty("user.variant.display",
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    53
                                Locale.getDefault().getVariant())).build();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    54
            fmt = builder.setLanguage(System.getProperty("user.language.format",
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    55
                                Locale.getDefault().getLanguage()))
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    56
                       .setScript(System.getProperty("user.script.format",
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    57
                                Locale.getDefault().getScript()))
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    58
                       .setRegion(System.getProperty("user.country.format",
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    59
                                Locale.getDefault().getCountry()))
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    60
                       .setVariant(System.getProperty("user.variant.format",
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    61
                                  Locale.getDefault().getVariant())).build();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    62
            checkDefault();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    63
            testGetSetDefault();
10339
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
    64
            testBug7079486();
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    65
        } finally {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    66
            // restore the reserved locale
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    67
            Locale.setDefault(reservedLocale);
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 7049
diff changeset
    68
        }
6490
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    69
    }
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    70
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    71
    static void checkDefault() {
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    72
        if (!base.equals(Locale.getDefault()) ||
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    73
            !disp.equals(Locale.getDefault(Locale.Category.DISPLAY)) ||
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    74
            !fmt.equals(Locale.getDefault(Locale.Category.FORMAT))) {
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    75
            throw new RuntimeException("Some of the return values from getDefault() do not agree with the locale derived from \"user.xxxx\" system properties");
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    76
        }
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    77
    }
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    78
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    79
    static void testGetSetDefault() {
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    80
        try {
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    81
            Locale.setDefault(null, null);
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    82
            throw new RuntimeException("setDefault(null, null) should throw a NullPointerException");
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    83
        } catch (NullPointerException npe) {}
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    84
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    85
        Locale.setDefault(Locale.CHINA);
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    86
        if (!Locale.CHINA.equals(Locale.getDefault(Locale.Category.DISPLAY)) ||
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    87
            !Locale.CHINA.equals(Locale.getDefault(Locale.Category.FORMAT))) {
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    88
            throw new RuntimeException("setDefault() should set all default locales for all categories");
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    89
        }
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
    90
    }
10339
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
    91
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
    92
    static void testBug7079486() {
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
    93
        Locale zh_Hans_CN = Locale.forLanguageTag("zh-Hans-CN");
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
    94
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
    95
        // make sure JRE has zh_Hans_CN localized string
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
    96
        if (zh_Hans_CN.getDisplayScript(Locale.US).equals(zh_Hans_CN.getDisplayScript(zh_Hans_CN))) {
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
    97
            return;
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
    98
        }
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
    99
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
   100
        Locale.setDefault(Locale.US);
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
   101
        String en_script = zh_Hans_CN.getDisplayScript();
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
   102
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
   103
        Locale.setDefault(Locale.Category.DISPLAY, zh_Hans_CN);
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
   104
        String zh_script = zh_Hans_CN.getDisplayScript();
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
   105
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
   106
        if (en_script.equals(zh_script)) {
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
   107
            throw new RuntimeException("Locale.getDisplayScript() (no args) does not honor default DISPLAY locale");
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
   108
        }
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 10138
diff changeset
   109
    }
6490
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
   110
}
98def8a0da66 6981466: Adding missing test LocaleCategory.java
naoto
parents:
diff changeset
   111