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.
   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 8177552
  26  * @summary Checks the compact number format with COMPAT provider. Since the
  27  *          compact number resources are only provided by CLDR, using COMPAT
  28  *          as a provider should always use the default patterns added in the
  29  *          FormatData.java resource bundle
  30  * @modules jdk.localedata
  31  * @run testng/othervm -Djava.locale.providers=COMPAT TestWithCompatProvider
  32  */
  33 import java.math.BigDecimal;
  34 import java.math.BigInteger;
  35 import java.text.NumberFormat;
  36 import java.text.ParseException;
  37 import java.util.Locale;
  38 import org.testng.annotations.DataProvider;
  39 import org.testng.annotations.Test;
  40 
  41 public class TestWithCompatProvider {
  42 
  43     private static final NumberFormat FORMAT_DZ_SHORT = NumberFormat
  44             .getCompactNumberInstance(new Locale("dz"), NumberFormat.Style.SHORT);
  45 
  46     private static final NumberFormat FORMAT_EN_US_SHORT = NumberFormat
  47             .getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
  48 
  49     @DataProvider(name = "format")
  50     Object[][] compactFormatData() {
  51         return new Object[][]{
  52             {FORMAT_DZ_SHORT, 1000.09, "1K"},
  53             {FORMAT_DZ_SHORT, -999.99, "-1K"},
  54             {FORMAT_DZ_SHORT, -0.0, "-0"},
  55             {FORMAT_DZ_SHORT, new BigInteger("12345678901234567890"), "12345679T"},
  56             {FORMAT_DZ_SHORT, new BigDecimal("12345678901234567890.89"), "12345679T"},
  57             {FORMAT_EN_US_SHORT, -999.99, "-1K"},
  58             {FORMAT_EN_US_SHORT, 9999, "10K"},
  59             {FORMAT_EN_US_SHORT, 3000.90, "3K"},
  60             {FORMAT_EN_US_SHORT, new BigInteger("12345678901234567890"), "12345679T"},
  61             {FORMAT_EN_US_SHORT, new BigDecimal("12345678901234567890.89"), "12345679T"},};
  62     }
  63 
  64     @DataProvider(name = "parse")
  65     Object[][] compactParseData() {
  66         return new Object[][]{
  67             {FORMAT_DZ_SHORT, "1K", 1000L},
  68             {FORMAT_DZ_SHORT, "-3K", -3000L},
  69             {FORMAT_DZ_SHORT, "12345700T", 1.23457E19},
  70             {FORMAT_EN_US_SHORT, "-99", -99L},
  71             {FORMAT_EN_US_SHORT, "10K", 10000L},
  72             {FORMAT_EN_US_SHORT, "12345679T", 1.2345679E19},};
  73     }
  74 
  75     @Test(dataProvider = "format")
  76     public void testFormat(NumberFormat cnf, Object number,
  77             String expected) {
  78         CompactFormatAndParseHelper.testFormat(cnf, number, expected);
  79     }
  80 
  81     @Test(dataProvider = "parse")
  82     public void testParse(NumberFormat cnf, String parseString,
  83             Number expected) throws ParseException {
  84         CompactFormatAndParseHelper.testParse(cnf, parseString, expected, null, null);
  85     }
  86 
  87 }