jdk/test/java/util/PluggableLocale/CurrencyNameProviderTest.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 15260 7af2d7a87806
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 15260
diff changeset
     2
 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10138
diff changeset
    29
import sun.util.locale.provider.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.util.resources.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class CurrencyNameProviderTest extends ProviderTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    public static void main(String[] s) {
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
    35
        Locale reservedLocale = Locale.getDefault();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
    36
        try {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
    37
            new CurrencyNameProviderTest();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
    38
        } finally {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
    39
            // restore the reserved locale
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
    40
            Locale.setDefault(reservedLocale);
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
    41
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    CurrencyNameProviderTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        test1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        test2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    void test1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        com.bar.CurrencyNameProviderImpl cnp = new com.bar.CurrencyNameProviderImpl();
14331
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
    51
        com.bar.CurrencyNameProviderImpl2 cnp2 = new com.bar.CurrencyNameProviderImpl2();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        Locale[] availloc = Locale.getAvailableLocales();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Locale[] testloc = availloc.clone();
14185
916ec0a4d039 8000245: SimpleDateFormat.format(date, StringBuffer, FieldPosition) doesn't work as expected with custom extensions
naoto
parents: 13583
diff changeset
    54
        List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getCurrencyNameProvider().getAvailableLocales());
14331
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
    55
        List<Locale> providerloc = new ArrayList<Locale>();
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
    56
        providerloc.addAll(Arrays.asList(cnp.getAvailableLocales()));
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
    57
        providerloc.addAll(Arrays.asList(cnp2.getAvailableLocales()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        for (Locale target: availloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            // pure JRE implementation
15260
7af2d7a87806 7162007: Clean up i18n related caches
naoto
parents: 14331
diff changeset
    61
            OpenListResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getCurrencyNames(target);
14185
916ec0a4d039 8000245: SimpleDateFormat.format(date, StringBuffer, FieldPosition) doesn't work as expected with custom extensions
naoto
parents: 13583
diff changeset
    62
            boolean jreSupportsTarget = jreimplloc.contains(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            for (Locale test: testloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                // get a Currency instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                Currency c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    c = Currency.getInstance(test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                } catch (IllegalArgumentException iae) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                // the localized symbol for the target locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                String currencyresult = c.getSymbol(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                // the localized name for the target locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                String nameresult = c.getDisplayName(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                // provider's name (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                String providerscurrency = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                String providersname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                if (providerloc.contains(target)) {
14331
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
    85
                    if (cnp.isSupportedLocale(target)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    providerscurrency = cnp.getSymbol(c.getCurrencyCode(), target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    providersname = cnp.getDisplayName(c.getCurrencyCode(), target);
14331
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
    88
                    } else {
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
    89
                        providerscurrency = cnp2.getSymbol(c.getCurrencyCode(), target);
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
    90
                        providersname = cnp2.getDisplayName(c.getCurrencyCode(), target);
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
    91
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
14185
916ec0a4d039 8000245: SimpleDateFormat.format(date, StringBuffer, FieldPosition) doesn't work as expected with custom extensions
naoto
parents: 13583
diff changeset
    94
                // JRE's name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                String jrescurrency = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                String jresname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                String key = c.getCurrencyCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                String nameKey = key.toLowerCase(Locale.ROOT);
14185
916ec0a4d039 8000245: SimpleDateFormat.format(date, StringBuffer, FieldPosition) doesn't work as expected with custom extensions
naoto
parents: 13583
diff changeset
    99
                if (jreSupportsTarget) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        jrescurrency = rb.getString(key);
14185
916ec0a4d039 8000245: SimpleDateFormat.format(date, StringBuffer, FieldPosition) doesn't work as expected with custom extensions
naoto
parents: 13583
diff changeset
   102
                    } catch (MissingResourceException mre) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        jresname = rb.getString(nameKey);
14185
916ec0a4d039 8000245: SimpleDateFormat.format(date, StringBuffer, FieldPosition) doesn't work as expected with custom extensions
naoto
parents: 13583
diff changeset
   105
                    } catch (MissingResourceException mre) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
14185
916ec0a4d039 8000245: SimpleDateFormat.format(date, StringBuffer, FieldPosition) doesn't work as expected with custom extensions
naoto
parents: 13583
diff changeset
   108
                checkValidity(target, jrescurrency, providerscurrency, currencyresult,
916ec0a4d039 8000245: SimpleDateFormat.format(date, StringBuffer, FieldPosition) doesn't work as expected with custom extensions
naoto
parents: 13583
diff changeset
   109
                              jreSupportsTarget && jrescurrency != null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                checkValidity(target, jresname, providersname, nameresult,
14185
916ec0a4d039 8000245: SimpleDateFormat.format(date, StringBuffer, FieldPosition) doesn't work as expected with custom extensions
naoto
parents: 13583
diff changeset
   111
                              jreSupportsTarget && jresname != null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    final String pattern = "###,###\u00A4";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    final String YEN_IN_OSAKA = "100,000\u5186\u3084\u3002";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    final String YEN_IN_KYOTO = "100,000\u5186\u3069\u3059\u3002";
14331
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   120
    final String YEN_IN_TOKYO= "100,000JPY-tokyo";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    final Locale OSAKA = new Locale("ja", "JP", "osaka");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    final Locale KYOTO = new Locale("ja", "JP", "kyoto");
14331
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   123
    final Locale TOKYO = new Locale("ja", "JP", "tokyo");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    Integer i = new Integer(100000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    String formatted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    DecimalFormat df;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    void test2() {
14331
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   129
        Locale defloc = Locale.getDefault();
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   130
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(OSAKA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            System.out.println(formatted = df.format(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if(!formatted.equals(YEN_IN_OSAKA)) {
14331
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   135
                throw new RuntimeException("formatted currency names mismatch. " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    "Should match with " + YEN_IN_OSAKA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            df.parse(YEN_IN_OSAKA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            Locale.setDefault(KYOTO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            System.out.println(formatted = df.format(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if(!formatted.equals(YEN_IN_KYOTO)) {
14331
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   145
                throw new RuntimeException("formatted currency names mismatch. " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    "Should match with " + YEN_IN_KYOTO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            df.parse(YEN_IN_KYOTO);
14331
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   150
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   151
            Locale.setDefault(TOKYO);
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   152
            df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance());
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   153
            System.out.println(formatted = df.format(i));
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   154
            if(!formatted.equals(YEN_IN_TOKYO)) {
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   155
                throw new RuntimeException("formatted currency names mismatch. " +
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   156
                    "Should match with " + YEN_IN_TOKYO);
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   157
            }
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   158
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   159
            df.parse(YEN_IN_TOKYO);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new RuntimeException("parse error occured" + pe);
14331
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   162
        } finally {
d3411e624053 8000997: Multiple locale sensitive services cannot be loaded
naoto
parents: 14185
diff changeset
   163
            Locale.setDefault(defloc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}