jdk/test/java/util/Formatter/FormatLocale.java
author naoto
Tue, 21 Jun 2016 15:15:05 -0700
changeset 39134 be0fe7a4190a
parent 38786 8e7b0ac05815
child 40797 4618571397a5
permissions -rw-r--r--
8159548: Formatter returns unexpected strings if locale is null. Reviewed-by: sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38786
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
     1
/*
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
     4
 *
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
     8
 *
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    13
 * accompanied this code).
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    14
 *
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    18
 *
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    21
 * questions.
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    22
 */
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    23
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    24
/**
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    25
 * @test
39134
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    26
 * @bug 8146156 8159548
38786
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    27
 * @summary test whether uppercasing follows Locale.Category.FORMAT locale.
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    28
 * @run main/othervm FormatLocale
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    29
 */
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    30
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    31
import java.time.LocalDate;
39134
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    32
import java.time.ZonedDateTime;
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    33
import java.time.ZoneId;
38786
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    34
import java.time.Month;
39134
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    35
import java.util.Calendar;
38786
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    36
import java.util.Formatter;
39134
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    37
import java.util.GregorianCalendar;
38786
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    38
import java.util.List;
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    39
import java.util.Locale;
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    40
import java.util.Locale.Category;
39134
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    41
import java.util.TimeZone;
38786
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    42
import java.util.stream.IntStream;
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    43
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    44
public class FormatLocale {
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    45
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    46
    static final Locale TURKISH = new Locale("tr");
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    47
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    48
    static final List<String> conversions = List.of(
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    49
        "%S",
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    50
        "%S",
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    51
        "%TB",
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    52
        "%G");
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    53
    static final List<Object> src = List.of(
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    54
        "Turkish",
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    55
        "Turkish",
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    56
        LocalDate.of(2016, Month.APRIL, 1),
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    57
        Float.valueOf(100_000_000));
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    58
    static final List<Locale> formatLocale = List.of(
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    59
        Locale.ENGLISH,
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    60
        TURKISH,
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    61
        TURKISH,
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    62
        Locale.FRANCE);
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    63
    static final List<String> expected = List.of(
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    64
        "TURKISH",
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    65
        "TURK\u0130SH",
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    66
        "N\u0130SAN",
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    67
        "1,00000E+08");
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    68
39134
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    69
    static void formatLocaleTest() {
38786
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    70
        StringBuilder sb = new StringBuilder();
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    71
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    72
        IntStream.range(0, src.size()).forEach(i -> {
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    73
            sb.setLength(0);
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    74
            Locale.setDefault(Locale.Category.FORMAT, formatLocale.get(i));
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    75
            new Formatter(sb).format(conversions.get(i), src.get(i));
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    76
            if (!sb.toString().equals(expected.get(i))) {
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    77
                throw new RuntimeException(
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    78
                    "Wrong uppercasing with Formatter.format(" +
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    79
                    "\"" + conversions.get(i) + "\"" +
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    80
                    ") in locale "
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    81
                    + formatLocale.get(i) +
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    82
                    ". Expected: " + expected.get(i) +
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    83
                    " Returned: " + sb.toString());
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    84
            }
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    85
        });
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
    86
    }
39134
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    87
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    88
    static void nullLocaleTest() {
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    89
        String fmt = "%1$ta %1$tA %1$th %1$tB %1tZ";
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    90
        String expected = "Fri Friday Jan January PST";
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    91
        StringBuilder sb = new StringBuilder();
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    92
        Locale orig = Locale.getDefault();
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    93
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    94
        try {
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    95
            Locale.setDefault(Locale.JAPAN);
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    96
            Formatter f = new Formatter(sb, (Locale)null);
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    97
            ZoneId zid = ZoneId.of("America/Los_Angeles");
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    98
            Calendar c = new GregorianCalendar(TimeZone.getTimeZone(zid), Locale.US);
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
    99
            c.set(2016, 0, 1, 0, 0, 0);
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   100
            f.format(fmt, c);
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   101
            if (!sb.toString().equals(expected)) {
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   102
                throw new RuntimeException(
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   103
                    "Localized text returned with null locale.\n" +
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   104
                    "    expected: " + expected + "\n" +
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   105
                    "    returned: " + sb.toString());
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   106
            }
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   107
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   108
            sb.setLength(0);
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   109
            ZonedDateTime zdt = ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, zid);
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   110
            f.format(fmt, zdt);
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   111
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   112
            if (!sb.toString().equals(expected)) {
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   113
                throw new RuntimeException(
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   114
                    "Localized text returned with null locale.\n" +
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   115
                    "    expected: " + expected + "\n" +
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   116
                    "    returned: " + sb.toString());
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   117
            }
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   118
        } finally {
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   119
            Locale.setDefault(orig);
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   120
        }
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   121
    }
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   122
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   123
    public static void main(String [] args) {
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   124
        formatLocaleTest();
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   125
        nullLocaleTest();
be0fe7a4190a 8159548: Formatter returns unexpected strings if locale is null.
naoto
parents: 38786
diff changeset
   126
    }
38786
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents:
diff changeset
   127
}