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