J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | /* |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | import java.text.*; |
| 28 | import java.util.*; |
| 29 | import sun.util.*; |
| 30 | import sun.util.resources.*; |
| 31 | |
| 32 | public class CurrencyNameProviderTest extends ProviderTest { |
| 33 | |
| 34 | public static void main(String[] s) { |
| 35 | new CurrencyNameProviderTest(); |
| 36 | } |
| 37 | |
| 38 | CurrencyNameProviderTest() { |
| 39 | test1(); |
| 40 | test2(); |
| 41 | } |
| 42 | |
| 43 | void test1() { |
| 44 | com.bar.CurrencyNameProviderImpl cnp = new com.bar.CurrencyNameProviderImpl(); |
| 45 | Locale[] availloc = Locale.getAvailableLocales(); |
| 46 | Locale[] testloc = availloc.clone(); |
| 47 | List<Locale> providerloc = Arrays.asList(cnp.getAvailableLocales()); |
| 48 | |
| 49 | for (Locale target: availloc) { |
| 50 | // pure JRE implementation |
| 51 | OpenListResourceBundle rb = (OpenListResourceBundle)LocaleData.getCurrencyNames(target); |
| 52 | boolean jreHasBundle = rb.getLocale().equals(target); |
| 53 | |
| 54 | for (Locale test: testloc) { |
| 55 | // get a Currency instance |
| 56 | Currency c = null; |
| 57 | try { |
| 58 | c = Currency.getInstance(test); |
| 59 | } catch (IllegalArgumentException iae) {} |
| 60 | |
| 61 | if (c == null) { |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | // the localized symbol for the target locale |
| 66 | String currencyresult = c.getSymbol(target); |
| 67 | |
| 68 | // the localized name for the target locale |
| 69 | String nameresult = c.getDisplayName(target); |
| 70 | |
| 71 | // provider's name (if any) |
| 72 | String providerscurrency = null; |
| 73 | String providersname = null; |
| 74 | if (providerloc.contains(target)) { |
| 75 | providerscurrency = cnp.getSymbol(c.getCurrencyCode(), target); |
| 76 | providersname = cnp.getDisplayName(c.getCurrencyCode(), target); |
| 77 | } |
| 78 | |
| 79 | // JRE's name (if any) |
| 80 | String jrescurrency = null; |
| 81 | String jresname = null; |
| 82 | String key = c.getCurrencyCode(); |
| 83 | String nameKey = key.toLowerCase(Locale.ROOT); |
| 84 | if (jreHasBundle) { |
| 85 | try { |
| 86 | jrescurrency = rb.getString(key); |
| 87 | } catch (MissingResourceException mre) { |
| 88 | // JRE does not have any resource, "jrescurrency" should remain null |
| 89 | } |
| 90 | try { |
| 91 | jresname = rb.getString(nameKey); |
| 92 | } catch (MissingResourceException mre) { |
| 93 | // JRE does not have any resource, "jresname" should remain null |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | checkValidity(target, jrescurrency, providerscurrency, currencyresult, jrescurrency!=null); |
| 98 | checkValidity(target, jresname, providersname, nameresult, |
| 99 | jreHasBundle && rb.handleGetKeys().contains(nameKey)); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | final String pattern = "###,###\u00A4"; |
| 106 | final String YEN_IN_OSAKA = "100,000\u5186\u3084\u3002"; |
| 107 | final String YEN_IN_KYOTO = "100,000\u5186\u3069\u3059\u3002"; |
| 108 | final Locale OSAKA = new Locale("ja", "JP", "osaka"); |
| 109 | final Locale KYOTO = new Locale("ja", "JP", "kyoto"); |
| 110 | Integer i = new Integer(100000); |
| 111 | String formatted; |
| 112 | DecimalFormat df; |
| 113 | |
| 114 | void test2() { |
| 115 | try { |
| 116 | df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(OSAKA)); |
| 117 | System.out.println(formatted = df.format(i)); |
| 118 | if(!formatted.equals(YEN_IN_OSAKA)) { |
| 119 | throw new RuntimeException("formatted zone names mismatch. " + |
| 120 | "Should match with " + YEN_IN_OSAKA); |
| 121 | } |
| 122 | |
| 123 | df.parse(YEN_IN_OSAKA); |
| 124 | |
| 125 | Locale.setDefault(KYOTO); |
| 126 | df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance()); |
| 127 | System.out.println(formatted = df.format(i)); |
| 128 | if(!formatted.equals(YEN_IN_KYOTO)) { |
| 129 | throw new RuntimeException("formatted zone names mismatch. " + |
| 130 | "Should match with " + YEN_IN_KYOTO); |
| 131 | } |
| 132 | |
| 133 | df.parse(YEN_IN_KYOTO); |
| 134 | } catch (ParseException pe) { |
| 135 | throw new RuntimeException("parse error occured" + pe); |
| 136 | } |
| 137 | } |
| 138 | } |