Path: blob/master/test/jdk/java/util/Formatter/spi/NoGroupingUsed.java
41153 views
/*1* Copyright (c) 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*/22/*23* @test24* @bug 8196399 820253725* @summary test Formatter if any ArithmeticException is thrown while26* formatting a number in the locale which does not use any27* grouping, but specifies a grouping separator.28* @library provider29* @build provider/module-info provider/test.NumberFormatProviderImpl30* @run main/othervm -Djava.locale.providers=SPI,COMPAT NoGroupingUsed31*/3233import java.util.Formatter;34import java.util.Locale;3536public class NoGroupingUsed {3738public static void main(String[] args) {39Locale locale = new Locale("xx", "YY");40String number = "1234567";41String formatString = "%,d";4243try {44testGrouping(locale, formatString, number);45} catch (ArithmeticException ex) {46throw new RuntimeException("[FAILED: ArithmeticException occurred"47+ " while formatting the number: " + number + ", with"48+ " format string: " + formatString + ", in locale: "49+ locale, ex);50}51}5253private static void testGrouping(Locale locale, String formatString, String number) {54// test using String.format55String result = String.format(locale, formatString, Integer.parseInt(number));56if (!number.equals(result)) {57throw new RuntimeException("[FAILED: Incorrect formatting"58+ " of number: " + number + " using String.format with format"59+ " string: " + formatString + " in locale: " + locale60+ ". Actual: " + result + ", Expected: " + number + "]");61}6263// test using Formatter's format64StringBuilder sb = new StringBuilder();65Formatter formatter = new Formatter(sb, locale);66formatter.format(formatString, Integer.parseInt(number));67if (!number.equals(sb.toString())) {68throw new RuntimeException("[FAILED: Incorrect formatting"69+ " of number: " + number + "using Formatter.format with"70+ " format string: " + formatString + " in locale: " + locale71+ ". Actual: " + sb.toString() + ", Expected: " + number + "]");72}73}74}757677