1 /*
2  * Copyright (c) 2007, 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 /*
25  * @test
26  * @bug 4052440 7199750 8000997 8062588 8210406
27  * @summary CurrencyNameProvider tests
28  * @library providersrc/foobarutils
29  *          providersrc/barprovider
30  * @modules java.base/sun.util.locale.provider
31  *          java.base/sun.util.resources
32  * @build com.foobar.Utils
33  *        com.bar.*
34  * @run main/othervm -Djava.locale.providers=JRE,SPI CurrencyNameProviderTest
35  */
36 
37 import java.text.DecimalFormat;
38 import java.text.DecimalFormatSymbols;
39 import java.text.ParseException;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Currency;
43 import java.util.List;
44 import java.util.Locale;
45 import java.util.MissingResourceException;
46 
47 import com.bar.CurrencyNameProviderImpl;
48 import com.bar.CurrencyNameProviderImpl2;
49 
50 import sun.util.locale.provider.LocaleProviderAdapter;
51 import sun.util.locale.provider.ResourceBundleBasedAdapter;
52 import sun.util.resources.OpenListResourceBundle;
53 
54 public class CurrencyNameProviderTest extends ProviderTest {
55 
main(String[] s)56     public static void main(String[] s) {
57         Locale reservedLocale = Locale.getDefault();
58         try {
59             new CurrencyNameProviderTest();
60         } finally {
61             // restore the reserved locale
62             Locale.setDefault(reservedLocale);
63         }
64     }
65 
CurrencyNameProviderTest()66     CurrencyNameProviderTest() {
67         test1();
68         test2();
69     }
70 
test1()71     void test1() {
72         CurrencyNameProviderImpl cnp = new CurrencyNameProviderImpl();
73         CurrencyNameProviderImpl2 cnp2 = new CurrencyNameProviderImpl2();
74         Locale[] availloc = Locale.getAvailableLocales();
75         Locale[] testloc = availloc.clone();
76         List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getCurrencyNameProvider().getAvailableLocales());
77         List<Locale> providerloc = new ArrayList<Locale>();
78         providerloc.addAll(Arrays.asList(cnp.getAvailableLocales()));
79         providerloc.addAll(Arrays.asList(cnp2.getAvailableLocales()));
80 
81         for (Locale target: availloc) {
82             // pure JRE implementation
83             OpenListResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getCurrencyNames(target);
84             boolean jreSupportsTarget = jreimplloc.contains(target);
85 
86             for (Locale test: testloc) {
87                 // get a Currency instance
88                 Currency c = null;
89                 try {
90                     c = Currency.getInstance(test);
91                 } catch (IllegalArgumentException iae) {}
92 
93                 if (c == null) {
94                     continue;
95                 }
96 
97                 // the localized symbol for the target locale
98                 String currencyresult = c.getSymbol(target);
99 
100                 // the localized name for the target locale
101                 String nameresult = c.getDisplayName(target);
102 
103                 // provider's name (if any)
104                 String providerscurrency = null;
105                 String providersname = null;
106                 if (providerloc.contains(target)) {
107                     if (cnp.isSupportedLocale(target)) {
108                     providerscurrency = cnp.getSymbol(c.getCurrencyCode(), target);
109                     providersname = cnp.getDisplayName(c.getCurrencyCode(), target);
110                     } else {
111                         providerscurrency = cnp2.getSymbol(c.getCurrencyCode(), target);
112                         providersname = cnp2.getDisplayName(c.getCurrencyCode(), target);
113                     }
114                 }
115 
116                 // JRE's name
117                 String jrescurrency = null;
118                 String jresname = null;
119                 String key = c.getCurrencyCode();
120                 String nameKey = key.toLowerCase(Locale.ROOT);
121                 if (jreSupportsTarget) {
122                     try {
123                         jrescurrency = rb.getString(key);
124                     } catch (MissingResourceException mre) {}
125                     try {
126                         jresname = rb.getString(nameKey);
127                     } catch (MissingResourceException mre) {}
128                 }
129 
130                 checkValidity(target, jrescurrency, providerscurrency, currencyresult,
131                               jreSupportsTarget && jrescurrency != null);
132                 checkValidity(target, jresname, providersname, nameresult,
133                               jreSupportsTarget && jresname != null);
134             }
135         }
136     }
137 
138 
139     final String pattern = "###,###\u00A4";
140     final String YEN_IN_OSAKA = "100,000\u5186\u3084\u3002";
141     final String YEN_IN_KYOTO = "100,000\u5186\u3069\u3059\u3002";
142     final String YEN_IN_TOKYO= "100,000JPY-tokyo";
143     final Locale OSAKA = new Locale("ja", "JP", "osaka");
144     final Locale KYOTO = new Locale("ja", "JP", "kyoto");
145     final Locale TOKYO = new Locale("ja", "JP", "tokyo");
146     Integer i = new Integer(100000);
147     String formatted;
148     DecimalFormat df;
149 
test2()150     void test2() {
151         Locale defloc = Locale.getDefault();
152 
153         try {
154             df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(OSAKA));
155             System.out.println(formatted = df.format(i));
156             if(!formatted.equals(YEN_IN_OSAKA)) {
157                 throw new RuntimeException("formatted currency names mismatch. " +
158                     "Should match with " + YEN_IN_OSAKA);
159             }
160 
161             df.parse(YEN_IN_OSAKA);
162 
163             Locale.setDefault(KYOTO);
164             df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance());
165             System.out.println(formatted = df.format(i));
166             if(!formatted.equals(YEN_IN_KYOTO)) {
167                 throw new RuntimeException("formatted currency names mismatch. " +
168                     "Should match with " + YEN_IN_KYOTO);
169             }
170 
171             df.parse(YEN_IN_KYOTO);
172 
173             Locale.setDefault(TOKYO);
174             df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance());
175             System.out.println(formatted = df.format(i));
176             if(!formatted.equals(YEN_IN_TOKYO)) {
177                 throw new RuntimeException("formatted currency names mismatch. " +
178                     "Should match with " + YEN_IN_TOKYO);
179             }
180 
181             df.parse(YEN_IN_TOKYO);
182         } catch (ParseException pe) {
183             throw new RuntimeException("parse error occured" + pe);
184         } finally {
185             Locale.setDefault(defloc);
186         }
187     }
188 }