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