1 /*
   2  * Copyright (c) 2017, 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 8176841
  28  * @summary Tests Currency class instantiates correctly with Unicode
  29  *      extensions
  30  * @modules jdk.localedata
  31  * @run testng/othervm CurrencyTests
  32  */
  33 
  34 import static org.testng.Assert.assertEquals;
  35 
  36 import java.util.Currency;
  37 import java.util.Locale;
  38 
  39 import org.testng.annotations.DataProvider;
  40 import org.testng.annotations.Test;
  41 
  42 /**
  43  * Test Currency with BCP47 U extensions
  44  */
  45 @Test
  46 public class CurrencyTests {
  47     private static final Currency USD = Currency.getInstance("USD");
  48     private static final Currency CAD = Currency.getInstance("CAD");
  49     private static final Currency JPY = Currency.getInstance("JPY");
  50 
  51     @DataProvider(name="getInstanceData")
  52     Object[][] getInstanceData() {
  53         return new Object[][] {
  54             // Locale, Expected Currency
  55             // "cu"
  56             {Locale.forLanguageTag("en-US-u-cu-jpy"), JPY},
  57             {Locale.forLanguageTag("ja-JP-u-cu-usd"), USD},
  58             {Locale.forLanguageTag("en-US-u-cu-foobar"), USD},
  59             {Locale.forLanguageTag("en-US-u-cu-zzz"), USD},
  60 
  61             // "rg"
  62             {Locale.forLanguageTag("en-US-u-rg-jpzzzz"), JPY},
  63             {Locale.forLanguageTag("ja-JP-u-rg-uszzzz"), USD},
  64             {Locale.forLanguageTag("ja-JP-u-rg-001zzzz"), JPY},
  65             {Locale.forLanguageTag("en-US-u-rg-jpz"), USD},
  66 
  67             // "cu" and "rg". "cu" should win
  68             {Locale.forLanguageTag("en-CA-u-cu-jpy-rg-uszzzz"), JPY},
  69 
  70             // invaid "cu" and valid "rg". "rg" should win
  71             {Locale.forLanguageTag("en-CA-u-cu-jpyy-rg-uszzzz"), USD},
  72             {Locale.forLanguageTag("en-CA-u-cu-zzz-rg-uszzzz"), USD},
  73 
  74             // invaid "cu" and invalid "rg". both should be ignored
  75             {Locale.forLanguageTag("en-CA-u-cu-jpyy-rg-jpzz"), CAD},
  76         };
  77     }
  78 
  79     @DataProvider(name="getSymbolData")
  80     Object[][] getSymbolData() {
  81         return new Object[][] {
  82             // Currency, DisplayLocale, expected Symbol 
  83             {USD, Locale.forLanguageTag("en-US-u-rg-jpzzzz"), "$"},
  84             {USD, Locale.forLanguageTag("en-US-u-rg-cazzzz"), "US$"},
  85             {USD, Locale.forLanguageTag("en-CA-u-rg-uszzzz"), "$"},
  86 
  87             {CAD, Locale.forLanguageTag("en-US-u-rg-jpzzzz"), "CA$"},
  88             {CAD, Locale.forLanguageTag("en-US-u-rg-cazzzz"), "$"},
  89             {CAD, Locale.forLanguageTag("en-CA-u-rg-uszzzz"), "CA$"},
  90 
  91             {JPY, Locale.forLanguageTag("ja-JP-u-rg-uszzzz"), "\uffe5"},
  92             {JPY, Locale.forLanguageTag("en-US-u-rg-jpzzzz"), "\u00a5"},
  93             {JPY, Locale.forLanguageTag("ko-KR-u-rg-jpzzzz"), "JP\u00a5"},
  94         };
  95     }
  96 
  97     @Test(dataProvider="getInstanceData")
  98     public void test_getInstance(Locale locale, Currency currencyExpected) {
  99         assertEquals(Currency.getInstance(locale), currencyExpected);
 100     }
 101 
 102     @Test(dataProvider="getSymbolData")
 103     public void test_getSymbol(Currency c, Locale locale, String expected) {
 104         assertEquals(c.getSymbol(locale), expected);
 105     }
 106 }