1 /*
   2  * Copyright (c) 2019, 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 /*
  25  *
  26  * @test
  27  * @bug 8215181
  28  * @summary Tests the "u-cf" extension
  29  * @modules jdk.localedata
  30  * @run testng/othervm CurrencyFormatTests
  31  */
  32 
  33 import static org.testng.Assert.assertEquals;
  34 
  35 import java.text.NumberFormat;
  36 import java.util.Locale;
  37 
  38 import org.testng.annotations.DataProvider;
  39 import org.testng.annotations.Test;
  40 
  41 /**
  42  * Test NumberFormat with BCP47 u-cf extensions. Note that this test depends
  43  * on the particular CLDR release. Results may vary on other CLDR releases.
  44  */
  45 @Test
  46 public class CurrencyFormatTests {
  47 
  48     @DataProvider(name="getInstanceData")
  49     Object[][] getInstanceData() {
  50         return new Object[][] {
  51             // Locale, amount, expected
  52             // US dollar
  53             {Locale.US, -100, "-$100.00"},
  54             {Locale.forLanguageTag("en-US-u-cf-standard"), -100, "-$100.00"},
  55             {Locale.forLanguageTag("en-US-u-cf-account"), -100, "($100.00)"},
  56             {Locale.forLanguageTag("en-US-u-cf-bogus"), -100, "-$100.00"},
  57 
  58             // Euro
  59             {Locale.forLanguageTag("en-AT"), -100, "-\u20ac\u00a0100,00"},
  60             {Locale.forLanguageTag("en-AT-u-cf-standard"), -100, "-\u20ac\u00a0100,00"},
  61             {Locale.forLanguageTag("en-AT-u-cf-account"), -100, "-\u20ac\u00a0100,00"},
  62             {Locale.forLanguageTag("en-AT-u-cf-bogus"), -100, "-\u20ac\u00a0100,00"},
  63 
  64             // Rupee
  65             {Locale.forLanguageTag("en-IN"), -100, "-\u20b9\u00a0100.00"},
  66             {Locale.forLanguageTag("en-IN-u-cf-standard"), -100, "-\u20b9\u00a0100.00"},
  67             {Locale.forLanguageTag("en-IN-u-cf-account"), -100, "(\u20b9100.00)"},
  68             {Locale.forLanguageTag("en-IN-u-cf-bogus"), -100, "-\u20b9\u00a0100.00"},
  69 
  70             // Swiss franc
  71             {Locale.forLanguageTag("en-CH"), -100, "CHF-100.00"},
  72             {Locale.forLanguageTag("en-CH-u-cf-standard"), -100, "CHF-100.00"},
  73             {Locale.forLanguageTag("en-CH-u-cf-account"), -100, "CHF-100.00"},
  74             {Locale.forLanguageTag("en-CH-u-cf-bogus"), -100, "CHF-100.00"},
  75 
  76             // Region override
  77             {Locale.forLanguageTag("en-US-u-rg-CHZZZZ"), -100, "CHF-100.00"},
  78             {Locale.forLanguageTag("en-US-u-rg-CHZZZZ-cf-standard"), -100, "CHF-100.00"},
  79             {Locale.forLanguageTag("en-US-u-rg-CHZZZZ-cf-account"), -100, "CHF-100.00"},
  80             {Locale.forLanguageTag("en-US-u-rg-CHZZZZ-cf-bogus"), -100, "CHF-100.00"},
  81         };
  82     }
  83 
  84     @Test(dataProvider="getInstanceData")
  85     public void test_getInstance(Locale locale, int amount, String expected) {
  86         assertEquals(NumberFormat.getCurrencyInstance(locale).format(amount), expected);
  87     }
  88 }