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 8199672
  26  * @summary test the Formatter.format() method with java.locale.providers=SPI,
  27  *          COMPAT. It should not throw ClassCastException if an SPI is
  28  *          used and NumberFormat.getInstance() does not return a
  29  *          DecimalFormat object.
  30  * @modules jdk.localedata
  31  * @library provider
  32  * @build provider/module-info provider/test.NumberFormatProviderImpl
  33  * @run main/othervm -Djava.locale.providers=SPI,COMPAT FormatterWithProvider
  34  */
  35 
  36 import java.util.Formatter;
  37 import java.util.Locale;
  38 
  39 public class FormatterWithProvider {
  40 
  41     public static void main(String[] args) {
  42 
  43         Integer number = 1234567;
  44         String formatString = "%,d";
  45 
  46         try {
  47             testFormatter(Locale.JAPANESE, formatString, number);
  48             testFormatter(Locale.FRENCH, formatString, number);
  49             testFormatter(new Locale("hi", "IN"), formatString, number);
  50 
  51         } catch (ClassCastException ex) {
  52             throw new RuntimeException("[FAILED: A ClassCastException is" +
  53                     " thrown while using Formatter.format() with VM" +
  54                     " argument java.locale.providers=SPI,COMPAT]", ex);
  55         }
  56     }
  57 
  58     private static void testFormatter(Locale locale, String formatString,
  59                                       Integer number) {
  60 
  61         // test using String.format
  62         String.format(locale, formatString, number);
  63         // test using Formatter's format
  64         Formatter formatter = new Formatter(locale);
  65         formatter.format(formatString, number);
  66     }
  67 
  68 }
  69