Path: blob/master/test/jdk/java/text/Format/CompactNumberFormat/TestMutatingInstance.java
41153 views
/*1* Copyright (c) 2018, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24/*25* @test26* @bug 817755227* @summary Checks the functioning of compact number format by changing the28* formatting parameters. For example, min fraction digits, grouping29* size etc.30* @modules jdk.localedata31* @run testng/othervm TestMutatingInstance32*/33import java.math.BigDecimal;34import java.math.BigInteger;35import java.text.CompactNumberFormat;36import java.text.DecimalFormatSymbols;37import java.text.NumberFormat;38import java.text.ParseException;39import java.util.Locale;40import org.testng.annotations.BeforeTest;41import org.testng.annotations.DataProvider;42import org.testng.annotations.Test;4344public class TestMutatingInstance {4546private static final NumberFormat FORMAT_FRACTION = NumberFormat47.getCompactNumberInstance(new Locale("en"), NumberFormat.Style.LONG);4849private static final CompactNumberFormat FORMAT_GROUPING = (CompactNumberFormat) NumberFormat50.getCompactNumberInstance(new Locale("en"), NumberFormat.Style.LONG);5152private static final NumberFormat FORMAT_MININTEGER = NumberFormat53.getCompactNumberInstance(new Locale("en"), NumberFormat.Style.LONG);5455private static final NumberFormat FORMAT_PARSEINTONLY = NumberFormat56.getCompactNumberInstance(new Locale("en"), NumberFormat.Style.LONG);5758// No compact patterns are specified for this instance except at index 4.59// This is to test how the behaviour differs between compact number formatting60// and general number formatting61private static final NumberFormat FORMAT_NO_PATTERNS = new CompactNumberFormat(62"#,##0.0#", DecimalFormatSymbols.getInstance(Locale.US),63new String[]{"", "", "", "", "00K", "", "", "", "", "", "", "", "", "", ""});6465@BeforeTest66public void mutateInstances() {67FORMAT_FRACTION.setMinimumFractionDigits(2);68FORMAT_GROUPING.setGroupingSize(3);69FORMAT_GROUPING.setGroupingUsed(true);70FORMAT_MININTEGER.setMinimumIntegerDigits(5);71FORMAT_PARSEINTONLY.setParseIntegerOnly(true);72FORMAT_PARSEINTONLY.setGroupingUsed(true);73// Setting min fraction digits and other fields does not effect74// the general number formatting behaviour, when no compact number75// patterns are specified76FORMAT_NO_PATTERNS.setMinimumFractionDigits(2);77}7879@DataProvider(name = "format")80Object[][] compactFormatData() {81return new Object[][]{82{FORMAT_FRACTION, 1900, "1.90 thousand"},83{FORMAT_FRACTION, 1000, "1.00 thousand"},84{FORMAT_FRACTION, 9090.99, "9.09 thousand"},85{FORMAT_FRACTION, new BigDecimal(12346567890987654.32),86"12346.57 trillion"},87{FORMAT_FRACTION, new BigInteger("12346567890987654"),88"12346.57 trillion"},89{FORMAT_GROUPING, new BigDecimal(12346567890987654.32),90"12,347 trillion"},91{FORMAT_GROUPING, 100000, "100 thousand"},92{FORMAT_MININTEGER, 10000, "00010 thousand"},93{FORMAT_NO_PATTERNS, 100000, "100,000"},94{FORMAT_NO_PATTERNS, 1000.998, "1,001"},95{FORMAT_NO_PATTERNS, 10900, "10.90K"},96{FORMAT_NO_PATTERNS, new BigDecimal(12346567890987654.32), "12,346,567,890,987,654"},};97}9899@DataProvider(name = "parse")100Object[][] compactParseData() {101return new Object[][]{102{FORMAT_FRACTION, "190 thousand", 190000L},103{FORMAT_FRACTION, "19.9 thousand", 19900L},104{FORMAT_GROUPING, "12,346 thousand", 12346000L},105{FORMAT_PARSEINTONLY, "12345 thousand", 12345000L},106{FORMAT_PARSEINTONLY, "12,345 thousand", 12345000L},107{FORMAT_PARSEINTONLY, "12.345 thousand", 12000L},};108}109110@Test(dataProvider = "format")111public void formatCompactNumber(NumberFormat nf,112Object number, String expected) {113CompactFormatAndParseHelper.testFormat(nf, number, expected);114}115116@Test(dataProvider = "parse")117public void parseCompactNumber(NumberFormat nf,118String parseString, Number expected) throws ParseException {119CompactFormatAndParseHelper.testParse(nf, parseString, expected, null, null);120}121122}123124125