1 /*
   2  * Copyright (c) 2017, 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 8190278
  26  * @summary checks the Scanner.useLocale() with java.locale.providers=SPI,
  27  *          COMPAT. It should not throw ClassCastException if any SPI is
  28  *          used and NumberFormat.getInstance() does not return a
  29  *          DecimalFormat object. Also, to test the behaviour of Scanner
  30  *          while scanning numbers in the format of Scanner's locale.
  31  * @modules jdk.localedata
  32  * @library provider
  33  * @build provider/module-info provider/test.NumberFormatProviderImpl
  34  *        provider/test.NumberFormatImpl
  35  * @run main/othervm -Djava.locale.providers=SPI,COMPAT UseLocaleWithProvider
  36  */
  37 
  38 import java.util.Locale;
  39 import java.util.Scanner;
  40 
  41 public class UseLocaleWithProvider {
  42 
  43     public static void main(String[] args) {
  44 
  45         try {
  46             testScannerUseLocale("-123.4", Locale.US, -123.4);
  47             testScannerUseLocale("-123,45", new Locale("fi", "FI"), -123.45);
  48             testScannerUseLocale("334,65", Locale.FRENCH, 334.65);
  49             testScannerUseLocale("4.334,65", Locale.GERMAN, 4334.65);
  50         } catch (ClassCastException ex) {
  51             throw new RuntimeException("[FAILED: With" +
  52                     " java.locale.providers=SPI,COMPAT, Scanner.useLocale()" +
  53                     " shouldn't throw ClassCastException]");
  54         }
  55     }
  56 
  57     private static void testScannerUseLocale(String number, Locale locale,
  58                                              Number actual) {
  59         Scanner sc = new Scanner(number).useLocale(locale);
  60         if (!sc.hasNextFloat() || sc.nextFloat() != actual.floatValue()) {
  61             throw new RuntimeException("[FAILED: With" +
  62                     " java.locale.providers=SPI,COMPAT, Scanner" +
  63                     ".hasNextFloat() or Scanner.nextFloat() is unable to" +
  64                     " scan the given number: " + number + ", in the given" +
  65                     " locale:" + locale + "]");
  66         }
  67     }
  68 }
  69