test/jdk/java/util/Formatter/NoGroupingUsed.java
author nishjain
Thu, 22 Mar 2018 12:59:58 +0530
changeset 49277 cf9e3c8607b7
permissions -rw-r--r--
8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero). 8199672: ClassCastException is thrown by java.util.Formatter when an NumberFormatProvider SPI is used. Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49277
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
     1
/*
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
     4
 *
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    10
 *
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    15
 * accompanied this code).
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    16
 *
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    20
 *
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    23
 * questions.
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    24
 */
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    25
/*
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    26
 * @test
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    27
 * @bug 8196399
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    28
 * @summary test Formatter if any ArithmeticException is thrown while
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    29
 *          formatting a number in the locale which does not use any
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    30
 *          grouping, but specifies a grouping separator e.g. hy_AM.
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    31
 * @modules jdk.localedata
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    32
 */
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    33
import java.util.Formatter;
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    34
import java.util.Locale;
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    35
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    36
public class NoGroupingUsed {
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    37
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    38
    public static void main(String[] args) {
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    39
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    40
        Locale locale = new Locale("hy", "AM");
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    41
        String number = "1234567";
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    42
        String formatString = "%,d";
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    43
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    44
        try {
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    45
            testGrouping(locale, formatString, number);
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    46
        } catch (ArithmeticException ex) {
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    47
            throw new RuntimeException("[FAILED: ArithmeticException occurred"
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    48
                    + " while formatting the number: " + number + ", with"
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    49
                    + " format string: " + formatString + ", in locale: "
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    50
                    + locale, ex);
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    51
        }
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    52
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    53
    }
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    54
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    55
    private static void testGrouping(Locale locale, String formatString, String number) {
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    56
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    57
        // test using String.format
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    58
        String result = String.format(locale, formatString, Integer.parseInt(number));
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    59
        if (!number.equals(result)) {
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    60
            throw new RuntimeException("[FAILED: Incorrect formatting"
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    61
                    + " of number: " + number + " using String.format with format"
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    62
                    + " string: " + formatString + " in locale: " + locale
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    63
                    + ". Actual: " + result + ", Expected: " + number + "]");
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    64
        }
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    65
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    66
        // test using Formatter's format
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    67
        StringBuilder sb = new StringBuilder();
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    68
        Formatter formatter = new Formatter(sb, locale);
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    69
        formatter.format(formatString, Integer.parseInt(number));
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    70
        if (!number.equals(sb.toString())) {
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    71
            throw new RuntimeException("[FAILED: Incorrect formatting"
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    72
                    + " of number: " + number + "using Formatter.format with"
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    73
                    + " format string: " + formatString + " in locale: " + locale
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    74
                    + ". Actual: " + sb.toString() + ", Expected: " + number + "]");
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    75
        }
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    76
    }
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    77
cf9e3c8607b7 8196399: Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).
nishjain
parents:
diff changeset
    78
}