Path: blob/master/test/jdk/java/util/Scanner/spi/UseLocaleWithProvider.java
41154 views
/*1* Copyright (c) 2017, 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 819027825* @summary checks the Scanner.useLocale() with java.locale.providers=SPI,26* COMPAT. It should not throw ClassCastException if any SPI is27* used and NumberFormat.getInstance() does not return a28* DecimalFormat object. Also, to test the behaviour of Scanner29* while scanning numbers in the format of Scanner's locale.30* @modules jdk.localedata31* @library provider32* @build provider/module-info provider/test.NumberFormatProviderImpl33* provider/test.NumberFormatImpl34* @run main/othervm -Djava.locale.providers=SPI,COMPAT UseLocaleWithProvider35*/3637import java.util.Locale;38import java.util.Scanner;3940public class UseLocaleWithProvider {4142public static void main(String[] args) {4344try {45testScannerUseLocale("-123.4", Locale.US, -123.4);46testScannerUseLocale("-123,45", new Locale("fi", "FI"), -123.45);47testScannerUseLocale("334,65", Locale.FRENCH, 334.65);48testScannerUseLocale("4.334,65", Locale.GERMAN, 4334.65);49} catch (ClassCastException ex) {50throw new RuntimeException("[FAILED: With" +51" java.locale.providers=SPI,COMPAT, Scanner.useLocale()" +52" shouldn't throw ClassCastException]");53}54}5556private static void testScannerUseLocale(String number, Locale locale,57Number actual) {58Scanner sc = new Scanner(number).useLocale(locale);59if (!sc.hasNextFloat() || sc.nextFloat() != actual.floatValue()) {60throw new RuntimeException("[FAILED: With" +61" java.locale.providers=SPI,COMPAT, Scanner" +62".hasNextFloat() or Scanner.nextFloat() is unable to" +63" scan the given number: " + number + ", in the given" +64" locale:" + locale + "]");65}66}67}68697071