Path: blob/master/test/jdk/java/text/Format/CompactNumberFormat/SPIProviderTest.java
41153 views
/*1* Copyright (c) 2019, 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 822275625* @summary Checks the plurals work with SPI provider26* @modules jdk.localedata27* @library provider28* @build provider/module-info provider/test.NumberFormatProviderImpl29* @run main/othervm -Djava.locale.providers=SPI,CLDR SPIProviderTest30*/3132import java.text.CompactNumberFormat;33import java.text.DecimalFormatSymbols;34import java.text.NumberFormat;35import java.text.ParseException;36import java.util.Arrays;37import java.util.Locale;3839public class SPIProviderTest {40private static final Locale QAA = Locale.forLanguageTag("qaa");41private static final Locale QAB = Locale.forLanguageTag("qab");4243public static void main(String... args) {44new SPIProviderTest();45}4647SPIProviderTest() {48Arrays.stream(testData())49.forEach(SPIProviderTest::testSPIProvider);50}5152Object[][] testData() {53return new Object[][]{54// Locale, Number, expected55{QAA, 1_000, "1K"},56{QAA, -1_000, "-1K"},57{QAA, 2_000, "2K"},58{QAA, -2_000, "-2K"},59{QAA, 1_000_000, "1M"},60{QAA, -1_000_000, "-1M"},61{QAA, 2_000_000, "2M"},62{QAA, -2_000_000, "-2M"},6364{QAB, 1_000, "1K"},65{QAB, -1_000, "(1K)"},66{QAB, 2_000, "2KK"},67{QAB, -2_000, "-2KK"},68{QAB, 3_000, "3KKK"},69{QAB, -3_000, "-3KKK"},70{QAB, 5_000, "5KKKK"},71{QAB, -5_000, "-5KKKK"},7273{QAB, 10_000, "10000"},74{QAB, -10_000, "-10000"},7576{QAB, 1_000_000, "1 M"},77{QAB, -1_000_000, "(1 M)"},78{QAB, 2_000_000, "2 MM"},79{QAB, -2_000_000, "(2 MM)"},80{QAB, 3_000_000, "3 MMM"},81{QAB, -3_000_000, "-3 MMM"},82{QAB, 5_000_000, "5 MMMM"},83{QAB, -5_000_000, "-5 MMMM"},8485};86}8788public static void testSPIProvider(Object... args) {89Locale loc = (Locale)args[0];90Number number = (Number)args[1];91String expected = (String)args[2];92System.out.printf("Testing locale: %s, number: %d, expected: %s\n", loc, number, expected);9394NumberFormat nf =95NumberFormat.getCompactNumberInstance(loc, NumberFormat.Style.SHORT);96String formatted = nf.format(number);97System.out.printf(" formatted: %s\n", formatted);98if (!formatted.equals(expected)) {99throw new RuntimeException("formatted and expected strings do not match.");100}101102try {103Number parsed = nf.parse(formatted);104System.out.printf(" parsed: %s\n", parsed);105if (parsed.intValue() != number.intValue()) {106throw new RuntimeException("parsed and input numbers do not match.");107}108} catch (ParseException pe) {109throw new RuntimeException(pe);110}111}112}113114115