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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 /*
  26  * @test
  27  * @bug 8177552
  28  * @summary Checks the functioning of compact number format by changing the
  29  *          formatting parameters. For example, min fraction digits, grouping
  30  *          size etc.
  31  * @modules jdk.localedata
  32  * @run testng/othervm TestMutatingInstance
  33  */
  34 import java.math.BigDecimal;
  35 import java.math.BigInteger;
  36 import java.text.CompactNumberFormat;
  37 import java.text.DecimalFormatSymbols;
  38 import java.text.NumberFormat;
  39 import java.text.ParseException;
  40 import java.util.Locale;
  41 import org.testng.annotations.BeforeTest;
  42 import org.testng.annotations.DataProvider;
  43 import org.testng.annotations.Test;
  44 
  45 public class TestMutatingInstance {
  46 
  47     private static final NumberFormat FORMAT_FRACTION = NumberFormat
  48             .getCompactNumberInstance(new Locale("en"), NumberFormat.Style.LONG);
  49 
  50     private static final CompactNumberFormat FORMAT_GROUPING = (CompactNumberFormat) NumberFormat
  51             .getCompactNumberInstance(new Locale("en"), NumberFormat.Style.LONG);
  52 
  53     private static final NumberFormat FORMAT_MININTEGER = NumberFormat
  54             .getCompactNumberInstance(new Locale("en"), NumberFormat.Style.LONG);
  55 
  56     private static final NumberFormat FORMAT_PARSEINTONLY = NumberFormat
  57             .getCompactNumberInstance(new Locale("en"), NumberFormat.Style.LONG);
  58 
  59     // No compact patterns are specified for this instance except at index 4.
  60     // This is to test how the behaviour differs between compact number formatting
  61     // and general number formatting
  62     private static final NumberFormat FORMAT_NO_PATTERNS = new CompactNumberFormat(
  63             "#,##0.0#", DecimalFormatSymbols.getInstance(Locale.US),
  64             new String[]{"", "", "", "", "00K", "", "", "", "", "", "", "", "", "", ""});
  65 
  66     @BeforeTest
  67     public void mutateInstances() {
  68         FORMAT_FRACTION.setMinimumFractionDigits(2);
  69         FORMAT_GROUPING.setGroupingSize(3);
  70         FORMAT_GROUPING.setGroupingUsed(true);
  71         FORMAT_MININTEGER.setMinimumIntegerDigits(5);
  72         FORMAT_PARSEINTONLY.setParseIntegerOnly(true);
  73         FORMAT_PARSEINTONLY.setGroupingUsed(true);
  74         // Setting min fraction digits and other fields does not effect
  75         // the general number formatting behaviour, when no compact number
  76         // patterns are specified
  77         FORMAT_NO_PATTERNS.setMinimumFractionDigits(2);
  78     }
  79 
  80     @DataProvider(name = "format")
  81     Object[][] compactFormatData() {
  82         return new Object[][]{
  83             {FORMAT_FRACTION, 1900, "1.90 thousand"},
  84             {FORMAT_FRACTION, 1000, "1.00 thousand"},
  85             {FORMAT_FRACTION, 9090.99, "9.09 thousand"},
  86             {FORMAT_FRACTION, new BigDecimal(12346567890987654.32),
  87                 "12346.57 trillion"},
  88             {FORMAT_FRACTION, new BigInteger("12346567890987654"),
  89                 "12346.57 trillion"},
  90             {FORMAT_GROUPING, new BigDecimal(12346567890987654.32),
  91                 "12,347 trillion"},
  92             {FORMAT_GROUPING, 100000, "100 thousand"},
  93             {FORMAT_MININTEGER, 10000, "00010 thousand"},
  94             {FORMAT_NO_PATTERNS, 100000, "100,000"},
  95             {FORMAT_NO_PATTERNS, 1000.998, "1,001"},
  96             {FORMAT_NO_PATTERNS, 10900, "10.90K"},
  97             {FORMAT_NO_PATTERNS, new BigDecimal(12346567890987654.32), "12,346,567,890,987,654"},};
  98     }
  99 
 100     @DataProvider(name = "parse")
 101     Object[][] compactParseData() {
 102         return new Object[][]{
 103             {FORMAT_FRACTION, "190 thousand", 190000L},
 104             {FORMAT_FRACTION, "19.9 thousand", 19900L},
 105             {FORMAT_GROUPING, "12,346 thousand", 12346000L},
 106             {FORMAT_PARSEINTONLY, "12345 thousand", 12345000L},
 107             {FORMAT_PARSEINTONLY, "12,345 thousand", 12345000L},
 108             {FORMAT_PARSEINTONLY, "12.345 thousand", 12000L},};
 109     }
 110 
 111     @Test(dataProvider = "format")
 112     public void formatCompactNumber(NumberFormat nf,
 113             Object number, String expected) {
 114         CompactFormatAndParseHelper.testFormat(nf, number, expected);
 115     }
 116 
 117     @Test(dataProvider = "parse")
 118     public void parseCompactNumber(NumberFormat nf,
 119             String parseString, Number expected) throws ParseException {
 120         CompactFormatAndParseHelper.testParse(nf, parseString, expected, null, null);
 121     }
 122 
 123 }